2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:35:53 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketIDLength = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-26 21:08:08 +03:00
|
|
|
valuePostfix = []byte{0x00}
|
|
|
|
bucketPostfix = []byte{0xFF}
|
|
|
|
sequencePrefix = []byte("$seq$")
|
2020-02-18 21:35:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// makeBucketID returns a deterministic key for the passed byte slice.
|
|
|
|
// Currently it returns the sha256 hash of the slice.
|
|
|
|
func makeBucketID(key []byte) [bucketIDLength]byte {
|
|
|
|
return sha256.Sum256(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidBucketID checks if the passed slice is the required length to be a
|
|
|
|
// valid bucket id.
|
|
|
|
func isValidBucketID(s []byte) bool {
|
|
|
|
return len(s) == bucketIDLength
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:08:08 +03:00
|
|
|
// makeKey concatenates parent, key and postfix into one byte slice.
|
|
|
|
// The postfix indicates the use of this key (whether bucket or value), while
|
|
|
|
// parent refers to the parent bucket.
|
|
|
|
func makeKey(parent, key, postfix []byte) []byte {
|
|
|
|
keyBuf := make([]byte, len(parent)+len(key)+len(postfix))
|
|
|
|
copy(keyBuf, parent)
|
|
|
|
copy(keyBuf[len(parent):], key)
|
|
|
|
copy(keyBuf[len(parent)+len(key):], postfix)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
return keyBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBucketKey returns a bucket key from the passed parent bucket id and
|
|
|
|
// the key.
|
|
|
|
func makeBucketKey(parent []byte, key []byte) []byte {
|
2020-06-26 21:08:08 +03:00
|
|
|
return makeKey(parent, key, bucketPostfix)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeValueKey returns a value key from the passed parent bucket id and
|
|
|
|
// the key.
|
|
|
|
func makeValueKey(parent []byte, key []byte) []byte {
|
2020-06-26 21:08:08 +03:00
|
|
|
return makeKey(parent, key, valuePostfix)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeSequenceKey returns a sequence key of the passed parent bucket id.
|
|
|
|
func makeSequenceKey(parent []byte) []byte {
|
2020-06-26 21:08:08 +03:00
|
|
|
keyBuf := make([]byte, len(sequencePrefix)+len(parent))
|
|
|
|
copy(keyBuf, sequencePrefix)
|
|
|
|
copy(keyBuf[len(sequencePrefix):], parent)
|
|
|
|
return keyBuf
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:08:08 +03:00
|
|
|
// isBucketKey returns true if the passed key is a bucket key, meaning it
|
|
|
|
// keys a bucket name.
|
|
|
|
func isBucketKey(key string) bool {
|
|
|
|
if len(key) < bucketIDLength+1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return key[len(key)-1] == bucketPostfix[0]
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:08:08 +03:00
|
|
|
// getKey chops out the key from the raw key (by removing the bucket id
|
|
|
|
// prefixing the key and the postfix indicating whether it is a bucket or
|
|
|
|
// a value key)
|
|
|
|
func getKey(rawKey string) []byte {
|
|
|
|
return []byte(rawKey[bucketIDLength : len(rawKey)-1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// getKeyVal chops out the key from the raw key (by removing the bucket id
|
|
|
|
// prefixing the key and the postfix indicating whether it is a bucket or
|
|
|
|
// a value key) and also returns the appropriate value for the key, which is
|
|
|
|
// nil in case of buckets (or the set value otherwise).
|
|
|
|
func getKeyVal(kv *KV) ([]byte, []byte) {
|
|
|
|
var val []byte
|
|
|
|
|
|
|
|
if !isBucketKey(kv.key) {
|
|
|
|
val = []byte(kv.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getKey(kv.key), val
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|