-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package transaction | ||
|
||
import ( | ||
"github.com/ethersphere/bee/pkg/storage" | ||
"github.com/ethersphere/bee/pkg/storage/storageutil" | ||
) | ||
|
||
type memcache struct { | ||
storage.IndexStore | ||
data map[string][]byte | ||
} | ||
|
||
func NewMemCache(st storage.IndexStore) *memcache { | ||
return &memcache{st, make(map[string][]byte)} | ||
} | ||
|
||
func (m *memcache) Get(i storage.Item) error { | ||
if val, ok := m.data[key(i)]; ok { | ||
return i.Unmarshal(val) | ||
} | ||
|
||
if err := m.IndexStore.Get(i); err != nil { | ||
return err | ||
} | ||
|
||
m.add(i) | ||
|
||
return nil | ||
} | ||
|
||
func (m *memcache) Has(k storage.Key) (bool, error) { | ||
if _, ok := m.data[key(k)]; ok { | ||
return true, nil | ||
} | ||
return m.IndexStore.Has(k) | ||
} | ||
|
||
func (m *memcache) Put(i storage.Item) error { | ||
m.add(i) | ||
return m.IndexStore.Put(i) | ||
} | ||
|
||
func (m *memcache) Delete(i storage.Item) error { | ||
delete(m.data, key(i)) | ||
return m.IndexStore.Delete(i) | ||
} | ||
|
||
// key returns a string representation of the given key. | ||
func key(key storage.Key) string { | ||
return storageutil.JoinFields(key.Namespace(), key.ID()) | ||
} | ||
|
||
// add caches given item. | ||
func (m *memcache) add(i storage.Item) { | ||
b, err := i.Marshal() | ||
if err != nil { | ||
return | ||
} | ||
|
||
m.data[key(i)] = b | ||
} | ||
|
||
func (m *memcache) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package transaction_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/pkg/storage/leveldbstore" | ||
"github.com/ethersphere/bee/pkg/storage/storagetest" | ||
"github.com/ethersphere/bee/pkg/storer/internal/transaction" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
t.Parallel() | ||
|
||
store, err := leveldbstore.New(t.TempDir(), nil) | ||
if err != nil { | ||
t.Fatalf("create store failed: %v", err) | ||
} | ||
|
||
cache := transaction.NewMemCache(store) | ||
storagetest.TestStore(t, cache) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters