示例#1
0
文件: stable.go 项目: sisatech/raft
func (stable *boltDBStable) StoreLogs(logs []*Rlog) {

	var lastLog *Rlog
	err := stable.db.Batch(func(tx *bolt.Tx) error {

		for _, log := range logs {

			bucket := tx.Bucket(boltLogs)
			key := convertUint64ToBytes(log.Index)

			encLog := make([]byte, 0, 512)
			enc := codec.NewEncoderBytes(&encLog, msgPackHandle)

			if err := enc.Encode(log); err != nil {
				return err
			}

			if err := bucket.Put(key, encLog); err != nil {
				return err
			}

			lastLog = log
		}

		return nil
	})

	if err != nil {
		panic("Could not store logs")
	}

}
示例#2
0
文件: raft.go 项目: sisatech/raft
func (r *Raft) writeSnapshot(snap *fsmSnapshot) error {

	encSnap := make([]byte, 0, 512)
	enc := codec.NewEncoderBytes(&encSnap, msgPackHandle)
	err := enc.Encode(snap)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(fmt.Sprintf("%s/snapshot/snapshot.bin", r.config.BaseDir), encSnap, 0700)

	r.workingSnapshot = snap
	return err

}