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 }
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 }