mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Store: Add Iterate function
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoryStore is a simple in-memory key/value store for testing purposes.
|
// MemoryStore is a simple in-memory key-value store for testing purposes.
|
||||||
type MemoryStore struct {
|
type MemoryStore struct {
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
data map[string][]byte
|
data map[string][]byte
|
||||||
@@ -39,7 +39,7 @@ func (ms *MemoryStore) ExpireKeys() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set stores the key/value pair.
|
// Set stores the key-value pair.
|
||||||
func (ms *MemoryStore) Set(key []byte, data []byte) error {
|
func (ms *MemoryStore) Set(key []byte, data []byte) error {
|
||||||
ms.mutex.Lock()
|
ms.mutex.Lock()
|
||||||
ms.data[string(key)] = data
|
ms.data[string(key)] = data
|
||||||
@@ -47,7 +47,7 @@ func (ms *MemoryStore) Set(key []byte, data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// StoreExpire stores the key/value pair and deletes it after the expiration time.
|
// StoreExpire stores the key-value pair and deletes it after the expiration time.
|
||||||
func (ms *MemoryStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
|
func (ms *MemoryStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
|
||||||
ms.mutex.Lock()
|
ms.mutex.Lock()
|
||||||
ms.expireMap[string(key)] = expiration
|
ms.expireMap[string(key)] = expiration
|
||||||
@@ -64,7 +64,7 @@ func (ms *MemoryStore) Get(key []byte) (data []byte, found bool) {
|
|||||||
return data, found
|
return data, found
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes a key/value pair.
|
// Delete deletes a key-value pair.
|
||||||
func (ms *MemoryStore) Delete(key []byte) {
|
func (ms *MemoryStore) Delete(key []byte) {
|
||||||
ms.mutex.Lock()
|
ms.mutex.Lock()
|
||||||
delete(ms.expireMap, string(key))
|
delete(ms.expireMap, string(key))
|
||||||
@@ -78,3 +78,17 @@ func (ms *MemoryStore) Count() uint64 {
|
|||||||
defer ms.mutex.Unlock()
|
defer ms.mutex.Unlock()
|
||||||
return uint64(len(ms.data))
|
return uint64(len(ms.data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterate iterates over all records.
|
||||||
|
func (ms *MemoryStore) Iterate(callback func(key, value []byte)) {
|
||||||
|
ms.mutex.Lock()
|
||||||
|
defer ms.mutex.Unlock()
|
||||||
|
|
||||||
|
for key, value := range ms.data {
|
||||||
|
ms.mutex.Unlock() // allow access to the map while calling the callback
|
||||||
|
|
||||||
|
callback([]byte(key), value)
|
||||||
|
|
||||||
|
ms.mutex.Lock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/akrylysov/pogreb"
|
"github.com/akrylysov/pogreb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PogrebStore is a key/value store using Pogreb.
|
// PogrebStore is a key-value store using Pogreb.
|
||||||
// Expiration is currently not supported.
|
// Expiration is currently not supported.
|
||||||
type PogrebStore struct {
|
type PogrebStore struct {
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
@@ -45,12 +45,12 @@ func (store *PogrebStore) ExpireKeys() {
|
|||||||
// Not yet implemented
|
// Not yet implemented
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store stores the key/value pair.
|
// Store stores the key-value pair.
|
||||||
func (store *PogrebStore) Set(key []byte, data []byte) error {
|
func (store *PogrebStore) Set(key []byte, data []byte) error {
|
||||||
return store.db.Put(key, data)
|
return store.db.Put(key, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StoreExpire stores the key/value pair and deletes it after the expiration time.
|
// StoreExpire stores the key-value pair and deletes it after the expiration time.
|
||||||
func (store *PogrebStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
|
func (store *PogrebStore) StoreExpire(key []byte, data []byte, expiration time.Time) error {
|
||||||
// Not yet implemented
|
// Not yet implemented
|
||||||
return errors.New("not yet implemented")
|
return errors.New("not yet implemented")
|
||||||
@@ -65,7 +65,7 @@ func (store *PogrebStore) Get(key []byte) (data []byte, found bool) {
|
|||||||
return value, true
|
return value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes a key/value pair.
|
// Delete deletes a key-value pair.
|
||||||
func (store *PogrebStore) Delete(key []byte) {
|
func (store *PogrebStore) Delete(key []byte) {
|
||||||
store.db.Delete(key)
|
store.db.Delete(key)
|
||||||
}
|
}
|
||||||
@@ -74,3 +74,16 @@ func (store *PogrebStore) Delete(key []byte) {
|
|||||||
func (store *PogrebStore) Count() uint64 {
|
func (store *PogrebStore) Count() uint64 {
|
||||||
return uint64(store.db.Count())
|
return uint64(store.db.Count())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterate iterates over all records.
|
||||||
|
func (store *PogrebStore) Iterate(callback func(key, value []byte)) {
|
||||||
|
iterator := store.db.Items()
|
||||||
|
for {
|
||||||
|
key, value, err := iterator.Next()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,17 +14,17 @@ import (
|
|||||||
|
|
||||||
// Store is the interface for implementing the storage mechanism for the DHT.
|
// Store is the interface for implementing the storage mechanism for the DHT.
|
||||||
type Store interface {
|
type Store interface {
|
||||||
// Set stores the key/value pair.
|
// Set stores the key-value pair.
|
||||||
Set(key []byte, data []byte) error
|
Set(key []byte, data []byte) error
|
||||||
|
|
||||||
// StoreExpire stores the key/value pair and deletes it after the expiration time.
|
// StoreExpire stores the key-value pair and deletes it after the expiration time.
|
||||||
// If key/value already exists, it will be overwritten and the new expiration time applies.
|
// If key-value already exists, it will be overwritten and the new expiration time applies.
|
||||||
StoreExpire(key []byte, data []byte, expiration time.Time) error
|
StoreExpire(key []byte, data []byte, expiration time.Time) error
|
||||||
|
|
||||||
// Get returns the value for the key if present.
|
// Get returns the value for the key if present.
|
||||||
Get(key []byte) (data []byte, found bool)
|
Get(key []byte) (data []byte, found bool)
|
||||||
|
|
||||||
// Delete deletes a key/value pair.
|
// Delete deletes a key-value pair.
|
||||||
Delete(key []byte)
|
Delete(key []byte)
|
||||||
|
|
||||||
// ExpireKeys is called to delete all keys that are marked for expiration.
|
// ExpireKeys is called to delete all keys that are marked for expiration.
|
||||||
@@ -32,4 +32,7 @@ type Store interface {
|
|||||||
|
|
||||||
// Count returns the total number of records stored.
|
// Count returns the total number of records stored.
|
||||||
Count() uint64
|
Count() uint64
|
||||||
|
|
||||||
|
// Iterate iterates over all records.
|
||||||
|
Iterate(callback func(key, value []byte))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user