-
Notifications
You must be signed in to change notification settings - Fork 0
/
xisdb.go
95 lines (83 loc) · 2.36 KB
/
xisdb.go
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
package xisdb
import (
"github.com/alexsward/xisdb/indexes"
"github.com/alexsward/xisdb/tree"
)
// This file contains xisdb functions that manage transactions for the user
// If you need complicated interactions, you need to use the db.Read() and
// db.ReadWrite() functions directly with a transaction function
// Bucket creates a bucket with the given name in the database, or no-op if it exists
func (db *DB) Bucket(name string) error {
return db.ReadWrite(func(tx *Tx) error {
_, err := tx.Bucket(name)
return err
})
}
// DeleteBucket will delete a bucket from the database, if it exists
func (db *DB) DeleteBucket(name string) error {
return db.ReadWrite(func(tx *Tx) error {
_, err := tx.DeleteBucket(name)
return err
})
}
// Get returns a value from the database
func (db *DB) Get(key string) (string, error) {
var val string
err := db.Read(func(tx *Tx) error {
v, err := tx.Get(key)
val = v
return err
})
return val, err
}
// Exists will tell you if a key exists in the database
func (db *DB) Exists(key string) (bool, error) {
does := false
err := db.Read(func(tx *Tx) error {
exists, err := tx.Exists(key)
does = exists
return err
})
return does, err
}
// Set adds/updates an object in the database
func (db *DB) Set(key, value string) error {
return db.ReadWrite(func(tx *Tx) error {
return tx.Set(key, value, nil)
})
}
// Delete removes an object from the database
func (db *DB) Delete(key string) (bool, error) {
var existed bool
err := db.ReadWrite(func(tx *Tx) error {
e, err := tx.Delete(key)
existed = e
return err
})
return existed, err
}
// AddIndex will add an index to the database
// Will match using the given Matcher and uses the tree.Comparator function
func (db *DB) AddIndex(name string, it IndexType, m indexes.Matcher, c tree.Comparator) error {
if name == "" {
return ErrInvalidIndexName
}
b := db.root()
if _, exists := b.indexes[name]; exists {
return ErrIndexAlreadyExists
}
return db.ReadWrite(func(tx *Tx) error {
return tx.AddIndex(name, it, m, c)
})
}
// DeleteIndex will remove an index by name, if it exists.
// Returns whether or not it was deleted along with any error that may have occured
func (db *DB) DeleteIndex(name string) (bool, error) {
var deleted bool
err := db.ReadWrite(func(tx *Tx) error {
existed, err := tx.DeleteIndex(name)
deleted = existed
return err
})
return deleted, err
}