Skip to content

Commit

Permalink
#18 WIP on binary graph serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Mar 29, 2019
1 parent 4b9da06 commit 44aedfc
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 420 deletions.
3 changes: 0 additions & 3 deletions cmd/xrncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ func main() {

// Read in the configuration file for the sdk
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(app.Bech32PrefixAccAddr, app.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(app.Bech32PrefixValAddr, app.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(app.Bech32PrefixConsAddr, app.Bech32PrefixConsPub)
config.Seal()

mc := []sdk.ModuleClients{
Expand Down
52 changes: 52 additions & 0 deletions graph/binary/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package binary

import (
"github.com/regen-network/regen-ledger/graph"
"github.com/regen-network/regen-ledger/graph/impl"
"github.com/regen-network/regen-ledger/x/schema"
)
import sdk "github.com/cosmos/cosmos-sdk/types"

const (
prefixGeoAddress byte = iota
prefixAccAddress
prefixHashID

prefixIDMin = prefixGeoAddress
prefixIDMax = prefixHashID
)

const (
prefixPropertyID byte = 0
)

type SchemaResolver interface {
GetPropertyByURL(url string) graph.Property
GetPropertyByID(id schema.PropertyID) graph.Property
GetPropertyID(p graph.Property) schema.PropertyID
}

type onChainSchemaResolver struct {
keeper schema.Keeper
ctx sdk.Context
}

func (res onChainSchemaResolver) GetPropertyID(p graph.Property) schema.PropertyID {
return res.keeper.GetPropertyID(res.ctx, p.URI().String())
}

func (res onChainSchemaResolver) GetPropertyByURL(url string) graph.Property {
id := res.keeper.GetPropertyID(res.ctx, url)
if id == 0 {
return nil
}
return res.GetPropertyByID(id)
}

func (res onChainSchemaResolver) GetPropertyByID(id schema.PropertyID) graph.Property {
prop, found := res.keeper.GetPropertyDefinition(res.ctx, id)
if !found {
return nil
}
return impl.NewProperty(prop, id, prop.URI())
}
41 changes: 18 additions & 23 deletions xrb/deserialize.go → graph/binary/deserialize.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
package xrb
package binary

import (
"encoding/binary"
"fmt"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/graph"
"github.com/regen-network/regen-ledger/graph/impl"
"github.com/regen-network/regen-ledger/x/schema"
"io"
)

func DeserializeGraph(resolver SchemaResolver, r io.ByteScanner) (g Graph, hash []byte, err error) {
ctx := &dszContext{resolver, r, newHasher(), 0}
func DeserializeGraph(resolver SchemaResolver, r io.ByteScanner) (g graph.Graph, err error) {
ctx := &dszContext{resolver, r, 0}
g, err = ctx.readGraph()
if err != nil {
return nil, nil, err
return nil, err
}
hash = ctx.h.hash()
return g, hash, nil
return g, nil
}

type dszContext struct {
resolver SchemaResolver
r io.ByteScanner
h *hasher
version uint64
}

func (ctx *dszContext) readGraph() (g *graph, err error) {
func (ctx *dszContext) readGraph() (g graph.Graph, err error) {
ctx.version = ctx.mustReadUvarint()
haveRootNode := ctx.mustReadByte()
g = &graph{nodeNames: []types.HasURI{}, nodes: make(map[string]*node)}
g = impl.NewGraph()
if haveRootNode == 1 {
r, err := ctx.readNodeProperties()
if err != nil {
return nil, err
}
g.rootNode = r
g.WithNode(r)
}
nNodes := ctx.mustReadVarint()
for i := int64(0); i < nNodes; i++ {
n, err := ctx.readNode()
// TODO verify ordering
// hash
if err != nil {
return nil, err
}
g.nodeNames = append(g.nodeNames, n.id)
g.nodes[n.id.String()] = n
g.WithNode(n)
}
return g, nil
}

func (ctx *dszContext) readNode() (n *node, err error) {
func (ctx *dszContext) readNode() (n graph.Node, err error) {
panic("TODO")
//id, err := ctx.readString()
//if err != nil {
Expand All @@ -64,23 +60,22 @@ func (ctx *dszContext) readNode() (n *node, err error) {
//return n, nil
}

func (ctx *dszContext) readNodeProperties() (n *node, err error) {
n = &node{properties: make(map[schema.PropertyID]interface{}), propertyNames: []Property{}}
func (ctx *dszContext) readNodeProperties() (n graph.Node, err error) {
n = impl.NewNode(nil)
nProps := ctx.mustReadVarint()
for i := int64(0); i < nProps; i++ {
url, value, err := ctx.readProperty()
prop, value, err := ctx.readProperty()
// TODO verify ordering
// hash
if err != nil {
return nil, err
}
n.propertyNames = append(n.propertyNames, url)
n.properties[url.ID()] = value
n.SetProperty(prop, value)
}
return n, nil
}

func (ctx *dszContext) readProperty() (prop Property, value interface{}, err error) {
func (ctx *dszContext) readProperty() (prop graph.Property, value interface{}, err error) {
prefix := ctx.mustReadByte()
if prefix != 0 {
return nil, nil, fmt.Errorf("unexpected property ID prefix %d", prefix)
Expand All @@ -100,7 +95,7 @@ func (ctx *dszContext) readProperty() (prop Property, value interface{}, err err
return prop, val, nil
}

func (ctx *dszContext) readValue(prop Property) (x interface{}, err error) {
func (ctx *dszContext) readValue(prop graph.Property) (x interface{}, err error) {
panic("TODO")
}

Expand Down
2 changes: 1 addition & 1 deletion xrb/doc.go → graph/binary/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Package xrb defines an efficient RDF binary serialization format
XRB (XRN RDF Binary) that integrates with the Regen Ledger schema module
*/
package xrb
package binary
40 changes: 40 additions & 0 deletions graph/binary/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package binary

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"net/url"
)

type AccAddressID struct {
sdk.AccAddress
}

func (id AccAddressID) URI() *url.URL {
uri, err := url.Parse(id.String())
if err != nil {
panic(err)
}
return uri
}

type HashID struct {
fragment string
}

func (id HashID) String() string {
return fmt.Sprintf("_:_.%s", id.fragment)
}

func (id HashID) URI() *url.URL {
// TODO this should really be a blank node or the actual hash of the graph plus the fragment
uri, err := url.Parse(fmt.Sprintf("root:#%s", id.fragment))
if err != nil {
panic(err)
}
return uri
}

//func (n node) GetClasses() []string {
// panic("implement me")
//}
5 changes: 3 additions & 2 deletions xrb/hash.go → graph/binary/hash.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package xrb
package binary

import (
"bufio"
"bytes"
"github.com/regen-network/regen-ledger/graph"
"strconv"
)

Expand All @@ -16,7 +17,7 @@ func newHasher() *hasher {
return &hasher{bufio.NewWriter(b), b}
}

func (ha *hasher) hashGraph(g Graph) {
func (ha *hasher) hashGraph(g graph.Graph) {
panic("TODO")
}

Expand Down
Loading

0 comments on commit 44aedfc

Please sign in to comment.