goku

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: MIT Imports: 8 Imported by: 0

README

Goku

Build Status Go Report codecov

Goku is a library written in Go to create a simple thread safe in-memory datastore that is persisted to disk.

Usage

package main

import (
    "fmt"
    "github.com/abhicnv007/goku"
)

func main() {
    g := goku.New(".db")
    defer g.Close()

    g.Add("foo", "bar")
    if val, ok := g.Get("foo"); ok {
        // use val
        fmt.Println(val) // Output: bar
    }
}

How does it work?

Goku creates an append only file at the location and logs out all operations to this file. In the event of a crash, the events from log are replayed and the data is reconstructed in memory.

g := goku.New(".db")
g.Add("foo", "bar")
// many more adds

// program crash

g = goku.New(".db") // load the data back from the file
g.Get("foo") // bar

The .db file can be freely trnasferred.

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/abhicnv007/goku
BenchmarkAdd-12    	  275427	     19766 ns/op	   10483 B/op	       2 allocs/op
BenchmarkGet-12    	44926000	       401 ns/op	       0 B/op	       0 allocs/op

TODO

  1. Allow for dump and restore
  2. Test file corruption

Documentation

Overview

Package goku is a library that implements an in memory but persistant datastore

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goku

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

Goku is the core data strcture of the library. It stores all the key value pairs and pointers to the log file

func New

func New(dbPath string) Goku

New creates a new instance of Goku. Each instance has it's own private store. It creates a append log at the path given, which contains all operations performed on Goku allowing recovery of data in case of program crash.

Example:

g := goku.New(".goku_data")

func (*Goku) Add

func (g *Goku) Add(key string, value string)

Add a key value pair to the Goku instance and also persists the operation to disk.

func (*Goku) Clear

func (g *Goku) Clear()

Clear deletes all elements and truncates the log from disk.

Example:

g := goku.New(".db")
defer g.Clear()

func (*Goku) Close

func (g *Goku) Close()

Close file handlers and remove lock files

Example:

g := goku.New(".db")
defer g.Close()

func (*Goku) Count

func (g *Goku) Count() int

Count returns the number of items saved

func (*Goku) Get

func (g *Goku) Get(key string) (val string, ok bool)

Get the value saved for a key

Directories

Path Synopsis
Package filelock implements a lock for mutually exclusive access of files
Package filelock implements a lock for mutually exclusive access of files

Jump to

Keyboard shortcuts

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