storage

package
v0.0.0-...-be88cca Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package storage implements a simple Key Value Storage system with nested transaction capabilities.

Example
package main

import (
	"fmt"
	"github.com/caasmo/kv-repl-barebones/storage"
)

func main() {

	cases := []struct {
		cmd string
		key string
		val string
	}{
		{cmd: "write", key: "a", val: "hi"},
		{cmd: "read", key: "a", val: ""},
		{cmd: "begin", key: "", val: ""},
		{cmd: "read", key: "a", val: ""},
		{cmd: "write", key: "a", val: "bye"},
		{cmd: "read", key: "a", val: ""},
		{cmd: "begin", key: "", val: ""},
		{cmd: "remove", key: "a", val: ""},
		{cmd: "read", key: "a", val: ""},
		{cmd: "commit", key: "", val: ""},
		{cmd: "read", key: "a", val: ""},
		{cmd: "write", key: "a", val: "bye now"},
		{cmd: "read", key: "a", val: ""},
		{cmd: "discard", key: "", val: ""},
		{cmd: "read", key: "a", val: ""},
	}

	store := storage.NewStore()
	for _, tc := range cases {
		v, err := store.Process(tc.cmd, tc.key, tc.val)
		if len(v) > 0 {
			fmt.Printf("%s\n", v)
		}
		if err != nil {
			fmt.Printf("%s\n", err)
		}
	}

}
Output:

hi
hi
bye
Key not found: a
Key not found: a
bye now
hi

Index

Examples

Constants

View Source
const (
	// Supported commands
	Write   = "write"
	Read    = "read"
	Remove  = "remove"
	Begin   = "begin"
	Commit  = "commit"
	Discard = "discard"
)

Variables

View Source
var (
	ErrNoCurrentTransation error = errors.New("There is no current transaction to commit")
	ErrKeyNotFound         error = errors.New("Key not found")
	ErrUnsupportedCommand  error = errors.New("Unsupported command")
)

Functions

This section is empty.

Types

type Store

type Store struct {
	// contains filtered or unexported fields
}

A Store represents a key value storage system with transaction capabilities. A Store contains the kvStore and a pointer to the data that can eventually be commited to the kvStore (currTx).

func NewStore

func NewStore() *Store

NewStore returns a Store.

func (*Store) Process

func (s *Store) Process(command, key, value string) (string, error)

Process processes a command.

It returns an error if the Store does not support the command. It also returns an error if the Store rejects the command.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL