From 81497e941dfadab55fae35f5b2989fff2d323e0c Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 16 Nov 2017 20:07:02 +0000 Subject: [PATCH] Add Copy() method to all keys for efficiently duplicating a key --- util/keys/db.go | 9 ++++++++ util/keys/dt.go | 10 ++++++++ util/keys/du.go | 10 ++++++++ util/keys/edge.go | 15 ++++++++++++ util/keys/fd.go | 11 +++++++++ util/keys/field.go | 12 ++++++++++ util/keys/full_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++ util/keys/index.go | 12 ++++++++++ util/keys/ix.go | 11 +++++++++ util/keys/keys_test.go | 14 ++++++++++++ util/keys/kv.go | 7 ++++++ util/keys/lv.go | 10 ++++++++ util/keys/ns.go | 8 +++++++ util/keys/nt.go | 9 ++++++++ util/keys/nu.go | 9 ++++++++ util/keys/patch.go | 12 ++++++++++ util/keys/point.go | 13 +++++++++++ util/keys/sc.go | 10 ++++++++ util/keys/st.go | 11 +++++++++ util/keys/table.go | 10 ++++++++ util/keys/tb.go | 10 ++++++++ util/keys/thing.go | 11 +++++++++ 22 files changed, 276 insertions(+) diff --git a/util/keys/db.go b/util/keys/db.go index 0e26a79b..13cbd379 100644 --- a/util/keys/db.go +++ b/util/keys/db.go @@ -26,6 +26,15 @@ func (k *DB) init() *DB { 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 func (k *DB) Encode() []byte { k.init() diff --git a/util/keys/dt.go b/util/keys/dt.go index 6de770c7..db1cb906 100644 --- a/util/keys/dt.go +++ b/util/keys/dt.go @@ -27,6 +27,16 @@ func (k *DT) init() *DT { 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 func (k *DT) Encode() []byte { k.init() diff --git a/util/keys/du.go b/util/keys/du.go index 02ff413b..c3dab4b0 100644 --- a/util/keys/du.go +++ b/util/keys/du.go @@ -27,6 +27,16 @@ func (k *DU) init() *DU { 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 func (k *DU) Encode() []byte { k.init() diff --git a/util/keys/edge.go b/util/keys/edge.go index 9e94e212..5eea7a20 100644 --- a/util/keys/edge.go +++ b/util/keys/edge.go @@ -39,6 +39,21 @@ func (k *Edge) init() *Edge { 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 func (k *Edge) Encode() []byte { k.init() diff --git a/util/keys/fd.go b/util/keys/fd.go index 51105be2..d2e6f672 100644 --- a/util/keys/fd.go +++ b/util/keys/fd.go @@ -28,6 +28,17 @@ func (k *FD) init() *FD { 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 func (k *FD) Encode() []byte { k.init() diff --git a/util/keys/field.go b/util/keys/field.go index 78de6df7..8fb5de45 100644 --- a/util/keys/field.go +++ b/util/keys/field.go @@ -29,6 +29,18 @@ func (k *Field) init() *Field { 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 func (k *Field) Encode() []byte { k.init() diff --git a/util/keys/full_test.go b/util/keys/full_test.go index ff8d79ed..d3c8bd05 100644 --- a/util/keys/full_test.go +++ b/util/keys/full_test.go @@ -66,6 +66,58 @@ type Full struct { 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) { return "Test key" } diff --git a/util/keys/index.go b/util/keys/index.go index 8b664eb0..f8fe127a 100644 --- a/util/keys/index.go +++ b/util/keys/index.go @@ -29,6 +29,18 @@ func (k *Index) init() *Index { 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 func (k *Index) Encode() []byte { k.init() diff --git a/util/keys/ix.go b/util/keys/ix.go index e04e3f3b..ab7a4d12 100644 --- a/util/keys/ix.go +++ b/util/keys/ix.go @@ -28,6 +28,17 @@ func (k *IX) init() *IX { 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 func (k *IX) Encode() []byte { k.init() diff --git a/util/keys/keys_test.go b/util/keys/keys_test.go index 96e1179a..414c7d10 100644 --- a/util/keys/keys_test.go +++ b/util/keys/keys_test.go @@ -18,6 +18,7 @@ import ( "bytes" "fmt" "math" + "reflect" "testing" "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) { for _, test := range tests { diff --git a/util/keys/kv.go b/util/keys/kv.go index 7f28ef22..aa55551e 100644 --- a/util/keys/kv.go +++ b/util/keys/kv.go @@ -24,6 +24,13 @@ func (k *KV) init() *KV { 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 func (k *KV) Encode() []byte { k.init() diff --git a/util/keys/lv.go b/util/keys/lv.go index a17aac3b..eec150af 100644 --- a/util/keys/lv.go +++ b/util/keys/lv.go @@ -27,6 +27,16 @@ func (k *LV) init() *LV { 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 func (k *LV) Encode() []byte { k.init() diff --git a/util/keys/ns.go b/util/keys/ns.go index d9ee76b0..7a5f0be4 100644 --- a/util/keys/ns.go +++ b/util/keys/ns.go @@ -25,6 +25,14 @@ func (k *NS) init() *NS { 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 func (k *NS) Encode() []byte { k.init() diff --git a/util/keys/nt.go b/util/keys/nt.go index 29b7c0da..f722cea2 100644 --- a/util/keys/nt.go +++ b/util/keys/nt.go @@ -26,6 +26,15 @@ func (k *NT) init() *NT { 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 func (k *NT) Encode() []byte { k.init() diff --git a/util/keys/nu.go b/util/keys/nu.go index 6ac720e3..c837cd06 100644 --- a/util/keys/nu.go +++ b/util/keys/nu.go @@ -26,6 +26,15 @@ func (k *NU) init() *NU { 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 func (k *NU) Encode() []byte { k.init() diff --git a/util/keys/patch.go b/util/keys/patch.go index 160fa2c1..d81b3be6 100644 --- a/util/keys/patch.go +++ b/util/keys/patch.go @@ -36,6 +36,18 @@ func (k *Patch) init() *Patch { 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 func (k *Patch) Encode() []byte { k.init() diff --git a/util/keys/point.go b/util/keys/point.go index 3a3505ac..506a938c 100644 --- a/util/keys/point.go +++ b/util/keys/point.go @@ -30,6 +30,19 @@ func (k *Point) init() *Point { 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 func (k *Point) Encode() []byte { k.init() diff --git a/util/keys/sc.go b/util/keys/sc.go index 0aa45414..153e03eb 100644 --- a/util/keys/sc.go +++ b/util/keys/sc.go @@ -27,6 +27,16 @@ func (k *SC) init() *SC { 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 func (k *SC) Encode() []byte { k.init() diff --git a/util/keys/st.go b/util/keys/st.go index 8fa360a4..94062a03 100644 --- a/util/keys/st.go +++ b/util/keys/st.go @@ -28,6 +28,17 @@ func (k *ST) init() *ST { 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 func (k *ST) Encode() []byte { k.init() diff --git a/util/keys/table.go b/util/keys/table.go index f65aa1c8..89a6ab92 100644 --- a/util/keys/table.go +++ b/util/keys/table.go @@ -27,6 +27,16 @@ func (k *Table) init() *Table { 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 func (k *Table) Encode() []byte { k.init() diff --git a/util/keys/tb.go b/util/keys/tb.go index 9dab7320..fc54dd6c 100644 --- a/util/keys/tb.go +++ b/util/keys/tb.go @@ -27,6 +27,16 @@ func (k *TB) init() *TB { 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 func (k *TB) Encode() []byte { k.init() diff --git a/util/keys/thing.go b/util/keys/thing.go index e4728751..652f32b1 100644 --- a/util/keys/thing.go +++ b/util/keys/thing.go @@ -28,6 +28,17 @@ func (k *Thing) init() *Thing { 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 func (k *Thing) Encode() []byte { k.init()