surrealpatch/kvs/boltdb/tx.go

381 lines
6.8 KiB
Go
Raw Normal View History

// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2016-07-16 13:43:53 +00:00
package boltdb
2016-05-17 21:17:42 +00:00
import (
"bytes"
"math"
2016-05-17 21:17:42 +00:00
"github.com/boltdb/bolt"
2016-07-16 13:43:53 +00:00
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/util/cryp"
"github.com/abcum/surreal/util/snap"
2016-05-17 21:17:42 +00:00
)
2016-05-19 11:32:55 +00:00
// TX is a distributed database transaction.
type TX struct {
2016-07-16 13:43:53 +00:00
ds *DS
do bool
ck []byte
2016-05-17 21:17:42 +00:00
tx *bolt.Tx
bu *bolt.Bucket
}
// All retrieves all key:value items in the db.
2016-07-16 13:43:53 +00:00
func (tx *TX) All() (kvs []kvs.KV, err error) {
2016-05-17 21:17:42 +00:00
err = tx.bu.ForEach(func(key, val []byte) (err error) {
kv, err := get(tx, key, val)
if err != nil {
return
}
kvs = append(kvs, kv)
2016-05-17 21:17:42 +00:00
return nil
2016-05-17 21:17:42 +00:00
})
return
}
// Get retrieves a single key:value item.
2016-07-16 13:43:53 +00:00
func (tx *TX) Get(key []byte) (kv kvs.KV, err error) {
2016-05-17 21:17:42 +00:00
val := tx.bu.Get(key)
return get(tx, key, val)
2016-05-17 21:17:42 +00:00
}
// MGet retrieves multiple key:value items.
2016-07-16 13:43:53 +00:00
func (tx *TX) MGet(keys ...[]byte) (kvs []kvs.KV, err error) {
2016-05-17 21:17:42 +00:00
for _, key := range keys {
2016-05-17 21:17:42 +00:00
val := tx.bu.Get(key)
kv, err := get(tx, key, val)
if err != nil {
return nil, err
}
kvs = append(kvs, kv)
2016-05-17 21:17:42 +00:00
}
return
}
// PGet retrieves the range of rows which are prefixed with `pre`.
2016-07-16 13:43:53 +00:00
func (tx *TX) PGet(pre []byte) (kvs []kvs.KV, err error) {
2016-05-17 21:17:42 +00:00
cu := tx.bu.Cursor()
for key, val := cu.Seek(pre); bytes.HasPrefix(key, pre); key, val = cu.Next() {
kv, err := get(tx, key, val)
if err != nil {
return nil, err
}
kvs = append(kvs, kv)
2016-05-17 21:17:42 +00:00
}
return
}
2016-05-17 21:17:42 +00:00
// RGet retrieves the range of `max` rows between `beg` (inclusive) and
// `end` (exclusive). To return the range in descending order, ensure
// that `end` sorts lower than `beg` in the key value store.
2016-07-16 13:43:53 +00:00
func (tx *TX) RGet(beg, end []byte, max uint64) (kvs []kvs.KV, err error) {
2016-05-17 21:17:42 +00:00
if max == 0 {
max = math.MaxUint64
}
2016-05-17 21:17:42 +00:00
cu := tx.bu.Cursor()
if bytes.Compare(beg, end) < 1 {
for key, val := cu.Seek(beg); key != nil && max > 0 && bytes.Compare(key, end) < 0; key, val = cu.Next() {
kv, err := get(tx, key, val)
if err != nil {
return nil, err
}
kvs = append(kvs, kv)
2016-05-17 21:17:42 +00:00
max--
}
}
if bytes.Compare(beg, end) > 1 {
2016-05-17 21:32:45 +00:00
for key, val := cu.Seek(end); key != nil && max > 0 && bytes.Compare(beg, key) < 0; key, val = cu.Prev() {
kv, err := get(tx, key, val)
if err != nil {
return nil, err
}
kvs = append(kvs, kv)
2016-05-17 21:17:42 +00:00
max--
}
}
return
}
// Put sets the value for a key.
2016-05-19 11:32:55 +00:00
func (tx *TX) Put(key, val []byte) (err error) {
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
if val, err = snap.Encode(val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
return
}
2016-07-18 22:00:01 +00:00
if val, err = cryp.Encrypt(tx.ds.ck, val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.CKError{Err: err}
2016-07-18 22:00:01 +00:00
return
}
2016-05-17 21:17:42 +00:00
if err = tx.bu.Put(key, val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
return
}
// CPut conditionally sets the value for a key if the existing value is equal
// to the expected value. To conditionally set a value only if there is no
// existing entry pass nil for the expected value.
2016-05-19 11:32:55 +00:00
func (tx *TX) CPut(key, val, exp []byte) (err error) {
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
now, _ := tx.Get(key)
2016-07-16 13:43:53 +00:00
act := now.(*KV).val
2016-07-16 13:43:53 +00:00
if !bytes.Equal(act, exp) {
2016-12-08 22:03:42 +00:00
err = &kvs.KVError{Err: err, Key: key, Val: act, Exp: exp}
return
}
if val, err = snap.Encode(val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
return
}
2016-05-17 21:17:42 +00:00
2016-07-18 22:00:01 +00:00
if val, err = cryp.Encrypt(tx.ds.ck, val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.CKError{Err: err}
2016-07-18 22:00:01 +00:00
return
}
2016-05-17 21:17:42 +00:00
if err = tx.bu.Put(key, val); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
return
}
// Del deletes a single key:value item.
2016-05-19 11:32:55 +00:00
func (tx *TX) Del(key []byte) (err error) {
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
return
}
// CDel conditionally deletes a key if the existing value is equal to the
// expected value.
2016-05-19 11:32:55 +00:00
func (tx *TX) CDel(key, exp []byte) (err error) {
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
now, _ := tx.Get(key)
2016-07-16 13:43:53 +00:00
act := now.(*KV).val
2016-05-17 21:17:42 +00:00
2016-07-16 13:43:53 +00:00
if !bytes.Equal(act, exp) {
2016-12-08 22:03:42 +00:00
err = &kvs.KVError{Err: err, Key: key, Val: act, Exp: exp}
2016-05-17 21:17:42 +00:00
return
}
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
return
}
// MDel deletes multiple key:value items.
2016-05-19 11:32:55 +00:00
func (tx *TX) MDel(keys ...[]byte) (err error) {
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
for _, key := range keys {
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
}
return
}
// PDel deletes the range of rows which are prefixed with `pre`.
2016-05-19 11:32:55 +00:00
func (tx *TX) PDel(pre []byte) (err error) {
2016-05-17 21:17:42 +00:00
cu := tx.bu.Cursor()
for key, _ := cu.Seek(pre); bytes.HasPrefix(key, pre); key, _ = cu.Seek(pre) {
2016-05-17 21:17:42 +00:00
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
}
return
}
2016-05-17 21:17:42 +00:00
// RDel deletes the range of `max` rows between `beg` (inclusive) and
// `end` (exclusive). To delete the range in descending order, ensure
// that `end` sorts lower than `beg` in the key value store.
2016-05-19 11:32:55 +00:00
func (tx *TX) RDel(beg, end []byte, max uint64) (err error) {
2016-05-17 21:17:42 +00:00
if max == 0 {
max = math.MaxUint64
}
2016-05-17 21:17:42 +00:00
if !tx.tx.Writable() {
2016-12-08 22:03:42 +00:00
err = &kvs.TXError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
cu := tx.bu.Cursor()
if bytes.Compare(beg, end) < 1 {
for key, _ := cu.Seek(beg); key != nil && max > 0 && bytes.Compare(key, end) < 0; key, _ = cu.Seek(beg) {
2016-05-17 21:17:42 +00:00
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
max--
}
}
if bytes.Compare(beg, end) > 1 {
for key, _ := cu.Seek(end); key != nil && max > 0 && bytes.Compare(beg, key) < 0; key, _ = cu.Seek(end) {
2016-05-17 21:17:42 +00:00
if err = tx.bu.Delete(key); err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
2016-05-17 21:17:42 +00:00
return
}
max--
}
}
return
}
func (tx *TX) Done() (val bool) {
return tx.do
}
2016-05-19 11:32:55 +00:00
func (tx *TX) Close() (err error) {
2016-05-17 21:17:42 +00:00
return tx.Rollback()
}
2016-10-18 12:49:46 +00:00
func (tx *TX) Cancel() (err error) {
return tx.Rollback()
}
2016-05-19 11:32:55 +00:00
func (tx *TX) Commit() (err error) {
tx.do = true
2016-10-18 12:49:46 +00:00
if tx.tx.Writable() {
return tx.tx.Commit()
}
return tx.tx.Rollback()
}
2016-05-19 11:32:55 +00:00
func (tx *TX) Rollback() (err error) {
tx.do = true
2016-05-17 21:17:42 +00:00
return tx.tx.Rollback()
}
func get(tx *TX, key, val []byte) (kv *KV, err error) {
2016-05-17 21:17:42 +00:00
kv = &KV{
2016-05-18 00:21:54 +00:00
exi: (val != nil),
key: key,
val: val,
2016-05-17 21:17:42 +00:00
}
2016-07-18 22:00:01 +00:00
kv.val, err = cryp.Decrypt(tx.ds.ck, kv.val)
if err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.CKError{Err: err}
2016-07-18 22:00:01 +00:00
return
}
kv.val, err = snap.Decode(kv.val)
if err != nil {
2016-12-08 22:03:42 +00:00
err = &kvs.DBError{Err: err}
return
}
2016-05-17 21:17:42 +00:00
return
}