Add Copy() method to all keys for efficiently duplicating a key

This commit is contained in:
Tobie Morgan Hitchcock 2017-11-16 20:07:02 +00:00
parent b957202ee5
commit 81497e941d
22 changed files with 276 additions and 0 deletions

View file

@ -26,6 +26,15 @@ func (k *DB) init() *DB {
return k return k
} }
// Copy creates a copy of the key
func (k *DB) Copy() *DB {
return &DB{
KV: k.KV,
NS: k.NS,
DB: k.DB,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *DB) Encode() []byte { func (k *DB) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *DT) init() *DT {
return k return k
} }
// Copy creates a copy of the key
func (k *DT) Copy() *DT {
return &DT{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TK: k.TK,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *DT) Encode() []byte { func (k *DT) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *DU) init() *DU {
return k return k
} }
// Copy creates a copy of the key
func (k *DU) Copy() *DU {
return &DU{
KV: k.KV,
NS: k.NS,
DB: k.DB,
US: k.US,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *DU) Encode() []byte { func (k *DU) Encode() []byte {
k.init() k.init()

View file

@ -39,6 +39,21 @@ func (k *Edge) init() *Edge {
return k return k
} }
// Copy creates a copy of the key
func (k *Edge) Copy() *Edge {
return &Edge{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
ID: k.ID,
TK: k.TK,
TP: k.TP,
FT: k.FT,
FK: k.FK,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Edge) Encode() []byte { func (k *Edge) Encode() []byte {
k.init() k.init()

View file

@ -28,6 +28,17 @@ func (k *FD) init() *FD {
return k return k
} }
// Copy creates a copy of the key
func (k *FD) Copy() *FD {
return &FD{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
FD: k.FD,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *FD) Encode() []byte { func (k *FD) Encode() []byte {
k.init() k.init()

View file

@ -29,6 +29,18 @@ func (k *Field) init() *Field {
return k return k
} }
// Copy creates a copy of the key
func (k *Field) Copy() *Field {
return &Field{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
ID: k.ID,
FD: k.FD,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Field) Encode() []byte { func (k *Field) Encode() []byte {
k.init() k.init()

View file

@ -66,6 +66,58 @@ type Full struct {
AIN []interface{} // []interface{} array AIN []interface{} // []interface{} array
} }
func (f *Full) Copy() Key {
return &Full{
N: f.N,
B: f.B,
F: f.F,
S: f.S,
T: f.T,
NI64: f.NI64,
NI32: f.NI32,
NI16: f.NI16,
NI8: f.NI8,
NI: f.NI,
I: f.I,
I8: f.I8,
I16: f.I16,
I32: f.I32,
I64: f.I64,
UI: f.UI,
UI8: f.UI8,
UI16: f.UI16,
UI32: f.UI32,
UI64: f.UI64,
NF64: f.NF64,
NF32: f.NF32,
F32: f.F32,
F64: f.F64,
AB: f.AB,
AS: f.AS,
AT: f.AT,
AI: f.AI,
AI8: f.AI8,
AI16: f.AI16,
AI32: f.AI32,
AI64: f.AI64,
AUI: f.AUI,
AUI8: f.AUI8,
AUI16: f.AUI16,
AUI32: f.AUI32,
AUI64: f.AUI64,
AF32: f.AF32,
AF64: f.AF64,
IN: f.IN,
IB: f.IB,
IF: f.IF,
IT: f.IT,
II: f.II,
ID: f.ID,
INA: f.INA,
AIN: f.AIN,
}
}
func (f *Full) String() (s string) { func (f *Full) String() (s string) {
return "Test key" return "Test key"
} }

View file

@ -29,6 +29,18 @@ func (k *Index) init() *Index {
return k return k
} }
// Copy creates a copy of the key
func (k *Index) Copy() *Index {
return &Index{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
IX: k.IX,
FD: k.FD,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Index) Encode() []byte { func (k *Index) Encode() []byte {
k.init() k.init()

View file

@ -28,6 +28,17 @@ func (k *IX) init() *IX {
return k return k
} }
// Copy creates a copy of the key
func (k *IX) Copy() *IX {
return &IX{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
IX: k.IX,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *IX) Encode() []byte { func (k *IX) Encode() []byte {
k.init() k.init()

View file

@ -18,6 +18,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"math" "math"
"reflect"
"testing" "testing"
"time" "time"
@ -430,6 +431,19 @@ func TestMain(t *testing.T) {
} }
func TestCopying(t *testing.T) {
for _, test := range tests {
Convey(test.str, t, func() {
val := reflect.ValueOf(test.obj).MethodByName("Copy").Call([]reflect.Value{})
So(val[0].Interface(), ShouldResemble, test.obj)
})
}
}
func TestDisplaying(t *testing.T) { func TestDisplaying(t *testing.T) {
for _, test := range tests { for _, test := range tests {

View file

@ -24,6 +24,13 @@ func (k *KV) init() *KV {
return k return k
} }
// Copy creates a copy of the key
func (k *KV) Copy() *KV {
return &KV{
KV: k.KV,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *KV) Encode() []byte { func (k *KV) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *LV) init() *LV {
return k return k
} }
// Copy creates a copy of the key
func (k *LV) Copy() *LV {
return &LV{
KV: k.KV,
NS: k.NS,
DB: k.DB,
LV: k.LV,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *LV) Encode() []byte { func (k *LV) Encode() []byte {
k.init() k.init()

View file

@ -25,6 +25,14 @@ func (k *NS) init() *NS {
return k return k
} }
// Copy creates a copy of the key
func (k *NS) Copy() *NS {
return &NS{
KV: k.KV,
NS: k.NS,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *NS) Encode() []byte { func (k *NS) Encode() []byte {
k.init() k.init()

View file

@ -26,6 +26,15 @@ func (k *NT) init() *NT {
return k return k
} }
// Copy creates a copy of the key
func (k *NT) Copy() *NT {
return &NT{
KV: k.KV,
NS: k.NS,
TK: k.TK,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *NT) Encode() []byte { func (k *NT) Encode() []byte {
k.init() k.init()

View file

@ -26,6 +26,15 @@ func (k *NU) init() *NU {
return k return k
} }
// Copy creates a copy of the key
func (k *NU) Copy() *NU {
return &NU{
KV: k.KV,
NS: k.NS,
US: k.US,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *NU) Encode() []byte { func (k *NU) Encode() []byte {
k.init() k.init()

View file

@ -36,6 +36,18 @@ func (k *Patch) init() *Patch {
return k return k
} }
// Copy creates a copy of the key
func (k *Patch) Copy() *Patch {
return &Patch{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
ID: k.ID,
AT: k.AT,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Patch) Encode() []byte { func (k *Patch) Encode() []byte {
k.init() k.init()

View file

@ -30,6 +30,19 @@ func (k *Point) init() *Point {
return k return k
} }
// Copy creates a copy of the key
func (k *Point) Copy() *Point {
return &Point{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
IX: k.IX,
FD: k.FD,
ID: k.ID,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Point) Encode() []byte { func (k *Point) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *SC) init() *SC {
return k return k
} }
// Copy creates a copy of the key
func (k *SC) Copy() *SC {
return &SC{
KV: k.KV,
NS: k.NS,
DB: k.DB,
SC: k.SC,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *SC) Encode() []byte { func (k *SC) Encode() []byte {
k.init() k.init()

View file

@ -28,6 +28,17 @@ func (k *ST) init() *ST {
return k return k
} }
// Copy creates a copy of the key
func (k *ST) Copy() *ST {
return &ST{
KV: k.KV,
NS: k.NS,
DB: k.DB,
SC: k.SC,
TK: k.TK,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *ST) Encode() []byte { func (k *ST) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *Table) init() *Table {
return k return k
} }
// Copy creates a copy of the key
func (k *Table) Copy() *Table {
return &Table{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Table) Encode() []byte { func (k *Table) Encode() []byte {
k.init() k.init()

View file

@ -27,6 +27,16 @@ func (k *TB) init() *TB {
return k return k
} }
// Copy creates a copy of the key
func (k *TB) Copy() *TB {
return &TB{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *TB) Encode() []byte { func (k *TB) Encode() []byte {
k.init() k.init()

View file

@ -28,6 +28,17 @@ func (k *Thing) init() *Thing {
return k return k
} }
// Copy creates a copy of the key
func (k *Thing) Copy() *Thing {
return &Thing{
KV: k.KV,
NS: k.NS,
DB: k.DB,
TB: k.TB,
ID: k.ID,
}
}
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Thing) Encode() []byte { func (k *Thing) Encode() []byte {
k.init() k.init()