added docs and comments
This commit is contained in:
parent
4eae02d0e2
commit
b87a297986
@ -1,3 +1,6 @@
|
|||||||
# kvrepl
|
# kvrepl
|
||||||
|
|
||||||
A simple command line REPL (read-eval-print loop) that drives a simple in-memory key/value storage system. This system should also allow for nested transactions. A transaction can then be committed or aborted.
|
[![GoDoc](https://godoc.org/s.mcquay.me/dm/kvrepl?status.svg)](https://godoc.org/s.mcquay.me/dm/kvrepl)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/s.mcquay.me/dm/kvrepl)](https://goreportcard.com/report/s.mcquay.me/dm/kvrepl)
|
||||||
|
|
||||||
|
A simple command line REPL (read-eval-print loop) that drives a simple in-memory key/value storage system. This system should also allow for nested transactions. A transaction can then be committed or aborted.
|
||||||
|
6
db.go
6
db.go
@ -2,6 +2,8 @@ package kvrepl
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
|
// DB is struct defining two maps, pkv (points to kv) and nest, which
|
||||||
|
// aids figuring out how nested the commits are for current transaction
|
||||||
type DB struct {
|
type DB struct {
|
||||||
KV1 map[string]string
|
KV1 map[string]string
|
||||||
KV2 map[string]string
|
KV2 map[string]string
|
||||||
@ -9,6 +11,7 @@ type DB struct {
|
|||||||
nest int
|
nest int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read reads a key out of a map and checks for parsing errors
|
||||||
func read(args []string, kv map[string]string) (string, error) {
|
func read(args []string, kv map[string]string) (string, error) {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return "", fmt.Errorf("incorrect usage: READ <key>")
|
return "", fmt.Errorf("incorrect usage: READ <key>")
|
||||||
@ -20,6 +23,7 @@ func read(args []string, kv map[string]string) (string, error) {
|
|||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write writes key/value to map and checks for parsing errors
|
||||||
func write(args []string, kv map[string]string) error {
|
func write(args []string, kv map[string]string) error {
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
return fmt.Errorf("incorrect usage: WRITE <key> <value>")
|
return fmt.Errorf("incorrect usage: WRITE <key> <value>")
|
||||||
@ -28,6 +32,7 @@ func write(args []string, kv map[string]string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// del deletes a key from map and checks for parsing errors
|
||||||
func del(args []string, kv map[string]string) error {
|
func del(args []string, kv map[string]string) error {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return fmt.Errorf("incorrect usage: DELETE <key>")
|
return fmt.Errorf("incorrect usage: DELETE <key>")
|
||||||
@ -40,6 +45,7 @@ func del(args []string, kv map[string]string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// whichDB returns which db is currently being written to
|
||||||
func whichDB(d *DB) map[string]string {
|
func whichDB(d *DB) map[string]string {
|
||||||
if d.PKV {
|
if d.PKV {
|
||||||
return d.KV1
|
return d.KV1
|
||||||
|
2
parse.go
2
parse.go
@ -6,6 +6,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Parse decides what commands to run and returns a string containing the
|
||||||
|
// value of a read or an error if one occured.
|
||||||
func Parse(s string, d *DB) (string, error) {
|
func Parse(s string, d *DB) (string, error) {
|
||||||
args := strings.Split(s, " ")
|
args := strings.Split(s, " ")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user