Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
bbe47145
Commit
bbe47145
authored
5 years ago
by
Imtiyaz Shaikh
Committed by
Imtiyaz Shaikh
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Additional Params for Host Struct
Signed-off-by:
Imtiyaz Shaikh
<
imtiyazshaikh5786@gmail.com
>
parent
2f7d7c39
2 merge requests
!180
Mep release 07042020
,
!175
Add: Graph Database support
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dalmdl/dgraph/dgraph.go
+23
-9
dalmdl/dgraph/dgraph.go
dalmdl/dgraph/dgraph_test.go
+158
-11
dalmdl/dgraph/dgraph_test.go
with
181 additions
and
20 deletions
dalmdl/dgraph/dgraph.go
+
23
−
9
View file @
bbe47145
...
@@ -3,22 +3,23 @@ package dgraph
...
@@ -3,22 +3,23 @@ package dgraph
import
(
import
(
"context"
"context"
"errors"
"errors"
"strconv"
"strings"
"strings"
"time"
"time"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
)
)
type
Host
struct
{
type
Host
struct
{
Name
string
Name
string
`json:"hostName"`
Addr
string
Server
string
`json:"server"`
Port
int
`json:"port"`
// UserName string
// UserName string
// Password string
// Password string
IsDefault
bool
IsDefault
bool
`json:"isDefault"`
IsD
elet
ed
bool
IsD
isabl
ed
bool
`json:"IsDisabled"`
}
}
type
Instance
struct
{
type
Instance
struct
{
...
@@ -39,10 +40,12 @@ var (
...
@@ -39,10 +40,12 @@ var (
// NewClient returns a new dgraph client for provided configuration.
// NewClient returns a new dgraph client for provided configuration.
func
NewClient
(
h
Host
)
(
*
dgo
.
Dgraph
,
error
)
{
func
NewClient
(
h
Host
)
(
*
dgo
.
Dgraph
,
error
)
{
if
strings
.
TrimSpace
(
h
.
Add
r
)
==
""
{
if
strings
.
TrimSpace
(
h
.
Serve
r
)
==
""
{
return
nil
,
errors
.
New
(
"host address can not be empty"
)
return
nil
,
errors
.
New
(
"host address can not be empty"
)
}
}
address
:=
bindDgraphServerWithPort
(
h
.
Server
,
h
.
Port
)
// Dial a gRPC connection. The address to dial to can be configured when
// Dial a gRPC connection. The address to dial to can be configured when
// setting up the dgraph cluster.
// setting up the dgraph cluster.
dialOpts
:=
[]
grpc
.
DialOption
{
dialOpts
:=
[]
grpc
.
DialOption
{
...
@@ -50,16 +53,16 @@ func NewClient(h Host) (*dgo.Dgraph, error) {
...
@@ -50,16 +53,16 @@ func NewClient(h Host) (*dgo.Dgraph, error) {
grpc
.
WithBlock
(),
// block till we connect to the server
grpc
.
WithBlock
(),
// block till we connect to the server
// grpc.WithTimeout(time.Second * 5),
// grpc.WithTimeout(time.Second * 5),
grpc
.
WithDefaultCallOptions
(
grpc
.
WithDefaultCallOptions
(
grpc
.
UseCompressor
(
gzip
.
Name
),
//
grpc.UseCompressor(gzip.Name),
grpc
.
WaitForReady
(
true
),
grpc
.
WaitForReady
(
true
),
),
),
}
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
TODO
(),
time
.
Second
*
5
)
// wait for 5 seconds to connect to grpc server. Exit if the deadline exceeds.
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
TODO
(),
time
.
Second
*
5
)
// wait for 5 seconds to connect to grpc server. Exit if the deadline exceeds.
defer
cancel
()
defer
cancel
()
d
,
err
:=
grpc
.
DialContext
(
ctx
,
h
.
Addr
,
dialOpts
...
)
d
,
err
:=
grpc
.
DialContext
(
ctx
,
address
,
dialOpts
...
)
if
err
==
context
.
DeadlineExceeded
{
if
err
==
context
.
DeadlineExceeded
{
return
nil
,
errors
.
New
(
"graphdb connect error, connection timed out for host "
+
h
.
Addr
)
return
nil
,
errors
.
New
(
"graphdb connect error, connection timed out for host "
+
address
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -250,3 +253,14 @@ func (dg *DGraphDAO) DropEdge(ctx context.Context, uid string, predicates ...str
...
@@ -250,3 +253,14 @@ func (dg *DGraphDAO) DropEdge(ctx context.Context, uid string, predicates ...str
_
,
err
=
instance
.
client
.
NewTxn
()
.
Mutate
(
ctx
,
mu
)
_
,
err
=
instance
.
client
.
NewTxn
()
.
Mutate
(
ctx
,
mu
)
return
err
return
err
}
}
func
bindDgraphServerWithPort
(
server
string
,
port
int
)
string
{
// if port is empty then used default port 27017 & bind to server ip
var
serverURI
string
if
port
<=
0
||
strings
.
TrimSpace
(
strconv
.
Itoa
(
port
))
==
""
{
serverURI
=
server
+
":9080"
}
else
{
serverURI
=
server
+
":"
+
strconv
.
Itoa
(
port
)
}
return
serverURI
}
This diff is collapsed.
Click to expand it.
dalmdl/dgraph/dgraph_test.go
+
158
−
11
View file @
bbe47145
package
dgraph
package
dgraph
import
(
import
(
"context"
"testing"
"testing"
)
)
func
TestNewClient
(
t
*
testing
.
T
)
{
var
dgraphHost
=
&
Host
{
Name
:
"DGraphHost"
,
Server
:
"10.1.20.14"
,
Port
:
9080
,
}
func
Test_NewClient
(
t
*
testing
.
T
)
{
type
args
struct
{
type
args
struct
{
h
Host
h
Host
}
}
...
@@ -15,29 +22,169 @@ func TestNewClient(t *testing.T) {
...
@@ -15,29 +22,169 @@ func TestNewClient(t *testing.T) {
wantErr
bool
wantErr
bool
}{
}{
{
{
name
:
"fail on invalid host"
,
name
:
"success on valid connection"
,
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Addr
:
"localhost:8080"
}},
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Server
:
"10.1.20.14"
,
Port
:
9080
}},
// want: nil,
wantErr
:
false
,
},
{
name
:
"fail on connection fail"
,
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Server
:
"10.1.20.14"
,
Port
:
8080
}},
wantErr
:
true
,
wantErr
:
true
,
},
},
{
{
name
:
"success on valid host"
,
name
:
"success on default port used"
,
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Addr
:
"localhost:9080"
}},
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Server
:
"10.1.20.14"
}},
// want: &dgo.Dgraph{},
wantErr
:
false
,
wantErr
:
false
,
},
},
{
name
:
"fail on blank address"
,
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Server
:
""
}},
wantErr
:
true
,
},
{
name
:
"fail on invalid address"
,
args
:
args
{
h
:
Host
{
Name
:
"graphDBHost"
,
Server
:
"10.1.0"
}},
wantErr
:
true
,
},
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
_
,
err
:=
NewClient
(
tt
.
args
.
h
)
_
,
err
:=
NewClient
(
tt
.
args
.
h
)
// t.Error("Error: ", err)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"NewClient() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
t
.
Errorf
(
"NewClient() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
return
}
}
// if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("NewClient() = %v, want %v", got, tt.want)
// }
})
})
}
}
}
}
func
Test_CreateSchema
(
t
*
testing
.
T
)
{
type
args
struct
{
c
context
.
Context
schema
string
}
tests
:=
[]
struct
{
name
string
args
args
wantErr
bool
}{
{
name
:
"create schema successfully"
,
args
:
args
{
c
:
context
.
Background
(),
schema
:
`
name: string @index(exact) .
age: int .
`
},
wantErr
:
false
,
},
{
name
:
"pass invalid schema"
,
args
:
args
{
c
:
context
.
Background
(),
schema
:
`
name string @index(exact) .
age int .
`
},
wantErr
:
true
,
},
{
name
:
"pass blank schema"
,
args
:
args
{
c
:
context
.
Background
(),
schema
:
``
},
wantErr
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
err
:=
InitInstances
([]
Host
{
*
dgraphHost
})
if
err
!=
nil
{
t
.
Errorf
(
"InitInstances() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
dg
:=
GetDAO
(
*&
dgraphHost
.
Name
)
err
=
dg
.
CreateSchema
(
tt
.
args
.
c
,
tt
.
args
.
schema
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"CreateSchema() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
})
}
}
func
Test_SetData
(
t
*
testing
.
T
)
{
type
args
struct
{
c
context
.
Context
data
[]
byte
commit
bool
}
tests
:=
[]
struct
{
name
string
args
args
wantErr
bool
}{
{
name
:
"success on correct data"
,
args
:
args
{
c
:
context
.
Background
(),
data
:
[]
byte
(
`{
"name": "Person 1",
"age": 29,
"follows": {
"name": "Person 2",
"age": 18,
"follows": {
"name": "Person 3",
"age": 37
}
}
}`
),
commit
:
true
},
wantErr
:
false
,
},
{
name
:
"failure on incorrect data"
,
args
:
args
{
c
:
context
.
Background
(),
data
:
[]
byte
(
``
),
commit
:
true
},
wantErr
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
err
:=
InitInstances
([]
Host
{
*
dgraphHost
})
if
err
!=
nil
{
t
.
Errorf
(
"InitInstances() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
dg
:=
GetDAO
(
*&
dgraphHost
.
Name
)
err
=
dg
.
SetData
(
tt
.
args
.
c
,
tt
.
args
.
data
,
tt
.
args
.
commit
)
if
err
!=
nil
{
t
.
Errorf
(
"SetData() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
}
}
func
Test_DropSchema
(
t
*
testing
.
T
)
{
type
args
struct
{
c
context
.
Context
}
tests
:=
[]
struct
{
name
string
args
args
wantErr
bool
}{
{
name
:
"Drop schema case"
,
args
:
args
{
c
:
context
.
Background
()},
wantErr
:
false
,
},
}
for
_
,
tt
:=
range
tests
{
err
:=
InitInstances
([]
Host
{
*
dgraphHost
})
if
err
!=
nil
{
t
.
Errorf
(
"InitInstances() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
dg
:=
GetDAO
(
*&
dgraphHost
.
Name
)
err
=
dg
.
DropSchema
(
tt
.
args
.
c
)
if
err
!=
nil
{
t
.
Errorf
(
"DropSchema() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
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
Menu
Explore
Projects
Groups
Topics
Snippets