Skip to content
Snippets Groups Projects
Commit 2f499c15 authored by MichaelJCompton's avatar MichaelJCompton
Browse files

godoc updates

parent ce06ecec
No related branches found
No related tags found
No related merge requests found
...@@ -92,7 +92,7 @@ func (req *Req) addMutation(e Edge, op opType) { ...@@ -92,7 +92,7 @@ func (req *Req) addMutation(e Edge, op opType) {
} }
// Set adds edge e to the set mutation of request req, thus scheduling the edge to be added to the graph when the request is run. // Set adds edge e to the set mutation of request req, thus scheduling the edge to be added to the graph when the request is run.
// The edge must be syntatically valid: have a valid source (a Node), predicate and target (a Node or value), otherwise an error is returned. // The edge must be syntactically valid: have a valid source (a Node), predicate and target (a Node or value), otherwise an error is returned.
// The edge is not checked agaist the schema until the request is run --- so setting a UID edge to a value, for example, doesn't result in an // The edge is not checked agaist the schema until the request is run --- so setting a UID edge to a value, for example, doesn't result in an
// error until the request is run. // error until the request is run.
func (req *Req) Set(e Edge) { func (req *Req) Set(e Edge) {
...@@ -160,7 +160,7 @@ type nquadOp struct { ...@@ -160,7 +160,7 @@ type nquadOp struct {
op opType op opType
} }
// Node representes a single node in the graph. // Node represents a single node in the graph.
type Node struct { type Node struct {
uid uint64 uid uint64
// We can do variables in mutations. // We can do variables in mutations.
...@@ -191,7 +191,7 @@ func (n *Node) ConnectTo(pred string, n1 Node) Edge { ...@@ -191,7 +191,7 @@ func (n *Node) ConnectTo(pred string, n1 Node) Edge {
// Edge create an edge with source Node n and predicate pred, but without a target. // Edge create an edge with source Node n and predicate pred, but without a target.
// The edge needs to be completed by calling Edge.ConnectTo() if the edge is a // The edge needs to be completed by calling Edge.ConnectTo() if the edge is a
// UID edge, or one of the Edge.SetValue...() functions if the edge is of a scalar type. // UID edge, or one of the Edge.SetValue...() functions if the edge is of a scalar type.
// The edge can't be commited to the store --- calling Req.Set() to add the edge to // The edge can't be committed to the store --- calling Req.Set() to add the edge to
// a request will result in an error --- until it is completed. // a request will result in an error --- until it is completed.
func (n *Node) Edge(pred string) Edge { func (n *Node) Edge(pred string) Edge {
e := Edge{} e := Edge{}
......
...@@ -21,7 +21,7 @@ mutations can be run from the client. There are essentially two modes ...@@ -21,7 +21,7 @@ mutations can be run from the client. There are essentially two modes
of client interaction: of client interaction:
- Request based interaction mode where the user program builds requests - Request based interaction mode where the user program builds requests
and recieves responses immediately after running, and and receives responses immediately after running, and
- Batch mode where clients submit many requests and let the client package - Batch mode where clients submit many requests and let the client package
batch those requests to the server. batch those requests to the server.
......
...@@ -87,7 +87,7 @@ func ExampleReq_Set() { ...@@ -87,7 +87,7 @@ func ExampleReq_Set() {
fmt.Printf("%+v\n", proto.MarshalTextString(resp)) fmt.Printf("%+v\n", proto.MarshalTextString(resp))
} }
func ExampleReq_BatchSet() { func ExampleDgraph_BatchSet() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC") x.Checkf(err, "While trying to dial gRPC")
defer conn.Close() defer conn.Close()
...@@ -120,7 +120,7 @@ func ExampleReq_BatchSet() { ...@@ -120,7 +120,7 @@ func ExampleReq_BatchSet() {
dgraphClient.BatchFlush() // Must be called to flush buffers after all mutations are added. dgraphClient.BatchFlush() // Must be called to flush buffers after all mutations are added.
} }
func ExampleReq_AddFacet() { func ExampleEdge_AddFacet() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC") x.Checkf(err, "While trying to dial gRPC")
defer conn.Close() defer conn.Close()
...@@ -159,8 +159,12 @@ func ExampleReq_AddFacet() { ...@@ -159,8 +159,12 @@ func ExampleReq_AddFacet() {
e.AddFacet("close", "true") e.AddFacet("close", "true")
req.Set(e) req.Set(e)
req.AddSchemaFromString(`
name: string @index(exact) .
`)
req.SetQuery(`{ req.SetQuery(`{
me(id: person1) { me(func: eq(name,"Steven Spielberg")) {
name @facets name @facets
friend @facets { friend @facets {
name name
...@@ -194,10 +198,13 @@ func ExampleReq_AddSchemaFromString() { ...@@ -194,10 +198,13 @@ func ExampleReq_AddSchemaFromString() {
req := client.Req{} req := client.Req{}
// Add a schema mutation to the request // Add a schema mutation to the request
req.AddSchemaFromString(` err = req.AddSchemaFromString(`
name: string @index . name: string @index(term) .
release_date: date @index . release_date: dateTime @index .
`) `)
if err != nil {
log.Fatalf("Error setting schema, %s", err)
}
// Query the changed schema // Query the changed schema
req.SetQuery(`schema {}`) req.SetQuery(`schema {}`)
...@@ -236,6 +243,9 @@ func ExampleReq_SetQuery() { ...@@ -236,6 +243,9 @@ func ExampleReq_SetQuery() {
req.Set(e) req.Set(e)
req.AddSchemaFromString(`name: string @index(exact) .`) req.AddSchemaFromString(`name: string @index(exact) .`)
if err != nil {
log.Fatalf("Error setting schema, %s", err)
}
req.SetQuery(`{ req.SetQuery(`{
me(func: eq(name, "Alice")) { me(func: eq(name, "Alice")) {
...@@ -244,9 +254,13 @@ func ExampleReq_SetQuery() { ...@@ -244,9 +254,13 @@ func ExampleReq_SetQuery() {
} }
}`) }`)
resp, err := dgraphClient.Run(context.Background(), &req) resp, err := dgraphClient.Run(context.Background(), &req)
if err != nil {
log.Fatalf("Error in getting response from server, %s", err)
}
fmt.Printf("%+v\n", proto.MarshalTextString(resp)) fmt.Printf("%+v\n", proto.MarshalTextString(resp))
} }
func ExampleReq_SetQueryWithVariables() { func ExampleReq_SetQueryWithVariables() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC") x.Checkf(err, "While trying to dial gRPC")
...@@ -293,7 +307,7 @@ func ExampleReq_SetQueryWithVariables() { ...@@ -293,7 +307,7 @@ func ExampleReq_SetQueryWithVariables() {
func ExampleReq_NodeUidVar() { func ExampleDgraph_NodeUidVar() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC") x.Checkf(err, "While trying to dial gRPC")
defer conn.Close() defer conn.Close()
...@@ -336,13 +350,15 @@ func ExampleReq_NodeUidVar() { ...@@ -336,13 +350,15 @@ func ExampleReq_NodeUidVar() {
// Get a node for the variable a in the query above. // Get a node for the variable a in the query above.
n, _ := dgraphClient.NodeUidVar("a") n, _ := dgraphClient.NodeUidVar("a")
e := n.Edge("falls.in") e = n.Edge("falls.in")
e.SetValueString("Rabbit hole") e.SetValueString("Rabbit hole")
req.Set(e) req.Set(e)
resp, err := dgraphClient.Run(context.Background(), &req) resp, err = dgraphClient.Run(context.Background(), &req)
x.Check(err) if err != nil {
fmt.Printf("Resp: %+v\n", resp) log.Fatalf("Error in getting response from server, %s", err)
}
fmt.Printf("%+v\n", proto.MarshalTextString(resp))
// This is equivalent to the single query and mutation // This is equivalent to the single query and mutation
...@@ -360,3 +376,116 @@ func ExampleReq_NodeUidVar() { ...@@ -360,3 +376,116 @@ func ExampleReq_NodeUidVar() {
// It's often easier to construct such things with client functions that // It's often easier to construct such things with client functions that
// by manipulating raw strings. // by manipulating raw strings.
} }
func ExampleEdge_SetValueBytes() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC")
defer conn.Close()
clientDir, err := ioutil.TempDir("", "client_")
x.Check(err)
dgraphClient := client.NewDgraphClient([]*grpc.ClientConn{conn}, client.DefaultOptions, clientDir)
req := client.Req{}
alice, err := dgraphClient.NodeBlank("alice")
if err != nil {
log.Fatal(err)
}
e := alice.Edge("name")
e.SetValueString("Alice")
req.Set(e)
e = alice.Edge("somestoredbytes")
err = e.SetValueBytes([]byte(`\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98`))
x.Check(err)
req.Set(e)
req.AddSchemaFromString(`name: string @index(exact) .`)
req.SetQuery(`{
q(func: eq(name, "Alice")) {
name
somestoredbytes
}
}`)
resp, err := dgraphClient.Run(context.Background(), &req)
if err != nil {
log.Fatalf("Error in getting response from server, %s", err)
}
fmt.Printf("%+v\n", proto.MarshalTextString(resp))
}
func ExampleUnmarshal() {
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
x.Checkf(err, "While trying to dial gRPC")
defer conn.Close()
clientDir, err := ioutil.TempDir("", "client_")
x.Check(err)
dgraphClient := client.NewDgraphClient([]*grpc.ClientConn{conn}, client.DefaultOptions, clientDir)
req := client.Req{}
// A mutation as a string, see ExampleReq_NodeUidVar, ExampleReq_SetQuery,
// etc for examples of mutations using client functions.
req.SetQuery(`
mutation {
schema {
name: string @index .
}
set {
_:person1 <name> "Alex" .
_:person2 <name> "Beatie" .
_:person3 <name> "Chris" .
_:person1 <friend> _:person2 .
_:person1 <friend> _:person3 .
}
}
{
friends(func: eq(name, "Alex")) {
name
friend {
name
}
}
}`)
// Run the request in the Dgraph server. The mutations are added, then
// the query is exectuted.
resp, err := dgraphClient.Run(context.Background(), &req)
if err != nil {
log.Fatalf("Error in getting response from server, %s", err)
}
// Unmarshal the response into a custom struct
// A type representing information in the graph.
type person struct {
Name string `dgraph:"name"`
Friends []person `dgraph:"friend"`
}
// A helper type matching the query root.
type friends struct {
Root person `dgraph:"friends"`
}
var f friends
err = client.Unmarshal(resp.N, &f)
if err != nil {
log.Fatal("Couldn't unmarshal response : ", err)
}
fmt.Println("Name : ", f.Root.Name)
fmt.Print("Friends : ")
for _, p := range f.Root.Friends {
fmt.Print(p.Name, " ")
}
fmt.Println()
}
...@@ -49,7 +49,7 @@ var ( ...@@ -49,7 +49,7 @@ var (
) )
// BatchMutationOptions sets the clients batch mode to Pending number of buffers // BatchMutationOptions sets the clients batch mode to Pending number of buffers
// each of Size. Running counters of number of rdfs procesessed, total time and // each of Size. Running counters of number of rdfs processed, total time and
// mutations per second are printed if PrintCounters is set true. // mutations per second are printed if PrintCounters is set true.
// See Counter. // See Counter.
type BatchMutationOptions struct { type BatchMutationOptions struct {
...@@ -383,7 +383,7 @@ func (d *Dgraph) BatchSet(e Edge) error { ...@@ -383,7 +383,7 @@ func (d *Dgraph) BatchSet(e Edge) error {
} }
// BatchDelete adds Edge e as a delete to the current batch mutation. Once added, // BatchDelete adds Edge e as a delete to the current batch mutation. Once added,
// the client will apply the mutation to the dgraph server when it is ready // the client will apply the mutation to the Dgraph server when it is ready
// to flush its buffers. The edge will be added to one of the batches as // to flush its buffers. The edge will be added to one of the batches as
// specified in d's BatchMutationOptions. If that batch fills, it eventually // specified in d's BatchMutationOptions. If that batch fills, it eventually
// flushes. But there is no guarantee of delivery before BatchFlush() is // flushes. But there is no guarantee of delivery before BatchFlush() is
...@@ -397,7 +397,7 @@ func (d *Dgraph) BatchDelete(e Edge) error { ...@@ -397,7 +397,7 @@ func (d *Dgraph) BatchDelete(e Edge) error {
return nil return nil
} }
// AddSchema adds the given schema mutatation to the batch of schema mutations. // AddSchema adds the given schema mutation to the batch of schema mutations.
// If the schema mutation applies an index to a UID edge, or if it adds // If the schema mutation applies an index to a UID edge, or if it adds
// reverse to a scalar edge, then the mutation is not added to the batch and an // reverse to a scalar edge, then the mutation is not added to the batch and an
// error is returned. Once added, the client will apply the schema mutation when // error is returned. Once added, the client will apply the schema mutation when
...@@ -415,7 +415,7 @@ func (d *Dgraph) AddSchema(s protos.SchemaUpdate) error { ...@@ -415,7 +415,7 @@ func (d *Dgraph) AddSchema(s protos.SchemaUpdate) error {
// edgename: uid @reverse . // edgename: uid @reverse .
// edge2: string @index(exact) . // edge2: string @index(exact) .
// etc. // etc.
// to use the form "mutuation { schema { ... }}" issue the mutation through // to use the form "mutation { schema { ... }}" issue the mutation through
// SetQuery rather than as a batch. // SetQuery rather than as a batch.
func (d *Dgraph) AddSchemaFromString(s string) error { func (d *Dgraph) AddSchemaFromString(s string) error {
schemaUpdate, err := schema.Parse(s) schemaUpdate, err := schema.Parse(s)
...@@ -488,7 +488,7 @@ func (d *Dgraph) NodeUid(uid uint64) Node { ...@@ -488,7 +488,7 @@ func (d *Dgraph) NodeUid(uid uint64) Node {
} }
// NodeBlank creates or returns a Node given a string name for the blank node. // NodeBlank creates or returns a Node given a string name for the blank node.
// Blank nodes do not exist as labelled nodes in dgraph. Blank nodes are used // Blank nodes do not exist as labelled nodes in Dgraph. Blank nodes are used
// as labels client side for loading and linking nodes correctly. If the // as labels client side for loading and linking nodes correctly. If the
// label is new in this session a new UID is allocated and assigned to the // label is new in this session a new UID is allocated and assigned to the
// label. If the label has already been assigned, the corresponding Node // label. If the label has already been assigned, the corresponding Node
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment