@ -2,7 +2,6 @@ package graphvent
import (
import (
"crypto/ecdh"
"crypto/ecdh"
"crypto/sha512"
"encoding/binary"
"encoding/binary"
"errors"
"errors"
"fmt"
"fmt"
@ -10,83 +9,11 @@ import (
"reflect"
"reflect"
"runtime"
"runtime"
"sync"
"sync"
"strconv"
badger "github.com/dgraph-io/badger/v3"
badger "github.com/dgraph-io/badger/v3"
)
)
func Hash ( base string , name string ) uint64 {
digest := append ( [ ] byte ( base ) , 0x00 )
digest = append ( digest , [ ] byte ( name ) ... )
hash := sha512 . Sum512 ( digest )
return binary . BigEndian . Uint64 ( hash [ 0 : 8 ] )
}
type ExtType uint64
type NodeType uint64
type SignalType uint64
type PolicyType uint64
type SerializedType uint64
func NewExtType ( name string ) ExtType {
return ExtType ( Hash ( ExtTypeBase , name ) )
}
func NewNodeType ( name string ) NodeType {
return NodeType ( Hash ( NodeTypeBase , name ) )
}
func NewSignalType ( name string ) SignalType {
return SignalType ( Hash ( SignalTypeBase , name ) )
}
func NewPolicyType ( name string ) PolicyType {
return PolicyType ( Hash ( PolicyTypeBase , name ) )
}
func NewSerializedType ( name string ) SerializedType {
val := SerializedType ( Hash ( SerializedTypeBase , name ) )
println ( fmt . Sprintf ( "TYPE: %s: %d" , name , val ) )
return val
}
const (
TagBase = "GraphventTag"
ExtTypeBase = "ExtType"
NodeTypeBase = "NodeType"
SignalTypeBase = "SignalType"
PolicyTypeBase = "PolicyType"
SerializedTypeBase = "SerializedType"
FieldNameBase = "FieldName"
)
var (
var (
ListenerExtType = NewExtType ( "LISTENER" )
LockableExtType = NewExtType ( "LOCKABLE" )
GQLExtType = NewExtType ( "GQL" )
GroupExtType = NewExtType ( "GROUP" )
ECDHExtType = NewExtType ( "ECDH" )
GQLNodeType = NewNodeType ( "GQL" )
StopSignalType = NewSignalType ( "STOP" )
CreateSignalType = NewSignalType ( "CREATE" )
StartSignalType = NewSignalType ( "START" )
ErrorSignalType = NewSignalType ( "ERROR" )
StatusSignalType = NewSignalType ( "STATUS" )
LinkSignalType = NewSignalType ( "LINK" )
LockSignalType = NewSignalType ( "LOCK" )
ReadSignalType = NewSignalType ( "READ" )
ReadResultSignalType = NewSignalType ( "READ_RESULT" )
ACLTimeoutSignalType = NewSignalType ( "ACL_TIMEOUT" )
MemberOfPolicyType = NewPolicyType ( "USER_OF" )
RequirementOfPolicyType = NewPolicyType ( "REQUIEMENT_OF" )
PerNodePolicyType = NewPolicyType ( "PER_NODE" )
AllNodesPolicyType = NewPolicyType ( "ALL_NODES" )
ErrorType = NewSerializedType ( "ERROR" )
NodeNotFoundError = errors . New ( "Node not found in DB" )
NodeNotFoundError = errors . New ( "Node not found in DB" )
ECDH = ecdh . X25519 ( )
ECDH = ecdh . X25519 ( )
)
)
@ -100,14 +27,18 @@ type NodeInfo struct {
Extensions [ ] ExtType
Extensions [ ] ExtType
}
}
type TypeSerialize func ( * Context , uint64 , reflect . Type , * reflect . Value ) ( SerializedValue , error )
type TypeDeserialize func ( * Context , SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error )
type TypeInfo struct {
type TypeInfo struct {
Type reflect . Type
Type reflect . Type
Serialize TypeSerialize
Serialize TypeSerialize
Deserialize TypeDeserialize
Deserialize TypeDeserialize
}
}
type KindInfo struct {
Type SerializedType
Serialize TypeSerialize
Deserialize TypeDeserialize
}
// A Context stores all the data to run a graphvent process
// A Context stores all the data to run a graphvent process
type Context struct {
type Context struct {
// DB is the database connection used to load and write nodes
// DB is the database connection used to load and write nodes
@ -309,149 +240,6 @@ func (ctx *Context) Send(messages Messages) error {
return nil
return nil
}
}
type KindInfo struct {
Type SerializedType
Serialize TypeSerialize
Deserialize TypeDeserialize
}
type SerializedValue struct {
TypeStack [ ] uint64
Data [ ] byte
}
func SerializeValue ( ctx * Context , value reflect . Value ) ( SerializedValue , error ) {
val , err := serializeValue ( ctx , value . Type ( ) , & value )
ctx . Log . Logf ( "serialize" , "SERIALIZED_VALUE(%+v): %+v - %+v" , value . Type ( ) , val . TypeStack , val . Data )
return val , err
}
func serializeValue ( ctx * Context , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var ctx_type uint64 = 0x00
ctype , exists := ctx . TypeReflects [ t ]
if exists == true {
type_info := ctx . Types [ ctype ]
ctx_type = uint64 ( ctype )
if type_info . Serialize != nil {
return type_info . Serialize ( ctx , ctx_type , t , value )
}
}
kind := t . Kind ( )
kind_info , handled := ctx . Kinds [ kind ]
if handled == false {
return SerializedValue { } , fmt . Errorf ( "Don't know how to serialize kind %+v" , kind )
} else if ctx_type == 0x00 {
ctx_type = uint64 ( kind_info . Type )
}
return kind_info . Serialize ( ctx , ctx_type , t , value )
}
func SerializeField ( ctx * Context , ext Extension , field_name string ) ( SerializedValue , error ) {
if ext == nil {
return SerializedValue { } , fmt . Errorf ( "Cannot get fields on nil Extension" )
}
ext_value := reflect . ValueOf ( ext ) . Elem ( )
field := ext_value . FieldByName ( field_name )
if field . IsValid ( ) == false {
return SerializedValue { } , fmt . Errorf ( "%s is not a field in %+v" , field_name , ext )
} else {
return SerializeValue ( ctx , field )
}
}
func ( value SerializedValue ) MarshalBinary ( ) ( [ ] byte , error ) {
data := make ( [ ] byte , value . SerializedSize ( ) )
binary . BigEndian . PutUint64 ( data [ 0 : 8 ] , uint64 ( len ( value . TypeStack ) ) )
binary . BigEndian . PutUint64 ( data [ 8 : 16 ] , uint64 ( len ( value . Data ) ) )
for i , t := range ( value . TypeStack ) {
type_start := ( i + 2 ) * 8
type_end := ( i + 3 ) * 8
binary . BigEndian . PutUint64 ( data [ type_start : type_end ] , t )
}
return append ( data , value . Data ... ) , nil
}
func ( value SerializedValue ) SerializedSize ( ) uint64 {
return uint64 ( ( len ( value . TypeStack ) + 2 ) * 8 )
}
func ParseSerializedValue ( data [ ] byte ) ( SerializedValue , [ ] byte , error ) {
if len ( data ) < 8 {
return SerializedValue { } , nil , fmt . Errorf ( "SerializedValue required to have at least 8 bytes when serialized" )
}
num_types := int ( binary . BigEndian . Uint64 ( data [ 0 : 8 ] ) )
data_size := int ( binary . BigEndian . Uint64 ( data [ 8 : 16 ] ) )
type_stack := make ( [ ] uint64 , num_types )
for i := 0 ; i < num_types ; i += 1 {
type_start := ( i + 2 ) * 8
type_end := ( i + 3 ) * 8
type_stack [ i ] = binary . BigEndian . Uint64 ( data [ type_start : type_end ] )
}
types_end := 8 * ( num_types + 2 )
data_end := types_end + data_size
return SerializedValue {
type_stack ,
data [ types_end : data_end ] ,
} , data [ data_end : ] , nil
}
func DeserializeValue ( ctx * Context , value SerializedValue , n int ) ( reflect . Type , [ ] reflect . Value , SerializedValue , error ) {
ctx . Log . Logf ( "serialize" , "DeserializeValue: %+v - %d" , value , n )
ret := make ( [ ] reflect . Value , n )
var deserialize TypeDeserialize = nil
var reflect_type reflect . Type = nil
ctx_type := value . TypeStack [ 0 ]
value . TypeStack = value . TypeStack [ 1 : ]
type_info , exists := ctx . Types [ SerializedType ( ctx_type ) ]
if exists == true {
deserialize = type_info . Deserialize
reflect_type = type_info . Type
} else {
kind , exists := ctx . KindTypes [ SerializedType ( ctx_type ) ]
if exists == false {
return nil , nil , SerializedValue { } , fmt . Errorf ( "Cannot deserialize 0x%x: unknown type/kind" , ctx_type )
}
kind_info := ctx . Kinds [ kind ]
deserialize = kind_info . Deserialize
}
ctx . Log . Logf ( "serialize" , "Deserializing: %d x %d" , ctx_type , n )
if value . Data == nil {
reflect_type , _ , val , err := deserialize ( ctx , value )
if err != nil {
return nil , nil , SerializedValue { } , err
}
return reflect_type , nil , val , nil
}
val := SerializedValue {
value . TypeStack ,
value . Data ,
}
for i := 0 ; i < n ; i += 1 {
val . TypeStack = value . TypeStack
var elem * reflect . Value
var err error
reflect_type , elem , val , err = deserialize ( ctx , val )
if err != nil {
return nil , nil , SerializedValue { } , err
}
ret [ i ] = * elem
}
ctx . Log . Logf ( "serialize" , "DeserializeValue: DONE %+v - %+v" , val , ret )
return reflect_type , ret , val , nil
}
// Create a new Context with the base library content added
// Create a new Context with the base library content added
func NewContext ( db * badger . DB , log Logger ) ( * Context , error ) {
func NewContext ( db * badger . DB , log Logger ) ( * Context , error ) {
ctx := & Context {
ctx := & Context {
@ -473,7 +261,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
var err error
var err error
err = ctx . RegisterKind ( reflect . Pointer , NewSerializedType ( "pointer" ) ,
err = ctx . RegisterKind ( reflect . Pointer , NewSerializedType ( "pointer" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte
var data [ ] byte
var elem_value * reflect . Value = nil
var elem_value * reflect . Value = nil
if value == nil {
if value == nil {
@ -493,12 +281,12 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
data = append ( data , elem . Data ... )
data = append ( data , elem . Data ... )
}
}
return SerializedValue {
return SerializedValue {
append ( [ ] uint64 { ctx_type } , elem . TypeStack ... ) ,
append ( [ ] SerializedType { ctx_type } , elem . TypeStack ... ) ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
if value . Data == nil {
if value . Data == nil {
elem_type , _ , _ , err := DeserializeValue ( ctx , value , 1 )
elem_type , _ , _ , err := DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
@ -509,14 +297,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
pointer_flags := value . Data [ 0 ]
pointer_flags := value . Data [ 0 ]
value . Data = value . Data [ 1 : ]
value . Data = value . Data [ 1 : ]
if pointer_flags == 0x00 {
if pointer_flags == 0x00 {
_ , elem_value , remaining_data , err := DeserializeValue ( ctx , value , 1 )
_ , elem_value , remaining_data , err := DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
pointer_value := elem_value [0 ] .Addr ( )
pointer_value := elem_value .Addr ( )
return pointer_value . Type ( ) , & pointer_value , remaining_data , nil
return pointer_value . Type ( ) , & pointer_value , remaining_data , nil
} else if pointer_flags == 0x01 {
} else if pointer_flags == 0x01 {
elem_type , _ , remaining_data , err := DeserializeValue ( ctx , value , 1 )
elem_type , _ , remaining_data , err := DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
@ -534,14 +322,12 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Struct , NewSerializedType ( "struct" ) ,
err = ctx . RegisterKind ( reflect . Struct , NewSerializedType ( "struct" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
// TODO: Switch from serializing each field as a []byte of a SerializedValue.MarshalBinary to just running serializeValue and adding the TypeStack and Data together
serialized_value := SerializedValue {
serialized_value := SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
nil ,
nil ,
}
}
num_fields := 0
field_values := map [ SerializedType ] SerializedValue { }
field_values := map [ int ] SerializedValue { }
for _ , field := range ( reflect . VisibleFields ( reflect_type ) ) {
for _ , field := range ( reflect . VisibleFields ( reflect_type ) ) {
gv_tag , tagged_gv := field . Tag . Lookup ( "gv" )
gv_tag , tagged_gv := field . Tag . Lookup ( "gv" )
if tagged_gv == false {
if tagged_gv == false {
@ -550,37 +336,22 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
continue
continue
} else {
} else {
// Add to the type stack and data stack
// Add to the type stack and data stack
field_index , err := strconv . Atoi ( gv_tag )
field_hash := Hash ( FieldNameBase , gv_tag )
if err != nil {
return SerializedValue { } , err
}
num_fields += 1
if value == nil {
if value == nil {
field_ser , err := serializeValue ( ctx , field . Type , nil )
field_ser , err := serializeValue ( ctx , field . Type , nil )
if err != nil {
if err != nil {
return SerializedValue { } , err
return SerializedValue { } , err
}
}
field_values [ field_ index ] = field_ser
field_values [ field_ hash ] = field_ser
} else {
} else {
field_value := value . FieldByIndex ( field . Index )
field_value := value . FieldByIndex ( field . Index )
field_ser , err := serializeValue ( ctx , field . Type , & field_value )
field_ser , err := serializeValue ( ctx , field . Type , & field_value )
if err != nil {
if err != nil {
return SerializedValue { } , err
return SerializedValue { } , err
}
}
field_values [ field_index ] = field_ser
field_values [ field_hash ] = field_ser
}
}
}
}
}
for i := 0 ; i < num_fields ; i += 1 {
field_value , exists := field_values [ i ]
if exists == false {
return SerializedValue { } , fmt . Errorf ( "%+v missing gv:%d" , reflect_type , i )
}
serialized_value . TypeStack = append ( serialized_value . TypeStack , field_value . TypeStack ... )
if value != nil {
serialized_value . Data = append ( serialized_value . Data , field_value . Data ... )
}
}
}
return serialized_value , nil
return serialized_value , nil
@ -592,14 +363,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Int , NewSerializedType ( "int" ) ,
err = ctx . RegisterKind ( reflect . Int , NewSerializedType ( "int" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 8 )
data = make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Int ( ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Int ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -618,7 +389,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Bool , NewSerializedType ( "bool" ) ,
err = ctx . RegisterKind ( reflect . Bool , NewSerializedType ( "bool" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
b := value . Bool ( )
b := value . Bool ( )
@ -629,7 +400,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -657,7 +428,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Float64 , NewSerializedType ( "float64" ) ,
err = ctx . RegisterKind ( reflect . Float64 , NewSerializedType ( "float64" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
// TODO: fix if underlying memory layout of float32 changes(or if it's architecture-dependent)
// TODO: fix if underlying memory layout of float32 changes(or if it's architecture-dependent)
@ -666,7 +437,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
binary . BigEndian . PutUint64 ( data , val )
binary . BigEndian . PutUint64 ( data , val )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -690,7 +461,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Float32 , NewSerializedType ( "float32" ) ,
err = ctx . RegisterKind ( reflect . Float32 , NewSerializedType ( "float32" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
// TODO: fix if underlying memory layout of float32 changes(or if it's architecture-dependent)
// TODO: fix if underlying memory layout of float32 changes(or if it's architecture-dependent)
@ -699,7 +470,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
binary . BigEndian . PutUint32 ( data , val )
binary . BigEndian . PutUint32 ( data , val )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -723,13 +494,13 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Uint32 , NewSerializedType ( "uint32" ) ,
err = ctx . RegisterKind ( reflect . Uint32 , NewSerializedType ( "uint32" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
data := make ( [ ] byte , 4 )
data := make ( [ ] byte , 4 )
if value != nil {
if value != nil {
binary . BigEndian . PutUint32 ( data , uint32 ( value . Uint ( ) ) )
binary . BigEndian . PutUint32 ( data , uint32 ( value . Uint ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -751,10 +522,10 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . String , NewSerializedType ( "string" ) ,
err = ctx . RegisterKind ( reflect . String , NewSerializedType ( "string" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
if value == nil {
if value == nil {
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
nil ,
nil ,
} , nil
} , nil
}
}
@ -763,7 +534,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
str := value . String ( )
str := value . String ( )
binary . BigEndian . PutUint64 ( data , uint64 ( len ( str ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( len ( str ) ) )
return SerializedValue {
return SerializedValue {
[ ] uint64 { uint64 ( ctx_type ) } ,
[ ] SerializedType { SerializedType ( ctx_type ) } ,
append ( data , [ ] byte ( str ) ... ) ,
append ( data , [ ] byte ( str ) ... ) ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -789,7 +560,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
err = ctx . RegisterKind ( reflect . Array , NewSerializedType ( "array" ) ,
err = ctx . RegisterKind ( reflect . Array , NewSerializedType ( "array" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte
var data [ ] byte
if value == nil {
if value == nil {
data = nil
data = nil
@ -799,7 +570,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
data := make ( [ ] byte , 8 )
data := make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Len ( ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Len ( ) ) )
var type_stack [ ] uint64 = nil
var type_stack [ ] SerializedType = nil
for i := 0 ; i < value . Len ( ) ; i += 1 {
for i := 0 ; i < value . Len ( ) ; i += 1 {
val := value . Index ( i )
val := value . Index ( i )
element , err := serializeValue ( ctx , reflect_type . Elem ( ) , & val )
element , err := serializeValue ( ctx , reflect_type . Elem ( ) , & val )
@ -807,7 +578,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return SerializedValue { } , err
return SerializedValue { } , err
}
}
if type_stack == nil {
if type_stack == nil {
type_stack = append ( [ ] uint64 { ctx_type } , element . TypeStack ... )
type_stack = append ( [ ] SerializedType { ctx_type } , element . TypeStack ... )
}
}
data = append ( data , element . Data ... )
data = append ( data , element . Data ... )
}
}
@ -819,7 +590,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
return SerializedValue {
return SerializedValue {
append ( [ ] uint64 { ctx_type } , elem . TypeStack ... ) ,
append ( [ ] SerializedType { ctx_type } , elem . TypeStack ... ) ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -830,9 +601,9 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Interface , NewSerializedType ( "interface" ) ,
err = ctx . RegisterKind ( reflect . Interface , NewSerializedType ( "interface" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte
var data [ ] byte
type_stack := [ ] uint64 { }
type_stack := [ ] SerializedType { }
if value == nil {
if value == nil {
data = nil
data = nil
} else if value . IsZero ( ) {
} else if value . IsZero ( ) {
@ -847,7 +618,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
type_stack = elem . TypeStack
type_stack = elem . TypeStack
}
}
return SerializedValue {
return SerializedValue {
append ( [ ] uint64 { ctx_type } , type_stack ... ) ,
append ( [ ] SerializedType { ctx_type } , type_stack ... ) ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -859,170 +630,23 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
err = ctx . RegisterKind ( reflect . Map , NewSerializedType ( "map" ) ,
err = ctx . RegisterKind ( reflect . Map , NewSerializedType ( "map" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte
return SerializedValue { } , fmt . Errorf ( "serialize map unimplemented" )
if value == nil {
data = nil
} else if value . IsZero ( ) {
data = [ ] byte { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF }
} else if value . Len ( ) == 0 {
data = [ ] byte { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 }
} else {
map_iter := value . MapRange ( )
key_data := [ ] byte { }
val_data := [ ] byte { }
var key_types [ ] uint64 = nil
var val_types [ ] uint64 = nil
map_len := 0
for map_iter . Next ( ) {
map_len += 1
key_value := map_iter . Key ( )
val_value := map_iter . Value ( )
key , err := serializeValue ( ctx , reflect_type . Key ( ) , & key_value )
if err != nil {
return SerializedValue { } , err
}
val , err := serializeValue ( ctx , reflect_type . Elem ( ) , & val_value )
if err != nil {
return SerializedValue { } , err
}
if key_types == nil {
key_types = key . TypeStack
val_types = val . TypeStack
}
key_data = append ( key_data , key . Data ... )
val_data = append ( val_data , val . Data ... )
}
type_stack := [ ] uint64 { ctx_type }
type_stack = append ( type_stack , key_types ... )
type_stack = append ( type_stack , val_types ... )
data := make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , uint64 ( map_len ) )
data = append ( data , key_data ... )
data = append ( data , val_data ... )
return SerializedValue {
type_stack ,
data ,
} , nil
}
key , err := serializeValue ( ctx , reflect_type . Key ( ) , nil )
if err != nil {
return SerializedValue { } , err
}
elem , err := serializeValue ( ctx , reflect_type . Elem ( ) , nil )
if err != nil {
return SerializedValue { } , err
}
type_stack := [ ] uint64 { ctx_type }
type_stack = append ( type_stack , key . TypeStack ... )
type_stack = append ( type_stack , elem . TypeStack ... )
return SerializedValue {
type_stack ,
data ,
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
if value . Data == nil {
return nil , nil , value , fmt . Errorf ( "deserialize map unimplemented" )
var key_type , elem_type reflect . Type
key_type , _ , value , err = DeserializeValue ( ctx , value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
elem_type , _ , value , err = DeserializeValue ( ctx , value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
return reflect . MapOf ( key_type , elem_type ) , nil , value , nil
} else if len ( value . Data ) < 8 {
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize map" )
} else {
map_len := binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] )
value . Data = value . Data [ 8 : ]
if map_len == 0xFFFFFFFFFFFFFFFF {
temp_value := SerializedValue {
value . TypeStack ,
nil ,
}
var key_type , elem_type reflect . Type
key_type , _ , temp_value , err = DeserializeValue ( ctx , temp_value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
elem_type , _ , temp_value , err = DeserializeValue ( ctx , temp_value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
map_type := reflect . MapOf ( key_type , elem_type )
map_value := reflect . New ( map_type ) . Elem ( )
return map_type , & map_value , SerializedValue {
temp_value . TypeStack ,
value . Data ,
} , nil
} else if map_len == 0x00 {
temp_value := SerializedValue {
value . TypeStack ,
nil ,
}
var key_type , elem_type reflect . Type
key_type , _ , temp_value , err = DeserializeValue ( ctx , temp_value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
elem_type , _ , temp_value , err = DeserializeValue ( ctx , temp_value , 1 )
if err != nil {
return nil , nil , SerializedValue { } , err
}
map_type := reflect . MapOf ( key_type , elem_type )
map_value := reflect . MakeMap ( map_type )
return map_type , & map_value , SerializedValue {
temp_value . TypeStack ,
value . Data ,
} , nil
} else {
var key_type , elem_type reflect . Type
var key_values , elem_values [ ] reflect . Value
key_type , key_values , value , err = DeserializeValue ( ctx , value , int ( map_len ) )
if err != nil {
return nil , nil , SerializedValue { } , err
}
elem_type , elem_values , value , err = DeserializeValue ( ctx , value , int ( map_len ) )
if err != nil {
return nil , nil , SerializedValue { } , err
}
map_type := reflect . MapOf ( key_type , elem_type )
map_value := reflect . MakeMap ( map_type )
for i := 0 ; i < int ( map_len ) ; i += 1 {
map_value . SetMapIndex ( key_values [ i ] , elem_values [ i ] )
}
return map_type , & map_value , value , nil
}
}
} )
} )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
err = ctx . RegisterKind ( reflect . Int8 , NewSerializedType ( "int8" ) ,
err = ctx . RegisterKind ( reflect . Int8 , NewSerializedType ( "int8" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = [ ] byte { byte ( value . Int ( ) ) }
data = [ ] byte { byte ( value . Int ( ) ) }
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1043,13 +667,13 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Uint8 , NewSerializedType ( "uint8" ) ,
err = ctx . RegisterKind ( reflect . Uint8 , NewSerializedType ( "uint8" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = [ ] byte { uint8 ( value . Uint ( ) ) }
data = [ ] byte { uint8 ( value . Uint ( ) ) }
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1070,14 +694,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Uint16 , NewSerializedType ( "uint16" ) ,
err = ctx . RegisterKind ( reflect . Uint16 , NewSerializedType ( "uint16" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 2 )
data = make ( [ ] byte , 2 )
binary . BigEndian . PutUint16 ( data , uint16 ( value . Uint ( ) ) )
binary . BigEndian . PutUint16 ( data , uint16 ( value . Uint ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1099,14 +723,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Int16 , NewSerializedType ( "int16" ) ,
err = ctx . RegisterKind ( reflect . Int16 , NewSerializedType ( "int16" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 2 )
data = make ( [ ] byte , 2 )
binary . BigEndian . PutUint16 ( data , uint16 ( value . Int ( ) ) )
binary . BigEndian . PutUint16 ( data , uint16 ( value . Int ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1128,14 +752,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Int32 , NewSerializedType ( "int32" ) ,
err = ctx . RegisterKind ( reflect . Int32 , NewSerializedType ( "int32" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 4 )
data = make ( [ ] byte , 4 )
binary . BigEndian . PutUint32 ( data , uint32 ( value . Int ( ) ) )
binary . BigEndian . PutUint32 ( data , uint32 ( value . Int ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1157,14 +781,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Uint , NewSerializedType ( "uint" ) ,
err = ctx . RegisterKind ( reflect . Uint , NewSerializedType ( "uint" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 8 )
data = make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , value . Uint ( ) )
binary . BigEndian . PutUint64 ( data , value . Uint ( ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1172,7 +796,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return reflect . TypeOf ( uint ( 0 ) ) , nil , value , nil
return reflect . TypeOf ( uint ( 0 ) ) , nil , value , nil
} else {
} else {
if len ( value . Data ) < 8 {
if len ( value . Data ) < 8 {
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize uint64 ")
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize SerializedType ")
}
}
val := uint ( binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] ) )
val := uint ( binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] ) )
value . Data = value . Data [ 8 : ]
value . Data = value . Data [ 8 : ]
@ -1185,23 +809,23 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return nil , err
return nil , err
}
}
err = ctx . RegisterKind ( reflect . Uint64 , NewSerializedType ( " uint64 ") ,
err = ctx . RegisterKind ( reflect . Uint64 , NewSerializedType ( " SerializedType ") ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 8 )
data = make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , value . Uint ( ) )
binary . BigEndian . PutUint64 ( data , value . Uint ( ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
if value . Data == nil {
if value . Data == nil {
return reflect . TypeOf ( uint64 ( 0 ) ) , nil , value , nil
return reflect . TypeOf ( SerializedType ( 0 ) ) , nil , value , nil
} else {
} else {
if len ( value . Data ) < 8 {
if len ( value . Data ) < 8 {
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize uint64 ")
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize SerializedType ")
}
}
val := binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] )
val := binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] )
value . Data = value . Data [ 8 : ]
value . Data = value . Data [ 8 : ]
@ -1215,14 +839,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterKind ( reflect . Int64 , NewSerializedType ( "int64" ) ,
err = ctx . RegisterKind ( reflect . Int64 , NewSerializedType ( "int64" ) ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
data = make ( [ ] byte , 8 )
data = make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Int ( ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Int ( ) ) )
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1230,7 +854,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return reflect . TypeOf ( int64 ( 0 ) ) , nil , value , nil
return reflect . TypeOf ( int64 ( 0 ) ) , nil , value , nil
} else {
} else {
if len ( value . Data ) < 8 {
if len ( value . Data ) < 8 {
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize uint64 ")
return nil , nil , SerializedValue { } , fmt . Errorf ( "Not enough data to deserialize SerializedType ")
}
}
val := int64 ( binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] ) )
val := int64 ( binary . BigEndian . Uint64 ( value . Data [ 0 : 8 ] ) )
value . Data = value . Data [ 8 : ]
value . Data = value . Data [ 8 : ]
@ -1243,10 +867,10 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return nil , err
return nil , err
}
}
err = ctx . RegisterKind ( reflect . Slice , NewSerializedType ( "slice" ) ,
err = ctx . RegisterKind ( reflect . Slice , SliceType ,
func ( ctx * Context , ctx_type uint64 , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , reflect_type reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte
var data [ ] byte
var type_stack [ ] uint64 = nil
type_stack := [ ] SerializedType { ctx_type }
if value == nil {
if value == nil {
data = nil
data = nil
} else if value . IsZero ( ) {
} else if value . IsZero ( ) {
@ -1256,19 +880,18 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
} else {
} else {
data := make ( [ ] byte , 8 )
data := make ( [ ] byte , 8 )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Len ( ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( value . Len ( ) ) )
var element SerializedValue
var err error
for i := 0 ; i < value . Len ( ) ; i += 1 {
for i := 0 ; i < value . Len ( ) ; i += 1 {
val := value . Index ( i )
val := value . Index ( i )
element , err : = serializeValue ( ctx , reflect_type . Elem ( ) , & val )
element , err = serializeValue ( ctx , reflect_type . Elem ( ) , & val )
if err != nil {
if err != nil {
return SerializedValue { } , err
return SerializedValue { } , err
}
}
if type_stack == nil {
type_stack = append ( [ ] uint64 { ctx_type } , element . TypeStack ... )
}
data = append ( data , element . Data ... )
data = append ( data , element . Data ... )
}
}
return SerializedValue {
return SerializedValue {
type_stack ,
append ( type_stack , element . TypeStack ... ) ,
data ,
data ,
} , nil
} , nil
}
}
@ -1277,12 +900,12 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return SerializedValue { } , err
return SerializedValue { } , err
}
}
return SerializedValue {
return SerializedValue {
append ( [ ] uint64 { ctx_type } , element . TypeStack ... ) ,
append ( type_stack , element . TypeStack ... ) ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
if value . Data == nil {
if value . Data == nil {
elem_type , _ , _ , err := DeserializeValue ( ctx , value , 1 )
elem_type , _ , _ , err := DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
@ -1296,7 +919,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
elem_type , _ , remaining , err := DeserializeValue ( ctx , SerializedValue {
elem_type , _ , remaining , err := DeserializeValue ( ctx , SerializedValue {
value . TypeStack ,
value . TypeStack ,
nil ,
nil ,
} , 1 )
} )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
@ -1307,7 +930,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
value . Data ,
value . Data ,
} , nil
} , nil
} else if slice_length == 0x00 {
} else if slice_length == 0x00 {
elem_type , _ , remaining , err := DeserializeValue ( ctx , value , 1 )
elem_type , _ , remaining , err := DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , SerializedValue { } , err
}
}
@ -1317,15 +940,28 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
value . Data ,
value . Data ,
} , nil
} , nil
} else {
} else {
elem_type , elements , remaining_data , err := DeserializeValue ( ctx , value , int ( slice_length ) )
var reflect_value * reflect . Value = nil
var reflect_type reflect . Type = nil
saved_type_stack := value . TypeStack
for i := 0 ; i < int ( slice_length ) ; i += 1 {
var element_type reflect . Type
var element_value * reflect . Value
element_type , element_value , value , err = DeserializeValue ( ctx , value )
if err != nil {
if err != nil {
return nil , nil , SerializedValue { } , err
return nil , nil , value , err
}
}
reflect_value := reflect . MakeSlice ( reflect . SliceOf ( elem_type ) , 0 , int ( slice_length ) )
if reflect_value == nil {
for i := 0 ; i < int ( slice_length ) ; i += 1 {
reflect_type = reflect . SliceOf ( element_type )
reflect_value = reflect . Append ( reflect_value , elements [ i ] )
real_value := reflect . MakeSlice ( reflect_type , int ( slice_length ) , int ( slice_length ) )
reflect_value = & real_value
}
if i != ( int ( slice_length ) - 1 ) {
value . TypeStack = saved_type_stack
}
slice_index_ptr := reflect_value . Index ( i )
slice_index_ptr . Set ( * element_value )
}
}
return reflect_value . Type ( ) , & reflect_value , remaining_data , nil
return reflect_ type, reflect_value , value , nil
}
}
}
}
} )
} )
@ -1334,10 +970,10 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterType ( reflect . TypeOf ( StringError ( "" ) ) , ErrorType ,
err = ctx . RegisterType ( reflect . TypeOf ( StringError ( "" ) ) , ErrorType ,
func ( ctx * Context , ctx_type uint64 , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
if value == nil {
if value == nil {
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
nil ,
nil ,
} , nil
} , nil
}
}
@ -1347,7 +983,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
str := string ( err )
str := string ( err )
binary . BigEndian . PutUint64 ( data , uint64 ( len ( str ) ) )
binary . BigEndian . PutUint64 ( data , uint64 ( len ( str ) ) )
return SerializedValue {
return SerializedValue {
[ ] uint64 { uint64 ( ctx_type ) } ,
[ ] SerializedType { SerializedType ( ctx_type ) } ,
append ( data , [ ] byte ( str ) ... ) ,
append ( data , [ ] byte ( str ) ... ) ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1355,7 +991,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
} )
} )
err = ctx . RegisterType ( reflect . TypeOf ( RandID ( ) ) , NewSerializedType ( "NodeID" ) ,
err = ctx . RegisterType ( reflect . TypeOf ( RandID ( ) ) , NewSerializedType ( "NodeID" ) ,
func ( ctx * Context , ctx_type uint64 , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var id_ser [ ] byte = nil
var id_ser [ ] byte = nil
if value != nil {
if value != nil {
var err error = nil
var err error = nil
@ -1365,7 +1001,7 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
id_ser ,
id_ser ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1376,14 +1012,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterType ( reflect . TypeOf ( Up ) , NewSerializedType ( "SignalDirection" ) ,
err = ctx . RegisterType ( reflect . TypeOf ( Up ) , NewSerializedType ( "SignalDirection" ) ,
func ( ctx * Context , ctx_type uint64 , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
val := value . Interface ( ) . ( SignalDirection )
val := value . Interface ( ) . ( SignalDirection )
data = [ ] byte { byte ( val ) }
data = [ ] byte { byte ( val ) }
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
@ -1394,14 +1030,14 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
}
}
err = ctx . RegisterType ( reflect . TypeOf ( ReqState ( 0 ) ) , NewSerializedType ( "ReqState" ) ,
err = ctx . RegisterType ( reflect . TypeOf ( ReqState ( 0 ) ) , NewSerializedType ( "ReqState" ) ,
func ( ctx * Context , ctx_type uint64 , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
func ( ctx * Context , ctx_type SerializedType , t reflect . Type , value * reflect . Value ) ( SerializedValue , error ) {
var data [ ] byte = nil
var data [ ] byte = nil
if value != nil {
if value != nil {
val := value . Interface ( ) . ( ReqState )
val := value . Interface ( ) . ( ReqState )
data = [ ] byte { byte ( val ) }
data = [ ] byte { byte ( val ) }
}
}
return SerializedValue {
return SerializedValue {
[ ] uint64 { ctx_type } ,
[ ] SerializedType { ctx_type } ,
data ,
data ,
} , nil
} , nil
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {
} , func ( ctx * Context , value SerializedValue ) ( reflect . Type , * reflect . Value , SerializedValue , error ) {