Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59 lines (51 sloc)
1.14 KB
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
// Package memtable contains implementation of key-value store | |
package memtable | |
import ( | |
"sync" | |
"time" | |
) | |
// KVRow individual row in db | |
type KVRow struct { | |
Key string | |
Value string | |
createdAt int64 | |
} | |
// KVStore DB memory map | |
type KVStore struct { | |
data map[string]KVRow | |
mux sync.Mutex | |
} | |
// Get value for a key | |
func (s *KVStore) Get(key string) (string, error) { | |
if s.data[key] != (KVRow{}) { | |
return s.data[key].Value, nil | |
} | |
return "", ErrKeyNotFound | |
} | |
// Create to save data | |
func (s *KVStore) Create(key, value string) (string, error) { | |
s.mux.Lock() | |
defer s.mux.Unlock() | |
s.data[key] = KVRow{key, value, time.Now().Unix()} | |
return "Inserted 1", nil | |
} | |
// Delete row data by key | |
func (s *KVStore) Delete(key string) (string, error) { | |
s.mux.Lock() | |
defer s.mux.Unlock() | |
if found, _ := s.Get(key); len(found) == 0 { | |
return "Deleted 0", ErrKeyNotFound | |
} | |
delete(s.data, key) | |
return "Deleted 1", nil | |
} | |
// Singleton KVStore instance | |
var once sync.Once | |
var store *KVStore | |
// NewDB returns a singleton KvStore instance | |
func NewDB() (store *KVStore) { | |
once.Do(func() { | |
store = &KVStore{data: make(map[string]KVRow)} | |
}) | |
return store | |
} |