func (bs *boltStorage) saveEntriesAndState(entries []raftpb.Entry, hardState raftpb.HardState) error {
	tx1, err := bs.db.Begin(true)
	if err != nil {
		return err
	}

	defer tx1.Rollback()
	b := tx1.Bucket(entriesBucket)
	for _, e := range entries {
		bites, err2 := proto.Marshal(&e)
		if err2 != nil {
			return err2
		}

		err2 = b.Put(uint64ToBytes(e.Index), bites)
		if err2 != nil {
			return err2
		}
	}

	if len(entries) > 0 {
		lastEntryIWrote := entries[len(entries)-1]
		lastIndexIWrote := uint64ToBytes(lastEntryIWrote.Index)
		c := tx1.Bucket(entriesBucket).Cursor()
		k, _ := c.Last()
		for ; k != nil && bytes.Compare(k, lastIndexIWrote) > 0; k, _ = c.Prev() {
			err2 := c.Delete()
			if err2 != nil {
				return err2
			}
		}
	}

	err = tx1.Commit()
	if err != nil {
		return err
	}

	if !raft.IsEmptyHardState(hardState) {
		tx2, err := bs.db.Begin(true)
		if err != nil {
			return err
		}

		defer tx2.Rollback()
		bites, err := proto.Marshal(&hardState)
		if err != nil {
			return err
		}

		err = tx2.Bucket(staticFieldsBucket).Put(hardStateKey, bites)
		if err != nil {
			return err
		}

		err = tx2.Commit()
		if err != nil {
			return err
		}
	}

	return nil
}
// The order in which entries and hardState are dealt with is actually important.
// Raft dictates that first the entries need to be written to disk and then
// states are supposed to be handled. Even though the store we use supports
// transactions and atomic writes...
The order in which entries and hardState are dealt with is actually important.
Raft dictates that first the entries need to be written to disk and then
states are supposed to be handled. Even though the store we use supports
transactions and atomic writes
// The order in which entries and hardState are dealt with is actually important . // Raft dictates that first the entries need to be written to disk and then // states are supposed to be handled . Even though the store we use supports // transactions and atomic writes ...
func (bs *boltStorage) saveEntriesAndState(entries []raftpb.Entry, hardState raftpb.HardState) error {
	tx1, err := bs.db.Begin(true)
	if err != nil {
		return err
	}
	defer tx1.Rollback()
	b := tx1.Bucket(entriesBucket)
	for _, e := range entries {
		bites, err2 := proto.Marshal(&e)
		if err2 != nil {
			return err2
		}
		err2 = b.Put(uint64ToBytes(e.Index), bites)
		if err2 != nil {
			return err2
		}
	}
	if len(entries) > 0 {
		lastEntryIWrote := entries[len(entries)-1]
		lastIndexIWrote := uint64ToBytes(lastEntryIWrote.Index)
		c := tx1.Bucket(entriesBucket).Cursor()
		k, _ := c.Last()
		for ; k != nil && bytes.Compare(k, lastIndexIWrote) > 0; k, _ = c.Prev() {
			err2 := c.Delete()
			if err2 != nil {
				return err2
			}
		}
	}
	err = tx1.Commit()
	if err != nil {
		return err
	}
	if !raft.IsEmptyHardState(hardState) {
		tx2, err := bs.db.Begin(true)
		if err != nil {
			return err
		}
		defer tx2.Rollback()
		bites, err := proto.Marshal(&hardState)
		if err != nil {
			return err
		}
		err = tx2.Bucket(staticFieldsBucket).Put(hardStateKey, bites)
		if err != nil {
			return err
		}
		err = tx2.Commit()
		if err != nil {
			return err
		}
	}
	return nil
}
func ( bs * boltStorage ) saveEntriesAndState ( entries [ ] raftpb . Entry , hardState raftpb . HardState ) error { tx1 , err := bs . db . Begin ( true ) if err != nil { return err } defer tx1 . Rollback ( ) b := tx1 . Bucket ( entriesBucket ) for _ , e := range entries { bites , err2 := proto . Marshal ( & e ) if err2 != nil { return err2 } err2 = b . Put ( uint64ToBytes ( e . Index ) , bites ) if err2 != nil { return err2 } } if len ( entries ) > 0 { lastEntryIWrote := entries [ len ( entries ) - 1 ] lastIndexIWrote := uint64ToBytes ( lastEntryIWrote . Index ) c := tx1 . Bucket ( entriesBucket ) . Cursor ( ) k , _ := c . Last ( ) for ; k != nil && bytes . Compare ( k , lastIndexIWrote ) > 0 ; k , _ = c . Prev ( ) { err2 := c . Delete ( ) if err2 != nil { return err2 } } } err = tx1 . Commit ( ) if err != nil { return err } if ! raft . IsEmptyHardState ( hardState ) { tx2 , err := bs . db . Begin ( true ) if err != nil { return err } defer tx2 . Rollback ( ) bites , err := proto . Marshal ( & hardState ) if err != nil { return err } err = tx2 . Bucket ( staticFieldsBucket ) . Put ( hardStateKey , bites ) if err != nil { return err } err = tx2 . Commit ( ) if err != nil { return err } } return nil }
The order in which entries and hardState are dealt with is actually important.
The order in which entries and hardState are dealt with is actually important .