From 1cb8a36a08883b785d9bb0a4be1ddc00f1f9d358 Mon Sep 17 00:00:00 2001 From: minchao Date: Wed, 27 Feb 2019 20:20:54 +0800 Subject: [PATCH] storage/memory: Fix upsert with pre-existing key will causes duplicate records (#88) Closes #80 Signed-off-by: minchao --- storage/manager_memory.go | 16 ++++++++++++++-- storage/manager_test.go | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/storage/manager_memory.go b/storage/manager_memory.go index 64621a84d..d12f51c3b 100644 --- a/storage/manager_memory.go +++ b/storage/manager_memory.go @@ -50,9 +50,21 @@ func (m *MemoryManager) Upsert(_ context.Context, collection, key string, value // no need to evaluate, just create collection if necessary. m.collection(collection) + m.Lock() - m.items[collection] = append(m.items[collection], memoryItem{Key: key, Data: b.Bytes()}) - m.Unlock() + defer m.Unlock() + + var found bool + for k, i := range m.items[collection] { + if i.Key == key { + m.items[collection][k].Data = b.Bytes() + found = true + break + } + } + if !found { + m.items[collection] = append(m.items[collection], memoryItem{Key: key, Data: b.Bytes()}) + } return nil } diff --git a/storage/manager_test.go b/storage/manager_test.go index e24126df8..ea81f36b6 100644 --- a/storage/manager_test.go +++ b/storage/manager_test.go @@ -89,6 +89,21 @@ func TestMemoryManager(t *testing.T) { assert.EqualValues(t, 1234, vs) }) + t.Run("case=upsert", func(t *testing.T) { + var v string + require.NoError(t, m.Upsert(ctx, "test-upsert", "foo", "bar")) + require.NoError(t, m.Get(ctx, "test-upsert", "foo", &v)) + assert.Equal(t, "bar", v) + + require.NoError(t, m.Upsert(ctx, "test-upsert", "foo", "baz")) + require.NoError(t, m.Get(ctx, "test-upsert", "foo", &v)) + assert.Equal(t, "baz", v) + + var vs []string + require.NoError(t, m.List(ctx, "test-upsert", &vs, 10, 0)) + assert.Equal(t, 1, len(vs)) + }) + t.Run("case=list", func(t *testing.T) { for i := 0; i < 10; i++ { require.NoError(t, m.Upsert(ctx, "test-list", fmt.Sprintf("list-%d", i), i))