示例#1
0
func (p *RelationsCache) GetRelation(id int64) (*element.Relation, error) {
	keyBuf := idToKeyBuf(id)
	data, err := p.db.Get(p.ro, keyBuf)
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, NotFound
	}
	relation, err := binary.UnmarshalRelation(data)
	if err != nil {
		return nil, err
	}
	relation.Id = id
	return relation, err
}
示例#2
0
func (p *RelationsCache) Iter() chan *element.Relation {
	rels := make(chan *element.Relation)
	go func() {
		ro := levigo.NewReadOptions()
		ro.SetFillCache(false)
		it := p.db.NewIterator(ro)
		// we need to Close the iter before closing the
		// chan (and thus signaling that we are done)
		// to avoid race where db is closed before the iterator
		defer close(rels)
		defer it.Close()
		it.SeekToFirst()
		for ; it.Valid(); it.Next() {
			rel, err := binary.UnmarshalRelation(it.Value())
			if err != nil {
				panic(err)
			}
			rel.Id = idFromKeyBuf(it.Key())

			rels <- rel
		}
	}()
	return rels
}