forked from forgejo/forgejo
Integrate public as bindata optionally (#293)
* Dropped unused codekit config * Integrated dynamic and static bindata for public * Ignore public bindata * Add a general generate make task * Integrated flexible public assets into web command * Updated vendoring, added all missiong govendor deps * Made the linter happy with the bindata and dynamic code * Moved public bindata definition to modules directory * Ignoring the new bindata path now * Updated to the new public modules import path * Updated public bindata command and drop the new prefix
This commit is contained in:
parent
4680c349dd
commit
b6a95a8cb3
691 changed files with 305318 additions and 1272 deletions
285
vendor/github.com/pingcap/tidb/structure/hash.go
generated
vendored
Normal file
285
vendor/github.com/pingcap/tidb/structure/hash.go
generated
vendored
Normal file
|
@ -0,0 +1,285 @@
|
|||
// Copyright 2015 PingCAP, Inc.
|
||||
//
|
||||
// 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package structure
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/kv"
|
||||
"github.com/pingcap/tidb/terror"
|
||||
)
|
||||
|
||||
// HashPair is the pair for (field, value) in a hash.
|
||||
type HashPair struct {
|
||||
Field []byte
|
||||
Value []byte
|
||||
}
|
||||
|
||||
type hashMeta struct {
|
||||
FieldCount int64
|
||||
}
|
||||
|
||||
func (meta hashMeta) Value() []byte {
|
||||
buf := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(buf[0:8], uint64(meta.FieldCount))
|
||||
return buf
|
||||
}
|
||||
|
||||
func (meta hashMeta) IsEmpty() bool {
|
||||
return meta.FieldCount <= 0
|
||||
}
|
||||
|
||||
// HSet sets the string value of a hash field.
|
||||
func (t *TxStructure) HSet(key []byte, field []byte, value []byte) error {
|
||||
return t.updateHash(key, field, func([]byte) ([]byte, error) {
|
||||
return value, nil
|
||||
})
|
||||
}
|
||||
|
||||
// HGet gets the value of a hash field.
|
||||
func (t *TxStructure) HGet(key []byte, field []byte) ([]byte, error) {
|
||||
dataKey := t.encodeHashDataKey(key, field)
|
||||
value, err := t.txn.Get(dataKey)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
return value, errors.Trace(err)
|
||||
}
|
||||
|
||||
// HInc increments the integer value of a hash field, by step, returns
|
||||
// the value after the increment.
|
||||
func (t *TxStructure) HInc(key []byte, field []byte, step int64) (int64, error) {
|
||||
base := int64(0)
|
||||
err := t.updateHash(key, field, func(oldValue []byte) ([]byte, error) {
|
||||
if oldValue != nil {
|
||||
var err error
|
||||
base, err = strconv.ParseInt(string(oldValue), 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
}
|
||||
base += step
|
||||
return []byte(strconv.FormatInt(base, 10)), nil
|
||||
})
|
||||
|
||||
return base, errors.Trace(err)
|
||||
}
|
||||
|
||||
// HGetInt64 gets int64 value of a hash field.
|
||||
func (t *TxStructure) HGetInt64(key []byte, field []byte) (int64, error) {
|
||||
value, err := t.HGet(key, field)
|
||||
if err != nil || value == nil {
|
||||
return 0, errors.Trace(err)
|
||||
}
|
||||
|
||||
var n int64
|
||||
n, err = strconv.ParseInt(string(value), 10, 64)
|
||||
return n, errors.Trace(err)
|
||||
}
|
||||
|
||||
func (t *TxStructure) updateHash(key []byte, field []byte, fn func(oldValue []byte) ([]byte, error)) error {
|
||||
dataKey := t.encodeHashDataKey(key, field)
|
||||
oldValue, err := t.loadHashValue(dataKey)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
newValue, err := fn(oldValue)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Check if new value is equal to old value.
|
||||
if bytes.Equal(oldValue, newValue) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = t.txn.Set(dataKey, newValue); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
metaKey := t.encodeHashMetaKey(key)
|
||||
meta, err := t.loadHashMeta(metaKey)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if oldValue == nil {
|
||||
meta.FieldCount++
|
||||
if err = t.txn.Set(metaKey, meta.Value()); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HLen gets the number of fields in a hash.
|
||||
func (t *TxStructure) HLen(key []byte) (int64, error) {
|
||||
metaKey := t.encodeHashMetaKey(key)
|
||||
meta, err := t.loadHashMeta(metaKey)
|
||||
if err != nil {
|
||||
return 0, errors.Trace(err)
|
||||
}
|
||||
return meta.FieldCount, nil
|
||||
}
|
||||
|
||||
// HDel deletes one or more hash fields.
|
||||
func (t *TxStructure) HDel(key []byte, fields ...[]byte) error {
|
||||
metaKey := t.encodeHashMetaKey(key)
|
||||
meta, err := t.loadHashMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
var value []byte
|
||||
for _, field := range fields {
|
||||
dataKey := t.encodeHashDataKey(key, field)
|
||||
|
||||
value, err = t.loadHashValue(dataKey)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if value != nil {
|
||||
if err = t.txn.Delete(dataKey); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
meta.FieldCount--
|
||||
}
|
||||
}
|
||||
|
||||
if meta.IsEmpty() {
|
||||
err = t.txn.Delete(metaKey)
|
||||
} else {
|
||||
err = t.txn.Set(metaKey, meta.Value())
|
||||
}
|
||||
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// HKeys gets all the fields in a hash.
|
||||
func (t *TxStructure) HKeys(key []byte) ([][]byte, error) {
|
||||
var keys [][]byte
|
||||
err := t.iterateHash(key, func(field []byte, value []byte) error {
|
||||
keys = append(keys, append([]byte{}, field...))
|
||||
return nil
|
||||
})
|
||||
|
||||
return keys, errors.Trace(err)
|
||||
}
|
||||
|
||||
// HGetAll gets all the fields and values in a hash.
|
||||
func (t *TxStructure) HGetAll(key []byte) ([]HashPair, error) {
|
||||
var res []HashPair
|
||||
err := t.iterateHash(key, func(field []byte, value []byte) error {
|
||||
pair := HashPair{
|
||||
Field: append([]byte{}, field...),
|
||||
Value: append([]byte{}, value...),
|
||||
}
|
||||
res = append(res, pair)
|
||||
return nil
|
||||
})
|
||||
|
||||
return res, errors.Trace(err)
|
||||
}
|
||||
|
||||
// HClear removes the hash value of the key.
|
||||
func (t *TxStructure) HClear(key []byte) error {
|
||||
metaKey := t.encodeHashMetaKey(key)
|
||||
meta, err := t.loadHashMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
err = t.iterateHash(key, func(field []byte, value []byte) error {
|
||||
k := t.encodeHashDataKey(key, field)
|
||||
return errors.Trace(t.txn.Delete(k))
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return errors.Trace(t.txn.Delete(metaKey))
|
||||
}
|
||||
|
||||
func (t *TxStructure) iterateHash(key []byte, fn func(k []byte, v []byte) error) error {
|
||||
dataPrefix := t.hashDataKeyPrefix(key)
|
||||
it, err := t.txn.Seek(dataPrefix)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
var field []byte
|
||||
|
||||
for it.Valid() {
|
||||
if !it.Key().HasPrefix(dataPrefix) {
|
||||
break
|
||||
}
|
||||
|
||||
_, field, err = t.decodeHashDataKey(it.Key())
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if err = fn(field, it.Value()); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
err = it.Next()
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TxStructure) loadHashMeta(metaKey []byte) (hashMeta, error) {
|
||||
v, err := t.txn.Get(metaKey)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
} else if err != nil {
|
||||
return hashMeta{}, errors.Trace(err)
|
||||
}
|
||||
|
||||
meta := hashMeta{FieldCount: 0}
|
||||
if v == nil {
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
if len(v) != 8 {
|
||||
return meta, errors.New("invalid list meta data")
|
||||
}
|
||||
|
||||
meta.FieldCount = int64(binary.BigEndian.Uint64(v[0:8]))
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func (t *TxStructure) loadHashValue(dataKey []byte) ([]byte, error) {
|
||||
v, err := t.txn.Get(dataKey)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
v = nil
|
||||
} else if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
212
vendor/github.com/pingcap/tidb/structure/list.go
generated
vendored
Normal file
212
vendor/github.com/pingcap/tidb/structure/list.go
generated
vendored
Normal file
|
@ -0,0 +1,212 @@
|
|||
// Copyright 2015 PingCAP, Inc.
|
||||
//
|
||||
// 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package structure
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/kv"
|
||||
"github.com/pingcap/tidb/terror"
|
||||
)
|
||||
|
||||
type listMeta struct {
|
||||
LIndex int64
|
||||
RIndex int64
|
||||
}
|
||||
|
||||
func (meta listMeta) Value() []byte {
|
||||
buf := make([]byte, 16)
|
||||
binary.BigEndian.PutUint64(buf[0:8], uint64(meta.LIndex))
|
||||
binary.BigEndian.PutUint64(buf[8:16], uint64(meta.RIndex))
|
||||
return buf
|
||||
}
|
||||
|
||||
func (meta listMeta) IsEmpty() bool {
|
||||
return meta.LIndex >= meta.RIndex
|
||||
}
|
||||
|
||||
// LPush prepends one or multiple values to a list.
|
||||
func (t *TxStructure) LPush(key []byte, values ...[]byte) error {
|
||||
return t.listPush(key, true, values...)
|
||||
}
|
||||
|
||||
// RPush appends one or multiple values to a list.
|
||||
func (t *TxStructure) RPush(key []byte, values ...[]byte) error {
|
||||
return t.listPush(key, false, values...)
|
||||
}
|
||||
|
||||
func (t *TxStructure) listPush(key []byte, left bool, values ...[]byte) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
index := int64(0)
|
||||
for _, v := range values {
|
||||
if left {
|
||||
meta.LIndex--
|
||||
index = meta.LIndex
|
||||
} else {
|
||||
index = meta.RIndex
|
||||
meta.RIndex++
|
||||
}
|
||||
|
||||
dataKey := t.encodeListDataKey(key, index)
|
||||
if err = t.txn.Set(dataKey, v); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return t.txn.Set(metaKey, meta.Value())
|
||||
}
|
||||
|
||||
// LPop removes and gets the first element in a list.
|
||||
func (t *TxStructure) LPop(key []byte) ([]byte, error) {
|
||||
return t.listPop(key, true)
|
||||
}
|
||||
|
||||
// RPop removes and gets the last element in a list.
|
||||
func (t *TxStructure) RPop(key []byte) ([]byte, error) {
|
||||
return t.listPop(key, false)
|
||||
}
|
||||
|
||||
func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
index := int64(0)
|
||||
if left {
|
||||
index = meta.LIndex
|
||||
meta.LIndex++
|
||||
} else {
|
||||
meta.RIndex--
|
||||
index = meta.RIndex
|
||||
}
|
||||
|
||||
dataKey := t.encodeListDataKey(key, index)
|
||||
|
||||
var data []byte
|
||||
data, err = t.txn.Get(dataKey)
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
if err = t.txn.Delete(dataKey); err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
if !meta.IsEmpty() {
|
||||
err = t.txn.Set(metaKey, meta.Value())
|
||||
} else {
|
||||
err = t.txn.Delete(metaKey)
|
||||
}
|
||||
|
||||
return data, errors.Trace(err)
|
||||
}
|
||||
|
||||
// LLen gets the length of a list.
|
||||
func (t *TxStructure) LLen(key []byte) (int64, error) {
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
return meta.RIndex - meta.LIndex, errors.Trace(err)
|
||||
}
|
||||
|
||||
// LIndex gets an element from a list by its index.
|
||||
func (t *TxStructure) LIndex(key []byte, index int64) ([]byte, error) {
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
index = adjustIndex(index, meta.LIndex, meta.RIndex)
|
||||
|
||||
if index >= meta.LIndex && index < meta.RIndex {
|
||||
return t.txn.Get(t.encodeListDataKey(key, index))
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LSet updates an element in the list by its index.
|
||||
func (t *TxStructure) LSet(key []byte, index int64, value []byte) error {
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
index = adjustIndex(index, meta.LIndex, meta.RIndex)
|
||||
|
||||
if index >= meta.LIndex && index < meta.RIndex {
|
||||
return t.txn.Set(t.encodeListDataKey(key, index), value)
|
||||
}
|
||||
return errors.Errorf("invalid index %d", index)
|
||||
}
|
||||
|
||||
// LClear removes the list of the key.
|
||||
func (t *TxStructure) LClear(key []byte) error {
|
||||
metaKey := t.encodeListMetaKey(key)
|
||||
meta, err := t.loadListMeta(metaKey)
|
||||
if err != nil || meta.IsEmpty() {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
for index := meta.LIndex; index < meta.RIndex; index++ {
|
||||
dataKey := t.encodeListDataKey(key, index)
|
||||
if err = t.txn.Delete(dataKey); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return t.txn.Delete(metaKey)
|
||||
}
|
||||
|
||||
func (t *TxStructure) loadListMeta(metaKey []byte) (listMeta, error) {
|
||||
v, err := t.txn.Get(metaKey)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
} else if err != nil {
|
||||
return listMeta{}, errors.Trace(err)
|
||||
}
|
||||
|
||||
meta := listMeta{0, 0}
|
||||
if v == nil {
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
if len(v) != 16 {
|
||||
return meta, errors.Errorf("invalid list meta data")
|
||||
}
|
||||
|
||||
meta.LIndex = int64(binary.BigEndian.Uint64(v[0:8]))
|
||||
meta.RIndex = int64(binary.BigEndian.Uint64(v[8:16]))
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func adjustIndex(index int64, min, max int64) int64 {
|
||||
if index >= 0 {
|
||||
return index + min
|
||||
}
|
||||
|
||||
return index + max
|
||||
}
|
72
vendor/github.com/pingcap/tidb/structure/string.go
generated
vendored
Normal file
72
vendor/github.com/pingcap/tidb/structure/string.go
generated
vendored
Normal file
|
@ -0,0 +1,72 @@
|
|||
// Copyright 2015 PingCAP, Inc.
|
||||
//
|
||||
// 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package structure
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/kv"
|
||||
"github.com/pingcap/tidb/terror"
|
||||
)
|
||||
|
||||
// Set sets the string value of the key.
|
||||
func (t *TxStructure) Set(key []byte, value []byte) error {
|
||||
ek := t.encodeStringDataKey(key)
|
||||
|
||||
return t.txn.Set(ek, value)
|
||||
}
|
||||
|
||||
// Get gets the string value of a key.
|
||||
func (t *TxStructure) Get(key []byte) ([]byte, error) {
|
||||
ek := t.encodeStringDataKey(key)
|
||||
value, err := t.txn.Get(ek)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
return value, errors.Trace(err)
|
||||
}
|
||||
|
||||
// GetInt64 gets the int64 value of a key.
|
||||
func (t *TxStructure) GetInt64(key []byte) (int64, error) {
|
||||
v, err := t.Get(key)
|
||||
if err != nil || v == nil {
|
||||
return 0, errors.Trace(err)
|
||||
}
|
||||
|
||||
n, err := strconv.ParseInt(string(v), 10, 64)
|
||||
return n, errors.Trace(err)
|
||||
}
|
||||
|
||||
// Inc increments the integer value of a key by step, returns
|
||||
// the value after the increment.
|
||||
func (t *TxStructure) Inc(key []byte, step int64) (int64, error) {
|
||||
ek := t.encodeStringDataKey(key)
|
||||
// txn Inc will lock this key, so we don't lock it here.
|
||||
n, err := kv.IncInt64(t.txn, ek, step)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
return n, errors.Trace(err)
|
||||
}
|
||||
|
||||
// Clear removes the string value of the key.
|
||||
func (t *TxStructure) Clear(key []byte) error {
|
||||
ek := t.encodeStringDataKey(key)
|
||||
err := t.txn.Delete(ek)
|
||||
if terror.ErrorEqual(err, kv.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
return errors.Trace(err)
|
||||
}
|
31
vendor/github.com/pingcap/tidb/structure/structure.go
generated
vendored
Normal file
31
vendor/github.com/pingcap/tidb/structure/structure.go
generated
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2015 PingCAP, Inc.
|
||||
//
|
||||
// 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package structure
|
||||
|
||||
import "github.com/pingcap/tidb/kv"
|
||||
|
||||
// NewStructure creates a TxStructure in transaction txn and with key prefix.
|
||||
func NewStructure(txn kv.Transaction, prefix []byte) *TxStructure {
|
||||
return &TxStructure{
|
||||
txn: txn,
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
// TxStructure supports some simple data structures like string, hash, list, etc... and
|
||||
// you can use these in a transaction.
|
||||
type TxStructure struct {
|
||||
txn kv.Transaction
|
||||
prefix []byte
|
||||
}
|
116
vendor/github.com/pingcap/tidb/structure/type.go
generated
vendored
Normal file
116
vendor/github.com/pingcap/tidb/structure/type.go
generated
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
// Copyright 2015 PingCAP, Inc.
|
||||
//
|
||||
// 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package structure
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/kv"
|
||||
"github.com/pingcap/tidb/util/codec"
|
||||
)
|
||||
|
||||
// TypeFlag is for data structure meta/data flag.
|
||||
type TypeFlag byte
|
||||
|
||||
const (
|
||||
// StringMeta is the flag for string meta.
|
||||
StringMeta TypeFlag = 'S'
|
||||
// StringData is the flag for string data.
|
||||
StringData TypeFlag = 's'
|
||||
// HashMeta is the flag for hash meta.
|
||||
HashMeta TypeFlag = 'H'
|
||||
// HashData is the flag for hash data.
|
||||
HashData TypeFlag = 'h'
|
||||
// ListMeta is the flag for list meta.
|
||||
ListMeta TypeFlag = 'L'
|
||||
// ListData is the flag for list data.
|
||||
ListData TypeFlag = 'l'
|
||||
)
|
||||
|
||||
func (t *TxStructure) encodeStringDataKey(key []byte) kv.Key {
|
||||
// for codec Encode, we may add extra bytes data, so here and following encode
|
||||
// we will use extra length like 4 for a little optimization.
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+24)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
return codec.EncodeUint(ek, uint64(StringData))
|
||||
}
|
||||
|
||||
func (t *TxStructure) encodeHashMetaKey(key []byte) kv.Key {
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+24)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
return codec.EncodeUint(ek, uint64(HashMeta))
|
||||
}
|
||||
|
||||
func (t *TxStructure) encodeHashDataKey(key []byte, field []byte) kv.Key {
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+len(field)+30)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
ek = codec.EncodeUint(ek, uint64(HashData))
|
||||
return codec.EncodeBytes(ek, field)
|
||||
}
|
||||
|
||||
func (t *TxStructure) decodeHashDataKey(ek kv.Key) ([]byte, []byte, error) {
|
||||
var (
|
||||
key []byte
|
||||
field []byte
|
||||
err error
|
||||
tp uint64
|
||||
)
|
||||
|
||||
if !bytes.HasPrefix(ek, t.prefix) {
|
||||
return nil, nil, errors.New("invalid encoded hash data key prefix")
|
||||
}
|
||||
|
||||
ek = ek[len(t.prefix):]
|
||||
|
||||
ek, key, err = codec.DecodeBytes(ek)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
ek, tp, err = codec.DecodeUint(ek)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Trace(err)
|
||||
} else if TypeFlag(tp) != HashData {
|
||||
return nil, nil, errors.Errorf("invalid encoded hash data key flag %c", byte(tp))
|
||||
}
|
||||
|
||||
_, field, err = codec.DecodeBytes(ek)
|
||||
return key, field, errors.Trace(err)
|
||||
}
|
||||
|
||||
func (t *TxStructure) hashDataKeyPrefix(key []byte) kv.Key {
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+24)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
return codec.EncodeUint(ek, uint64(HashData))
|
||||
}
|
||||
|
||||
func (t *TxStructure) encodeListMetaKey(key []byte) kv.Key {
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+24)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
return codec.EncodeUint(ek, uint64(ListMeta))
|
||||
}
|
||||
|
||||
func (t *TxStructure) encodeListDataKey(key []byte, index int64) kv.Key {
|
||||
ek := make([]byte, 0, len(t.prefix)+len(key)+36)
|
||||
ek = append(ek, t.prefix...)
|
||||
ek = codec.EncodeBytes(ek, key)
|
||||
ek = codec.EncodeUint(ek, uint64(ListData))
|
||||
return codec.EncodeInt(ek, index)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue