This repository was archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
133 lines (113 loc) · 3.53 KB
/
store.go
File metadata and controls
133 lines (113 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2018 Anders Normal. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
// Package store provides a simple persistent key-value store for Go values.
// It stores any gob-encodable Go value via a identifiying string. it is very
// lightweight and ideal to be used in applications to persist state.
//
// The API is simple - you can Get(), Put() and Delete() entries.
// These methods are goroutine-safe.
//
// store used LevelDB to store key/value and the encoding/gob package for
// encoding and decoding the values.
package store
import (
"bytes"
"encoding/gob"
"errors"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
// Store represents the key/value store.
// Use the Open() method to open one, and Close() to close one.
type Store struct {
db *leveldb.DB
}
var (
// ErrKeyNotExist is returned when the supplied key to Get() or Delete()
// does not exists in the database.
ErrKeyNotExist = errors.New("store: key does not exist")
// ErrBadValue is returned when the value supplied to Put() in the database
// could not be encoded or is nil.
ErrBadValue = errors.New("store: bad value")
)
// Open a key/value store. "path" is the full path to LevelDB database file.
// The DB will be created if not exist, unless ErrorIfMissing is true.
// You can pass in options to LevelDB via "o".
func Open(path string, o *opt.Options) (*Store, error) {
var err error
db, err := leveldb.OpenFile(path, o)
if err != nil {
return nil, err
}
var store = new(Store)
store.db = db
return store, err
}
// Get a value from the store. "value" must be a pointer to a type,
// because of underlying gob. If the key is not present in the store,
// an ErrKeyNotExist is returned.
//
// type MyStruct struct {
// Numbers []int
// }
// var val MyStruct
// if err := s.Get(store.Byte("key42"), &val); err == store.ErrKeyNotExist {
// // "key42" not found
// }
// if err != nil {
// // some other error occured
// }
//
// The value passed to Get() can be nil. Which gives you the same result as
// Exist().
//
// if err := s.Get(store.Byte("key42"), nil); err == nil {
// fmt.Println("entry is present")
// }
func (s *Store) Get(key []byte, value interface{}) error {
var err error
v, err := s.db.Get(key, nil)
if err != nil || value == nil {
return err
}
d := gob.NewDecoder(bytes.NewReader(v))
return d.Decode(value)
}
// Put is creating an entry in the store. It overwrites any previous existing
// value for the given key. The passed value is gob-encoded and stored.
// The value cannot be nil and Put() returns an ErrBadValue.
//
// err := s.Put(store.Byte("key42"), 42)
// err := s.Put(store.Byte("key42"), "the question to live and the universe.")
// m := map[string]int{
// "foo": 0,
// "bar": 1
// }
// err := s.Put(store.Byte("key42"), m)
func (s *Store) Put(key []byte, value interface{}) error {
var err error
if value == nil {
return ErrBadValue
}
var b bytes.Buffer
if err = gob.NewEncoder(&b).Encode(value); err != nil {
return err
}
return s.db.Put(key, b.Bytes(), nil)
}
// Delete an entry of a given key from the store.
// If no such key is present in the store it will not return an error.
//
// s.Delete(store.Byte("key42"))
func (s *Store) Delete(key []byte) error {
return s.db.Delete(key, nil)
}
// Close closes the key/value database file.
func (s *Store) Close() error {
return s.db.Close()
}
// Byte is converting a string to []byte stream.
func Byte(b string) []byte {
return []byte(b)
}