Skip to content

Commit

Permalink
don't store name uselessly
Browse files Browse the repository at this point in the history
  • Loading branch information
BrannonKing authored and roylee17 committed Jul 21, 2021
1 parent ac6a7ad commit 7dff7f9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion claimtrie/node/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions claimtrie/node/noderepo/noderepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 8 additions & 4 deletions claimtrie/node/noderepo/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 7dff7f9

Please sign in to comment.