Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dgraph
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
dgraph
Commits
b1856e4d
Commit
b1856e4d
authored
7 years ago
by
MichaelJCompton
Browse files
Options
Downloads
Patches
Plain Diff
doc changes and new example
parent
fd767bf6
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
client/client.go
+49
-39
49 additions, 39 deletions
client/client.go
client/examples_test.go
+165
-13
165 additions, 13 deletions
client/examples_test.go
client/mutations.go
+53
-63
53 additions, 63 deletions
client/mutations.go
with
267 additions
and
115 deletions
client/client.go
+
49
−
39
View file @
b1856e4d
...
...
@@ -38,9 +38,9 @@ const (
DEL
)
// A Req represents a single request to the backend Dgraph instance. Each request may contain
multiple set, delete and
// schema mutations, and a single GraphQL+- query. I
F
the query contains
GraphQL variables, then the map giving values to these
//
must be stored in the request with the q
uery.
// A Req represents a single request to the backend Dgraph instance. Each request may contain
//
multiple set, delete and
schema mutations, and a single GraphQL+- query. I
f
the query contains
//
GraphQL variables, then it must be set with SetQueryWithVariables rather than SetQ
uery.
type
Req
struct
{
gr
protos
.
Request
}
...
...
@@ -91,10 +91,11 @@ 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.
// 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
// error until 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 have a valid 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 error until
// the request is run.
func
(
req
*
Req
)
Set
(
e
Edge
)
error
{
if
err
:=
e
.
validate
();
err
!=
nil
{
return
err
...
...
@@ -103,8 +104,9 @@ func (req *Req) Set(e Edge) error {
return
nil
}
// Delete adds edge e to the delete mutation of request req, thus scheduling the edge to be removed from the graph when the request is run.
// The edge must have a valid source (a Node), predicate and target (a Node or value), otherwise an error is returned. The edge need not represent
// Delete adds edge e to the delete mutation of request req, thus scheduling the edge to be removed
// from the graph when the request is run. The edge must have a valid target (a Node or value),
// otherwise an error is returned. The edge need not represent
// an edge in the graph --- applying such a mutation simply has no effect.
func
(
req
*
Req
)
Delete
(
e
Edge
)
error
{
if
err
:=
e
.
validate
();
err
!=
nil
{
...
...
@@ -212,7 +214,7 @@ func (n *Node) Edge(pred string) Edge {
return
e
}
// An Edge represents an edge between a source node and a target (either a node or a value
.
)
// An Edge represents an edge between a source node and a target (either a node or a value)
.
// Facets are stored in the edge. See Node.Edge(), Node.ConnectTo(), Edge.ConnecTo(),
// Edge.AddFacet and the Edge.SetValue...() functions to
// make a valid edge for a set or delete mutation.
...
...
@@ -220,7 +222,7 @@ type Edge struct {
nq
protos
.
NQuad
}
// NewEdge creates an
d
Edge from an NQuad.
// NewEdge creates an Edge from an NQuad.
func
NewEdge
(
nq
protos
.
NQuad
)
Edge
{
return
Edge
{
nq
}
}
...
...
@@ -257,10 +259,11 @@ func validateStr(val string) error {
return
nil
}
// SetValueString sets the value of Edge e as string val and sets the type of the edge to types.StringID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// The string must escape " with \, otherwise the edge and type are left unchanged and an error returned.
// SetValueString sets the value of Edge e as string val and sets the type of the edge to
// types.StringID. If the edge had previous been assigned another value (even of another type),
// the value and type are overwritten. If the edge has previously been connected to a node, the
// edge and type are left unchanged and ErrConnected is returned. The string must
// escape " with \, otherwise the edge and type are left unchanged and an error returned.
func
(
e
*
Edge
)
SetValueString
(
val
string
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -279,8 +282,9 @@ func (e *Edge) SetValueString(val string) error {
}
// SetValueInt sets the value of Edge e as int64 val and sets the type of the edge to types.IntID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// If the edge had previous been assigned another value (even of another type), the value and type
// are overwritten. If the edge has previously been connected to a node, the edge and type are
// left unchanged and ErrConnected is returned.
func
(
e
*
Edge
)
SetValueInt
(
val
int64
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -294,9 +298,10 @@ func (e *Edge) SetValueInt(val int64) error {
return
nil
}
// SetValueFloat sets the value of Edge e as float64 val and sets the type of the edge to types.FloatID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// SetValueFloat sets the value of Edge e as float64 val and sets the type of the edge to
// types.FloatID. If the edge had previous been assigned another value (even of another type),
// the value and type are overwritten. If the edge has previously been connected to a node, the
// edge and type are left unchanged and ErrConnected is returned.
func
(
e
*
Edge
)
SetValueFloat
(
val
float64
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -311,8 +316,9 @@ func (e *Edge) SetValueFloat(val float64) error {
}
// SetValueBool sets the value of Edge e as bool val and sets the type of the edge to types.BoolID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// If the edge had previous been assigned another value (even of another type), the value and type
// are overwritten. If the edge has previously been connected to a node, the edge and type are
// left unchanged and ErrConnected is returned.
func
(
e
*
Edge
)
SetValueBool
(
val
bool
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -326,9 +332,10 @@ func (e *Edge) SetValueBool(val bool) error {
return
nil
}
// SetValuePassword sets the value of Edge e as password string val and sets the type of the edge to types.PasswordID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// SetValuePassword sets the value of Edge e as password string val and sets the type of the edge
// to types.PasswordID. If the edge had previous been assigned another value (even of another
// type), the value and type are overwritten. If the edge has previously been connected to a
// node, the edge and type are left unchanged and ErrConnected is returned.
func
(
e
*
Edge
)
SetValuePassword
(
val
string
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -342,9 +349,10 @@ func (e *Edge) SetValuePassword(val string) error {
return
nil
}
// SetValueDatetime sets the value of Edge e as time.Time dateTime and sets the type of the edge to types.DateTimeID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// SetValueDatetime sets the value of Edge e as time.Time dateTime and sets the type of the edge
// to types.DateTimeID. If the edge had previous been assigned another value (even of another
// type), the value and type are overwritten. If the edge has previously been connected to a node,
// the edge and type are left unchanged and ErrConnected is returned.
func
(
e
*
Edge
)
SetValueDatetime
(
dateTime
time
.
Time
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -358,10 +366,11 @@ func (e *Edge) SetValueDatetime(dateTime time.Time) error {
return
nil
}
// SetValueGeoJson sets the value of Edge e as the GeoJSON object parsed from json string and sets the type of the edge to types.GeoID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// If the string fails to parse with geojson.Unmarshal() the edge is left unchanged and an error returned.
// SetValueGeoJson sets the value of Edge e as the GeoJSON object parsed from json string and sets
// the type of the edge to types.GeoID. If the edge had previous been assigned another value (even
// of another type), the value and type are overwritten. If the edge has previously been connected
// to a node, the edge and type are left unchanged and ErrConnected is returned. If the string
// fails to parse with geojson.Unmarshal() the edge is left unchanged and an error returned.
func
(
e
*
Edge
)
SetValueGeoJson
(
json
string
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
@@ -383,9 +392,10 @@ func (e *Edge) SetValueGeoJson(json string) error {
return
nil
}
// SetValueDefault sets the value of Edge e as string val and sets the type of the edge to types.DefaultID.
// If the edge had previous been assigned another value (even of another type), the value and type are overwritten.
// If the edge has previously been connected to a node, the edge and type are left unchanged and ErrConnected is returned.
// SetValueDefault sets the value of Edge e as string val and sets the type of the edge to
// types.DefaultID. If the edge had previous been assigned another value (even of another
// type), the value and type are overwritten. If the edge has previously been connected to
// a node, the edge and type are left unchanged and ErrConnected is returned.
// The string must escape " with \, otherwise the edge and type are left unchanged and an error returned.
func
(
e
*
Edge
)
SetValueDefault
(
val
string
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
...
...
@@ -404,10 +414,10 @@ func (e *Edge) SetValueDefault(val string) error {
return
nil
}
// SetValueBytes allows setting the value of an edge to raw bytes and sets the type of the edge
to types.BinaryID.
// If the edge had previous been assigned another value (even of another type),
the value and type are overwritten.
// If the edge has previously been connected to a node, the
edge and type are left unchanged and ErrConnected is returned.
// the bytes are encoded as base64.
// SetValueBytes allows setting the value of an edge to raw bytes and sets the type of the edge
//
to types.BinaryID.
If the edge had previous been assigned another value (even of another type),
//
the value and type are overwritten.
If the edge has previously been connected to a node, the
//
edge and type are left unchanged and ErrConnected is returned.
the bytes are encoded as base64.
func
(
e
*
Edge
)
SetValueBytes
(
val
[]
byte
)
error
{
if
len
(
e
.
nq
.
ObjectId
)
>
0
{
return
ErrConnected
...
...
This diff is collapsed.
Click to expand it.
client/examples_test.go
+
165
−
13
View file @
b1856e4d
...
...
@@ -23,6 +23,7 @@ import (
"log"
"strconv"
"strings"
"time"
"github.com/dgraph-io/dgraph/client"
"github.com/dgraph-io/dgraph/x"
...
...
@@ -74,7 +75,7 @@ func ExampleReq_Set() {
err
=
req
.
Set
(
e
)
x
.
Check
(
err
)
// If the old variable was written over or outof scope we can lookup person1 again,
// If the old variable was written over or out
of scope we can lookup person1 again,
// the string->node mapping is remembered by the client for this session.
p
,
err
:=
dgraphClient
.
NodeBlank
(
"person1"
)
e
=
p
.
Edge
(
"salary"
)
...
...
@@ -86,9 +87,80 @@ func ExampleReq_Set() {
if
err
!=
nil
{
log
.
Fatalf
(
"Error in getting response from server, %s"
,
err
)
}
// proto.MarshalTextString(resp) can be used to print the raw response as text. Client
// programs usually use Umarshal to unpack query responses to a struct (or the protocol
// buffer can be accessed with resp.N)
fmt
.
Printf
(
"%+v
\n
"
,
proto
.
MarshalTextString
(
resp
))
}
func
ExampleReq_Delete
()
{
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
)
// Create new request
req
:=
client
.
Req
{}
// Create a node for person1 (the blank node label "person1" exists
// client-side so the mutation can correctly link nodes. It is not
// persisted in the server)
person1
,
err
:=
dgraphClient
.
NodeBlank
(
"person1"
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
person2
,
err
:=
dgraphClient
.
NodeBlank
(
"person2"
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
e
:=
person1
.
Edge
(
"name"
)
e
.
SetValueString
(
"Steven Spallding"
)
err
=
req
.
Set
(
e
)
x
.
Check
(
err
)
e
=
person2
.
Edge
(
"name"
)
e
.
SetValueString
(
"Steven Stevenson"
)
err
=
req
.
Set
(
e
)
x
.
Check
(
err
)
e
=
person1
.
ConnectTo
(
"friend"
,
person2
)
// Add person1, person2 and friend edge to store
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
))
// Now remove the friend edge
// If the old variable was written over or out of scope we can lookup person1 again,
// the string->node mapping is remembered by the client for this session.
p1
,
err
:=
dgraphClient
.
NodeBlank
(
"person1"
)
p2
,
err
:=
dgraphClient
.
NodeBlank
(
"person2"
)
e
=
p1
.
ConnectTo
(
"friend"
,
p2
)
req
=
client
.
Req
{}
req
.
Delete
(
e
)
// Run the mutation to delete the edge
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
ExampleDgraph_BatchSet
()
{
conn
,
err
:=
grpc
.
Dial
(
"127.0.0.1:9080"
,
grpc
.
WithInsecure
())
x
.
Checkf
(
err
,
"While trying to dial gRPC"
)
...
...
@@ -139,7 +211,7 @@ func ExampleEdge_AddFacet() {
log
.
Fatal
(
err
)
}
e
:=
person1
.
Edge
(
"name"
)
e
.
SetValueString
(
"Steven S
pielberg
"
)
e
.
SetValueString
(
"Steven S
tevenson
"
)
// Add facets since and alias to the edge.
e
.
AddFacet
(
"since"
,
"2006-01-02T15:04:05"
)
...
...
@@ -164,12 +236,16 @@ func ExampleEdge_AddFacet() {
err
=
req
.
Set
(
e
)
x
.
Check
(
err
)
req
.
AddSchemaFromString
(
`
name: string @index(exact) .
`
)
req
.
AddSchemaFromString
(
`name: string @index(exact) .`
)
resp
,
err
:=
dgraphClient
.
Run
(
context
.
Background
(),
&
req
)
if
err
!=
nil
{
log
.
Fatalf
(
"Error in getting response from server, %s"
,
err
)
}
req
=
client
.
Req
{}
req
.
SetQuery
(
`{
me
(func: eq(name,"Steven S
pielberg
")) {
query
(func: eq(name,"Steven S
tevenson
")) {
name @facets
friend @facets {
name
...
...
@@ -177,13 +253,50 @@ name: string @index(exact) .
}
}`
)
// Run the request in the Dgraph server. The mutations are added, then
// the query is exectuted.
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
))
// Types representing information in the graph.
type
nameFacets
struct
{
Since
time
.
Time
`dgraph:"since"`
Alias
string
`dgraph:"alias"`
}
type
friendFacets
struct
{
Close
bool
`dgraph:"close"`
}
type
Person
struct
{
Name
string
`dgraph:"name"`
NameFacets
nameFacets
`dgraph:"name@facets"`
Friends
[]
Person
`dgraph:"friend"`
FriendFacets
friendFacets
`dgraph:"@facets"`
}
// Helper type to unmarshal query
type
Res
struct
{
Root
Person
`dgraph:"query"`
}
var
pq
Res
err
=
client
.
Unmarshal
(
resp
.
N
,
&
pq
)
if
err
!=
nil
{
log
.
Fatal
(
"Couldn't unmarshal response : "
,
err
)
}
fmt
.
Println
(
"Found : "
,
pq
.
Root
.
Name
)
fmt
.
Println
(
"Who likes to be called : "
,
pq
.
Root
.
NameFacets
.
Alias
,
" since "
,
pq
.
Root
.
NameFacets
.
Since
)
fmt
.
Println
(
"Friends : "
)
for
i
:=
range
pq
.
Root
.
Friends
{
fmt
.
Print
(
"
\t
"
,
pq
.
Root
.
Friends
[
i
]
.
Name
)
if
pq
.
Root
.
Friends
[
i
]
.
FriendFacets
.
Close
{
fmt
.
Println
(
" who is a close friend."
)
}
else
{
fmt
.
Println
(
" who is not a close friend."
)
}
}
}
func
ExampleReq_AddSchemaFromString
()
{
...
...
@@ -264,7 +377,20 @@ func ExampleReq_SetQuery() {
if
err
!=
nil
{
log
.
Fatalf
(
"Error in getting response from server, %s"
,
err
)
}
fmt
.
Printf
(
"%+v
\n
"
,
proto
.
MarshalTextString
(
resp
))
type
Alice
struct
{
Name
string
`dgraph:"name"`
WhatHappened
string
`dgraph:"falls.in"`
}
type
Res
struct
{
Root
Alice
`dgraph:"me"`
}
var
r
Res
err
=
client
.
Unmarshal
(
resp
.
N
,
&
r
)
x
.
Check
(
err
)
fmt
.
Printf
(
"Alice: %+v
\n\n
"
,
r
.
Root
)
}
func
ExampleReq_SetQueryWithVariables
()
{
...
...
@@ -309,7 +435,20 @@ func ExampleReq_SetQueryWithVariables() {
}`
,
variables
)
resp
,
err
:=
dgraphClient
.
Run
(
context
.
Background
(),
&
req
)
fmt
.
Printf
(
"%+v
\n
"
,
proto
.
MarshalTextString
(
resp
))
type
Alice
struct
{
Name
string
`dgraph:"name"`
WhatHappened
string
`dgraph:"falls.in"`
}
type
Res
struct
{
Root
Alice
`dgraph:"me"`
}
var
r
Res
err
=
client
.
Unmarshal
(
resp
.
N
,
&
r
)
x
.
Check
(
err
)
fmt
.
Printf
(
"Alice: %+v
\n\n
"
,
r
.
Root
)
}
func
ExampleDgraph_NodeUidVar
()
{
...
...
@@ -422,7 +561,20 @@ func ExampleEdge_SetValueBytes() {
if
err
!=
nil
{
log
.
Fatalf
(
"Error in getting response from server, %s"
,
err
)
}
fmt
.
Printf
(
"%+v
\n
"
,
proto
.
MarshalTextString
(
resp
))
type
Alice
struct
{
Name
string
`dgraph:"name"`
ByteValue
[]
byte
`dgraph:"somestoredbytes"`
}
type
Res
struct
{
Root
Alice
`dgraph:"q"`
}
var
r
Res
err
=
client
.
Unmarshal
(
resp
.
N
,
&
r
)
x
.
Check
(
err
)
fmt
.
Printf
(
"Alice: %+v
\n\n
"
,
r
.
Root
)
}
func
ExampleUnmarshal
()
{
...
...
This diff is collapsed.
Click to expand it.
client/mutations.go
+
53
−
63
View file @
b1856e4d
...
...
@@ -49,10 +49,9 @@ var (
emptyEdge
Edge
)
// BatchMutationOptions sets the clients batch mode to Pending number of buffers
// each of Size. Running counters of number of rdfs processed, total time and
// mutations per second are printed if PrintCounters is set true.
// See Counter.
// BatchMutationOptions sets the clients batch mode to Pending number of buffers each of Size.
// Running counters of number of rdfs processed, total time and mutations per second are printed
// if PrintCounters is set true. See Counter.
type
BatchMutationOptions
struct
{
Size
int
Pending
int
...
...
@@ -153,9 +152,8 @@ func (a *allocator) assignOrGet(id string) (uid uint64, isNew bool, err error) {
return
}
// Counter keeps a track of various parameters about a batch mutation.
// Running totals are printed if BatchMutationOptions
// PrintCounters is set to true.
// Counter keeps a track of various parameters about a batch mutation. Running totals are printed
// if BatchMutationOptions PrintCounters is set to true.
type
Counter
struct
{
// Number of RDF's processed by server.
Rdfs
uint64
...
...
@@ -165,9 +163,8 @@ type Counter struct {
Elapsed
time
.
Duration
}
// A Dgraph is the data structure held by the user program for all
// interactions with the Dgraph server. After making grpc
// connection a new Dgraph is created by function NewDgraphClient.
// A Dgraph is the data structure held by the user program for all interactions with the Dgraph
// server. After making grpc connection a new Dgraph is created by function NewDgraphClient.
type
Dgraph
struct
{
opts
BatchMutationOptions
...
...
@@ -187,10 +184,9 @@ type Dgraph struct {
start
time
.
Time
}
// NewDgraphClient creates a new Dgraph for interacting with
// the Dgraph store connected to in conns. The Dgraph
// client stores blanknode to uid, and XIDnode to uid
// mappings on disk in clientDir.
// NewDgraphClient creates a new Dgraph for interacting with the Dgraph store connected to in
// conns. The Dgraph client stores blanknode to uid, and XIDnode to uid mappings on disk
// in clientDir.
func
NewDgraphClient
(
conns
[]
*
grpc
.
ClientConn
,
opts
BatchMutationOptions
,
clientDir
string
)
*
Dgraph
{
var
clients
[]
protos
.
DgraphClient
for
_
,
conn
:=
range
conns
{
...
...
@@ -368,12 +364,10 @@ LOOP:
d
.
wg
.
Done
()
}
// BatchSet adds Edge e as a set to the current batch mutation. Once added,
// 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
// specified in d's BatchMutationOptions. If that batch fills, it eventually
// flushes. But there is no guarantee of delivery before BatchFlush() is
// called.
// BatchSet adds Edge e as a set to the current batch mutation. Once added, 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 specified in d's BatchMutationOptions. If that batch fills, it
// eventually flushes. But there is no guarantee of delivery before BatchFlush() is called.
func
(
d
*
Dgraph
)
BatchSet
(
e
Edge
)
error
{
d
.
nquads
<-
nquadOp
{
e
:
e
,
...
...
@@ -383,12 +377,10 @@ func (d *Dgraph) BatchSet(e Edge) error {
return
nil
}
// 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
// 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
// flushes. But there is no guarantee of delivery before BatchFlush() is
// called.
// 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 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 flushes. But there is no guarantee of delivery before BatchFlush() is called.
func
(
d
*
Dgraph
)
BatchDelete
(
e
Edge
)
error
{
d
.
nquads
<-
nquadOp
{
e
:
e
,
...
...
@@ -398,11 +390,10 @@ func (d *Dgraph) BatchDelete(e Edge) error {
return
nil
}
// 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
// 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
// it is ready to flush its buffers.
// 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 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 it is ready to flush its buffers.
func
(
d
*
Dgraph
)
AddSchema
(
s
protos
.
SchemaUpdate
)
error
{
if
err
:=
checkSchema
(
s
);
err
!=
nil
{
return
err
...
...
@@ -413,9 +404,10 @@ func (d *Dgraph) AddSchema(s protos.SchemaUpdate) error {
// AddSchemaFromString parses s for schema mutations and adds each update in s
// to the batch using AddSchema. The given string should be of the form:
// edgename: uid @reverse .
// edge2: string @index(exact) .
// etc.
//
// edgename: uid @reverse .
// edge2: string @index(exact) .
//
// to use the form "mutation { schema { ... }}" issue the mutation through
// SetQuery rather than as a batch.
func
(
d
*
Dgraph
)
AddSchemaFromString
(
s
string
)
error
{
...
...
@@ -437,10 +429,9 @@ func (d *Dgraph) AddSchemaFromString(s string) error {
return
nil
}
// BatchFlush waits for all pending requests to complete. It should always be called
// after all BatchSet and BatchDeletes have been called. Calling BatchFlush
// ends the client session and will cause a panic if further AddSchema,
// BatchSet or BatchDelete functions are called.
// BatchFlush waits for all pending requests to complete. It should always be called after all
// BatchSet and BatchDeletes have been called. Calling BatchFlush ends the client session and
// will cause a panic if further AddSchema, BatchSet or BatchDelete functions are called.
func
(
d
*
Dgraph
)
BatchFlush
()
{
close
(
d
.
nquads
)
close
(
d
.
schema
)
...
...
@@ -450,8 +441,11 @@ func (d *Dgraph) BatchFlush() {
}
}
// Run runs the request in req and returns with the completed response from
// the server. Calling Run has no effect on batched mutations.
// Run runs the request in req and returns with the completed response from the server. Calling
// Run has no effect on batched mutations.
//
// Mutations in the request are run before a query --- except when query variables link the
// mutation and query (see for example NodeUidVar) when the query is run first.
func
(
d
*
Dgraph
)
Run
(
ctx
context
.
Context
,
req
*
Req
)
(
*
protos
.
Response
,
error
)
{
return
d
.
dc
[
rand
.
Intn
(
len
(
d
.
dc
))]
.
Run
(
ctx
,
&
req
.
gr
)
}
...
...
@@ -465,9 +459,9 @@ func (d *Dgraph) Counter() Counter {
}
}
// CheckVersion checks if the version of dgraph and dgraphloader are the same.
//
If either the
versions don't match or the version information could not be
//
obtained an error message is
printed.
// CheckVersion checks if the version of dgraph and dgraphloader are the same.
If either the
// versions don't match or the version information could not be
obtained an error message is
// printed.
func
(
d
*
Dgraph
)
CheckVersion
(
ctx
context
.
Context
)
{
v
,
err
:=
d
.
dc
[
rand
.
Intn
(
len
(
d
.
dc
))]
.
CheckVersion
(
ctx
,
&
protos
.
Check
{})
if
err
!=
nil
{
...
...
@@ -488,11 +482,10 @@ func (d *Dgraph) NodeUid(uid uint64) Node {
return
Node
{
uid
:
uid
}
}
// 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
// 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. If the label has already been assigned, the corresponding 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 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. If the label has already been assigned, the corresponding Node
// is returned.
func
(
d
*
Dgraph
)
NodeBlank
(
varname
string
)
(
Node
,
error
)
{
if
len
(
varname
)
==
0
{
...
...
@@ -511,12 +504,11 @@ func (d *Dgraph) NodeBlank(varname string) (Node, error) {
return
Node
{
uid
:
uid
},
nil
}
// NodeXid creates or returns a Node given a string name for an XID node.
// An XID node identifies a node with an edge _xid_, as in
// node --- _xid_ ---> XID string
// See https://docs.dgraph.io/query-language/#external-ids
// If the XID has already been allocated in this client session
// the allocated UID is returned, otherwise a new UID is allocated
// NodeXid creates or returns a Node given a string name for an XID node. An XID node identifies a
// node with an edge _xid_, as in
// node --- _xid_ ---> XID string
// See https://docs.dgraph.io/query-language/#external-ids If the XID has already been allocated
// in this client session the allocated UID is returned, otherwise a new UID is allocated
// for xid and returned.
func
(
d
*
Dgraph
)
NodeXid
(
xid
string
,
storeXid
bool
)
(
Node
,
error
)
{
if
len
(
xid
)
==
0
{
...
...
@@ -535,16 +527,14 @@ func (d *Dgraph) NodeXid(xid string, storeXid bool) (Node, error) {
return
n
,
nil
}
// NodeUidVar creates a Node from a variable name. When building a request,
// set and delete mutations may depend on the request's query, as in:
// https://docs.dgraph.io/query-language/#variables-in-mutations
// Such query variables in mutations could be built into the raw query string,
// but it is often more convenient to use client functions than manipulate
// strings.
// NodeUidVar creates a Node from a variable name. When building a request, set and delete
// mutations may depend on the request's query, as in:
// https://docs.dgraph.io/query-language/#variables-in-mutations Such query variables in mutations
// could be built into the raw query string, but it is often more convenient to use client
// functions than manipulate strings.
//
// A request with a query and mutations (including variables in mutations)
// will run in the same manner as if the query and mutations were set into
// the query string.
// A request with a query and mutations (including variables in mutations) will run in the same
// manner as if the query and mutations were set into the query string.
func
(
d
*
Dgraph
)
NodeUidVar
(
name
string
)
(
Node
,
error
)
{
if
len
(
name
)
==
0
{
return
Node
{},
ErrEmptyVar
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment