Update keys package

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-09 20:35:19 +00:00
parent 99ea0a3368
commit f56196a150
26 changed files with 481 additions and 259 deletions

View file

@ -14,40 +14,32 @@
package keys package keys
import (
"fmt"
)
// DB ... // DB ...
type DB struct { type DB struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
} }
// init initialises the key // init initialises the key
func (k *DB) init() *DB { func (k *DB) init() *DB {
k.CF = "!"
k.TK = "d"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB) return encode(k.KV, k.NS, "*", k.DB)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *DB) Decode(data []byte) { func (k *DB) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB) decode(data, &k.KV, &k.NS, &skip, &k.DB)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *DB) String() string { func (k *DB) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB) return output(k.KV, k.NS, "*", k.DB)
} }

View file

@ -24,7 +24,7 @@ type decoder struct {
r *reader r *reader
} }
// Decode decodes an encoded string using the unicode collation algorithm. // decode decodes an encoded string using the unicode collation algorithm.
func decode(data []byte, items ...interface{}) { func decode(data []byte, items ...interface{}) {
newDecoder(bytes.NewReader(data)).Decode(items...) newDecoder(bytes.NewReader(data)).Decode(items...)
} }

View file

@ -14,42 +14,33 @@
package keys package keys
import ( // DT ...
"fmt" type DT struct {
)
// RU ...
type RU struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TK interface{}
RU interface{}
} }
// init initialises the key // init initialises the key
func (k *RU) init() *RU { func (k *DT) init() *DT {
k.CF = "!"
k.TK = "r"
return k return k
} }
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *RU) Encode() []byte { func (k *DT) Encode() []byte {
k.init() k.init()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.RU) return encode(k.KV, k.NS, "*", k.DB, "!", "t", k.TK)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *RU) Decode(data []byte) { func (k *DT) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.TB, &k.RU) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.TK)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *RU) String() string { func (k *DT) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.RU) return output(k.KV, k.NS, "*", k.DB, "!", "t", k.TK)
} }

46
util/keys/du.go Normal file
View file

@ -0,0 +1,46 @@
// 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.
package keys
// DU ...
type DU struct {
KV interface{}
NS interface{}
DB interface{}
US interface{}
}
// init initialises the key
func (k *DU) init() *DU {
return k
}
// Encode encodes the key into binary
func (k *DU) Encode() []byte {
k.init()
return encode(k.KV, k.NS, "*", k.DB, "!", "u", k.US)
}
// Decode decodes the key from binary
func (k *DU) Decode(data []byte) {
k.init()
decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.US)
}
// String returns a string representation of the key
func (k *DU) String() string {
k.init()
return output(k.KV, k.NS, "*", k.DB, "!", "u", k.US)
}

View file

@ -14,25 +14,26 @@
package keys package keys
import (
"fmt"
)
// Edge ... // Edge ...
type Edge struct { type Edge struct {
KV interface{} KV interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
ID interface{} ID interface{}
TK interface{}
TP interface{} TP interface{}
FT interface{}
FK interface{} FK interface{}
} }
// init initialises the key // init initialises the key
func (k *Edge) init() *Edge { func (k *Edge) init() *Edge {
if k.TK == nil { switch k.TK {
case "«»":
case "", "":
case "«", "»":
default:
k.TK = "«»" k.TK = "«»"
} }
return k return k
@ -41,17 +42,17 @@ func (k *Edge) init() *Edge {
// 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()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.TP, k.FK) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID, k.TK, k.TP, k.FT, k.FK)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Edge) Decode(data []byte) { func (k *Edge) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.ID, &k.TP, &k.FK) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.ID, &k.TK, &k.TP, &k.FT, &k.FK)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Edge) String() string { func (k *Edge) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s/%s", k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.TP, k.FK) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID, k.TK, k.TP, k.FT, k.FK)
} }

View file

@ -24,7 +24,7 @@ type encoder struct {
w *writer w *writer
} }
// Encode encodes an interface using the unicode collation algorithm. // encode encodes an interface using the unicode collation algorithm.
func encode(items ...interface{}) []byte { func encode(items ...interface{}) []byte {
b := &bytes.Buffer{} b := &bytes.Buffer{}
newEncoder(b).Encode(items...) newEncoder(b).Encode(items...)

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// FD ... // FD ...
type FD struct { type FD struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
@ -31,25 +25,23 @@ type FD struct {
// init initialises the key // init initialises the key
func (k *FD) init() *FD { func (k *FD) init() *FD {
k.CF = "!"
k.TK = "f"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.FD) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "!", "f", k.FD)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *FD) Decode(data []byte) { func (k *FD) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.TB, &k.FD) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &skip, &k.FD)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *FD) String() string { func (k *FD) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.FD) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "!", "f", k.FD)
} }

View file

@ -14,46 +14,35 @@
package keys package keys
import ( // Field ...
"fmt" type Field struct {
"time"
)
// Event ...
type Event struct {
KV interface{} KV interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
ID interface{} ID interface{}
TP interface{} FD interface{}
AT time.Time
} }
// init initialises the key // init initialises the key
func (k *Event) init() *Event { func (k *Field) init() *Field {
k.TK = "•"
if k.AT.IsZero() {
k.AT = time.Now()
}
return k return k
} }
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *Event) Encode() []byte { func (k *Field) Encode() []byte {
k.init() k.init()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.TP, k.AT) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID, "*", k.FD)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Event) Decode(data []byte) { func (k *Field) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.ID, &k.TP, &k.AT) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.ID, &skip, &k.FD)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Event) String() string { func (k *Field) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s/%s", k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.TP, k.AT.Format(time.RFC3339Nano)) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID, "*", k.FD)
} }

View file

@ -14,41 +14,35 @@
package keys package keys
import (
"fmt"
)
// Index ... // Index ...
type Index struct { type Index struct {
KV interface{} KV interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
IX interface{} IX interface{}
FD interface{} FD interface{}
} }
// init initialises the key // init initialises the key
func (k *Index) init() *Index { func (k *Index) init() *Index {
k.TK = "∆"
return k return k
} }
// 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()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.IX, k.FD) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "¤", k.IX, k.FD)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Index) Decode(data []byte) { func (k *Index) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.IX, &k.FD) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.IX, &k.FD)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Index) String() string { func (k *Index) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%v", k.KV, k.NS, k.DB, k.TB, k.TK, k.IX, k.FD) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "¤", k.IX, k.FD)
} }

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// IX ... // IX ...
type IX struct { type IX struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
@ -31,25 +25,23 @@ type IX struct {
// init initialises the key // init initialises the key
func (k *IX) init() *IX { func (k *IX) init() *IX {
k.CF = "!"
k.TK = "i"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.IX) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "!", "i", k.IX)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *IX) Decode(data []byte) { func (k *IX) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.TB, &k.IX) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &skip, &k.IX)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *IX) String() string { func (k *IX) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.TB, k.IX) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "!", "i", k.IX)
} }

View file

@ -38,6 +38,10 @@ var (
EndOfTime = time.Now().AddDate(50, 0, 0) EndOfTime = time.Now().AddDate(50, 0, 0)
) )
var (
skip interface{}
)
var ( var (
cEND = byte(0x00) cEND = byte(0x00)
cPRE = byte(0x01) cPRE = byte(0x01)

View file

@ -54,6 +54,22 @@ func ShouldNotPrefix(actual interface{}, expected ...interface{}) string {
} }
} }
func ShouldSortBefore(actual interface{}, expected ...interface{}) string {
if bytes.Compare(actual.([]byte), expected[0].([]byte)) > 0 {
return fmt.Sprintf("%v should sort before \n%v\n%s\n%s", actual, expected[0], actual, expected[0])
} else {
return ""
}
}
func ShouldSortAfter(actual interface{}, expected ...interface{}) string {
if bytes.Compare(actual.([]byte), expected[0].([]byte)) < 0 {
return fmt.Sprintf("%v should sort after \n%v\n%s\n%s", actual, expected[0], actual, expected[0])
} else {
return ""
}
}
func TestMain(t *testing.T) { func TestMain(t *testing.T) {
clock, _ := time.Parse(time.RFC3339, "1987-06-22T08:00:00.123456789Z") clock, _ := time.Parse(time.RFC3339, "1987-06-22T08:00:00.123456789Z")
@ -63,116 +79,161 @@ func TestMain(t *testing.T) {
obj Key obj Key
new Key new Key
}{ }{
{
str: "/surreal",
obj: &KV{KV: "surreal"},
new: &KV{},
},
{ {
str: "/surreal/!/¥", str: "/surreal/!/¥",
obj: &CK{KV: "surreal"}, obj: &CK{KV: "surreal"},
new: &CK{}, new: &CK{},
}, },
{ {
str: "/surreal/!/n/abcum", str: "/surreal/abcum",
obj: &NS{KV: "surreal", NS: "abcum"}, obj: &NS{KV: "surreal", NS: "abcum"},
new: &NS{}, new: &NS{},
}, },
{ {
str: "/surreal/!/d/abcum/database", str: "/surreal/abcum/!/t/default",
obj: &NT{KV: "surreal", NS: "abcum", TK: "default"},
new: &NT{},
},
{
str: "/surreal/abcum/!/u/info@abcum.com",
obj: &NU{KV: "surreal", NS: "abcum", US: "info@abcum.com"},
new: &NU{},
},
{
str: "/surreal/abcum/*/database",
obj: &DB{KV: "surreal", NS: "abcum", DB: "database"}, obj: &DB{KV: "surreal", NS: "abcum", DB: "database"},
new: &DB{}, new: &DB{},
}, },
{ {
str: "/surreal/!/t/abcum/database/person", str: "/surreal/abcum/*/database/!/l/df8c74fa-428a-42b7-b279-b5fbe33d72a7",
obj: &LV{KV: "surreal", NS: "abcum", DB: "database", LV: "df8c74fa-428a-42b7-b279-b5fbe33d72a7"},
new: &LV{},
},
{
str: "/surreal/abcum/*/database/!/s/admin",
obj: &SC{KV: "surreal", NS: "abcum", DB: "database", SC: "admin"},
new: &SC{},
},
{
str: "/surreal/abcum/*/database/!/s/admin/!/t/default",
obj: &ST{KV: "surreal", NS: "abcum", DB: "database", SC: "admin", TK: "default"},
new: &ST{},
},
{
str: "/surreal/abcum/*/database/!/t/default",
obj: &DT{KV: "surreal", NS: "abcum", DB: "database", TK: "default"},
new: &DT{},
},
{
str: "/surreal/abcum/*/database/!/u/info@abcum.com",
obj: &DU{KV: "surreal", NS: "abcum", DB: "database", US: "info@abcum.com"},
new: &DU{},
},
{
str: "/surreal/abcum/*/database/!/v/ages",
obj: &VW{KV: "surreal", NS: "abcum", DB: "database", VW: "ages"},
new: &VW{},
},
{
str: "/surreal/abcum/*/database/*/person",
obj: &TB{KV: "surreal", NS: "abcum", DB: "database", TB: "person"}, obj: &TB{KV: "surreal", NS: "abcum", DB: "database", TB: "person"},
new: &TB{}, new: &TB{},
}, },
{ {
str: "/surreal/!/f/abcum/database/person/fullname", str: "/surreal/abcum/*/database/*/person/!/f/fullname",
obj: &FD{KV: "surreal", NS: "abcum", DB: "database", TB: "person", FD: "fullname"}, obj: &FD{KV: "surreal", NS: "abcum", DB: "database", TB: "person", FD: "fullname"},
new: &FD{}, new: &FD{},
}, },
{ {
str: "/surreal/!/r/abcum/database/person/select", str: "/surreal/abcum/*/database/*/person/!/i/teenagers",
obj: &RU{KV: "surreal", NS: "abcum", DB: "database", TB: "person", RU: "select"},
new: &RU{},
},
{
str: "/surreal/!/i/abcum/database/person/teenagers",
obj: &IX{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "teenagers"}, obj: &IX{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "teenagers"},
new: &IX{}, new: &IX{},
}, },
{ {
str: "/surreal/abcum/database/person/*/\x00", str: "/surreal/abcum/*/database/*/person/*",
obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: Prefix},
new: &Thing{},
},
{
str: "/surreal/abcum/database/person/*/873c2f37-ea03-4c5e-843e-cf393af44155",
obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155"},
new: &Thing{},
},
{
str: "/surreal/abcum/database/person",
obj: &Table{KV: "surreal", NS: "abcum", DB: "database", TB: "person"}, obj: &Table{KV: "surreal", NS: "abcum", DB: "database", TB: "person"},
new: &Table{}, new: &Table{},
}, },
{ {
str: "/surreal/abcum/database/person/*/\xff", str: "/surreal/abcum/*/database/*/person/*/\x00",
obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: Prefix},
new: &Thing{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155",
obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155"},
new: &Thing{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155/*/name.first",
obj: &Field{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", FD: "name.first"},
new: &Field{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155/*/name.last",
obj: &Field{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", FD: "name.last"},
new: &Field{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155/«/clicked/link/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "«", TP: "clicked", FT: "link", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155/«»/clicked/link/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "«»", TP: "clicked", FT: "link", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/*/database/*/person/*/873c2f37-ea03-4c5e-843e-cf393af44155/»/clicked/link/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "»", TP: "clicked", FT: "link", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/*/database/*/person/*/\xff",
obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: Suffix}, obj: &Thing{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: Suffix},
new: &Thing{}, new: &Thing{},
}, },
{ {
str: "/surreal/abcum/database/person/~/873c2f37-ea03-4c5e-843e-cf393af44155/1987-06-22T08:00:00.123456789Z", str: "/surreal/abcum/*/database/*/person/~/873c2f37-ea03-4c5e-843e-cf393af44155/1987-06-22T08:00:00.123456789Z",
obj: &Patch{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", AT: clock}, obj: &Patch{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", AT: clock},
new: &Patch{}, new: &Patch{},
}, },
{ {
str: "/surreal/abcum/database/person/~/test/1987-06-22T08:00:00.123456789Z", str: "/surreal/abcum/*/database/*/person/~/test/1987-06-22T08:00:00.123456789Z",
obj: &Patch{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "test", AT: clock}, obj: &Patch{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "test", AT: clock},
new: &Patch{}, new: &Patch{},
}, },
{ {
str: "/surreal/abcum/database/person/•/873c2f37-ea03-4c5e-843e-cf393af44155/friend/1987-06-22T08:00:00.123456789Z", str: "/surreal/abcum/*/database/*/person/¤/names/[false account:1 lastname <nil> firstname]",
obj: &Event{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TP: "friend", AT: clock},
new: &Event{},
},
{
str: "/surreal/abcum/database/person/«»/873c2f37-ea03-4c5e-843e-cf393af44155/clicked/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TP: "clicked", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/database/person/«/873c2f37-ea03-4c5e-843e-cf393af44155/clicked/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "«", TP: "clicked", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/database/person/»/873c2f37-ea03-4c5e-843e-cf393af44155/clicked/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "»", TP: "clicked", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/database/person/«»/873c2f37-ea03-4c5e-843e-cf393af44155/clicked/b38d7aa1-60d6-4f2d-8702-46bd0fa961fe",
obj: &Edge{KV: "surreal", NS: "abcum", DB: "database", TB: "person", ID: "873c2f37-ea03-4c5e-843e-cf393af44155", TK: "«»", TP: "clicked", FK: "b38d7aa1-60d6-4f2d-8702-46bd0fa961fe"},
new: &Edge{},
},
{
str: "/surreal/abcum/database/person/∆/names/[lastname firstname]",
obj: &Index{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "names", FD: []interface{}{"lastname", "firstname"}},
new: &Index{},
},
{
str: "/surreal/abcum/database/person/∆/names/[false account:1 lastname <nil> firstname]",
obj: &Index{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "names", FD: []interface{}{false, "account:1", "lastname", nil, "firstname"}}, obj: &Index{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "names", FD: []interface{}{false, "account:1", "lastname", nil, "firstname"}},
new: &Index{}, new: &Index{},
}, },
{ {
str: "/surreal/abcum/database/person/∆/uniqs/[lastname firstname]/873c2f37-ea03-4c5e-843e-cf393af44155", str: "/surreal/abcum/*/database/*/person/¤/names/[lastname firstname]",
obj: &Point{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "uniqs", FD: []interface{}{"lastname", "firstname"}, ID: "873c2f37-ea03-4c5e-843e-cf393af44155"}, obj: &Index{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "names", FD: []interface{}{"lastname", "firstname"}},
new: &Index{},
},
{
str: "/surreal/abcum/*/database/*/person/¤/names/[lastname firstname]/873c2f37-ea03-4c5e-843e-cf393af44155",
obj: &Point{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "names", FD: []interface{}{"lastname", "firstname"}, ID: "873c2f37-ea03-4c5e-843e-cf393af44155"},
new: &Point{}, new: &Point{},
}, },
{ {
str: "/surreal/abcum/database/person/∆/uniqs/[false account:1 lastname <nil> firstname]/873c2f37-ea03-4c5e-843e-cf393af44155", str: "/surreal/abcum/*/database/*/person/¤/uniqs/[false account:1 lastname <nil> firstname]/873c2f37-ea03-4c5e-843e-cf393af44155",
obj: &Point{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "uniqs", FD: []interface{}{false, "account:1", "lastname", nil, "firstname"}, ID: "873c2f37-ea03-4c5e-843e-cf393af44155"}, obj: &Point{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "uniqs", FD: []interface{}{false, "account:1", "lastname", nil, "firstname"}, ID: "873c2f37-ea03-4c5e-843e-cf393af44155"},
new: &Point{}, new: &Point{},
}, },
{
str: "/surreal/abcum/*/database/*/person/¤/uniqs/[lastname firstname]/873c2f37-ea03-4c5e-843e-cf393af44155",
obj: &Point{KV: "surreal", NS: "abcum", DB: "database", TB: "person", IX: "uniqs", FD: []interface{}{"lastname", "firstname"}, ID: "873c2f37-ea03-4c5e-843e-cf393af44155"},
new: &Point{},
},
{ {
str: "Test key", str: "Test key",
new: &Full{}, new: &Full{},
@ -262,6 +323,9 @@ func TestMain(t *testing.T) {
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "b"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "b"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "bB"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "bB"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "c"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "c"},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test1", TP: "friend", FK: int8(2)},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test1", TP: "friend", FK: int8(3)},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test2", TP: "friend", FK: int8(1)},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "z"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "z"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "Â"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "Â"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "Ä"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "Ä"},
@ -276,10 +340,6 @@ func TestMain(t *testing.T) {
&Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), AT: time.Now()}, &Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), AT: time.Now()},
&Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), AT: time.Now()}, &Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), AT: time.Now()},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), TP: "friend", FK: int8(2)},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(1), TP: "friend", FK: int8(3)},
&Edge{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: int8(2), TP: "friend", FK: int8(1)},
&Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: Prefix}, &Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: Prefix},
&Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"account:abcum", false, "Smith", nil, "Zoe"}}, &Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"account:abcum", false, "Smith", nil, "Zoe"}},
@ -307,14 +367,14 @@ func TestMain(t *testing.T) {
nos []Key nos []Key
}{ }{
{ {
obj: &Table{KV: "kv", NS: "ns", DB: "db", TB: "person"}, obj: &TB{KV: "kv", NS: "ns", DB: "db", TB: "person"},
yes: []Key{ yes: []Key{
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Prefix}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Prefix},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Suffix}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Suffix},
&Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test", AT: clock}, &Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test", AT: clock},
&Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"1", "2"}}, &Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"1", "2"}},
&Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"3", "4"}}, &Point{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"3", "4"}},
}, },
nos: []Key{ nos: []Key{
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "other", ID: "test"}, &Thing{KV: "kv", NS: "ns", DB: "db", TB: "other", ID: "test"},
@ -323,6 +383,23 @@ func TestMain(t *testing.T) {
&Thing{KV: "other", NS: "ns", DB: "db", TB: "person", ID: "test"}, &Thing{KV: "other", NS: "ns", DB: "db", TB: "person", ID: "test"},
}, },
}, },
{
obj: &Table{KV: "kv", NS: "ns", DB: "db", TB: "person"},
yes: []Key{
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Prefix},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test"},
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Suffix},
},
nos: []Key{
&Thing{KV: "kv", NS: "ns", DB: "db", TB: "other", ID: "test"},
&Thing{KV: "kv", NS: "ns", DB: "other", TB: "person", ID: "test"},
&Thing{KV: "kv", NS: "other", DB: "db", TB: "person", ID: "test"},
&Thing{KV: "other", NS: "ns", DB: "db", TB: "person", ID: "test"},
&Patch{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: "test", AT: clock},
&Index{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"1", "2"}},
&Point{KV: "kv", NS: "ns", DB: "db", TB: "person", IX: "names", FD: []interface{}{"3", "4"}},
},
},
{ {
obj: &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Ignore}, obj: &Thing{KV: "kv", NS: "ns", DB: "db", TB: "person", ID: Ignore},
yes: []Key{ yes: []Key{
@ -363,30 +440,28 @@ func TestDisplaying(t *testing.T) {
for _, test := range tests { for _, test := range tests {
Convey(test.str, t, func() { Convey(test.str, t, func() {
Convey("String should match", func() {
So(test.obj.String(), ShouldEqual, test.str) So(test.obj.String(), ShouldEqual, test.str)
}) })
})
} }
} }
func TestEncoding(t *testing.T) { func TestEncoding(t *testing.T) {
for _, test := range tests { for i, test := range tests {
Convey(test.str, t, func() { Convey(test.str, t, func() {
enc := test.obj.Encode() enc := test.obj.Encode()
test.new.Decode(enc) test.new.Decode(enc)
Convey("Key should encode and decode", func() {
Printf("%s\n\n%#q\n\n%v\n\n", test.str, enc, enc)
So(test.new, ShouldResemble, test.obj) So(test.new, ShouldResemble, test.obj)
})
if i > 0 && i < len(tests)-1 {
old := tests[i-1].obj.Encode()
So(old, ShouldSortBefore, enc)
}
}) })
@ -425,14 +500,9 @@ func TestSorting(t *testing.T) {
txt := fmt.Sprintf("%#v", sorts[i-1]) txt := fmt.Sprintf("%#v", sorts[i-1])
Convey(txt, t, func() { Convey(txt, t, func() {
one := sorts[i-1].Encode() one := sorts[i-1].Encode()
two := sorts[i].Encode() two := sorts[i].Encode()
So(one, ShouldSortBefore, two)
Convey("Key should sort before next key", func() {
Printf("%#v\n%#v\n------\n%#v\n%#v\n------\n%#q\n%#q", sorts[i-1], sorts[i], one, two, one, two)
So(string(one), ShouldBeLessThanOrEqualTo, string(two))
})
}) })
} }

43
util/keys/kv.go Normal file
View file

@ -0,0 +1,43 @@
// 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.
package keys
// KV ...
type KV struct {
KV interface{}
}
// init initialises the key
func (k *KV) init() *KV {
return k
}
// Encode encodes the key into binary
func (k *KV) Encode() []byte {
k.init()
return encode(k.KV)
}
// Decode decodes the key from binary
func (k *KV) Decode(data []byte) {
k.init()
decode(data, &k.KV)
}
// String returns a string representation of the key
func (k *KV) String() string {
k.init()
return output(k.KV)
}

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// LV ... // LV ...
type LV struct { type LV struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
LV interface{} LV interface{}
@ -30,25 +24,23 @@ type LV struct {
// init initialises the key // init initialises the key
func (k *LV) init() *LV { func (k *LV) init() *LV {
k.CF = "!"
k.TK = "l"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.LV) return encode(k.KV, k.NS, "*", k.DB, "!", "l", k.LV)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *LV) Decode(data []byte) { func (k *LV) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.LV) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.LV)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *LV) String() string { func (k *LV) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.LV) return output(k.KV, k.NS, "*", k.DB, "!", "l", k.LV)
} }

View file

@ -14,39 +14,31 @@
package keys package keys
import (
"fmt"
)
// NS ... // NS ...
type NS struct { type NS struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
} }
// init initialises the key // init initialises the key
func (k *NS) init() *NS { func (k *NS) init() *NS {
k.CF = "!"
k.TK = "n"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS) return encode(k.KV, k.NS)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *NS) Decode(data []byte) { func (k *NS) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS) decode(data, &k.KV, &k.NS)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *NS) String() string { func (k *NS) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS) return output(k.KV, k.NS)
} }

45
util/keys/nt.go Normal file
View file

@ -0,0 +1,45 @@
// 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.
package keys
// NT ...
type NT struct {
KV interface{}
NS interface{}
TK interface{}
}
// init initialises the key
func (k *NT) init() *NT {
return k
}
// Encode encodes the key into binary
func (k *NT) Encode() []byte {
k.init()
return encode(k.KV, k.NS, "!", "t", k.TK)
}
// Decode decodes the key from binary
func (k *NT) Decode(data []byte) {
k.init()
decode(data, &k.KV, &k.NS, &skip, &skip, &k.TK)
}
// String returns a string representation of the key
func (k *NT) String() string {
k.init()
return output(k.KV, k.NS, "!", "t", k.TK)
}

45
util/keys/nu.go Normal file
View file

@ -0,0 +1,45 @@
// 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.
package keys
// NU ...
type NU struct {
KV interface{}
NS interface{}
US interface{}
}
// init initialises the key
func (k *NU) init() *NU {
return k
}
// Encode encodes the key into binary
func (k *NU) Encode() []byte {
k.init()
return encode(k.KV, k.NS, "!", "u", k.US)
}
// Decode decodes the key from binary
func (k *NU) Decode(data []byte) {
k.init()
decode(data, &k.KV, &k.NS, &skip, &skip, &k.US)
}
// String returns a string representation of the key
func (k *NU) String() string {
k.init()
return output(k.KV, k.NS, "!", "u", k.US)
}

30
util/keys/output.go Normal file
View file

@ -0,0 +1,30 @@
// 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.
package keys
import "fmt"
// output decodes an encoded string using the unicode collation algorithm.
func output(items ...interface{}) (out string) {
for _, v := range items {
switch v.(type) {
default:
out = out + fmt.Sprintf("/%s", v)
case []interface{}:
out = out + fmt.Sprintf("/%v", v)
}
}
return
}

View file

@ -15,7 +15,6 @@
package keys package keys
import ( import (
"fmt"
"time" "time"
) )
@ -25,14 +24,12 @@ type Patch struct {
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
ID interface{} ID interface{}
AT time.Time AT time.Time
} }
// init initialises the key // init initialises the key
func (k *Patch) init() *Patch { func (k *Patch) init() *Patch {
k.TK = "~"
if k.AT.IsZero() { if k.AT.IsZero() {
k.AT = time.Now() k.AT = time.Now()
} }
@ -42,17 +39,17 @@ func (k *Patch) init() *Patch {
// 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()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.AT) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "~", k.ID, k.AT)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Patch) Decode(data []byte) { func (k *Patch) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.ID, &k.AT) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.ID, &k.AT)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Patch) String() string { func (k *Patch) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%s", k.KV, k.NS, k.DB, k.TB, k.TK, k.ID, k.AT.Format(time.RFC3339Nano)) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "~", k.ID, k.AT.Format(time.RFC3339Nano))
} }

View file

@ -14,17 +14,12 @@
package keys package keys
import (
"fmt"
)
// Point ... // Point ...
type Point struct { type Point struct {
KV interface{} KV interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
IX interface{} IX interface{}
FD interface{} FD interface{}
ID interface{} ID interface{}
@ -32,24 +27,23 @@ type Point struct {
// init initialises the key // init initialises the key
func (k *Point) init() *Point { func (k *Point) init() *Point {
k.TK = "∆"
return k return k
} }
// 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()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.IX, k.FD, k.ID) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "¤", k.IX, k.FD, k.ID)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Point) Decode(data []byte) { func (k *Point) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.IX, &k.FD, &k.ID) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.IX, &k.FD, &k.ID)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Point) String() string { func (k *Point) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s/%v/%s", k.KV, k.NS, k.DB, k.TB, k.TK, k.IX, k.FD, k.ID) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "¤", k.IX, k.FD, k.ID)
} }

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// SC ... // SC ...
type SC struct { type SC struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
SC interface{} SC interface{}
@ -30,25 +24,23 @@ type SC struct {
// init initialises the key // init initialises the key
func (k *SC) init() *SC { func (k *SC) init() *SC {
k.CF = "!"
k.TK = "s"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.SC) return encode(k.KV, k.NS, "*", k.DB, "!", "s", k.SC)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *SC) Decode(data []byte) { func (k *SC) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.SC) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.SC)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *SC) String() string { func (k *SC) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.SC) return output(k.KV, k.NS, "*", k.DB, "!", "s", k.SC)
} }

47
util/keys/st.go Normal file
View file

@ -0,0 +1,47 @@
// 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.
package keys
// ST ...
type ST struct {
KV interface{}
NS interface{}
DB interface{}
SC interface{}
TK interface{}
}
// init initialises the key
func (k *ST) init() *ST {
return k
}
// Encode encodes the key into binary
func (k *ST) Encode() []byte {
k.init()
return encode(k.KV, k.NS, "*", k.DB, "!", "s", k.SC, "!", "t", k.TK)
}
// Decode decodes the key from binary
func (k *ST) Decode(data []byte) {
k.init()
decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.SC, &skip, &skip, &k.TK)
}
// String returns a string representation of the key
func (k *ST) String() string {
k.init()
return output(k.KV, k.NS, "*", k.DB, "!", "s", k.SC, "!", "t", k.TK)
}

View file

@ -14,10 +14,6 @@
package keys package keys
import (
"fmt"
)
// Table ... // Table ...
type Table struct { type Table struct {
KV interface{} KV interface{}
@ -34,17 +30,17 @@ func (k *Table) init() *Table {
// 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()
return encode(k.KV, k.NS, k.DB, k.TB) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "*")
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Table) Decode(data []byte) { func (k *Table) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Table) String() string { func (k *Table) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s", k.KV, k.NS, k.DB, k.TB) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "*")
} }

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// TB ... // TB ...
type TB struct { type TB struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
@ -30,25 +24,23 @@ type TB struct {
// init initialises the key // init initialises the key
func (k *TB) init() *TB { func (k *TB) init() *TB {
k.CF = "!"
k.TK = "t"
return k return k
} }
// 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()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.TB) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *TB) Decode(data []byte) { func (k *TB) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.TB) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *TB) String() string { func (k *TB) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.TB) return output(k.KV, k.NS, "*", k.DB, "*", k.TB)
} }

View file

@ -14,40 +14,34 @@
package keys package keys
import (
"fmt"
)
// Thing ... // Thing ...
type Thing struct { type Thing struct {
KV interface{} KV interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
TB interface{} TB interface{}
TK interface{}
ID interface{} ID interface{}
} }
// init initialises the key // init initialises the key
func (k *Thing) init() *Thing { func (k *Thing) init() *Thing {
k.TK = "*"
return k return k
} }
// 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()
return encode(k.KV, k.NS, k.DB, k.TB, k.TK, k.ID) return encode(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *Thing) Decode(data []byte) { func (k *Thing) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.NS, &k.DB, &k.TB, &k.TK, &k.ID) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &k.TB, &skip, &k.ID)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *Thing) String() string { func (k *Thing) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s", k.KV, k.NS, k.DB, k.TB, k.TK, k.ID) return output(k.KV, k.NS, "*", k.DB, "*", k.TB, "*", k.ID)
} }

View file

@ -14,15 +14,9 @@
package keys package keys
import (
"fmt"
)
// VW ... // VW ...
type VW struct { type VW struct {
KV interface{} KV interface{}
CF interface{}
TK interface{}
NS interface{} NS interface{}
DB interface{} DB interface{}
VW interface{} VW interface{}
@ -30,25 +24,23 @@ type VW struct {
// init initialises the key // init initialises the key
func (k *VW) init() *VW { func (k *VW) init() *VW {
k.CF = "!"
k.TK = "v"
return k return k
} }
// Encode encodes the key into binary // Encode encodes the key into binary
func (k *VW) Encode() []byte { func (k *VW) Encode() []byte {
k.init() k.init()
return encode(k.KV, k.CF, k.TK, k.NS, k.DB, k.VW) return encode(k.KV, k.NS, "*", k.DB, "!", "v", k.VW)
} }
// Decode decodes the key from binary // Decode decodes the key from binary
func (k *VW) Decode(data []byte) { func (k *VW) Decode(data []byte) {
k.init() k.init()
decode(data, &k.KV, &k.CF, &k.TK, &k.NS, &k.DB, &k.VW) decode(data, &k.KV, &k.NS, &skip, &k.DB, &skip, &skip, &k.VW)
} }
// String returns a string representation of the key // String returns a string representation of the key
func (k *VW) String() string { func (k *VW) String() string {
k.init() k.init()
return fmt.Sprintf("/%s/%s/%s/%s/%s/%s", k.KV, k.CF, k.TK, k.NS, k.DB, k.VW) return output(k.KV, k.NS, "*", k.DB, "!", "v", k.VW)
} }