Example #1
0
func (stable *boltDBStable) GetLogsFrom(from uint64) []*Rlog {

	logs := []*Rlog{}

	err := stable.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(boltLogs)
		c := b.Cursor()

		// if from > last index return empty
		if index, _ := c.Last(); index != nil {
			if from > convertBytesToUint64(index) {
				return nil
			}
		}

		for k, v := c.Seek(convertUint64ToBytes(from)); k != nil; k, v = c.Next() {
			log := &Rlog{}
			dec := codec.NewDecoderBytes(v, msgPackHandle)
			if err := dec.Decode(log); err != nil {
				return err
			}
			logs = append(logs, log)
		}

		return nil
	})

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

	return logs
}
Example #2
0
func (stable *boltDBStable) GetLog(k uint64) (*Rlog, error) {

	log := &Rlog{}

	err := stable.db.View(func(tx *bolt.Tx) error {

		bucket := tx.Bucket(boltLogs)
		val := bucket.Get(convertUint64ToBytes(k))

		if val == nil {
			return ErrKeyNotFound
		}

		dec := codec.NewDecoderBytes(val, msgPackHandle)
		err := dec.Decode(log)

		if err != nil {
			return err
		}

		return nil
	})

	if err != nil && err != ErrKeyNotFound {
		panic("Could not get log")
	} else if err != nil {
		return nil, err
	}

	return log, nil
}
Example #3
0
func (stable *boltDBStable) LastLog() *Rlog {

	retLog := &Rlog{
		Index: 0,
	}

	err := stable.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(boltLogs)
		c := b.Cursor()

		var log []byte
		if _, log = c.Last(); log == nil {
			return nil
		}

		dec := codec.NewDecoderBytes(log, msgPackHandle)
		err := dec.Decode(retLog)
		return err

	})

	if err != nil {
		panic("Could not get last log")
	}

	return retLog
}
Example #4
0
func (r *Raft) readSnapshot() (*fsmSnapshot, error) {

	snap, err := ioutil.ReadFile(fmt.Sprintf("%s/snapshot/snapshot.bin", r.config.BaseDir))
	if err != nil {
		return nil, err
	}

	snapshot := &fsmSnapshot{}

	dec := codec.NewDecoderBytes(snap, msgPackHandle)
	err = dec.Decode(snapshot)
	if err != nil {
		return nil, err
	}

	return snapshot, nil

}