425 lines
9.3 KiB
Go
425 lines
9.3 KiB
Go
package boltrest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// This is a custom library for saving/syncing data
|
|
// in a local bolt db and a remote db through a
|
|
// rest api.
|
|
|
|
// DB is a struct for accomplishing this
|
|
type DB struct {
|
|
localFile string
|
|
remoteURL string
|
|
apiToken string
|
|
remoteMD5 string
|
|
online bool
|
|
isOpen bool
|
|
localDB *bolt.DB
|
|
}
|
|
|
|
// Create returns the DB object
|
|
func Create(filename string) (*DB, error) {
|
|
var err error
|
|
b := DB{localFile: filename}
|
|
if err = b.Open(); err != nil {
|
|
return nil, err
|
|
}
|
|
defer b.Close()
|
|
// Go ahead and make sure it's fresh
|
|
b.RefreshDB()
|
|
return &b, nil
|
|
}
|
|
|
|
// Open opens the DB so things can be done
|
|
func (b *DB) Open() error {
|
|
var err error
|
|
if !b.isOpen {
|
|
b.localDB, err = bolt.Open(b.localFile, 0644, nil)
|
|
if err == nil {
|
|
b.isOpen = true
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Close closes the DB
|
|
func (b *DB) Close() error {
|
|
var err error
|
|
if b.isOpen {
|
|
err = b.localDB.Close()
|
|
if err == nil {
|
|
b.isOpen = false
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// GetFilename returns the local file for the DB
|
|
func (b *DB) GetFilename() string {
|
|
return b.localFile
|
|
}
|
|
|
|
// Offline sets this DB to offline mode
|
|
// That means it won't try to sync anything
|
|
func (b *DB) Offline() {
|
|
b.online = false
|
|
}
|
|
|
|
// MkBucketPath builds all buckets in the string slice
|
|
func (b *DB) MkBucketPath(path []string) error {
|
|
var err error
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.localDB.Update(func(tx *bolt.Tx) error {
|
|
var err error
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
// Create it
|
|
bkt, err = tx.CreateBucket([]byte(path[0]))
|
|
if err != nil {
|
|
// error creating
|
|
return err
|
|
}
|
|
}
|
|
if len(path) > 1 {
|
|
path = path[1:]
|
|
for i := range path {
|
|
nextBkt := bkt.Bucket([]byte(path[i]))
|
|
if nextBkt == nil {
|
|
// Create it
|
|
nextBkt, err = bkt.CreateBucket([]byte(path[i]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
bkt = nextBkt
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
return err
|
|
}
|
|
|
|
// GetValue returns the value at path
|
|
// path is a '/' separated list of tokens
|
|
// the last token is a key, all others are buckets
|
|
func (b *DB) GetValue(path []string, key string) (string, error) {
|
|
var err error
|
|
var ret string
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
b.localDB.View(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
for idx := 1; idx < len(path); idx++ {
|
|
bkt = bkt.Bucket([]byte(path[idx]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
|
}
|
|
}
|
|
// newBkt should have the last bucket in the path
|
|
ret = string(bkt.Get([]byte(key)))
|
|
return nil
|
|
})
|
|
return ret, err
|
|
}
|
|
|
|
// SetValue sets the value at path to val
|
|
// path is a slice of tokens
|
|
func (b *DB) SetValue(path []string, key, val string) error {
|
|
var err error
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.MkBucketPath(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b.localDB.Update(func(tx *bolt.Tx) error {
|
|
newBkt := tx.Bucket([]byte(path[0]))
|
|
if newBkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
for idx := 1; idx < len(path); idx++ {
|
|
newBkt, err = newBkt.CreateBucketIfNotExists([]byte(path[idx]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// newBkt should have the last bucket in the path
|
|
return newBkt.Put([]byte(key), []byte(val))
|
|
})
|
|
return err
|
|
}
|
|
|
|
// GetInt returns the value at 'path'
|
|
// path is a '/' separated list of tokens
|
|
// the last token is a key, all others are buckets
|
|
// If the value cannot be parsed as an int, error
|
|
func (b *DB) GetInt(path []string, key string) (int, error) {
|
|
var ret int
|
|
r, err := b.GetValue(path, key)
|
|
if err == nil {
|
|
ret, err = strconv.Atoi(r)
|
|
}
|
|
return ret, err
|
|
}
|
|
|
|
// SetInt Sets an integer value
|
|
func (b *DB) SetInt(path []string, key string, val int) error {
|
|
return b.SetValue(path, key, strconv.Itoa(val))
|
|
}
|
|
|
|
// GetBool returns the value at 'path'
|
|
// If the value cannot be parsed as a bool, error
|
|
// We check 'true/false' and '1/0', else error
|
|
func (b *DB) GetBool(path []string, key string) (bool, error) {
|
|
var ret bool
|
|
r, err := b.GetValue(path, key)
|
|
if err == nil {
|
|
if r == "true" || r == "1" {
|
|
ret = true
|
|
} else if r != "false" && r != "0" {
|
|
err = fmt.Errorf("Cannot parse as a boolean")
|
|
}
|
|
}
|
|
return ret, err
|
|
}
|
|
|
|
// SetBool Sets a boolean value
|
|
func (b *DB) SetBool(path []string, key string, val bool) error {
|
|
if val {
|
|
return b.SetValue(path, key, "true")
|
|
}
|
|
return b.SetValue(path, key, "false")
|
|
}
|
|
|
|
// GetTimestamp returns the value at 'path'
|
|
// If the value cannot be parsed as a RFC3339, error
|
|
func (b *DB) GetTimestamp(path []string, key string) (time.Time, error) {
|
|
r, err := b.GetValue(path, key)
|
|
if err == nil {
|
|
return time.Parse(time.RFC3339, r)
|
|
}
|
|
return time.Unix(0, 0), err
|
|
}
|
|
|
|
// SetTimestamp returns the value at 'path'
|
|
func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
|
|
return b.SetValue(path, key, val.Format(time.RFC3339))
|
|
}
|
|
|
|
// GetBucketList returns a list of all sub-buckets at path
|
|
func (b *DB) GetBucketList(path []string) ([]string, error) {
|
|
var err error
|
|
var ret []string
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.localDB.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
var newBkt *bolt.Bucket
|
|
var berr error
|
|
if len(path) > 1 {
|
|
for idx := 1; idx < len(path); idx++ {
|
|
newBkt = bkt.Bucket([]byte(path[idx]))
|
|
if newBkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
|
}
|
|
}
|
|
bkt = newBkt
|
|
}
|
|
|
|
// newBkt should have the last bucket in the path
|
|
berr = bkt.ForEach(func(k, v []byte) error {
|
|
if v == nil {
|
|
// Must be a bucket
|
|
ret = append(ret, string(k))
|
|
}
|
|
return nil
|
|
})
|
|
return berr
|
|
})
|
|
return ret, err
|
|
}
|
|
|
|
// GetKeyList returns a list of all keys at path
|
|
func (b *DB) GetKeyList(path []string) ([]string, error) {
|
|
var err error
|
|
var ret []string
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.localDB.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
if len(path) > 1 {
|
|
var newBkt *bolt.Bucket
|
|
for idx := 1; idx < len(path); idx++ {
|
|
newBkt = bkt.Bucket([]byte(path[idx]))
|
|
if newBkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
|
}
|
|
}
|
|
bkt = newBkt
|
|
}
|
|
// newBkt should have the last bucket in the path
|
|
berr := bkt.ForEach(func(k, v []byte) error {
|
|
if v != nil {
|
|
// Not a bucket
|
|
ret = append(ret, string(k))
|
|
}
|
|
return nil
|
|
})
|
|
return berr
|
|
})
|
|
return ret, err
|
|
}
|
|
|
|
// DeletePair deletes the pair with key at path
|
|
func (b *DB) DeletePair(path []string, key string) error {
|
|
var err error
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.localDB.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
if len(path) > 1 {
|
|
var newBkt *bolt.Bucket
|
|
for idx := 1; idx < len(path); idx++ {
|
|
newBkt = bkt.Bucket([]byte(path[idx]))
|
|
if newBkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
|
}
|
|
}
|
|
bkt = newBkt
|
|
}
|
|
// bkt should have the last bucket in the path
|
|
// Test to make sure that key is a pair, if so, delete it
|
|
if tst := bkt.Bucket([]byte(key)); tst == nil {
|
|
return bkt.Delete([]byte(key))
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
// DeleteBucket deletes the bucket key at path
|
|
func (b *DB) DeleteBucket(path []string, key string) error {
|
|
var err error
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
b.RefreshDB()
|
|
err = b.localDB.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket([]byte(path[0]))
|
|
if bkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + path[0])
|
|
}
|
|
var newBkt *bolt.Bucket
|
|
for idx := 1; idx < len(path); idx++ {
|
|
newBkt = bkt.Bucket([]byte(path[idx]))
|
|
if newBkt == nil {
|
|
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
|
}
|
|
}
|
|
// newBkt should have the last bucket in the path
|
|
// Test to make sure that key is a bucket, if so, delete it
|
|
if tst := newBkt.Bucket([]byte(key)); tst != nil {
|
|
return newBkt.Delete([]byte(key))
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
// RefreshDB makes sure that the DB is fresh with the server version
|
|
func (b *DB) RefreshDB() error {
|
|
var err error
|
|
if !b.isOpen {
|
|
err = b.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.Close()
|
|
}
|
|
if b.online {
|
|
// TODO: Compare latest change here with lates on server
|
|
// Then sync, if needed
|
|
}
|
|
return err
|
|
}
|
|
|
|
type setURLResponse struct {
|
|
dbMD5 string
|
|
}
|
|
|
|
// SetDBURL sets the DB's remote URL
|
|
func (b *DB) SetDBURL(url string) (bool, string) {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return false, ""
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
sur := setURLResponse{}
|
|
json.Unmarshal(body, sur)
|
|
return true, sur.dbMD5
|
|
}
|