Use cork serialization for data storage

This commit is contained in:
Tobie Morgan Hitchcock 2016-10-02 00:17:19 +01:00
parent 48345de7d4
commit 25c86a49da
5 changed files with 140 additions and 267 deletions

8
glide.lock generated
View file

@ -1,8 +1,10 @@
hash: a89525c2d3169ec3099a7be4eb2b604a41cbece4246074204c009c240e606a42
updated: 2016-09-06T13:06:35.093520212+01:00
hash: 0402d462f75cf177f62e8a0c786da7e9ca6a22535e6412965b600141842d6dae
updated: 2016-10-02T00:13:11.619047909+01:00
imports:
- name: github.com/abcum/cork
version: d47d0e1881409c934d55048fd062a004a967959c
- name: github.com/abcum/fibre
version: 29d9f69b6f170015ffc87d614e16032f27bb3c38
version: 0382d1281adf8f26d58db19abcd2dfa634306f7a
subpackages:
- mw
- name: github.com/abcum/logrus

View file

@ -26,6 +26,7 @@ import:
- package: github.com/yuin/gopher-lua
- package: github.com/pkg/profile
version: ^1.2.0
- package: github.com/abcum/cork
testImport:
- package: github.com/smartystreets/goconvey
subpackages:

View file

@ -54,8 +54,8 @@ type CommitStatement struct{}
// UseStatement represents a SQL USE statement.
type UseStatement struct {
NS string `codec:"-"`
DB string `codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -64,10 +64,10 @@ type UseStatement struct {
// InfoStatement represents an SQL INFO statement.
type InfoStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -76,84 +76,84 @@ type InfoStatement struct {
// SelectStatement represents a SQL SELECT statement.
type SelectStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Expr []*Field `codec:"expr"`
What []Expr `codec:"what"`
Cond []Expr `codec:"cond"`
Group []*Group `codec:"group"`
Order []*Order `codec:"order"`
Limit Expr `codec:"limit"`
Start Expr `codec:"start"`
Version Expr `codec:"version"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Expr []*Field `cork:"expr" codec:"expr"`
What []Expr `cork:"what" codec:"what"`
Cond []Expr `cork:"cond" codec:"cond"`
Group []*Group `cork:"group" codec:"group"`
Order []*Order `cork:"order" codec:"order"`
Limit Expr `cork:"limit" codec:"limit"`
Start Expr `cork:"start" codec:"start"`
Version Expr `cork:"version" codec:"version"`
Echo Token `cork:"echo" codec:"echo"`
}
// CreateStatement represents a SQL CREATE statement.
type CreateStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []Expr `codec:"what"`
Data []Expr `codec:"data"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []Expr `cork:"what" codec:"what"`
Data []Expr `cork:"data" codec:"data"`
Echo Token `cork:"echo" codec:"echo"`
}
// UpdateStatement represents a SQL UPDATE statement.
type UpdateStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []Expr `codec:"what"`
Data []Expr `codec:"data"`
Cond []Expr `codec:"cond"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []Expr `cork:"what" codec:"what"`
Data []Expr `cork:"data" codec:"data"`
Cond []Expr `cork:"cond" codec:"cond"`
Echo Token `cork:"echo" codec:"echo"`
}
// ModifyStatement represents a SQL MODIFY statement.
type ModifyStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []Expr `codec:"what"`
Diff []Expr `codec:"diff"`
Cond []Expr `codec:"cond"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []Expr `cork:"what" codec:"what"`
Diff []Expr `cork:"diff" codec:"diff"`
Cond []Expr `cork:"cond" codec:"cond"`
Echo Token `cork:"echo" codec:"echo"`
}
// DeleteStatement represents a SQL DELETE statement.
type DeleteStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Hard bool `codec:"hard"`
What []Expr `codec:"what"`
Cond []Expr `codec:"cond"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Hard bool `cork:"hard" codec:"hard"`
What []Expr `cork:"what" codec:"what"`
Cond []Expr `cork:"cond" codec:"cond"`
Echo Token `cork:"echo" codec:"echo"`
}
// RelateStatement represents a SQL RELATE statement.
type RelateStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Type []Expr `codec:"type"`
From []Expr `codec:"from"`
To []Expr `codec:"to"`
Data []Expr `codec:"data"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Type []Expr `cork:"type" codec:"type"`
From []Expr `cork:"from" codec:"from"`
To []Expr `cork:"to" codec:"to"`
Data []Expr `cork:"data" codec:"data"`
Echo Token `cork:"echo" codec:"echo"`
}
// RecordStatement represents a SQL RECORD statement.
type RecordStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Type []Expr `codec:"type"`
When Expr `codec:"when"`
Data []Expr `codec:"data"`
Echo Token `codec:"echo"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Type []Expr `cork:"type" codec:"type"`
When Expr `cork:"when" codec:"when"`
Data []Expr `cork:"data" codec:"data"`
Echo Token `cork:"echo" codec:"echo"`
}
// --------------------------------------------------
@ -162,18 +162,18 @@ type RecordStatement struct {
// DefineTableStatement represents an SQL DEFINE TABLE statement.
type DefineTableStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
}
// RemoveTableStatement represents an SQL REMOVE TABLE statement.
type RemoveTableStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -182,22 +182,23 @@ type RemoveTableStatement struct {
// DefineRulesStatement represents an SQL DEFINE RULES statement.
type DefineRulesStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []string `codec:"-"`
When []string `codec:"-"`
Rule string `codec:"rule"`
Code string `codec:"code"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
When []string `cork:"-" codec:"-"`
Rule string `cork:"rule" codec:"rule"`
Code string `cork:"code" codec:"code"`
Cond []Expr `cork:"cond" codec:"cond"`
}
// RemoveRulesStatement represents an SQL REMOVE RULES statement.
type RemoveRulesStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What []string `codec:"-"`
When []string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
When []string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -206,31 +207,31 @@ type RemoveRulesStatement struct {
// DefineFieldStatement represents an SQL DEFINE FIELD statement.
type DefineFieldStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"name"`
What []string `codec:"-"`
Type string `codec:"type"`
Enum []interface{} `codec:"enum"`
Code string `codec:"code"`
Min float64 `codec:"min"`
Max float64 `codec:"max"`
Match string `codec:"match"`
Default interface{} `codec:"default"`
Notnull bool `codec:"notnull"`
Readonly bool `codec:"readonly"`
Mandatory bool `codec:"mandatory"`
Validate bool `codec:"validate"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"name" codec:"name"`
What []string `cork:"-" codec:"-"`
Type string `cork:"type" codec:"type"`
Enum []interface{} `cork:"enum" codec:"enum"`
Code string `cork:"code" codec:"code"`
Min float64 `cork:"min" codec:"min"`
Max float64 `cork:"max" codec:"max"`
Match string `cork:"match" codec:"match"`
Default interface{} `cork:"default" codec:"default"`
Notnull bool `cork:"notnull" codec:"notnull"`
Readonly bool `cork:"readonly" codec:"readonly"`
Mandatory bool `cork:"mandatory" codec:"mandatory"`
Validate bool `cork:"validate" codec:"validate"`
}
// RemoveFieldStatement represents an SQL REMOVE FIELD statement.
type RemoveFieldStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"-"`
What []string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -239,22 +240,22 @@ type RemoveFieldStatement struct {
// DefineIndexStatement represents an SQL DEFINE INDEX statement.
type DefineIndexStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"name"`
What []string `codec:"-"`
Cols []string `codec:"cols"`
Uniq bool `codec:"unique"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"name" codec:"name"`
What []string `cork:"-" codec:"-"`
Cols []string `cork:"cols" codec:"cols"`
Uniq bool `cork:"unique" codec:"unique"`
}
// RemoveIndexStatement represents an SQL REMOVE INDEX statement.
type RemoveIndexStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"-"`
What []string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"-" codec:"-"`
What []string `cork:"-" codec:"-"`
}
// --------------------------------------------------
@ -263,22 +264,22 @@ type RemoveIndexStatement struct {
// DefineViewStatement represents an SQL DEFINE VIEW statement.
type DefineViewStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"name"`
Expr []*Field `codec:"expr"`
What []Expr `codec:"what"`
Cond []Expr `codec:"cond"`
Group []*Group `codec:"group"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"name" codec:"name"`
Expr []*Field `cork:"expr" codec:"expr"`
What []Expr `cork:"what" codec:"what"`
Cond []Expr `cork:"cond" codec:"cond"`
Group []*Group `cork:"group" codec:"group"`
}
// RemoveViewStatement represents an SQL REMOVE VIEW statement.
type RemoveViewStatement struct {
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
Name string `codec:"-"`
KV string `cork:"-" codec:"-"`
NS string `cork:"-" codec:"-"`
DB string `cork:"-" codec:"-"`
Name string `cork:"-" codec:"-"`
}
// --------------------------------------------------

View file

@ -52,7 +52,7 @@ func (d *Doc) Copy() (i interface{}) {
// Encode encodes the data object to a byte slice.
func (d *Doc) Encode() (dst []byte) {
dst = pack.Encode(&d.data)
dst = pack.Encode(d.data)
return
}

View file

@ -16,27 +16,13 @@ package pack
import (
"bytes"
"reflect"
"time"
"encoding/gob"
"github.com/ugorji/go/codec"
"github.com/abcum/cork"
"github.com/abcum/surreal/sql"
)
var jh codec.JsonHandle
var ch codec.CborHandle
var bh codec.BincHandle
var mh codec.MsgpackHandle
func init() {
// GOB
gob.Register(time.Time{})
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
gob.Register(sql.Null{})
gob.Register(sql.Void{})
gob.Register(sql.Empty{})
@ -62,137 +48,20 @@ func init() {
gob.Register(sql.DefineFieldStatement{})
gob.Register(sql.DefineIndexStatement{})
// JSON
jh.Canonical = false
jh.CheckCircularRef = false
jh.AsSymbols = codec.AsSymbolDefault
jh.SliceType = reflect.TypeOf([]interface{}(nil))
jh.MapType = reflect.TypeOf(map[string]interface{}(nil))
// CBOR
ch.Canonical = false
ch.CheckCircularRef = false
ch.AsSymbols = codec.AsSymbolDefault
ch.SliceType = reflect.TypeOf([]interface{}(nil))
ch.MapType = reflect.TypeOf(map[string]interface{}(nil))
// BINC
bh.Canonical = false
bh.CheckCircularRef = false
bh.AsSymbols = codec.AsSymbolDefault
bh.SliceType = reflect.TypeOf([]interface{}(nil))
bh.MapType = reflect.TypeOf(map[string]interface{}(nil))
bh.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
bh.SetBytesExt(reflect.TypeOf(sql.All{}), 2, extSqlAll{})
bh.SetBytesExt(reflect.TypeOf(sql.Asc{}), 3, extSqlAsc{})
bh.SetBytesExt(reflect.TypeOf(sql.Desc{}), 4, extSqlDesc{})
bh.SetBytesExt(reflect.TypeOf(sql.Null{}), 5, extSqlNull{})
bh.SetBytesExt(reflect.TypeOf(sql.Void{}), 6, extSqlVoid{})
bh.SetBytesExt(reflect.TypeOf(sql.Empty{}), 7, extSqlEmpty{})
bh.SetBytesExt(reflect.TypeOf(sql.Ident{}), 8, extSqlIdent{})
bh.SetBytesExt(reflect.TypeOf(sql.Table{}), 9, extSqlTable{})
bh.SetBytesExt(reflect.TypeOf(sql.Thing{}), 10, extSqlThing{})
bh.SetBytesExt(reflect.TypeOf(sql.Field{}), 11, extSqlField{})
bh.SetBytesExt(reflect.TypeOf(sql.Group{}), 12, extSqlGroup{})
bh.SetBytesExt(reflect.TypeOf(sql.Order{}), 13, extSqlOrder{})
// PACK
mh.WriteExt = true
mh.Canonical = false
mh.RawToString = true
mh.CheckCircularRef = false
mh.AsSymbols = codec.AsSymbolDefault
mh.SliceType = reflect.TypeOf([]interface{}(nil))
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
mh.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
mh.SetBytesExt(reflect.TypeOf(sql.All{}), 2, extSqlAll{})
mh.SetBytesExt(reflect.TypeOf(sql.Asc{}), 3, extSqlAsc{})
mh.SetBytesExt(reflect.TypeOf(sql.Desc{}), 4, extSqlDesc{})
mh.SetBytesExt(reflect.TypeOf(sql.Null{}), 5, extSqlNull{})
mh.SetBytesExt(reflect.TypeOf(sql.Void{}), 6, extSqlVoid{})
mh.SetBytesExt(reflect.TypeOf(sql.Empty{}), 7, extSqlEmpty{})
mh.SetBytesExt(reflect.TypeOf(sql.Ident{}), 8, extSqlIdent{})
mh.SetBytesExt(reflect.TypeOf(sql.Table{}), 9, extSqlTable{})
mh.SetBytesExt(reflect.TypeOf(sql.Thing{}), 10, extSqlThing{})
mh.SetBytesExt(reflect.TypeOf(sql.Field{}), 11, extSqlField{})
mh.SetBytesExt(reflect.TypeOf(sql.Group{}), 12, extSqlGroup{})
mh.SetBytesExt(reflect.TypeOf(sql.Order{}), 13, extSqlOrder{})
}
// Encode encodes a data object into a GOB.
func Encode(src interface{}) (dst []byte) {
return ToGOB(src)
buf := bytes.NewBuffer(nil)
// gob.NewEncoder(buf).Encode(src)
cork.NewEncoder(buf).Encode(src)
return buf.Bytes()
}
// Decode decodes a GOB into a data object.
func Decode(src []byte, dst interface{}) {
FromGOB(src, dst)
}
// ToGOB encodes a data object into a GOB.
func ToGOB(src interface{}) (dst []byte) {
buf := bytes.NewBuffer(nil)
gob.NewEncoder(buf).Encode(src)
return buf.Bytes()
}
// FromGOB decodes a GOB into a data object.
func FromGOB(src []byte, dst interface{}) {
buf := bytes.NewBuffer(src)
gob.NewDecoder(buf).Decode(dst)
}
// ToJSON encodes a data object to a JSON byte slice.
func ToJSON(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &jh).Encode(src)
return
}
// FromJSON decodes a JSON byte slice into a data object.
func FromJSON(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &jh).Decode(dst)
return
}
// ToCBOR encodes a data object to a CBOR byte slice.
func ToCBOR(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &ch).Encode(src)
return
}
// FromCBOR decodes a CBOR byte slice into a data object.
func FromCBOR(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &ch).Decode(dst)
return
}
// ToBINC encodes a data object to a BINC byte slice.
func ToBINC(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &bh).Encode(src)
return
}
// FromBINC decodes a BINC byte slice into a data object.
func FromBINC(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &bh).Decode(dst)
return
}
// ToPACK encodes a data object to a MsgPack byte slice.
func ToPACK(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &mh).Encode(src)
return
}
// FromPACK decodes a MsgPack byte slice into a data object.
func FromPACK(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &mh).Decode(dst)
// gob.NewDecoder(buf).Decode(&dst)
cork.NewDecoder(buf).Decode(dst)
return
}