Implementing the first challenge
This commit is contained in:
+17
-3
@@ -1,5 +1,7 @@
|
|||||||
package db0101
|
package db0101
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
type KV struct {
|
type KV struct {
|
||||||
mem map[string][]byte
|
mem map[string][]byte
|
||||||
}
|
}
|
||||||
@@ -11,10 +13,22 @@ func (kv *KV) Open() error {
|
|||||||
|
|
||||||
func (kv *KV) Close() error { return nil }
|
func (kv *KV) Close() error { return nil }
|
||||||
|
|
||||||
func (kv *KV) Get(key []byte) (val []byte, ok bool, err error)
|
func (kv *KV) Get(key []byte) (val []byte, ok bool, err error) {
|
||||||
|
val, ok = kv.mem[string(key)]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (kv *KV) Set(key []byte, val []byte) (updated bool, err error)
|
func (kv *KV) Set(key []byte, val []byte) (updated bool, err error) {
|
||||||
|
prev, exist := kv.mem[string(key)]
|
||||||
|
kv.mem[string(key)] = val
|
||||||
|
updated = !exist || !bytes.Equal(prev, val)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (kv *KV) Del(key []byte) (deleted bool, err error)
|
func (kv *KV) Del(key []byte) (deleted bool, err error) {
|
||||||
|
_, deleted = kv.mem[string(key)]
|
||||||
|
delete(kv.mem, string(key))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// QzBQWVJJOUhU https://trialofcode.org/
|
// QzBQWVJJOUhU https://trialofcode.org/
|
||||||
|
|||||||
Reference in New Issue
Block a user