Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiee committed Sep 13, 2022
1 parent 2d510b1 commit abc6803
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ run-examples:
go run basic_example/graph_client_basic_example.go && \
go run basic_example/parameter_example.go && \
go run gorountines_example/graph_client_goroutines_example.go && \
go run json_example/parse_json_example.go
go run json_example/parse_json_example.go && \
go run session_pool_example/session_pool_example.go
1 change: 1 addition & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ func prepareSpace(t *testing.T, spaceName string) error {
}
checkConResp(t, query, resp)
time.Sleep(5 * time.Second)

return nil
}

Expand Down
15 changes: 10 additions & 5 deletions session_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
//
// Release:
// sessionPool.close()
//
// Notice that all queries will be executed in the default space specified in the pool config.
type SessionPool struct {
idleSessions list.List
activeSessions list.List
Expand Down Expand Up @@ -76,9 +78,8 @@ func (pool *SessionPool) init() error {

// Execute returns the result of the given query as a ResultSet
// Notice there are some limitations:
// 1. The query should not be a plain space switch statement, e.g. "USE space test_space",
// 1. The query should not be a plain space switch statement, e.g. "USE test_space",
// but queries like "use space xxx; match (v) return v" are accepted.

func (pool *SessionPool) Execute(stmt string) (*ResultSet, error) {
return pool.ExecuteWithParameter(stmt, map[string]interface{}{})
}
Expand Down Expand Up @@ -114,7 +115,7 @@ func (pool *SessionPool) ExecuteWithParameter(stmt string, params map[string]int
// if the space was changed in after the execution of the given query,
// change it back to the default space specified in the pool config
if resSet.GetSpaceName() != "" && resSet.GetSpaceName() != pool.conf.spaceName {
stmt = fmt.Sprintf("USE SPACE %s", pool.conf.spaceName)
stmt = fmt.Sprintf("USE %s", pool.conf.spaceName)
resp, err := session.connection.execute(session.sessionID, stmt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -300,11 +301,15 @@ func (pool *SessionPool) newSession() (*Session, error) {
return nil, err
}

stmt := fmt.Sprintf("USE SPACE %s", pool.conf.spaceName)
_, err = newSession.connection.execute(newSession.sessionID, stmt)
stmt := fmt.Sprintf("USE %s", pool.conf.spaceName)
createSpaceResp, err := newSession.connection.execute(newSession.sessionID, stmt)
if err != nil {
return nil, err
}
if createSpaceResp.GetErrorCode() != nebula.ErrorCode_SUCCEEDED {
return nil, fmt.Errorf("failed to use space %s: %s",
pool.conf.spaceName, createSpaceResp.GetErrorMsg())
}
return &newSession, nil
}

Expand Down
27 changes: 18 additions & 9 deletions session_pool_example/session_pool_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
import (
"fmt"
"strings"
"sync"
"time"

nebula "github.com/vesoft-inc/nebula-go/v3"
Expand Down Expand Up @@ -87,13 +88,21 @@ func main() {
// Extract data from the resultSet
{
query := "GO FROM 'Bob' OVER like YIELD $^.person.name, $^.person.age, like.likeness"
// Send query
resultSet, err := sessionPool.Execute(query)
if err != nil {
fmt.Print(err.Error())
return
}
checkResultSet(query, resultSet)
// Send query in goroutine
wg := sync.WaitGroup{}
wg.Add(1)
var resultSet *nebula.ResultSet
go func(wg *sync.WaitGroup) {
defer wg.Done()

resultSet, err = sessionPool.Execute(query)
if err != nil {
fmt.Print(err.Error())
return
}
checkResultSet(query, resultSet)
}(&wg)
wg.Wait()

// Get all column names from the resultSet
colNames := resultSet.GetColNames()
Expand Down Expand Up @@ -137,7 +146,7 @@ func main() {
checkResultSet(query, resultSet)
}
fmt.Print("\n")
log.Info("Nebula Go Client Gorountines Example Finished")
log.Info("Nebula Go Client Session Pool Example Finished")
}

// Just a helper function to create a space for this example to run.
Expand Down Expand Up @@ -173,7 +182,7 @@ func prepareSpace() {
{
// Prepare the query
createSchema := "CREATE SPACE IF NOT EXISTS example_space(vid_type=FIXED_STRING(20)); " +
"USE basic_example_space;" +
"USE example_space;" +
"CREATE TAG IF NOT EXISTS person(name string, age int);" +
"CREATE EDGE IF NOT EXISTS like(likeness double)"

Expand Down
33 changes: 32 additions & 1 deletion session_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,36 @@ func TestSessionPoolMultiThread(t *testing.T) {

// TODO(Aiee): add more test cases
func TestSessionPoolSpaceChange(t *testing.T) {
// query that changes space is not allowed in session pool. i.g. `USE space nba`
err := prepareSpace(t, "test_space_1")
if err != nil {
t.Fatal(err)
}
defer dropSpace(t, "test_space_1")

err = prepareSpace(t, "test_space_2")
if err != nil {
t.Fatal(err)
}
defer dropSpace(t, "test_space_2")

hostAddress := HostAddress{Host: address, Port: port}
config, err := NewSessionPoolConf("root", "nebula", []HostAddress{hostAddress}, "test_space_1")
if err != nil {
t.Errorf("failed to create session pool config, %s", err.Error())
}

// create session pool
sessionPool, err := NewSessionPool(*config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}
defer sessionPool.Close()

// execute query
resultSet, err := sessionPool.Execute("USE test_space_2; SHOW HOSTS;")
if err != nil {
t.Fatal(err)
}
assert.True(t, resultSet.IsSucceed(), fmt.Errorf("error code: %d, error msg: %s",
resultSet.GetErrorCode(), resultSet.GetErrorMsg()))
}

0 comments on commit abc6803

Please sign in to comment.