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 hash: 0402d462f75cf177f62e8a0c786da7e9ca6a22535e6412965b600141842d6dae
updated: 2016-09-06T13:06:35.093520212+01:00 updated: 2016-10-02T00:13:11.619047909+01:00
imports: imports:
- name: github.com/abcum/cork
version: d47d0e1881409c934d55048fd062a004a967959c
- name: github.com/abcum/fibre - name: github.com/abcum/fibre
version: 29d9f69b6f170015ffc87d614e16032f27bb3c38 version: 0382d1281adf8f26d58db19abcd2dfa634306f7a
subpackages: subpackages:
- mw - mw
- name: github.com/abcum/logrus - name: github.com/abcum/logrus

View file

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

View file

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

View file

@ -16,27 +16,13 @@ package pack
import ( import (
"bytes" "bytes"
"reflect"
"time"
"encoding/gob" "encoding/gob"
"github.com/abcum/cork"
"github.com/ugorji/go/codec"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
var jh codec.JsonHandle
var ch codec.CborHandle
var bh codec.BincHandle
var mh codec.MsgpackHandle
func init() { func init() {
// GOB
gob.Register(time.Time{})
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
gob.Register(sql.Null{}) gob.Register(sql.Null{})
gob.Register(sql.Void{}) gob.Register(sql.Void{})
gob.Register(sql.Empty{}) gob.Register(sql.Empty{})
@ -62,137 +48,20 @@ func init() {
gob.Register(sql.DefineFieldStatement{}) gob.Register(sql.DefineFieldStatement{})
gob.Register(sql.DefineIndexStatement{}) 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. // Encode encodes a data object into a GOB.
func Encode(src interface{}) (dst []byte) { 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. // Decode decodes a GOB into a data object.
func Decode(src []byte, dst interface{}) { 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) buf := bytes.NewBuffer(src)
gob.NewDecoder(buf).Decode(dst) // gob.NewDecoder(buf).Decode(&dst)
} cork.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)
return return
} }