jsonstore

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2018 License: MIT Imports: 7 Imported by: 17

README ΒΆ

jsonstore πŸͺ

GoDoc

JSONStore is a Go-library for a simple thread-safe in-memory JSON key-store with persistent backend. It's made for those times where you don't need a RDBMS like MySQL, or a NoSQL like MongoDB - basically when you just need a simple keystore. A really simple keystore. JSONStore is used in those times you don't need a distributed keystore like etcd, or a remote keystore Redis or a local keystore like Bolt. Its really for those times where you just need a JSON file.

Usage

First, install the library using:

go get -u -v github.com/schollz/jsonstore

Then you can add it to your program. Check out the examples, or see below for basic usage:

ks := new(jsonstore.JSONStore)

// set a key to any object you want
type Human struct {
  Name   string
  Height float64
}
err := ks.Set("human:1", Human{"Dante", 5.4})
if err != nil {
  panic(err)
}

// Saving will automatically gzip if .gz is provided
if err = jsonstore.Save(ks, "humans.json.gz"); err != nil {
  panic(err)
}

// Load any JSON / GZipped JSON
ks2, err := jsonstore.Open("humans.json.gz")
if err != nil {
  panic(err)
}

// get the data back via an interface
var human Human
err = ks2.Get("human:1", &human)
if err != nil {
  panic(err)
}
fmt.Println(human.Name) // Prints 'Dante'

The datastore on disk is then contains:

$ zcat humans.json.gz
{
"human:1": "{\"Name\":\"Dante\",\"Height\":5.4}"
}

JSONStore in the wild:

Dev

Benchmark against using Redis and BoltDB as KeyStores using Go1.8 (Intel i5-4310U CPU @ 2.00GHz). Take away is that setting/getting is faster in JSONStore (because its just a map), but opening is much slower (because its a file that is read into memory). So don't use this if you have to store 1,000,000+ things!

$ go test -bench=. tests/redis/* > redis.txt
$ go test -bench=. tests/bolt/* > bolt.txt
$ go test -bench=. > jsonstore.txt
$ benchcmp bolt.txt jsonstore.txt
benchmark                old ns/op     new ns/op     delta
BenchmarkSet-4           5471747       1847          -99.97%
BenchmarkGet-4           2424          1479          -38.99%
BenchmarkOpen100-4       11168         148035        +1225.53%
BenchmarkOpen10000-4     10095         19722376      +195267.77%
$ benchcmp redis.txt jsonstore.txt
benchmark                old ns/op     new ns/op     delta
BenchmarkSet-4           24717         1847          -92.53%
BenchmarkGet-4           22561         1479          -93.44%
BenchmarkOpen100-4       6221          148035        +2279.60%
BenchmarkOpen10000-4     4951          19722376      +398251.36%

License

MIT

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Save ΒΆ

func Save(ks *JSONStore, filename string) (err error)

Save writes the jsonstore to disk

Types ΒΆ

type JSONStore ΒΆ

type JSONStore struct {
	Data map[string]json.RawMessage
	sync.RWMutex
}

JSONStore is the basic store object.

func Open ΒΆ

func Open(filename string) (*JSONStore, error)

Open will load a jsonstore from a file.

func (*JSONStore) Delete ΒΆ

func (s *JSONStore) Delete(key string)

Delete removes a key from the store.

func (*JSONStore) Get ΒΆ

func (s *JSONStore) Get(key string, v interface{}) error

Get will return the value associated with a key.

func (*JSONStore) GetAll ΒΆ

func (s *JSONStore) GetAll(re *regexp.Regexp) map[string]json.RawMessage

GetAll is like a filter with a regexp. If the regexp is nil, then it returns everything.

func (*JSONStore) Keys ΒΆ

func (s *JSONStore) Keys() []string

Keys returns all the keys currently in map

func (*JSONStore) Set ΒΆ

func (s *JSONStore) Set(key string, value interface{}) error

Set saves a value at the given key.

type NoSuchKeyError ΒΆ

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

NoSuchKeyError is thrown when calling Get with invalid key

func (NoSuchKeyError) Error ΒΆ

func (err NoSuchKeyError) Error() string

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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