Last active
September 9, 2020 07:26
-
-
Save tonyalaribe/e82838a32a2d677a726c7f2ade228838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"ID": "user1", | |
"Children": [ | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
}, | |
{ | |
"ID": "user3", | |
"Children": [ | |
{ | |
"ID": "user4", | |
"Children": [ | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
}, | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
} | |
], | |
"Name": "User four" | |
}, | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
}, | |
{ | |
"ID": "user6", | |
"Children": [ | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
} | |
], | |
"Name": "User six" | |
} | |
], | |
"Name": "User Three" | |
}, | |
{ | |
"ID": null, | |
"Children": null, | |
"Name": null | |
} | |
], | |
"Name": "user One" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"github.com/cayleygraph/cayley" | |
"github.com/cayleygraph/cayley/graph" | |
_ "github.com/cayleygraph/cayley/graph/mongo" | |
"github.com/cayleygraph/cayley/quad" | |
"github.com/cayleygraph/cayley/schema" | |
) | |
type Item struct { | |
ID quad.Value `quad:"@id"` | |
Children []Item `quad:"isChildOf < *"` | |
Name quad.Value `quad:"name < *"` | |
} | |
func (c *Config) addToTree(name,user, parent string) error { | |
// Initialize the database | |
graph.IgnoreDuplicates = true | |
err := graph.InitQuadStore("mongo", c.MONGOSERVER, nil) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println(c.MONGOSERVER) | |
store, err := cayley.NewGraph("mongo", c.MONGOSERVER, nil) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
defer store.Close() | |
err = store.AddQuad(quad.Make(quad.IRI(user), quad.IRI("isChildOf"), quad.IRI(parent), nil)) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
err = store.AddQuad(quad.Make(quad.IRI(name), quad.IRI("name"), quad.IRI(user), nil)) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
return nil | |
} | |
func (c *Config) getTree(user string) { | |
err := graph.InitQuadStore("mongo", c.MONGOSERVER, nil) | |
if err != nil { | |
log.Println(err) | |
} | |
store, err := cayley.NewGraph("mongo", c.MONGOSERVER, nil) | |
if err != nil { | |
log.Println(err) | |
} | |
defer store.Close() | |
var items []Item | |
err = schema.LoadTo(nil, store, &items, quad.IRI(user)) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Printf("%+v", items) | |
//var jsonx []Item | |
err = json.NewEncoder(os.Stdout).Encode(items) | |
if err != nil { | |
log.Println(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func TestTree(t *testing.T) { | |
config := Config{ | |
MONGOSERVER: "", | |
} | |
err := config.addToTree("user One", "user1", "") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User Two", "user2", "user1") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User Three", "user3", "user1") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User Eleven", "user11", "user1") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User four", "user4", "user3") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User five", "user5", "user3") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User six", "user6", "user3") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User seven", "user7", "user4") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User eight", "user8", "user4") | |
if err != nil { | |
t.Error(err) | |
} | |
err = config.addToTree("User nine", "user9", "user6") | |
if err != nil { | |
t.Error(err) | |
} | |
config.getTree("user1") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to create quads and walk/transverse an entire tree or graph in cayley graph db