// loadMergeMarshal loads an existing bunch, merges the IdRefs and // marshals the result again. func (index *bunchRefCache) loadMergeMarshal(keyBuf []byte, newBunch []element.IdRefs) []byte { data, err := index.db.Get(index.ro, keyBuf) if err != nil { panic(err) } var bunch []element.IdRefs if data != nil { bunch = binary.UnmarshalIdRefsBunch(data) } if bunch == nil { bunch = newBunch } else { bunch = mergeBunch(bunch, newBunch) } data = binary.MarshalIdRefsBunch(bunch) return data }
func (index *bunchRefCache) Add(id, ref int64) error { keyBuf := idToKeyBuf(index.getBunchId(id)) data, err := index.db.Get(index.ro, keyBuf) if err != nil { return err } var idRefs []element.IdRefs if data != nil { idRefs = binary.UnmarshalIdRefsBunch(data) } idRefBunch := idRefBunch{index.getBunchId(id), idRefs} idRef := idRefBunch.getCreate(id) idRef.Add(ref) data = binary.MarshalIdRefsBunch(idRefBunch.idRefs) return index.db.Put(index.wo, keyBuf, data) }
func (index *bunchRefCache) Delete(id int64) error { if index.linearImport { panic("programming error: delete not supported in linearImport mode") } keyBuf := idToKeyBuf(index.getBunchId(id)) data, err := index.db.Get(index.ro, keyBuf) if err != nil { return err } if data != nil { idRefs := binary.UnmarshalIdRefsBunch(data) idRefBunch := idRefBunch{index.getBunchId(id), idRefs} idRef := idRefBunch.get(id) if idRef != nil { idRef.Refs = []int64{} data := binary.MarshalIdRefsBunch(idRefs) return index.db.Put(index.wo, keyBuf, data) } } return nil }