don't store name uselessly

This commit is contained in:
Brannon King 2021-07-21 12:13:05 -04:00 committed by Roy Lee
parent ac6a7ad121
commit 7dff7f9dd8
3 changed files with 12 additions and 8 deletions

View file

@ -226,7 +226,7 @@ func (nm *BaseManager) IncrementHeightTo(height int32) ([][]byte, error) {
names = append(names, nm.changes[i].Name)
}
if err := nm.repo.AppendChanges(nm.changes); err != nil {
if err := nm.repo.AppendChanges(nm.changes); err != nil { // destroys names
return nil, fmt.Errorf("save changes to node repo: %w", err)
}

View file

@ -179,10 +179,10 @@ func TestIterator(t *testing.T) {
err = repo.AppendChanges(creation)
r.NoError(err)
var received []change.Change
i := 0
repo.IterateChildren([]byte{}, func(changes []change.Change) bool {
received = append(received, changes...)
r.Equal(creation[i], changes[0])
i++
return true
})
r.Equal(creation, received)
}

View file

@ -96,12 +96,14 @@ func (repo *Pebble) AppendChanges(changes []change.Change) error {
// TODO: switch to buffer pool and reuse encoder
for _, chg := range changes {
name := chg.Name
chg.Name = nil // don't waste the storage space on this (annotation a better approach?)
value, err := msgpack.Marshal(chg)
if err != nil {
return fmt.Errorf("msgpack marshal value: %w", err)
}
err = batch.Merge(chg.Name, value, pebble.NoSync)
err = batch.Merge(name, value, pebble.NoSync)
if err != nil {
return fmt.Errorf("pebble set: %w", err)
}
@ -124,10 +126,10 @@ func (repo *Pebble) LoadChanges(name []byte) ([]change.Change, error) {
defer closer.Close()
}
return unmarshalChanges(data)
return unmarshalChanges(name, data)
}
func unmarshalChanges(data []byte) ([]change.Change, error) {
func unmarshalChanges(name, data []byte) ([]change.Change, error) {
var changes []change.Change
dec := msgpack.GetDecoder()
defer msgpack.PutDecoder(dec)
@ -140,6 +142,7 @@ func unmarshalChanges(data []byte) ([]change.Change, error) {
if err != nil {
return nil, fmt.Errorf("msgpack unmarshal: %w", err)
}
chg.Name = name
changes = append(changes, chg)
}
@ -189,7 +192,8 @@ func (repo *Pebble) IterateChildren(name []byte, f func(changes []change.Change)
defer iter.Close()
for iter.First(); iter.Valid(); iter.Next() {
changes, err := unmarshalChanges(iter.Value())
// NOTE! iter.Key() is ephemeral!
changes, err := unmarshalChanges(iter.Key(), iter.Value())
if err != nil {
panic(err)
}