Skip to content

Commit 4c1402a

Browse files
improve consistency between root-level and glv-level examples
1 parent 452d10d commit 4c1402a

File tree

6 files changed

+41
-46
lines changed

6 files changed

+41
-46
lines changed

glv-examples/gremlin-go/basic_gremlin.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import (
2525
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2626
)
2727

28+
var serverURL = "ws://localhost:8182/gremlin"
29+
var vertexLabel = "person"
30+
2831
func main() {
29-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
32+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3033
if err != nil {
3134
fmt.Println(err)
3235
return
@@ -35,9 +38,9 @@ func main() {
3538
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
3639

3740
// Basic Gremlin: adding and retrieving data
38-
v1, err := g.AddV("person").Property("name", "marko").Next()
39-
v2, err := g.AddV("person").Property("name", "stephen").Next()
40-
v3, err := g.AddV("person").Property("name", "vadas").Next()
41+
v1, err := g.AddV(vertexLabel).Property("name", "marko").Next()
42+
v2, err := g.AddV(vertexLabel).Property("name", "stephen").Next()
43+
v3, err := g.AddV(vertexLabel).Property("name", "vadas").Next()
4144
v1Vertex, err := v1.GetVertex()
4245
v2Vertex, err := v2.GetVertex()
4346
v3Vertex, err := v3.GetVertex()
@@ -58,11 +61,11 @@ func main() {
5861
}
5962

6063
// Retrieve the data from the "marko" vertex
61-
marko, err := g.V().Has("person", "name", "marko").Values("name").Next()
64+
marko, err := g.V().Has(vertexLabel, "name", "marko").Values("name").Next()
6265
fmt.Println("name:", marko.GetString())
6366

6467
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
65-
peopleMarkoKnows, err := g.V().Has("person", "name", "marko").Out("knows").Values("name").ToList()
68+
peopleMarkoKnows, err := g.V().Has(vertexLabel, "name", "marko").Out("knows").Values("name").ToList()
6669
for _, person := range peopleMarkoKnows {
6770
fmt.Println("marko knows", person.GetString())
6871
}

glv-examples/gremlin-go/connections.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import (
2525
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2626
)
2727

28+
var serverURL = "ws://localhost:8182/gremlin"
29+
var vertexLabel = "connection"
30+
2831
func main() {
2932
withRemote()
3033
withConfigs()
3134
}
3235

3336
func withRemote() {
34-
// Creating the connection to the server
35-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
37+
// Creating the connection to the server
38+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3639

3740
// Error handling
3841
if err != nil {
@@ -43,22 +46,18 @@ func withRemote() {
4346
// Cleanup
4447
defer driverRemoteConnection.Close()
4548

46-
// Creating the graph traversal
49+
// Creating the graph traversal
4750
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
4851

49-
// Drop existing vertices
50-
prom := g.V().Drop().Iterate()
51-
<-prom
52-
53-
// Simple query to verify connection
54-
g.AddV("connection").Iterate()
55-
count, _ := g.V().HasLabel("connection").Count().Next()
56-
fmt.Println("Vertex count:", *count)
52+
// Simple query to verify connection
53+
g.AddV(vertexLabel).Iterate()
54+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
55+
fmt.Println("Vertex count:", *count)
5756
}
5857

5958
func withConfigs() {
6059
// Connecting to the server with customized configurations
61-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin",
60+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL,
6261
func(settings *gremlingo.DriverRemoteConnectionSettings) {
6362
settings.TraversalSource = "g"
6463
settings.NewConnectionThreshold = 4
@@ -75,7 +74,7 @@ func withConfigs() {
7574
defer driverRemoteConnection.Close()
7675
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
7776

78-
g.AddV("connection").Iterate()
79-
count, _ := g.V().HasLabel("connection").Count().Next()
80-
fmt.Println("Vertex count:", *count)
81-
}
77+
g.AddV(vertexLabel).Iterate()
78+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
79+
fmt.Println("Vertex count:", *count)
80+
}

glv-examples/gremlin-go/modern_traversals.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import (
2828
var __ = gremlingo.T__
2929
var T = gremlingo.T
3030
var P = gremlingo.P
31+
var serverURL = "ws://localhost:8182/gremlin"
3132

3233
func main() {
33-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
34+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3435
if err != nil {
3536
fmt.Println(err)
3637
return

gremlin-go/examples/basic_gremlin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ import (
2626
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2727
)
2828

29-
func getEnvOrDefaultString(key string, defaultValue string) string {
30-
value := os.Getenv(key)
31-
if len(value) != 0 {
29+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
30+
var vertexLabel = getEnv("VERTEX_LABEL", "person")
31+
32+
func getEnv(key, defaultValue string) string {
33+
if value := os.Getenv(key); value != "" {
3234
return value
3335
}
3436
return defaultValue
3537
}
3638

3739
func main() {
38-
serverURL := getEnvOrDefaultString("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
39-
vertexLabel := getEnvOrDefaultString("VERTEX_LABEL", "person")
4040
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
4141
if err != nil {
4242
fmt.Println(err)

gremlin-go/examples/connections.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2727
)
2828

29-
func getEnvOrDefaultString(key string, defaultValue string) string {
30-
value := os.Getenv(key)
31-
if len(value) != 0 {
29+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
30+
var vertexLabel = getEnv("VERTEX_LABEL", "connection")
31+
32+
func getEnv(key, defaultValue string) string {
33+
if value := os.Getenv(key); value != "" {
3234
return value
3335
}
3436
return defaultValue
@@ -40,9 +42,6 @@ func main() {
4042
}
4143

4244
func withRemote() {
43-
serverURL := getEnvOrDefaultString("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
44-
vertexLabel := getEnvOrDefaultString("VERTEX_LABEL", "connection")
45-
4645
// Creating the connection to the server
4746
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
4847

@@ -60,17 +59,11 @@ func withRemote() {
6059

6160
// Simple query to verify connection
6261
g.AddV(vertexLabel).Iterate()
63-
count, _ := g.V().Count().Next()
62+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
6463
fmt.Println("Vertex count:", *count)
65-
66-
// clean added data
67-
g.V().HasLabel(vertexLabel).Drop().Iterate()
6864
}
6965

7066
func withConfigs() {
71-
serverURL := getEnvOrDefaultString("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
72-
vertexLabel := getEnvOrDefaultString("VERTEX_LABEL", "connection")
73-
7467
// Connecting to the server with customized configurations
7568
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL,
7669
func(settings *gremlingo.DriverRemoteConnectionSettings) {
@@ -90,6 +83,6 @@ func withConfigs() {
9083
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
9184

9285
g.AddV(vertexLabel).Iterate()
93-
count, _ := g.V().Count().Next()
86+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
9487
fmt.Println("Vertex count:", *count)
9588
}

gremlin-go/examples/modern_traversals.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ import (
2929
var __ = gremlingo.T__
3030
var T = gremlingo.T
3131
var P = gremlingo.P
32+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
3233

33-
func getEnvOrDefaultString(key string, defaultValue string) string {
34-
value := os.Getenv(key)
35-
if len(value) != 0 {
34+
func getEnv(key, defaultValue string) string {
35+
if value := os.Getenv(key); value != "" {
3636
return value
3737
}
3838
return defaultValue
3939
}
4040

4141
func main() {
42-
serverURL := getEnvOrDefaultString("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
4342
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
4443
if err != nil {
4544
fmt.Println(err)

0 commit comments

Comments
 (0)