Пример #1
0
func ExampleInstruction() {
	var i x.Instruction
	i.SubjectId = "sid"
	i.SubjectType = "stype"

	b, err := i.GobEncode()
	if err != nil {
		panic(err)
	}

	var o x.Instruction
	if err := o.GobDecode(b); err != nil {
		panic(err)
	}

	fmt.Println(o.SubjectId)
	fmt.Println(o.SubjectType)
	// Output:
	// sid
	// stype
}
Пример #2
0
func (l *Leveldb) GetEntity(_, id string) (result []x.Instruction, rerr error) {
	slice := util.BytesPrefix([]byte(id))
	iter := l.db.NewIterator(slice, nil)
	for iter.Next() {
		buf := iter.Value()
		if buf == nil {
			break
		}
		var i x.Instruction
		if err := i.GobDecode(buf); err != nil {
			x.LogErr(log, err).Error("While decoding")
			return result, err
		}
		result = append(result, i)
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		x.LogErr(log, err).Error("While iterating")
	}
	return result, err
}
Пример #3
0
func (l *Leveldb) Iterate(fromId string, num int,
	ch chan x.Entity) (rnum int, rlast x.Entity, rerr error) {
	slice := util.Range{Start: []byte(fromId)}
	iter := l.db.NewIterator(&slice, nil)

	rnum = 0
	handled := make(map[x.Entity]bool)
	for iter.Next() {
		buf := iter.Value()
		if buf == nil {
			break
		}
		var i x.Instruction
		if err := i.GobDecode(buf); err != nil {
			x.LogErr(log, err).Error("While decoding")
			return rnum, rlast, err
		}
		e := x.Entity{Kind: i.SubjectType, Id: i.SubjectId}
		rlast = e
		if _, present := handled[e]; present {
			continue
		}
		ch <- e
		handled[e] = true
		rnum += 1
		if rnum >= num {
			break
		}
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		x.LogErr(log, err).Error("While iterating")
	}
	return rnum, rlast, err
}