Skip to content

Commit

Permalink
#18 WIP on efficient binary RDF format
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Mar 27, 2019
1 parent f4d5288 commit 027cbfe
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/schema/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func NewHandler(keeper Keeper) sdk.Handler {
}

func handlePropertyDefinition(ctx sdk.Context, keeper Keeper, def PropertyDefinition) sdk.Result {
_, url, err := keeper.DefineProperty(ctx, def)
id, url, err := keeper.DefineProperty(ctx, def)
if err != nil {
return err.Result()
}
res := sdk.Result{}
res.Tags = res.Tags.AppendTag("property.url", url)
res.Tags = res.Tags.AppendTag("property.url", url).AppendTag("property.id", fmt.Sprintf("%d", id))
return res
}
1 change: 1 addition & 0 deletions x/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (s *TestSuite) TestDefinePropertyHandler() {
res := s.handler(s.ctx, prop1)
s.Require().Equal(sdk.CodeOK, res.Code)
s.Require().Equal(prop1.URL(), string(res.Tags[0].Value))
s.Require().Equal("1", string(res.Tags[1].Value))
}

func TestTestSuite(t *testing.T) {
Expand Down
166 changes: 166 additions & 0 deletions xrb/xrb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
Package xrb defines an efficient RDF binary serialization format
XRN (XRN RDF Binary) that integrates with the Regen Ledger schema module
*/
package xrb

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

type Node interface {
ID() string
GetProperty(url string) interface{}
}

type serializer struct {
properties map[string]propInfo
}

type propInfo struct {
id schema.PropertyID
def schema.PropertyDefinition
}

type graph struct {
nodes []node
}

type node struct {
id string
properties map[string]interface{}
}

func (s serializer) SerializeGraph(w io.Writer, g graph) {
// Write the format version
mustWriteVarUint64(w, 0)
// TODO add chain height and maybe highest known property id
for _, n := range g.nodes {
s.SerializeNode(w, n)
}
}

func (s serializer) SerializeNode(w io.Writer, n node) {
writeString(w, n.id)
for name, val := range n.properties {
s.writeProperty(w, name, val)
}
}

func mustWriteVarUint64(w io.Writer, x uint64) {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, x)
mustWrite(w, buf[:n])
}

func mustWriteVarInt(w io.Writer, x int) {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, int64(x))
mustWrite(w, buf[:n])
}

func mustWrite(w io.Writer, buf []byte) {
_, err := w.Write(buf)
if err != nil {
panic(err)
}
}

func writeString(w io.Writer, str string) {
panic("TODO")
}

func writeBool(w io.Writer, x bool) {
panic("TODO")
}

func writeFloat64(w io.Writer, x float64) {
panic("TODO")
}

func (s serializer) writeProperty(w io.Writer, name string, value interface{}) {
info, found := s.properties[name]
if !found {
panic("TODO")
}

mustWriteVarUint64(w, uint64(info.id))

s.writePropertyValue(w, info.def.Many, info.def.PropertyType, value)
}

func (s serializer) writePropertyValue(w io.Writer, many bool, ty schema.PropertyType, value interface{}) {
if !many {
s.writePropertyOne(w, ty, value)
} else {
s.writePropertyMany(w, ty, value)
}
}

func (s serializer) writePropertyOne(w io.Writer, ty schema.PropertyType, value interface{}) {
switch ty {
case schema.TyString:
x, ok := value.(string)
if !ok {
panic(fmt.Sprintf("Expected string value, got %+v", value))
}
writeString(w, x)
case schema.TyDouble:
x, ok := value.(float64)
if !ok {
panic(fmt.Sprintf("Expected float64 value, got %+v", value))
}
writeFloat64(w, x)
case schema.TyBool:
x, ok := value.(bool)
if !ok {
panic(fmt.Sprintf("Expected bool value, got %+v", value))
}
writeBool(w, x)
case schema.TyInteger:
panic("Can't handle integer values yet")
case schema.TyObject:
panic("Can't handle object values yet")
default:
}
}

func (s serializer) writePropertyMany(w io.Writer, ty schema.PropertyType, value interface{}) {
switch ty {
case schema.TyString:
arr, ok := value.([]string)
if !ok {
panic(fmt.Sprintf("Expected []string value, got %+v", value))
}
mustWriteVarInt(w, len(arr))
for _, x := range arr {
writeString(w, x)
}
case schema.TyDouble:
arr, ok := value.([]float64)
if !ok {
panic(fmt.Sprintf("Expected []float64 value, got %+v", value))
}
mustWriteVarInt(w, len(arr))
for _, x := range arr {
writeFloat64(w, x)
}
case schema.TyBool:
arr, ok := value.([]bool)
if !ok {
panic(fmt.Sprintf("Expected []bool value, got %+v", value))
}
mustWriteVarInt(w, len(arr))
for _, x := range arr {
writeBool(w, x)
}
case schema.TyInteger:
panic("Can't handle integer values yet")
case schema.TyObject:
panic("Can't handle object values yet")
default:
}
}
1 change: 1 addition & 0 deletions xrb/xrb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package xrb

0 comments on commit 027cbfe

Please sign in to comment.