From f92ab131e4e40da401faf1523412cbd08af93f4f Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Thu, 26 Mar 2015 14:28:49 -0700 Subject: [PATCH] KVStore gtreap implementation copies value bytes --- index/store/gtreap/gtreap.go | 7 +++++-- index/store/gtreap/gtreap_test.go | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index/store/gtreap/gtreap.go b/index/store/gtreap/gtreap.go index 5992ca66..514455b3 100644 --- a/index/store/gtreap/gtreap.go +++ b/index/store/gtreap/gtreap.go @@ -125,7 +125,7 @@ func (w *Store) Iterator(k []byte) store.KVIterator { func (w *Store) Set(k, v []byte) (err error) { w.m.Lock() - w.t = w.t.Upsert(&Item{k: k, v: v}, rand.Int()) + w.t = w.t.Upsert(&Item{k: k, v: append([]byte(nil), v...)}, rand.Int()) w.m.Unlock() return nil } @@ -243,7 +243,7 @@ func (w *Iterator) Close() error { } func (w *Batch) Set(k, v []byte) { - w.items = append(w.items, &Item{k, v}) + w.items = append(w.items, &Item{k, append([]byte(nil), v...)}) } func (w *Batch) Delete(k []byte) { @@ -269,11 +269,14 @@ func (w *Batch) Execute() (err error) { if itm != nil { v = itm.(*Item).v } + // NOTE: mc.Merge() doesn't modify its k or v params. v, err := mc.Merge(k, v) if err != nil { return err } if v != nil { + // NOTE: We don't re-copy the result from mc.Merge(), + // as it'll return brand new unshared bytes. t = t.Upsert(&Item{k: k, v: v}, rand.Int()) } else { t = t.Delete(&Item{k: k}) diff --git a/index/store/gtreap/gtreap_test.go b/index/store/gtreap/gtreap_test.go index 9c66e968..19cbf317 100644 --- a/index/store/gtreap/gtreap_test.go +++ b/index/store/gtreap/gtreap_test.go @@ -38,7 +38,6 @@ func TestReaderIsolation(t *testing.T) { } func CommonTestKVStore(t *testing.T, s store.KVStore) { - writer, err := s.Writer() if err != nil { t.Error(err) @@ -162,11 +161,13 @@ func CommonTestReaderIsolation(t *testing.T, s store.KVStore) { if err != nil { t.Error(err) } - err = writer.Set([]byte("b"), []byte("val-b")) + valB := []byte("val-b") + err = writer.Set([]byte("b"), valB) if err != nil { t.Fatal(err) } writer.Close() + valB[0] = 'X' // Modify the bytes so we check that writer got its own copy. // ensure that a newer reader sees it newReader, err := s.Reader() @@ -214,5 +215,4 @@ func CommonTestReaderIsolation(t *testing.T, s store.KVStore) { if count != 1 { t.Errorf("expected iterator to see 1, saw %d", count) } - }