Skip to content

Commit

Permalink
Refactor nebula go client implementation
Browse files Browse the repository at this point in the history
Add editorconfig and use log library to output msg

Add print exec response result util function signature

Simplify nebula client

Merge open/authenticate and close/signout

Rename session ID

Rename Open/Close to Connect/Disconnect

Delete session ID parameter of Execute

Remove print response function temporarily
  • Loading branch information
yixinglu committed Sep 24, 2019
1 parent 6f51048 commit e891a89
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[{Makefile,go.mod,go.sum,*.go}]
indent_style = tab
indent_size = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ Official Nebula Go client which communicates with the server using [fbthrift](ht

Please be careful do not to modify the files in the graph directory, these codes were all generated by fbthrift.

## Install

```shell
$ go get -u -v github.com/vesoft-inc/nebula-go
```
48 changes: 21 additions & 27 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
package ngdb

import (
"fmt"
"log"
"time"

"github.com/facebook/fbthrift/thrift/lib/go/thrift"
"github.com/vesoft-inc/nebula-go/graph"
"time"
)

type GraphOptions struct {
Expand All @@ -24,8 +25,9 @@ var defaultGraphOptions = GraphOptions{
}

type GraphClient struct {
graph graph.GraphServiceClient
option GraphOptions
graph graph.GraphServiceClient
option GraphOptions
sessionID int64
}

func WithTimeout(duration time.Duration) GraphOption {
Expand All @@ -43,7 +45,6 @@ func NewClient(address string, opts ...GraphOption) (client *GraphClient, err er
timeoutOption := thrift.SocketTimeout(options.Timeout)
addressOption := thrift.SocketAddr(address)
transport, err := thrift.NewSocket(timeoutOption, addressOption)

if err != nil {
return nil, err
}
Expand All @@ -55,36 +56,29 @@ func NewClient(address string, opts ...GraphOption) (client *GraphClient, err er
return graph, nil
}

func (client *GraphClient) Open() error {
err := client.graph.Transport.Open()
if err != nil {
// Open transport and authenticate
func (client *GraphClient) Connect(username, password string) error {
if err := client.graph.Transport.Open(); err != nil {
return err
}
return nil
}

func (client *GraphClient) Close() {
client.graph.Close()
}

func (client *GraphClient) Authenticate(username string, password string) (response *graph.AuthResponse, err error) {
if response, err := client.graph.Authenticate(username, password); err != nil {
return nil, err
if resp, err := client.graph.Authenticate(username, password); err != nil {
log.Printf("ErrorCode: %v, ErrorMsg: %s", resp.GetErrorCode(), resp.GetErrorMsg())
return err
} else {
return response, nil
client.sessionID = resp.GetSessionID()
return nil
}
}

func (client *GraphClient) Signout(sessionID int64) {
if err := client.graph.Signout(sessionID); err != nil {
fmt.Printf("Signout Failed : %v", err)
// Close transport and signout
func (client *GraphClient) Disconnect() error {
if err := client.graph.Close(); err != nil {
return err
}
return client.graph.Signout(client.sessionID)
}

func (client *GraphClient) Execute(sessionID int64, stmt string) (response *graph.ExecutionResponse, err error) {
if response, err := client.graph.Execute(sessionID, stmt); err != nil {
return nil, err
} else {
return response, nil
}
func (client *GraphClient) Execute(stmt string) (*graph.ExecutionResponse, error) {
return client.graph.Execute(client.sessionID, stmt)
}

0 comments on commit e891a89

Please sign in to comment.