commit of code
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package kvrepl
|
||||
|
||||
import "fmt"
|
||||
|
||||
type DB struct {
|
||||
KV1 map[string]string
|
||||
KV2 map[string]string
|
||||
PKV bool
|
||||
nest int
|
||||
}
|
||||
|
||||
func read(args []string, kv map[string]string) (string, error) {
|
||||
if len(args) != 1 {
|
||||
return "", fmt.Errorf("incorrect usage: READ <key>")
|
||||
}
|
||||
i, ok := kv[args[0]]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("key does not exist")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func write(args []string, kv map[string]string) error {
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("incorrect usage: WRITE <key> <value>")
|
||||
}
|
||||
kv[args[0]] = args[1]
|
||||
return nil
|
||||
}
|
||||
|
||||
func del(args []string, kv map[string]string) error {
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("incorrect usage: DELETE <key>")
|
||||
}
|
||||
_, ok := kv[args[0]]
|
||||
if !ok {
|
||||
return fmt.Errorf("key does not exist")
|
||||
}
|
||||
delete(kv, args[0])
|
||||
return nil
|
||||
}
|
||||
|
||||
func whichDB(d *DB) map[string]string {
|
||||
if d.PKV {
|
||||
return d.KV1
|
||||
}
|
||||
return d.KV2
|
||||
}
|
||||
Reference in New Issue
Block a user