Skip to content

Commit

Permalink
fix byte copy in store outputs saving
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Mar 21, 2024
1 parent 00d10a5 commit 17c5215
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions storage/store/partial_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,19 @@ func (p *PartialKV) ApplyDeltasReverse(deltas []*pbsubstreams.StoreDelta) {
panic("caching store cannot be used with deltas")
}

// apparently this is faster than append() method
func cloneBytes(b []byte) []byte {
out := make([]byte, len(b))
copy(out, b)
return out
}

func (p *PartialKV) Set(ord uint64, key string, value string) {
p.operations.Operations = append(p.operations.Operations, &pbssinternal.Operation{
Type: pbssinternal.Operation_SET,
Ord: ord,
Key: key,
Value: []byte(value),
Value: cloneBytes([]byte(value)),
})

p.baseStore.Set(ord, key, value)
Expand All @@ -234,7 +241,7 @@ func (p *PartialKV) SetBytes(ord uint64, key string, value []byte) {
Type: pbssinternal.Operation_SET_BYTES,
Ord: ord,
Key: key,
Value: value,
Value: cloneBytes(value),
})

p.baseStore.SetBytes(ord, key, value)
Expand All @@ -245,7 +252,7 @@ func (p *PartialKV) SetIfNotExists(ord uint64, key string, value string) {
Type: pbssinternal.Operation_SET_IF_NOT_EXISTS,
Ord: ord,
Key: key,
Value: []byte(value),
Value: cloneBytes([]byte(value)),
})

p.baseStore.SetIfNotExists(ord, key, value)
Expand All @@ -256,7 +263,7 @@ func (p *PartialKV) SetBytesIfNotExists(ord uint64, key string, value []byte) {
Type: pbssinternal.Operation_SET_BYTES_IF_NOT_EXISTS,
Ord: ord,
Key: key,
Value: value,
Value: cloneBytes(value),
})

p.baseStore.SetBytesIfNotExists(ord, key, value)
Expand All @@ -267,7 +274,7 @@ func (p *PartialKV) Append(ord uint64, key string, value []byte) error {
Type: pbssinternal.Operation_APPEND,
Ord: ord,
Key: key,
Value: value,
Value: cloneBytes(value),
})

return p.baseStore.Append(ord, key, value)
Expand Down

0 comments on commit 17c5215

Please sign in to comment.