You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by xi...@apache.org on 2026/02/10 00:17:25 UTC
(tinkerpop) branch master updated: Go GLV added HTTP streaming & interceptors (#3299)
This is an automated email from the ASF dual-hosted git repository.
xiazcy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/master by this push:
new 334e124eb3 Go GLV added HTTP streaming & interceptors (#3299)
334e124eb3 is described below
commit 334e124eb3d07d37daadd0509fbe2d375c586284
Author: Yang Xia <55...@users.noreply.github.com>
AuthorDate: Mon Feb 9 16:17:19 2026 -0800
Go GLV added HTTP streaming & interceptors (#3299)
Added streaming deserializer, combined httpProtocol/httpTransport, added interceptors which replaces AuthInfo, added basic & sigv4 auth interceptors, updated connection settings. Refactored serializer to write to io.Writer for symmetry and renamed serializer files to reflect function.
---
CHANGELOG.asciidoc | 3 +
gremlin-go/design.md | 48 +-
gremlin-go/driver/auth.go | 104 ++
gremlin-go/driver/authInfo.go | 95 --
gremlin-go/driver/auth_test.go | 114 ++
gremlin-go/driver/client.go | 82 +-
gremlin-go/driver/client_test.go | 8 -
gremlin-go/driver/connection.go | 286 ++++-
gremlin-go/driver/connection_test.go | 452 +++++++-
gremlin-go/driver/cucumber/cucumberSteps_test.go | 2 +-
gremlin-go/driver/cucumber/cucumberWorld.go | 4 +-
gremlin-go/driver/driverRemoteConnection.go | 61 +-
gremlin-go/driver/driverRemoteConnection_test.go | 22 +-
gremlin-go/driver/error_codes.go | 8 +-
gremlin-go/driver/graphBinary.go | 1105 --------------------
gremlin-go/driver/graphBinaryDeserializer.go | 617 +++++++++++
gremlin-go/driver/graphBinaryDeserializer_test.go | 402 +++++++
gremlin-go/driver/graphBinarySerializer.go | 579 ++++++++++
gremlin-go/driver/graphBinarySerializer_test.go | 537 ++++++++++
gremlin-go/driver/graphBinary_test.go | 363 -------
gremlin-go/driver/gremlinlang.go | 14 -
gremlin-go/driver/httpProtocol.go | 157 ---
gremlin-go/driver/httpTransporter.go | 145 ---
gremlin-go/driver/logger.go | 3 +-
gremlin-go/driver/performance/performanceSuite.go | 3 -
gremlin-go/driver/request.go | 2 +-
gremlin-go/driver/resources/error-messages/en.json | 7 +-
.../driver/resources/logger-messages/en.json | 4 +-
gremlin-go/driver/resultSet.go | 4 +-
gremlin-go/driver/serializer.go | 141 +--
gremlin-go/driver/strategies_test.go | 57 +-
gremlin-go/driver/traversal.go | 30 -
gremlin-go/driver/traversal_test.go | 6 +-
gremlin-go/go.mod | 14 +
gremlin-go/go.sum | 28 +
35 files changed, 3292 insertions(+), 2215 deletions(-)
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 02a8fb2144..3ebdd97fe8 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -30,6 +30,9 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
* Removed redundant Gremlin Server integration testing: `org.apache.tinkerpop.gremlin.process.*` and related infrastructure.
* Removed obsolete `gremlin-groovy` translation testing: `ParameterizedGroovyTranslatorProvider` and related infrastructure.
* Replace `Bytecode` with `GremlinLang` & update serialization to GraphBinary 4 for `gremlin-go`
+* Added `RequestInterceptor` to `gremlin-go` with `auth` reference implementations to replace `authInfo`
+* Refactored GraphBinary serializers to use `io.Writer` and `io.Reader` instead of `*bytes.Buffer` for streaming capacities
+* Refactored `httpProtocol` and `httpTransport` in `gremlin-go` into single `connection.go` that handles HTTP request and response
* Bumped SLF4j to 2.0.16.
* Bumped GMavenPlus to 4.1.1.
* Bumped to Guice 6.0.0.
diff --git a/gremlin-go/design.md b/gremlin-go/design.md
index 7e12d1673d..758e246062 100644
--- a/gremlin-go/design.md
+++ b/gremlin-go/design.md
@@ -37,7 +37,7 @@ A `Client` represents the entry point to interaction with a Gremlin-supported se
##### Cardinalities
-* One `gremlinClient`
+* One `connection`
##### Lifecycle and States
@@ -47,11 +47,11 @@ A `Client` represents the entry point to interaction with a Gremlin-supported se
```mermaid
classDiagram
class Client
- Client: gremlinClient gremlinClient
+ Client: conn *connection
Client: NewClient(host, configurations) Client
Client: Close()
Client: Submit(traversal) ResultSet
- Client: submit(bytecode) ResultSet
+ Client: SubmitWithOptions(traversalString, options) ResultSet
```
```mermaid
@@ -64,44 +64,38 @@ sequenceDiagram
```
-#### gremlinClient
+#### connection
-The `gremlinClient` entity handles invoking serialization and deserialization of data, as well as handling the lifecycle of raw data passed to and received from the `httpTransporter` layer. Upon sending a request, an instance of `gremlinClient` starts a `goroutine` to asynchronously read and populate data into a `ResultSet`.
+The `connection` entity is the unified HTTP communication layer that handles serialization, HTTP request/response, and streaming deserialization. It manages the lifecycle of requests sent to and responses received from a Gremlin Server. Upon sending a request, the `connection` starts a `goroutine` to asynchronously stream and populate data into a `ResultSet`.
##### Cardinalities
-* One `transporter`
* One `serializer`
* One `http.Client`
##### Lifecycle and States
-* States
- * `closed bool`
-* When `close()` is invoked, set the `closed` boolean to `true` which will terminate the `goroutine` used for asynchronously reading, and invoke `close()` on the `transporter`.
+* When `close()` is invoked, idle HTTP connections are closed.
```mermaid
classDiagram
- class gremlinClient
- gremlinClient: httpClient *http.Client
- gremlinClient: serializer *serializer
- gremlinClient: connSettings *connectionSettings
- gremlinClient: close()
- gremlinClient: send(request)
- gremlinClient: receive(rs ResultSet, msg []byte)
+ class connection
+ connection: httpClient *http.Client
+ connection: serializer *GraphBinarySerializer
+ connection: connSettings *connectionSettings
+ connection: close()
+ connection: submit(request) ResultSet
```
```mermaid
sequenceDiagram
- client->>gremlinClient: send(request)
- gremlinClient-->>client: ResultSet
- gremlinClient->>serializer: serializeMessage(request)
- serializer-->>gremlinClient: bytes
- gremlinClient->>httpTransporter: Write(bytes)
- gremlinClient->>httpTransporter: Read()
- httpTransporter-->>gremlinClient: bytes
- gremlinClient->>serializer: deserializeMessage(response)
- gremlinClient-->>gremlinClient: population of ResultSet
+ Client->>connection: submit(request)
+ connection-->>Client: ResultSet
+ connection->>serializer: SerializeMessage(request)
+ serializer-->>connection: bytes
+ connection->>httpClient: Do(httpRequest)
+ httpClient-->>connection: httpResponse
+ connection->>connection: streamToResultSet (async)
```
@@ -125,10 +119,6 @@ classDiagram
serializer: deserializeMessage([]byte) response
```
-#### httpTransporter
-
-The `httpTransporter` is responsible for sending and receiving bytes to/from the server. It is intended for one `httpTransporter` to be instantiated per http request and response.
-
#### Result
A `Result` represents an individual output from a Gremlin traversal query. Its interface provides the ability to transform the output into Go data types for use.
diff --git a/gremlin-go/driver/auth.go b/gremlin-go/driver/auth.go
new file mode 100644
index 0000000000..70cff17a6c
--- /dev/null
+++ b/gremlin-go/driver/auth.go
@@ -0,0 +1,104 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package gremlingo
+
+import (
+ "context"
+ "encoding/base64"
+ "sync"
+ "time"
+
+ "github.com/aws/aws-sdk-go-v2/aws"
+ v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
+ "github.com/aws/aws-sdk-go-v2/config"
+)
+
+// BasicAuth returns a RequestInterceptor that adds Basic authentication header.
+func BasicAuth(username, password string) RequestInterceptor {
+ encoded := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
+ return func(req *HttpRequest) error {
+ req.Headers.Set(HeaderAuthorization, "Basic "+encoded)
+ return nil
+ }
+}
+
+// Sigv4Auth returns a RequestInterceptor that signs requests using AWS SigV4.
+// It uses the default AWS credential chain (env vars, shared config, IAM role, etc.)
+func Sigv4Auth(region, service string) RequestInterceptor {
+ return Sigv4AuthWithCredentials(region, service, nil)
+}
+
+// Sigv4AuthWithCredentials returns a RequestInterceptor that signs requests using AWS SigV4
+// with the provided credentials provider. If provider is nil, uses default credential chain.
+//
+// Caches the signer and credentials provider for efficiency.
+func Sigv4AuthWithCredentials(region, service string, credentialsProvider aws.CredentialsProvider) RequestInterceptor {
+ // Create signer once - it's stateless and safe to reuse
+ signer := v4.NewSigner()
+
+ // Cache for resolved credentials provider (lazy initialization)
+ var cachedProvider aws.CredentialsProvider
+ var providerOnce sync.Once
+ var providerErr error
+
+ return func(req *HttpRequest) error {
+ ctx := context.Background()
+
+ // Resolve credentials provider once if not provided
+ provider := credentialsProvider
+ if provider == nil {
+ providerOnce.Do(func() {
+ cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
+ if err != nil {
+ providerErr = err
+ return
+ }
+ cachedProvider = cfg.Credentials
+ })
+ if providerErr != nil {
+ return providerErr
+ }
+ provider = cachedProvider
+ }
+
+ // Retrieve credentials (the provider handles caching internally)
+ creds, err := provider.Retrieve(ctx)
+ if err != nil {
+ return err
+ }
+
+ stdReq, err := req.ToStdRequest()
+ if err != nil {
+ return err
+ }
+ stdReq.Body = nil // Body is handled separately via payload hash
+
+ if err := signer.SignHTTP(ctx, creds, stdReq, req.PayloadHash(), service, region, time.Now()); err != nil {
+ return err
+ }
+
+ // Copy signed headers back to HttpRequest
+ for k, v := range stdReq.Header {
+ req.Headers[k] = v
+ }
+
+ return nil
+ }
+}
diff --git a/gremlin-go/driver/authInfo.go b/gremlin-go/driver/authInfo.go
deleted file mode 100644
index 0671136470..0000000000
--- a/gremlin-go/driver/authInfo.go
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements. See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership. The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License. You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied. See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package gremlingo
-
-import "net/http"
-
-// AuthInfoProvider is an interface that allows authentication information to be specified.
-type AuthInfoProvider interface {
- GetHeader() http.Header
- GetBasicAuth() (ok bool, username, password string)
-}
-
-// AuthInfo is an option struct that allows authentication information to be specified statically.
-// Authentication can be provided via http.Header directly.
-// Basic authentication can also be used via the BasicAuthInfo function.
-type AuthInfo struct {
- Header http.Header
- Username string
- Password string
-}
-
-var _ AuthInfoProvider = (*AuthInfo)(nil)
-
-// GetHeader provides a safe way to get a header from the AuthInfo even if it is nil.
-// This way we don't need any additional logic in the transport layer.
-func (authInfo *AuthInfo) GetHeader() http.Header {
- if authInfo == nil {
- return nil
- } else {
- return authInfo.Header
- }
-}
-
-// GetBasicAuth provides a safe way to check if basic auth info is available from the AuthInfo even if it is nil.
-// This way we don't need any additional logic in the transport layer.
-func (authInfo *AuthInfo) GetBasicAuth() (bool, string, string) {
- if authInfo == nil || (authInfo.Username == "" && authInfo.Password == "") {
- return false, "", ""
- }
- return true, authInfo.Username, authInfo.Password
-}
-
-// BasicAuthInfo provides a way to generate AuthInfo. Enter username and password and get the AuthInfo back.
-func BasicAuthInfo(username string, password string) *AuthInfo {
- return &AuthInfo{Username: username, Password: password}
-}
-
-// HeaderAuthInfo provides a way to generate AuthInfo with only Header information.
-func HeaderAuthInfo(header http.Header) *AuthInfo {
- return &AuthInfo{Header: header}
-}
-
-// DynamicAuth is an AuthInfoProvider that allows dynamic credential generation.
-type DynamicAuth struct {
- fn func() AuthInfoProvider
-}
-
-var (
- _ AuthInfoProvider = (*DynamicAuth)(nil)
-
- // NoopAuthInfo is a no-op AuthInfoProvider that can be used to disable authentication.
- NoopAuthInfo = NewDynamicAuth(func() AuthInfoProvider { return &AuthInfo{} })
-)
-
-// NewDynamicAuth provides a way to generate dynamic credentials with the specified generator function.
-func NewDynamicAuth(f func() AuthInfoProvider) *DynamicAuth {
- return &DynamicAuth{fn: f}
-}
-
-// GetHeader calls the stored function to get the header dynamically.
-func (d *DynamicAuth) GetHeader() http.Header {
- return d.fn().GetHeader()
-}
-
-// GetBasicAuth calls the stored function to get basic authentication dynamically.
-func (d *DynamicAuth) GetBasicAuth() (bool, string, string) {
- return d.fn().GetBasicAuth()
-}
diff --git a/gremlin-go/driver/auth_test.go b/gremlin-go/driver/auth_test.go
new file mode 100644
index 0000000000..c0cce3ac9a
--- /dev/null
+++ b/gremlin-go/driver/auth_test.go
@@ -0,0 +1,114 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package gremlingo
+
+import (
+ "context"
+ "encoding/base64"
+ "strings"
+ "testing"
+
+ "github.com/aws/aws-sdk-go-v2/aws"
+ "github.com/stretchr/testify/assert"
+)
+
+func createMockRequest() *HttpRequest {
+ req, _ := NewHttpRequest("POST", "https://localhost:8182/gremlin")
+ req.Headers.Set("Content-Type", graphBinaryMimeType)
+ req.Headers.Set("Accept", graphBinaryMimeType)
+ req.Body = []byte(`{"gremlin":"g.V()"}`)
+ return req
+}
+
+func TestBasicAuth(t *testing.T) {
+ t.Run("adds authorization header", func(t *testing.T) {
+ req := createMockRequest()
+ assert.Empty(t, req.Headers.Get(HeaderAuthorization))
+
+ interceptor := BasicAuth("username", "password")
+ err := interceptor(req)
+
+ assert.NoError(t, err)
+ authHeader := req.Headers.Get(HeaderAuthorization)
+ assert.True(t, strings.HasPrefix(authHeader, "Basic "))
+
+ // Verify encoding
+ encoded := strings.TrimPrefix(authHeader, "Basic ")
+ decoded, err := base64.StdEncoding.DecodeString(encoded)
+ assert.NoError(t, err)
+ assert.Equal(t, "username:password", string(decoded))
+ })
+}
+
+// mockCredentialsProvider implements aws.CredentialsProvider for testing
+type mockCredentialsProvider struct {
+ accessKey string
+ secretKey string
+ sessionToken string
+}
+
+func (m *mockCredentialsProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
+ return aws.Credentials{
+ AccessKeyID: m.accessKey,
+ SecretAccessKey: m.secretKey,
+ SessionToken: m.sessionToken,
+ }, nil
+}
+
+func TestSigv4Auth(t *testing.T) {
+ t.Run("adds signed headers", func(t *testing.T) {
+ req := createMockRequest()
+ assert.Empty(t, req.Headers.Get("Authorization"))
+ assert.Empty(t, req.Headers.Get("X-Amz-Date"))
+
+ provider := &mockCredentialsProvider{
+ accessKey: "MOCK_ACCESS_KEY",
+ secretKey: "MOCK_SECRET_KEY",
+ }
+ interceptor := Sigv4AuthWithCredentials("us-west-2", "neptune-db", provider)
+ err := interceptor(req)
+
+ assert.NoError(t, err)
+ assert.NotEmpty(t, req.Headers.Get("X-Amz-Date"))
+ authHeader := req.Headers.Get("Authorization")
+ assert.True(t, strings.HasPrefix(authHeader, "AWS4-HMAC-SHA256 Credential=MOCK_ACCESS_KEY"))
+ assert.Contains(t, authHeader, "us-west-2/neptune-db/aws4_request")
+ assert.Contains(t, authHeader, "Signature=")
+ })
+
+ t.Run("adds session token when provided", func(t *testing.T) {
+ req := createMockRequest()
+ assert.Empty(t, req.Headers.Get("X-Amz-Security-Token"))
+
+ provider := &mockCredentialsProvider{
+ accessKey: "MOCK_ACCESS_KEY",
+ secretKey: "MOCK_SECRET_KEY",
+ sessionToken: "MOCK_SESSION_TOKEN",
+ }
+ interceptor := Sigv4AuthWithCredentials("us-west-2", "neptune-db", provider)
+ err := interceptor(req)
+
+ assert.NoError(t, err)
+ assert.Equal(t, "MOCK_SESSION_TOKEN", req.Headers.Get("X-Amz-Security-Token"))
+ authHeader := req.Headers.Get("Authorization")
+ assert.True(t, strings.HasPrefix(authHeader, "AWS4-HMAC-SHA256 Credential="))
+ assert.Contains(t, authHeader, "Signature=")
+ })
+}
diff --git a/gremlin-go/driver/client.go b/gremlin-go/driver/client.go
index ecacb54c5d..65086cfcd6 100644
--- a/gremlin-go/driver/client.go
+++ b/gremlin-go/driver/client.go
@@ -22,40 +22,47 @@ package gremlingo
import (
"crypto/tls"
"reflect"
- "runtime"
"time"
"golang.org/x/text/language"
)
-const keepAliveIntervalDefault = 5 * time.Second
-const writeDeadlineDefault = 3 * time.Second
const connectionTimeoutDefault = 5 * time.Second
-// ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. The default is 1MB.
-// If a buffer size is set zero, then the transporter default size is used. The I/O buffer
-// sizes do not limit the size of the messages that can be sent or received.
-const readBufferSizeDefault = 1048576
-const writeBufferSizeDefault = 1048576
-
// ClientSettings is used to modify a Client's settings on initialization.
type ClientSettings struct {
TraversalSource string
LogVerbosity LogVerbosity
Logger Logger
Language language.Tag
- AuthInfo AuthInfoProvider
TlsConfig *tls.Config
- KeepAliveInterval time.Duration
- WriteDeadline time.Duration
ConnectionTimeout time.Duration
EnableCompression bool
- ReadBufferSize int
- WriteBufferSize int
- // Maximum number of concurrent connections. Default: number of runtime processors
+ // MaximumConcurrentConnections is the maximum number of concurrent TCP connections
+ // to the Gremlin server. This limits how many requests can be in-flight simultaneously.
+ // Default: 128. Set to 0 to use the default.
MaximumConcurrentConnections int
- EnableUserAgentOnConnect bool
+
+ // MaxIdleConnections is the maximum number of idle (keep-alive) connections to retain
+ // in the connection pool. Idle connections are reused for subsequent requests.
+ // Default: 8. Set to 0 to use the default.
+ MaxIdleConnections int
+
+ // IdleConnectionTimeout is how long an idle connection remains in the pool before
+ // being closed. Set this to match your server's idle timeout if needed.
+ // Default: 180 seconds (3 minutes). Set to 0 to use the default.
+ IdleConnectionTimeout time.Duration
+
+ // KeepAliveInterval is the interval between TCP keep-alive probes on idle connections.
+ // This helps detect dead connections and keeps connections alive through firewalls.
+ // Default: 30 seconds. Set to 0 to use the default.
+ KeepAliveInterval time.Duration
+
+ EnableUserAgentOnConnect bool
+
+ // RequestInterceptors are functions that modify HTTP requests before sending.
+ RequestInterceptors []RequestInterceptor
}
// Client is used to connect and interact with a Gremlin-supported server.
@@ -64,7 +71,7 @@ type Client struct {
traversalSource string
logHandler *logHandler
connectionSettings *connectionSettings
- httpProtocol *httpProtocol
+ conn *connection
}
// NewClient creates a Client and configures it with the given parameters.
@@ -75,44 +82,46 @@ func NewClient(url string, configurations ...func(settings *ClientSettings)) (*C
LogVerbosity: Info,
Logger: &defaultLogger{},
Language: language.English,
- AuthInfo: &AuthInfo{},
TlsConfig: &tls.Config{},
- KeepAliveInterval: keepAliveIntervalDefault,
- WriteDeadline: writeDeadlineDefault,
ConnectionTimeout: connectionTimeoutDefault,
EnableCompression: false,
EnableUserAgentOnConnect: true,
- ReadBufferSize: readBufferSizeDefault,
- WriteBufferSize: writeBufferSizeDefault,
- MaximumConcurrentConnections: runtime.NumCPU(),
+ MaximumConcurrentConnections: 0, // Use default (128)
+ MaxIdleConnections: 0, // Use default (8)
+ IdleConnectionTimeout: 0, // Use default (180s)
+ KeepAliveInterval: 0, // Use default (30s)
}
for _, configuration := range configurations {
configuration(settings)
}
connSettings := &connectionSettings{
- authInfo: settings.AuthInfo,
tlsConfig: settings.TlsConfig,
- keepAliveInterval: settings.KeepAliveInterval,
- writeDeadline: settings.WriteDeadline,
connectionTimeout: settings.ConnectionTimeout,
+ maxConnsPerHost: settings.MaximumConcurrentConnections,
+ maxIdleConnsPerHost: settings.MaxIdleConnections,
+ idleConnTimeout: settings.IdleConnectionTimeout,
+ keepAliveInterval: settings.KeepAliveInterval,
enableCompression: settings.EnableCompression,
- readBufferSize: settings.ReadBufferSize,
- writeBufferSize: settings.WriteBufferSize,
enableUserAgentOnConnect: settings.EnableUserAgentOnConnect,
}
logHandler := newLogHandler(settings.Logger, settings.LogVerbosity, settings.Language)
- httpProt := newHttpProtocol(logHandler, url, connSettings)
+ conn := newConnection(logHandler, url, connSettings)
+
+ // Add user-provided interceptors
+ for _, interceptor := range settings.RequestInterceptors {
+ conn.AddInterceptor(interceptor)
+ }
client := &Client{
url: url,
traversalSource: settings.TraversalSource,
logHandler: logHandler,
connectionSettings: connSettings,
- httpProtocol: httpProt,
+ conn: conn,
}
return client, nil
@@ -121,8 +130,10 @@ func NewClient(url string, configurations ...func(settings *ClientSettings)) (*C
// Close closes the client via connection.
// This is idempotent due to the underlying close() methods being idempotent as well.
func (client *Client) Close() {
- // TODO check what needs to be closed
client.logHandler.logf(Info, closeClient, client.url)
+ if client.conn != nil {
+ client.conn.close()
+ }
}
func (client *Client) errorCallback() {
@@ -133,10 +144,7 @@ func (client *Client) errorCallback() {
func (client *Client) SubmitWithOptions(traversalString string, requestOptions RequestOptions) (ResultSet, error) {
client.logHandler.logf(Debug, submitStartedString, traversalString)
request := MakeStringRequest(traversalString, client.traversalSource, requestOptions)
-
- // TODO interceptors (ie. auth)
-
- rs, err := client.httpProtocol.send(&request)
+ rs, err := client.conn.submit(&request)
return rs, err
}
@@ -152,10 +160,8 @@ func (client *Client) Submit(traversalString string, bindings ...map[string]inte
}
// submitGremlinLang submits GremlinLang to the server to execute and returns a ResultSet.
-// TODO test and update when connection is set up
func (client *Client) submitGremlinLang(gremlinLang *GremlinLang) (ResultSet, error) {
client.logHandler.logf(Debug, submitStartedString, *gremlinLang)
- // TODO placeholder
requestOptionsBuilder := new(RequestOptionsBuilder)
if len(gremlinLang.GetParameters()) > 0 {
requestOptionsBuilder.SetBindings(gremlinLang.GetParameters())
@@ -165,7 +171,7 @@ func (client *Client) submitGremlinLang(gremlinLang *GremlinLang) (ResultSet, er
}
request := MakeStringRequest(gremlinLang.GetGremlin(), client.traversalSource, requestOptionsBuilder.Create())
- return client.httpProtocol.send(&request)
+ return client.conn.submit(&request)
}
func applyOptionsConfig(builder *RequestOptionsBuilder, config map[string]interface{}) *RequestOptionsBuilder {
diff --git a/gremlin-go/driver/client_test.go b/gremlin-go/driver/client_test.go
index 9e223dd193..fdfd1204da 100644
--- a/gremlin-go/driver/client_test.go
+++ b/gremlin-go/driver/client_test.go
@@ -30,7 +30,6 @@ func TestClient(t *testing.T) {
// Integration test variables.
testNoAuthUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
testNoAuthEnable := getEnvOrDefaultBool("RUN_INTEGRATION_TESTS", true)
- testNoAuthAuthInfo := &AuthInfo{}
testNoAuthTlsConfig := &tls.Config{}
t.Run("Test client.SubmitWithOptions()", func(t *testing.T) {
@@ -38,7 +37,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
})
assert.NoError(t, err)
assert.NotNil(t, client)
@@ -58,7 +56,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
})
assert.NoError(t, err)
assert.NotNil(t, client)
@@ -74,7 +71,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
settings.TraversalSource = testServerModernGraphAlias
})
assert.NoError(t, err)
@@ -97,7 +93,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
settings.TraversalSource = testServerModernGraphAlias
})
assert.NoError(t, err)
@@ -122,7 +117,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
settings.TraversalSource = testServerModernGraphAlias
})
assert.NoError(t, err)
@@ -147,7 +141,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
settings.TraversalSource = testServerModernGraphAlias
})
@@ -170,7 +163,6 @@ func TestClient(t *testing.T) {
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
settings.TraversalSource = testServerCrewGraphAlias
})
diff --git a/gremlin-go/driver/connection.go b/gremlin-go/driver/connection.go
index 9dbb1ec8ea..6c2daf7410 100644
--- a/gremlin-go/driver/connection.go
+++ b/gremlin-go/driver/connection.go
@@ -20,18 +20,294 @@ under the License.
package gremlingo
import (
+ "bytes"
+ "compress/zlib"
+ "crypto/sha256"
"crypto/tls"
+ "encoding/hex"
+ "io"
+ "net"
+ "net/http"
+ "net/url"
"time"
)
+// Common HTTP header keys
+const (
+ HeaderContentType = "Content-Type"
+ HeaderAccept = "Accept"
+ HeaderUserAgent = "User-Agent"
+ HeaderAcceptEncoding = "Accept-Encoding"
+ HeaderAuthorization = "Authorization"
+)
+
+// HttpRequest represents an HTTP request that can be modified by interceptors.
+type HttpRequest struct {
+ Method string
+ URL *url.URL
+ Headers http.Header
+ Body []byte
+}
+
+// NewHttpRequest creates a new HttpRequest with the given method and URL.
+func NewHttpRequest(method, rawURL string) (*HttpRequest, error) {
+ u, err := url.Parse(rawURL)
+ if err != nil {
+ return nil, err
+ }
+ return &HttpRequest{
+ Method: method,
+ URL: u,
+ Headers: make(http.Header),
+ }, nil
+}
+
+// ToStdRequest converts HttpRequest to a standard http.Request for signing.
+// Returns nil if the request cannot be created (invalid method or URL).
+func (r *HttpRequest) ToStdRequest() (*http.Request, error) {
+ req, err := http.NewRequest(r.Method, r.URL.String(), bytes.NewReader(r.Body))
+ if err != nil {
+ return nil, err
+ }
+ req.Header = r.Headers
+ return req, nil
+}
+
+// PayloadHash returns the SHA256 hash of the request body for SigV4 signing.
+func (r *HttpRequest) PayloadHash() string {
+ if len(r.Body) == 0 {
+ return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // SHA256 of empty string
+ }
+ h := sha256.Sum256(r.Body)
+ return hex.EncodeToString(h[:])
+}
+
+// RequestInterceptor is a function that modifies an HTTP request before it is sent.
+type RequestInterceptor func(*HttpRequest) error
+
+// connectionSettings holds configuration for the connection.
type connectionSettings struct {
- authInfo AuthInfoProvider
tlsConfig *tls.Config
- keepAliveInterval time.Duration
- writeDeadline time.Duration
connectionTimeout time.Duration
+ maxConnsPerHost int
+ maxIdleConnsPerHost int
+ idleConnTimeout time.Duration
+ keepAliveInterval time.Duration
enableCompression bool
- readBufferSize int
- writeBufferSize int
enableUserAgentOnConnect bool
}
+
+// connection handles HTTP request/response for Gremlin queries.
+// This is the transport layer for communicating with a Gremlin server.
+type connection struct {
+ url string
+ httpClient *http.Client
+ connSettings *connectionSettings
+ logHandler *logHandler
+ serializer *GraphBinarySerializer
+ interceptors []RequestInterceptor
+}
+
+// Connection pool defaults aligned with Java driver
+const (
+ defaultMaxConnsPerHost = 128 // Java: ConnectionPool.MAX_POOL_SIZE
+ defaultMaxIdleConnsPerHost = 8 // Keep some connections warm
+ defaultIdleConnTimeout = 180 * time.Second // Java: CONNECTION_IDLE_TIMEOUT_MILLIS
+ defaultConnectionTimeout = 15 * time.Second // Java: CONNECTION_SETUP_TIMEOUT_MILLIS
+ defaultKeepAliveInterval = 30 * time.Second // TCP keep-alive probe interval
+)
+
+func newConnection(handler *logHandler, url string, connSettings *connectionSettings) *connection {
+ // Apply defaults for zero values
+ connectionTimeout := connSettings.connectionTimeout
+ if connectionTimeout == 0 {
+ connectionTimeout = defaultConnectionTimeout
+ }
+
+ maxConnsPerHost := connSettings.maxConnsPerHost
+ if maxConnsPerHost == 0 {
+ maxConnsPerHost = defaultMaxConnsPerHost
+ }
+
+ maxIdleConnsPerHost := connSettings.maxIdleConnsPerHost
+ if maxIdleConnsPerHost == 0 {
+ maxIdleConnsPerHost = defaultMaxIdleConnsPerHost
+ }
+
+ idleConnTimeout := connSettings.idleConnTimeout
+ if idleConnTimeout == 0 {
+ idleConnTimeout = defaultIdleConnTimeout
+ }
+
+ keepAliveInterval := connSettings.keepAliveInterval
+ if keepAliveInterval == 0 {
+ keepAliveInterval = defaultKeepAliveInterval
+ }
+
+ transport := &http.Transport{
+ DialContext: (&net.Dialer{
+ Timeout: connectionTimeout,
+ KeepAlive: keepAliveInterval,
+ }).DialContext,
+ TLSClientConfig: connSettings.tlsConfig,
+ MaxConnsPerHost: maxConnsPerHost,
+ MaxIdleConnsPerHost: maxIdleConnsPerHost,
+ IdleConnTimeout: idleConnTimeout,
+ DisableCompression: !connSettings.enableCompression,
+ }
+
+ return &connection{
+ url: url,
+ httpClient: &http.Client{Transport: transport}, // No Timeout - allows streaming
+ connSettings: connSettings,
+ logHandler: handler,
+ serializer: newGraphBinarySerializer(handler),
+ }
+}
+
+// AddInterceptor adds a request interceptor to the chain.
+func (c *connection) AddInterceptor(interceptor RequestInterceptor) {
+ c.interceptors = append(c.interceptors, interceptor)
+}
+
+// submit sends request and streams results directly to ResultSet
+func (c *connection) submit(req *request) (ResultSet, error) {
+ rs := newChannelResultSet()
+
+ data, err := c.serializer.SerializeMessage(req)
+ if err != nil {
+ rs.Close()
+ return rs, err
+ }
+
+ go c.executeAndStream(data, rs)
+
+ return rs, nil
+}
+
+func (c *connection) executeAndStream(data []byte, rs ResultSet) {
+ defer rs.Close()
+
+ // Create HttpRequest for interceptors
+ httpReq, err := NewHttpRequest(http.MethodPost, c.url)
+ if err != nil {
+ c.logHandler.logf(Error, failedToSendRequest, err.Error())
+ rs.setError(err)
+ return
+ }
+ httpReq.Body = data
+
+ // Set default headers before interceptors
+ c.setHttpRequestHeaders(httpReq)
+
+ // Apply interceptors
+ for _, interceptor := range c.interceptors {
+ if err := interceptor(httpReq); err != nil {
+ c.logHandler.logf(Error, failedToSendRequest, err.Error())
+ rs.setError(err)
+ return
+ }
+ }
+
+ // Create actual http.Request from HttpRequest
+ req, err := http.NewRequest(httpReq.Method, httpReq.URL.String(), bytes.NewReader(httpReq.Body))
+ if err != nil {
+ c.logHandler.logf(Error, failedToSendRequest, err.Error())
+ rs.setError(err)
+ return
+ }
+ req.Header = httpReq.Headers
+
+ resp, err := c.httpClient.Do(req)
+ if err != nil {
+ c.logHandler.logf(Error, failedToSendRequest, err.Error())
+ rs.setError(err)
+ return
+ }
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ c.logHandler.logf(Debug, failedToCloseResponseBody, err.Error())
+ }
+ }()
+
+ reader, zlibReader, err := c.getReader(resp)
+ if err != nil {
+ c.logHandler.logf(Error, failedToReceiveResponse, err.Error())
+ rs.setError(err)
+ return
+ }
+ if zlibReader != nil {
+ defer func() {
+ if err := zlibReader.Close(); err != nil {
+ c.logHandler.logf(Debug, failedToCloseDecompReader, err.Error())
+ }
+ }()
+ }
+
+ c.streamToResultSet(reader, rs)
+}
+
+// setHttpRequestHeaders sets default headers on HttpRequest (for interceptors)
+func (c *connection) setHttpRequestHeaders(req *HttpRequest) {
+ req.Headers.Set(HeaderContentType, graphBinaryMimeType)
+ req.Headers.Set(HeaderAccept, graphBinaryMimeType)
+
+ if c.connSettings.enableUserAgentOnConnect {
+ req.Headers.Set(HeaderUserAgent, userAgent)
+ }
+ if c.connSettings.enableCompression {
+ req.Headers.Set(HeaderAcceptEncoding, "deflate")
+ }
+}
+
+func (c *connection) getReader(resp *http.Response) (io.Reader, io.Closer, error) {
+ if resp.Header.Get("Content-Encoding") == "deflate" {
+ zr, err := zlib.NewReader(resp.Body)
+ if err != nil {
+ return nil, nil, err
+ }
+ return zr, zr, nil
+ }
+ return resp.Body, nil, nil
+}
+
+func (c *connection) streamToResultSet(reader io.Reader, rs ResultSet) {
+ d := NewGraphBinaryDeserializer(reader)
+ if err := d.ReadHeader(); err != nil {
+ if err != io.EOF {
+ c.logHandler.logf(Error, failedToReceiveResponse, err.Error())
+ rs.setError(err)
+ }
+ return
+ }
+
+ for {
+ obj, err := d.ReadFullyQualified()
+ if err != nil {
+ if err != io.EOF {
+ c.logHandler.logf(Error, failedToReceiveResponse, err.Error())
+ rs.setError(err)
+ }
+ return
+ }
+
+ if marker, ok := obj.(Marker); ok && marker == EndOfStream() {
+ code, msg, _, err := d.ReadStatus()
+ if err != nil {
+ c.logHandler.logf(Error, failedToReceiveResponse, err.Error())
+ rs.setError(err)
+ return
+ }
+ if code != 200 && code != 0 {
+ rs.setError(newError(err0502ResponseReadLoopError, msg, code))
+ }
+ return
+ }
+
+ rs.Channel() <- &Result{obj}
+ }
+}
+
+func (c *connection) close() {
+ c.httpClient.CloseIdleConnections()
+}
diff --git a/gremlin-go/driver/connection_test.go b/gremlin-go/driver/connection_test.go
index d27e65d84c..23b52517c4 100644
--- a/gremlin-go/driver/connection_test.go
+++ b/gremlin-go/driver/connection_test.go
@@ -20,17 +20,24 @@ under the License.
package gremlingo
import (
+ "bytes"
"crypto/tls"
"fmt"
+ "io"
"math/big"
+ "net/http"
+ "net/http/httptest"
"os"
"reflect"
"sort"
"strconv"
"sync"
"testing"
+ "time"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "golang.org/x/text/language"
)
const personLabel = "Person"
@@ -55,15 +62,10 @@ var testNames = []string{"Lyndon", "Yang", "Simon", "Rithin", "Alexey", "Valenty
func newDefaultConnectionSettings() *connectionSettings {
return &connectionSettings{
- authInfo: &AuthInfo{},
tlsConfig: &tls.Config{},
- keepAliveInterval: keepAliveIntervalDefault,
- writeDeadline: writeDeadlineDefault,
connectionTimeout: connectionTimeoutDefault,
enableCompression: false,
enableUserAgentOnConnect: true,
- readBufferSize: readBufferSizeDefault,
- writeBufferSize: writeBufferSizeDefault,
}
}
@@ -90,11 +92,10 @@ func addTestData(t *testing.T, g *GraphTraversalSource) {
assert.Nil(t, <-promise)
}
-func getTestGraph(t *testing.T, url string, auth AuthInfoProvider, tls *tls.Config) *GraphTraversalSource {
+func getTestGraph(t *testing.T, url string, tls *tls.Config) *GraphTraversalSource {
remote, err := NewDriverRemoteConnection(url,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = tls
- settings.AuthInfo = auth
settings.TraversalSource = testServerGraphAlias
})
assert.Nil(t, err)
@@ -104,8 +105,8 @@ func getTestGraph(t *testing.T, url string, auth AuthInfoProvider, tls *tls.Conf
return g
}
-func initializeGraph(t *testing.T, url string, auth AuthInfoProvider, tls *tls.Config) *GraphTraversalSource {
- g := getTestGraph(t, url, auth, tls)
+func initializeGraph(t *testing.T, url string, tls *tls.Config) *GraphTraversalSource {
+ g := getTestGraph(t, url, tls)
// Drop the graph and check that it is empty.
dropGraph(t, g)
@@ -244,11 +245,6 @@ func getEnvOrDefaultBool(key string, defaultValue bool) bool {
return defaultValue
}
-func getBasicAuthInfo() *AuthInfo {
- return BasicAuthInfo(getEnvOrDefaultString("GREMLIN_GO_BASIC_AUTH_USERNAME", "stephen"),
- getEnvOrDefaultString("GREMLIN_GO_BASIC_AUTH_PASSWORD", "password"))
-}
-
func skipTestsIfNotEnabled(t *testing.T, testSuiteName string, testSuiteEnabled bool) {
if !testSuiteEnabled {
t.Skipf("Skipping %s because %s tests are not enabled.", t.Name(), testSuiteName)
@@ -259,29 +255,62 @@ func TestConnection(t *testing.T) {
// Integration test variables.
testNoAuthUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
testNoAuthEnable := getEnvOrDefaultBool("RUN_INTEGRATION_TESTS", true)
- testNoAuthAuthInfo := &AuthInfo{}
testNoAuthTlsConfig := &tls.Config{}
// No authentication integration test with graphs loaded and alias configured server
testNoAuthWithAliasUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
testNoAuthWithAliasEnable := getEnvOrDefaultBool("RUN_INTEGRATION_WITH_ALIAS_TESTS", true)
- testNoAuthWithAliasAuthInfo := &AuthInfo{}
testNoAuthWithAliasTlsConfig := &tls.Config{}
// Basic authentication integration test variables.
testBasicAuthUrl := getEnvOrDefaultString("GREMLIN_SERVER_BASIC_AUTH_URL", basicAuthWithSsl)
testBasicAuthEnable := getEnvOrDefaultBool("RUN_BASIC_AUTH_INTEGRATION_TESTS", false)
- testBasicAuthAuthInfo := getBasicAuthInfo()
+ testBasicAuthUsername := getEnvOrDefaultString("GREMLIN_GO_BASIC_AUTH_USERNAME", "stephen")
+ testBasicAuthPassword := getEnvOrDefaultString("GREMLIN_GO_BASIC_AUTH_PASSWORD", "password")
testBasicAuthTlsConfig := &tls.Config{InsecureSkipVerify: true}
+ // this test is used to test the ws->http POC changes via manual execution with a local TP 4.0 gremlin server running on 8182
+ t.Run("Test client.submit()", func(t *testing.T) {
+ skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
+
+ tlsConf := tls.Config{
+ InsecureSkipVerify: true,
+ }
+
+ client, err := NewClient(testNoAuthUrl,
+ //client, err := NewClient(noAuthSslUrl,
+ func(settings *ClientSettings) {
+ settings.TlsConfig = &tlsConf
+ settings.EnableCompression = true
+ settings.TraversalSource = testServerModernGraphAlias
+ })
+ assert.Nil(t, err)
+ assert.NotNil(t, client)
+ defer client.Close()
+
+ // synchronous
+ for i := 0; i < 5; i++ {
+ submitCount(i, client, t)
+ }
+
+ // async
+ var wg sync.WaitGroup
+ for i := 0; i < 5; i++ {
+ wg.Add(1)
+ go func(i int) {
+ defer wg.Done()
+ submitCount(i, client, t)
+ }(i)
+ }
+ wg.Wait()
+ })
+
t.Run("Test client.submit() with concurrency", func(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
client, err := NewClient(testNoAuthUrl,
func(settings *ClientSettings) {
settings.TlsConfig = testNoAuthTlsConfig
- settings.AuthInfo = testNoAuthAuthInfo
- settings.WriteBufferSize = 1024
settings.EnableCompression = true
settings.TraversalSource = testServerModernGraphAlias
})
@@ -310,7 +339,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
// Read test data out of the graph and check that it is correct.
@@ -325,7 +354,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
readWithNextAndHasNext(t, g)
@@ -336,7 +365,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
resultSet, err := g.V().HasLabel(personLabel).Properties(nameKey).GetResultSet()
@@ -360,7 +389,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := getTestGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := getTestGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
// Drop the graph.
@@ -405,7 +434,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
// Read test data out of the graph and check that it is correct.
@@ -421,7 +450,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
// Run traversal and test Next/HasNext calls
@@ -447,7 +476,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
readUsingAnonymousTraversal(t, g)
@@ -476,7 +505,9 @@ func TestConnection(t *testing.T) {
remote, err := NewDriverRemoteConnection(testBasicAuthUrl,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = testBasicAuthTlsConfig
- settings.AuthInfo = testBasicAuthAuthInfo
+ settings.RequestInterceptors = []RequestInterceptor{
+ BasicAuth(testBasicAuthUsername, testBasicAuthPassword),
+ }
})
assert.Nil(t, err)
assert.NotNil(t, remote)
@@ -502,7 +533,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
r, err := g.WithSack(1).V().Has("name", "Lyndon").Values("foo").Sack(Operator.Sum).Sack().ToList()
@@ -521,7 +552,7 @@ func TestConnection(t *testing.T) {
// skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
//
// // Initialize graph
- // g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ // g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
// defer g.remoteConnection.Close()
//
// r, err := g.V().Has("name", "Lyndon").Values("foo").Profile().ToList()
@@ -539,7 +570,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
prop := &BigDecimal{11, big.NewInt(int64(22))}
@@ -559,7 +590,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
defer g.remoteConnection.Close()
prop := &ByteBuffer{[]byte{byte(127), byte(255)}}
@@ -579,7 +610,6 @@ func TestConnection(t *testing.T) {
remote, err := NewDriverRemoteConnection(testNoAuthWithAliasUrl,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = testNoAuthWithAliasTlsConfig
- settings.AuthInfo = testNoAuthWithAliasAuthInfo
settings.TraversalSource = testServerModernGraphAlias
})
assert.Nil(t, err)
@@ -599,7 +629,7 @@ func TestConnection(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
// Initialize graph
- g := initializeGraph(t, testNoAuthUrl, testNoAuthAuthInfo, testNoAuthTlsConfig)
+ g := initializeGraph(t, testNoAuthUrl, testNoAuthTlsConfig)
// Drop the graph.
dropGraph(t, g)
@@ -607,8 +637,7 @@ func TestConnection(t *testing.T) {
// Add vertices and edges to graph.
rs, err := g.AddV("person").Property("id", T__.Unfold().Property().AddV()).ToList()
assert.Nil(t, rs)
- fmt.Println(err.Error())
- assert.True(t, isSameErrorCode(newError(err0502ResponseHandlerReadLoopError), err))
+ assert.True(t, isSameErrorCode(newError(err0502ResponseError), err))
rs, err = g.V().Count().ToList()
assert.NotNil(t, rs)
@@ -621,7 +650,7 @@ func TestConnection(t *testing.T) {
t.Run("Get all properties when materializeProperties is all", func(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
// vertex contains 2 properties, name and age
@@ -634,7 +663,7 @@ func TestConnection(t *testing.T) {
t.Run("Skip properties when materializeProperties is tokens", func(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
// vertex contains 2 properties, name and age
@@ -647,7 +676,7 @@ func TestConnection(t *testing.T) {
t.Run("Get all properties when no materializeProperties", func(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
r, err := g.V().Has("person", "name", "marko").Next()
@@ -659,7 +688,7 @@ func TestConnection(t *testing.T) {
t.Run("Test DriverRemoteConnection Traversal With materializeProperties in Modern Graph", func(t *testing.T) {
skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthEnable)
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
vertices, err := g.With("materializeProperties", MaterializeProperties.Tokens).V().ToList()
@@ -760,3 +789,348 @@ func submitCount(i int, client *Client, t *testing.T) {
assert.Equal(t, 6+i, c)
_, _ = fmt.Fprintf(os.Stdout, "Received result : %s\n", result)
}
+
+func TestStreamingResultDelivery(t *testing.T) {
+ testNoAuthWithAliasEnable := getEnvOrDefaultBool("RUN_INTEGRATION_WITH_ALIAS_TESTS", true)
+ skipTestsIfNotEnabled(t, integrationTestSuiteName, testNoAuthWithAliasEnable)
+ remote, err := NewDriverRemoteConnection(getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl),
+ func(settings *DriverRemoteConnectionSettings) {
+ settings.TlsConfig = &tls.Config{}
+ settings.TraversalSource = "ggrateful"
+ })
+ assert.Nil(t, err)
+ assert.NotNil(t, remote)
+ g := Traversal_().With(remote)
+ defer g.remoteConnection.Close()
+
+ t.Run("first result arrives before all results", func(t *testing.T) {
+ start := time.Now()
+ rs, err := g.V().Properties().GetResultSet()
+ assert.Nil(t, err)
+
+ // First result should arrive quickly
+ _, ok, err := rs.One()
+ firstResultTime := time.Since(start)
+ assert.Nil(t, err)
+ assert.True(t, ok)
+
+ // Drain remaining
+ _, err = rs.All()
+ assert.Nil(t, err)
+ totalTime := time.Since(start)
+
+ t.Logf("First result: %v, Total: %v, Ratio: %.2f%%",
+ firstResultTime, totalTime, float64(firstResultTime)/float64(totalTime)*100)
+ })
+
+ t.Run("results arrive incrementally", func(t *testing.T) {
+ rs, err := g.V().Properties().GetResultSet()
+ assert.Nil(t, err)
+
+ var timestamps []time.Time
+ start := time.Now()
+
+ for {
+ _, ok, err := rs.One()
+ assert.Nil(t, err)
+ if !ok {
+ break
+ }
+ timestamps = append(timestamps, time.Now())
+ }
+
+ if len(timestamps) < 2 {
+ t.Skip("need more results to test incremental delivery")
+ }
+
+ firstHalf := timestamps[len(timestamps)/2].Sub(start)
+ total := timestamps[len(timestamps)-1].Sub(start)
+ t.Logf("Half results at: %v, All results at: %v", firstHalf, total)
+ })
+}
+
+// Unit tests for connection (moved from httpConnection_test.go)
+
+func newTestLogHandler() *logHandler {
+ return newLogHandler(&defaultLogger{}, Warning, language.English)
+}
+
+func TestNewConnection(t *testing.T) {
+ t.Run("creates connection with default settings", func(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost:8182/gremlin", &connectionSettings{})
+
+ assert.NotNil(t, conn.httpClient)
+ assert.NotNil(t, conn.httpClient.Transport)
+ })
+
+ t.Run("applies TLS config", func(t *testing.T) {
+ tlsConfig := &tls.Config{InsecureSkipVerify: true}
+ conn := newConnection(newTestLogHandler(), "https://localhost:8182/gremlin", &connectionSettings{
+ tlsConfig: tlsConfig,
+ })
+
+ transport := conn.httpClient.Transport.(*http.Transport)
+ assert.Equal(t, tlsConfig, transport.TLSClientConfig)
+ })
+}
+
+func TestSetHttpRequestHeaders(t *testing.T) {
+ t.Run("sets content type and accept headers", func(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost/gremlin", &connectionSettings{})
+ req, _ := NewHttpRequest(http.MethodPost, "http://localhost/gremlin")
+
+ conn.setHttpRequestHeaders(req)
+
+ assert.Equal(t, graphBinaryMimeType, req.Headers.Get("Content-Type"))
+ assert.Equal(t, graphBinaryMimeType, req.Headers.Get("Accept"))
+ })
+
+ t.Run("sets user agent when enabled", func(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost/gremlin", &connectionSettings{
+ enableUserAgentOnConnect: true,
+ })
+ req, _ := NewHttpRequest(http.MethodPost, "http://localhost/gremlin")
+
+ conn.setHttpRequestHeaders(req)
+
+ assert.NotEmpty(t, req.Headers.Get(HeaderUserAgent))
+ })
+
+ t.Run("sets compression header when enabled", func(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost/gremlin", &connectionSettings{
+ enableCompression: true,
+ })
+ req, _ := NewHttpRequest(http.MethodPost, "http://localhost/gremlin")
+
+ conn.setHttpRequestHeaders(req)
+
+ assert.Equal(t, "deflate", req.Headers.Get("Accept-Encoding"))
+ })
+}
+
+func TestGetReader(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost/gremlin", &connectionSettings{})
+
+ t.Run("returns body for non-compressed response", func(t *testing.T) {
+ resp := &http.Response{
+ Header: http.Header{},
+ Body: http.NoBody,
+ }
+
+ reader, closer, err := conn.getReader(resp)
+
+ assert.NoError(t, err)
+ assert.Nil(t, closer)
+ assert.Equal(t, resp.Body, reader)
+ })
+
+ t.Run("returns zlib reader for deflate response", func(t *testing.T) {
+ // Valid zlib compressed empty data
+ zlibData := []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01}
+ resp := &http.Response{
+ Header: http.Header{"Content-Encoding": []string{"deflate"}},
+ Body: io.NopCloser(bytes.NewReader(zlibData)),
+ }
+
+ reader, closer, err := conn.getReader(resp)
+
+ assert.NoError(t, err)
+ assert.NotNil(t, closer)
+ assert.NotNil(t, reader)
+ require.NoError(t, closer.Close())
+ })
+
+ t.Run("returns error for invalid zlib data", func(t *testing.T) {
+ resp := &http.Response{
+ Header: http.Header{"Content-Encoding": []string{"deflate"}},
+ Body: io.NopCloser(bytes.NewReader([]byte{0x00, 0x00})),
+ }
+
+ _, _, err := conn.getReader(resp)
+
+ assert.Error(t, err)
+ })
+}
+
+func TestConnectionWithMockServer(t *testing.T) {
+ t.Run("handles connection error", func(t *testing.T) {
+ conn := newConnection(newTestLogHandler(), "http://localhost:99999/gremlin", &connectionSettings{
+ connectionTimeout: 100 * time.Millisecond,
+ })
+
+ rs, err := conn.submit(&request{gremlin: "g.V()", fields: map[string]interface{}{}})
+ assert.NoError(t, err) // submit returns nil, error goes to ResultSet
+
+ // All() blocks until stream closes, then we can check error
+ _, _ = rs.All()
+ assert.Error(t, rs.GetError())
+ })
+
+ t.Run("receives headers from request", func(t *testing.T) {
+ headersCh := make(chan http.Header, 1)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ headersCh <- r.Header.Clone()
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer server.Close()
+
+ conn := newConnection(newTestLogHandler(), server.URL, &connectionSettings{
+ enableUserAgentOnConnect: true,
+ enableCompression: true,
+ })
+
+ rs, err := conn.submit(&request{gremlin: "g.V()", fields: map[string]interface{}{}})
+ require.NoError(t, err)
+
+ select {
+ case receivedHeaders := <-headersCh:
+ assert.Equal(t, graphBinaryMimeType, receivedHeaders.Get("Content-Type"))
+ assert.Equal(t, "deflate", receivedHeaders.Get("Accept-Encoding"))
+ assert.NotEmpty(t, receivedHeaders.Get(userAgentHeader))
+ case <-time.After(time.Second):
+ t.Fatal("timeout waiting for request")
+ }
+
+ _, _ = rs.All() // drain
+ })
+}
+
+// Tests for connection pool configuration settings
+
+func TestConnectionPoolSettings(t *testing.T) {
+ t.Run("default values are applied when settings are 0", func(t *testing.T) {
+ // Create connection with empty settings (all zeros)
+ conn := newConnection(newTestLogHandler(), "http://localhost:8182/gremlin", &connectionSettings{})
+
+ transport := conn.httpClient.Transport.(*http.Transport)
+
+ // Verify default values are applied
+ assert.Equal(t, 128, transport.MaxConnsPerHost, "MaxConnsPerHost should default to 128")
+ assert.Equal(t, 8, transport.MaxIdleConnsPerHost, "MaxIdleConnsPerHost should default to 8")
+ assert.Equal(t, 180*time.Second, transport.IdleConnTimeout, "IdleConnTimeout should default to 180s")
+ })
+
+ t.Run("custom values are passed through to http.Transport", func(t *testing.T) {
+ customSettings := &connectionSettings{
+ maxConnsPerHost: 256,
+ maxIdleConnsPerHost: 16,
+ idleConnTimeout: 300 * time.Second,
+ keepAliveInterval: 60 * time.Second,
+ connectionTimeout: 30 * time.Second,
+ }
+
+ conn := newConnection(newTestLogHandler(), "http://localhost:8182/gremlin", customSettings)
+
+ transport := conn.httpClient.Transport.(*http.Transport)
+
+ // Verify custom values are applied
+ assert.Equal(t, 256, transport.MaxConnsPerHost, "MaxConnsPerHost should be custom value")
+ assert.Equal(t, 16, transport.MaxIdleConnsPerHost, "MaxIdleConnsPerHost should be custom value")
+ assert.Equal(t, 300*time.Second, transport.IdleConnTimeout, "IdleConnTimeout should be custom value")
+ })
+
+ t.Run("partial custom settings use defaults for unset values", func(t *testing.T) {
+ // Only set maxConnsPerHost, leave others at 0
+ customSettings := &connectionSettings{
+ maxConnsPerHost: 64,
+ }
+
+ conn := newConnection(newTestLogHandler(), "http://localhost:8182/gremlin", customSettings)
+
+ transport := conn.httpClient.Transport.(*http.Transport)
+
+ // Verify custom value is applied
+ assert.Equal(t, 64, transport.MaxConnsPerHost, "MaxConnsPerHost should be custom value")
+ // Verify defaults are used for unset values
+ assert.Equal(t, 8, transport.MaxIdleConnsPerHost, "MaxIdleConnsPerHost should default to 8")
+ assert.Equal(t, 180*time.Second, transport.IdleConnTimeout, "IdleConnTimeout should default to 180s")
+ })
+}
+
+func TestClientSettingsWiring(t *testing.T) {
+ t.Run("ClientSettings wires connection pool settings", func(t *testing.T) {
+ client, err := NewClient("http://localhost:8182/gremlin",
+ func(settings *ClientSettings) {
+ settings.MaximumConcurrentConnections = 200
+ settings.MaxIdleConnections = 20
+ settings.IdleConnectionTimeout = 240 * time.Second
+ settings.KeepAliveInterval = 45 * time.Second
+ })
+ require.NoError(t, err)
+ defer client.Close()
+
+ // Verify settings were wired to connectionSettings
+ assert.Equal(t, 200, client.connectionSettings.maxConnsPerHost)
+ assert.Equal(t, 20, client.connectionSettings.maxIdleConnsPerHost)
+ assert.Equal(t, 240*time.Second, client.connectionSettings.idleConnTimeout)
+ assert.Equal(t, 45*time.Second, client.connectionSettings.keepAliveInterval)
+
+ // Verify settings were applied to http.Transport
+ transport := client.conn.httpClient.Transport.(*http.Transport)
+ assert.Equal(t, 200, transport.MaxConnsPerHost)
+ assert.Equal(t, 20, transport.MaxIdleConnsPerHost)
+ assert.Equal(t, 240*time.Second, transport.IdleConnTimeout)
+ })
+
+ t.Run("ClientSettings uses defaults when not configured", func(t *testing.T) {
+ client, err := NewClient("http://localhost:8182/gremlin")
+ require.NoError(t, err)
+ defer client.Close()
+
+ // Verify defaults are used (0 in settings means use default)
+ assert.Equal(t, 0, client.connectionSettings.maxConnsPerHost)
+ assert.Equal(t, 0, client.connectionSettings.maxIdleConnsPerHost)
+ assert.Equal(t, time.Duration(0), client.connectionSettings.idleConnTimeout)
+ assert.Equal(t, time.Duration(0), client.connectionSettings.keepAliveInterval)
+
+ // Verify defaults were applied to http.Transport
+ transport := client.conn.httpClient.Transport.(*http.Transport)
+ assert.Equal(t, 128, transport.MaxConnsPerHost)
+ assert.Equal(t, 8, transport.MaxIdleConnsPerHost)
+ assert.Equal(t, 180*time.Second, transport.IdleConnTimeout)
+ })
+}
+
+func TestDriverRemoteConnectionSettingsWiring(t *testing.T) {
+ t.Run("DriverRemoteConnectionSettings wires connection pool settings", func(t *testing.T) {
+ drc, err := NewDriverRemoteConnection("http://localhost:8182/gremlin",
+ func(settings *DriverRemoteConnectionSettings) {
+ settings.MaximumConcurrentConnections = 150
+ settings.MaxIdleConnections = 15
+ settings.IdleConnectionTimeout = 200 * time.Second
+ settings.KeepAliveInterval = 40 * time.Second
+ })
+ require.NoError(t, err)
+ defer drc.Close()
+
+ // Verify settings were wired to connectionSettings
+ assert.Equal(t, 150, drc.client.connectionSettings.maxConnsPerHost)
+ assert.Equal(t, 15, drc.client.connectionSettings.maxIdleConnsPerHost)
+ assert.Equal(t, 200*time.Second, drc.client.connectionSettings.idleConnTimeout)
+ assert.Equal(t, 40*time.Second, drc.client.connectionSettings.keepAliveInterval)
+
+ // Verify settings were applied to http.Transport
+ transport := drc.client.conn.httpClient.Transport.(*http.Transport)
+ assert.Equal(t, 150, transport.MaxConnsPerHost)
+ assert.Equal(t, 15, transport.MaxIdleConnsPerHost)
+ assert.Equal(t, 200*time.Second, transport.IdleConnTimeout)
+ })
+
+ t.Run("DriverRemoteConnectionSettings uses defaults when not configured", func(t *testing.T) {
+ drc, err := NewDriverRemoteConnection("http://localhost:8182/gremlin")
+ require.NoError(t, err)
+ defer drc.Close()
+
+ // Verify defaults are used (0 in settings means use default)
+ assert.Equal(t, 0, drc.client.connectionSettings.maxConnsPerHost)
+ assert.Equal(t, 0, drc.client.connectionSettings.maxIdleConnsPerHost)
+ assert.Equal(t, time.Duration(0), drc.client.connectionSettings.idleConnTimeout)
+ assert.Equal(t, time.Duration(0), drc.client.connectionSettings.keepAliveInterval)
+
+ // Verify defaults were applied to http.Transport
+ transport := drc.client.conn.httpClient.Transport.(*http.Transport)
+ assert.Equal(t, 128, transport.MaxConnsPerHost)
+ assert.Equal(t, 8, transport.MaxIdleConnsPerHost)
+ assert.Equal(t, 180*time.Second, transport.IdleConnTimeout)
+ })
+}
diff --git a/gremlin-go/driver/cucumber/cucumberSteps_test.go b/gremlin-go/driver/cucumber/cucumberSteps_test.go
index 7a5761ab6d..cb10c970a7 100644
--- a/gremlin-go/driver/cucumber/cucumberSteps_test.go
+++ b/gremlin-go/driver/cucumber/cucumberSteps_test.go
@@ -430,7 +430,7 @@ func (tg *tinkerPopGraph) nothingShouldHappenBecause(arg1 *godog.DocString) erro
func (tg *tinkerPopGraph) chooseGraph(graphName string) error {
tg.graphName = graphName
data := tg.graphDataMap[graphName]
- tg.g = gremlingo.Traversal_().With(data.connection)
+ tg.g = gremlingo.Traversal_().With(data.connection).With("language", "gremlin-lang")
if graphName == "empty" {
err := tg.cleanEmptyDataGraph(tg.g)
if err != nil {
diff --git a/gremlin-go/driver/cucumber/cucumberWorld.go b/gremlin-go/driver/cucumber/cucumberWorld.go
index ac92ea1db8..dcbec09ef4 100644
--- a/gremlin-go/driver/cucumber/cucumberWorld.go
+++ b/gremlin-go/driver/cucumber/cucumberWorld.go
@@ -109,7 +109,7 @@ func (t *CucumberWorld) loadAllDataGraph() {
if err != nil {
panic(fmt.Sprintf("Failed to create connection '%v'", err))
}
- g := gremlingo.Traversal_().With(connection)
+ g := gremlingo.Traversal_().With(connection).With("language", "gremlin-lang")
t.graphDataMap[name] = &DataGraph{
name: name,
connection: connection,
@@ -130,7 +130,7 @@ func (t *CucumberWorld) loadEmptyDataGraph() {
func (t *CucumberWorld) reloadEmptyData() {
graphData := t.getDataGraphFromMap("empty")
- g := gremlingo.Traversal_().With(graphData.connection)
+ g := gremlingo.Traversal_().With(graphData.connection).With("language", "gremlin-lang")
graphData.vertices = getVertices(g)
graphData.edges = getEdges(g)
}
diff --git a/gremlin-go/driver/driverRemoteConnection.go b/gremlin-go/driver/driverRemoteConnection.go
index e4ec91d7bf..7b85ca0c4a 100644
--- a/gremlin-go/driver/driverRemoteConnection.go
+++ b/gremlin-go/driver/driverRemoteConnection.go
@@ -21,7 +21,6 @@ package gremlingo
import (
"crypto/tls"
- "runtime"
"time"
"golang.org/x/text/language"
@@ -33,22 +32,33 @@ type DriverRemoteConnectionSettings struct {
LogVerbosity LogVerbosity
Logger Logger
Language language.Tag
- AuthInfo AuthInfoProvider
TlsConfig *tls.Config
- KeepAliveInterval time.Duration
- WriteDeadline time.Duration
ConnectionTimeout time.Duration
EnableCompression bool
EnableUserAgentOnConnect bool
- ReadBufferSize int
- WriteBufferSize int
- // Minimum amount of concurrent active traversals on a connection to trigger creation of a new connection
- NewConnectionThreshold int
- // Maximum number of concurrent connections. Default: number of runtime processors
+ // MaximumConcurrentConnections is the maximum number of concurrent TCP connections
+ // to the Gremlin server. This limits how many requests can be in-flight simultaneously.
+ // Default: 128. Set to 0 to use the default.
MaximumConcurrentConnections int
- // Initial amount of instantiated connections. Default: 1
- InitialConcurrentConnections int
+
+ // MaxIdleConnections is the maximum number of idle (keep-alive) connections to retain
+ // in the connection pool. Idle connections are reused for subsequent requests.
+ // Default: 8. Set to 0 to use the default.
+ MaxIdleConnections int
+
+ // IdleConnectionTimeout is how long an idle connection remains in the pool before
+ // being closed. Set this to match your server's idle timeout if needed.
+ // Default: 180 seconds (3 minutes). Set to 0 to use the default.
+ IdleConnectionTimeout time.Duration
+
+ // KeepAliveInterval is the interval between TCP keep-alive probes on idle connections.
+ // This helps detect dead connections and keeps connections alive through firewalls.
+ // Default: 30 seconds. Set to 0 to use the default.
+ KeepAliveInterval time.Duration
+
+ // RequestInterceptors are functions that modify HTTP requests before sending.
+ RequestInterceptors []RequestInterceptor
}
// DriverRemoteConnection is a remote connection.
@@ -70,44 +80,46 @@ func NewDriverRemoteConnection(
LogVerbosity: Info,
Logger: &defaultLogger{},
Language: language.English,
- AuthInfo: &AuthInfo{},
TlsConfig: &tls.Config{},
- KeepAliveInterval: keepAliveIntervalDefault,
- WriteDeadline: writeDeadlineDefault,
ConnectionTimeout: connectionTimeoutDefault,
EnableCompression: false,
EnableUserAgentOnConnect: true,
- ReadBufferSize: readBufferSizeDefault,
- WriteBufferSize: writeBufferSizeDefault,
- MaximumConcurrentConnections: runtime.NumCPU(),
+ MaximumConcurrentConnections: 0, // Use default (128)
+ MaxIdleConnections: 0, // Use default (8)
+ IdleConnectionTimeout: 0, // Use default (180s)
+ KeepAliveInterval: 0, // Use default (30s)
}
for _, configuration := range configurations {
configuration(settings)
}
connSettings := &connectionSettings{
- authInfo: settings.AuthInfo,
tlsConfig: settings.TlsConfig,
- keepAliveInterval: settings.KeepAliveInterval,
- writeDeadline: settings.WriteDeadline,
connectionTimeout: settings.ConnectionTimeout,
+ maxConnsPerHost: settings.MaximumConcurrentConnections,
+ maxIdleConnsPerHost: settings.MaxIdleConnections,
+ idleConnTimeout: settings.IdleConnectionTimeout,
+ keepAliveInterval: settings.KeepAliveInterval,
enableCompression: settings.EnableCompression,
- readBufferSize: settings.ReadBufferSize,
- writeBufferSize: settings.WriteBufferSize,
enableUserAgentOnConnect: settings.EnableUserAgentOnConnect,
}
logHandler := newLogHandler(settings.Logger, settings.LogVerbosity, settings.Language)
- httpProt := newHttpProtocol(logHandler, url, connSettings)
+ conn := newConnection(logHandler, url, connSettings)
+
+ // Add user-provided interceptors
+ for _, interceptor := range settings.RequestInterceptors {
+ conn.AddInterceptor(interceptor)
+ }
client := &Client{
url: url,
traversalSource: settings.TraversalSource,
logHandler: logHandler,
connectionSettings: connSettings,
- httpProtocol: httpProt,
+ conn: conn,
}
return &DriverRemoteConnection{client: client, isClosed: false, settings: settings}, nil
@@ -136,7 +148,6 @@ func (driver *DriverRemoteConnection) Submit(traversalString string) (ResultSet,
}
// submitGremlinLang sends a GremlinLang traversal to the server.
-// TODO test and update when connection is set up
func (driver *DriverRemoteConnection) submitGremlinLang(gremlinLang *GremlinLang) (ResultSet, error) {
if driver.isClosed {
return nil, newError(err0203SubmitGremlinLangToClosedConnectionError)
diff --git a/gremlin-go/driver/driverRemoteConnection_test.go b/gremlin-go/driver/driverRemoteConnection_test.go
index d702d1809e..bdae0c0aa8 100644
--- a/gremlin-go/driver/driverRemoteConnection_test.go
+++ b/gremlin-go/driver/driverRemoteConnection_test.go
@@ -20,7 +20,6 @@ under the License.
package gremlingo
import (
- "net/http"
"testing"
"github.com/stretchr/testify/assert"
@@ -28,20 +27,11 @@ import (
func TestAuthentication(t *testing.T) {
- t.Run("Test BasicAuthInfo.", func(t *testing.T) {
- header := BasicAuthInfo("Lyndon", "Bauto")
- assert.Nil(t, header.GetHeader())
- b, _, _ := header.GetBasicAuth()
- assert.True(t, b)
- })
-
- t.Run("Test GetHeader.", func(t *testing.T) {
- header := &AuthInfo{}
- assert.Nil(t, header.GetHeader())
- header = nil
- assert.Nil(t, header.GetHeader())
- httpHeader := http.Header{}
- header = &AuthInfo{Header: httpHeader}
- assert.Equal(t, httpHeader, header.GetHeader())
+ t.Run("Test BasicAuth interceptor", func(t *testing.T) {
+ interceptor := BasicAuth("user", "pass")
+ req, _ := NewHttpRequest("POST", "http://localhost:8182/gremlin")
+ err := interceptor(req)
+ assert.Nil(t, err)
+ assert.Contains(t, req.Headers.Get(HeaderAuthorization), "Basic ")
})
}
diff --git a/gremlin-go/driver/error_codes.go b/gremlin-go/driver/error_codes.go
index 03a4b1cf05..bc113c0441 100644
--- a/gremlin-go/driver/error_codes.go
+++ b/gremlin-go/driver/error_codes.go
@@ -57,10 +57,10 @@ const (
err0409GetSerializerToReadUnknownCustomTypeError errorCode = "E0409_GRAPH_BINARY_GETSERIALIZERTOREAD_UNKNOWN_CUSTOM_TYPE_ERROR"
// response handling errors
- err0501ResponseHandlerResultSetNotCreatedError errorCode = "E0501_PROTOCOL_RESPONSEHANDLER_NO_RESULTSET_ON_DATA_RECEIVE"
- err0502ResponseHandlerReadLoopError errorCode = "E0502_PROTOCOL_RESPONSEHANDLER_READ_LOOP_ERROR"
- err0502ResponseHandlerError errorCode = "E0502_PROTOCOL_RESPONSEHANDLER_ERROR"
- err0503ResponseHandlerAuthError errorCode = "E0503_PROTOCOL_RESPONSEHANDLER_AUTH_ERROR"
+ err0501ResponseResultSetNotCreatedError errorCode = "E0501_RESPONSE_NO_RESULTSET_ON_DATA_RECEIVE"
+ err0502ResponseReadLoopError errorCode = "E0502_RESPONSE_READ_LOOP_ERROR"
+ err0502ResponseError errorCode = "E0502_RESPONSE_ERROR"
+ err0503ResponseAuthError errorCode = "E0503_RESPONSE_AUTH_ERROR"
// result.go errors
err0601ResultNotVertexError errorCode = "E0601_RESULT_NOT_VERTEX_ERROR"
diff --git a/gremlin-go/driver/graphBinary.go b/gremlin-go/driver/graphBinary.go
deleted file mode 100644
index c553b0a2e2..0000000000
--- a/gremlin-go/driver/graphBinary.go
+++ /dev/null
@@ -1,1105 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements. See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership. The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License. You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied. See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package gremlingo
-
-import (
- "bytes"
- "encoding/binary"
- "fmt"
- "math"
- "math/big"
- "reflect"
- "time"
-
- "github.com/google/uuid"
-)
-
-// Version 1.0
-
-// dataType graphBinary types.
-type dataType uint8
-
-// dataType defined as constants.
-const (
- customType dataType = 0x00
- intType dataType = 0x01
- longType dataType = 0x02
- stringType dataType = 0x03
- datetimeType dataType = 0x04
- doubleType dataType = 0x07
- floatType dataType = 0x08
- listType dataType = 0x09
- mapType dataType = 0x0a
- setType dataType = 0x0b
- uuidType dataType = 0x0c
- edgeType dataType = 0x0d
- pathType dataType = 0x0e
- propertyType dataType = 0x0f
- vertexType dataType = 0x11
- vertexPropertyType dataType = 0x12
- directionType dataType = 0x18
- tType dataType = 0x20
- bigDecimalType dataType = 0x22
- bigIntegerType dataType = 0x23
- byteType dataType = 0x24
- byteBuffer dataType = 0x25
- shortType dataType = 0x26
- booleanType dataType = 0x27
- mergeType dataType = 0x2e
- gTypeType dataType = 0x30
- durationType dataType = 0x81
- markerType dataType = 0xfd
- nullType dataType = 0xFE
-)
-
-var nullBytes = []byte{nullType.getCodeByte(), 0x01}
-
-func (dataType dataType) getCodeByte() byte {
- return byte(dataType)
-}
-
-func (dataType dataType) getCodeBytes() []byte {
- return []byte{dataType.getCodeByte()}
-}
-
-// graphBinaryTypeSerializer struct for the different types of serializers.
-type graphBinaryTypeSerializer struct {
- logHandler *logHandler
-}
-
-func (serializer *graphBinaryTypeSerializer) writeType(value interface{}, buffer *bytes.Buffer, writer writer) ([]byte, error) {
- return serializer.writeTypeValue(value, buffer, writer, true)
-}
-
-func (serializer *graphBinaryTypeSerializer) writeTypeValue(value interface{}, buffer *bytes.Buffer, writer writer, nullable bool) ([]byte, error) {
- if value == nil {
- if !nullable {
- serializer.logHandler.log(Error, unexpectedNull)
- return nil, newError(err0401WriteTypeValueUnexpectedNullError)
- }
- serializer.writeValueFlagNull(buffer)
- return buffer.Bytes(), nil
- }
- if nullable {
- serializer.writeValueFlagNone(buffer)
- }
- return writer(value, buffer, serializer)
-}
-
-// Format: {length}{item_0}...{item_n}
-func listWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- v := reflect.ValueOf(value)
- valLen := v.Len()
- err := binary.Write(buffer, binary.BigEndian, int32(valLen))
- if err != nil {
- return nil, err
- }
- for i := 0; i < valLen; i++ {
- _, err := typeSerializer.write(v.Index(i).Interface(), buffer)
- if err != nil {
- return nil, err
- }
- }
- return buffer.Bytes(), nil
-}
-
-// Format: {length}{value}
-func byteBufferWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- var v ByteBuffer
- if reflect.TypeOf(value).Kind() == reflect.Ptr {
- v = *(value.(*ByteBuffer))
- } else {
- v = value.(ByteBuffer)
- }
-
- err := binary.Write(buffer, binary.BigEndian, int32(len(v.Data)))
- if err != nil {
- return nil, err
- }
- buffer.Write(v.Data)
-
- return buffer.Bytes(), nil
-}
-
-// Format: {length}{item_0}...{item_n}
-// Item format: {type_code}{type_info}{value_flag}{value}
-func mapWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- if value == nil {
- _, err := typeSerializer.writeValue(int32(0), buffer, false)
- if err != nil {
- return nil, err
- }
- return buffer.Bytes(), nil
- }
-
- v := reflect.ValueOf(value)
- keys := v.MapKeys()
- err := binary.Write(buffer, binary.BigEndian, int32(len(keys)))
- if err != nil {
- return nil, err
- }
- for _, k := range keys {
- convKey := k.Convert(v.Type().Key())
- // serialize k
- _, err := typeSerializer.write(k.Interface(), buffer)
- if err != nil {
- return nil, err
- }
- // serialize v.MapIndex(c_key)
- val := v.MapIndex(convKey)
- _, err = typeSerializer.write(val.Interface(), buffer)
- if err != nil {
- return nil, err
- }
- }
- return buffer.Bytes(), nil
-}
-
-func stringWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, int32(len(value.(string))))
- if err != nil {
- return nil, err
- }
- _, err = buffer.WriteString(value.(string))
- return buffer.Bytes(), err
-}
-
-func longWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- switch v := value.(type) {
- case int:
- value = int64(v)
- case uint32:
- value = int64(v)
- }
- err := binary.Write(buffer, binary.BigEndian, value)
- return buffer.Bytes(), err
-}
-
-func intWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- switch v := value.(type) {
- case uint16:
- value = int32(v)
- }
- err := binary.Write(buffer, binary.BigEndian, value.(int32))
- return buffer.Bytes(), err
-}
-
-func shortWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- switch v := value.(type) {
- case int8:
- value = int16(v)
- }
- err := binary.Write(buffer, binary.BigEndian, value.(int16))
- return buffer.Bytes(), err
-}
-
-// Golang stores BigIntegers with big.Int types
-// it contains an unsigned representation of the number and uses a boolean to track +ve and -ve
-// getSignedBytesFromBigInt gives us the signed(two's complement) byte array that represents the unsigned byte array in
-// big.Int
-func getSignedBytesFromBigInt(n *big.Int) []byte {
- var one = big.NewInt(1)
- if n.Sign() == 1 {
- // add a buffer 0x00 byte to the start of byte array if number is positive and has a 1 in its MSB
- b := n.Bytes()
- if b[0]&0x80 > 0 {
- b = append([]byte{0}, b...)
- }
- return b
- } else if n.Sign() == -1 {
- // Convert Unsigned byte array to signed byte array
- length := uint(n.BitLen()/8+1) * 8
- b := new(big.Int).Add(n, new(big.Int).Lsh(one, length)).Bytes()
- // Strip any redundant 0xff bytes from the front of the byte array if the following byte starts with a 1
- if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
- b = b[1:]
- }
- return b
- }
- return []byte{}
-}
-
-// Format: {length}{value_0}...{value_n}
-func bigIntWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- var v big.Int
- switch val := value.(type) {
- case uint:
- v = *(new(big.Int).SetUint64(uint64(val)))
- case uint64:
- v = *(new(big.Int).SetUint64(val))
- default:
- if reflect.TypeOf(value).Kind() == reflect.Ptr {
- v = *(value.(*big.Int))
- } else {
- v = value.(big.Int)
- }
- }
- signedBytes := getSignedBytesFromBigInt(&v)
- err := binary.Write(buffer, binary.BigEndian, int32(len(signedBytes)))
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(signedBytes); i++ {
- err := binary.Write(buffer, binary.BigEndian, signedBytes[i])
- if err != nil {
- return nil, err
- }
- }
- return buffer.Bytes(), nil
-}
-
-// Format: {scale}{unscaled_value}
-func bigDecimalWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- var v BigDecimal
- if reflect.TypeOf(value).Kind() == reflect.Ptr {
- v = *(value.(*BigDecimal))
- } else {
- v = value.(BigDecimal)
- }
- err := binary.Write(buffer, binary.BigEndian, v.Scale)
- if err != nil {
- return nil, err
- }
-
- return bigIntWriter(v.UnscaledValue, buffer, typeSerializer)
-}
-
-// Format: {Id}{Label}{properties}
-func vertexWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- v := value.(*Vertex)
- _, err := typeSerializer.write(v.Id, buffer)
- if err != nil {
- return nil, err
- }
-
- // Not fully qualified.
- _, err = typeSerializer.writeValue([1]string{v.Label}, buffer, false)
- if err != nil {
- return nil, err
- }
- // Note that as TinkerPop currently send "references" only, properties will always be null
- buffer.Write(nullBytes)
- return buffer.Bytes(), nil
-}
-
-// Format: {Id}{Label}{inVId}{inVLabel}{outVId}{outVLabel}{parent}{properties}
-func edgeWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- e := value.(*Edge)
- _, err := typeSerializer.write(e.Id, buffer)
- if err != nil {
- return nil, err
- }
-
- // Not fully qualified
- _, err = typeSerializer.writeValue([1]string{e.Label}, buffer, false)
- if err != nil {
- return nil, err
- }
-
- // Write in-vertex
- _, err = typeSerializer.write(e.InV.Id, buffer)
- if err != nil {
- return nil, err
- }
-
- // Not fully qualified.
- _, err = typeSerializer.writeValue([1]string{e.InV.Label}, buffer, false)
- if err != nil {
- return nil, err
- }
- // Write out-vertex
- _, err = typeSerializer.write(e.OutV.Id, buffer)
- if err != nil {
- return nil, err
- }
-
- // Not fully qualified.
- _, err = typeSerializer.writeValue([1]string{e.OutV.Label}, buffer, false)
- if err != nil {
- return nil, err
- }
-
- // Note that as TinkerPop currently send "references" only, parent and properties will always be null
- buffer.Write(nullBytes)
- buffer.Write(nullBytes)
- return buffer.Bytes(), nil
-}
-
-// Format: {Key}{Value}{parent}
-func propertyWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- v := value.(*Property)
-
- // Not fully qualified.
- _, err := typeSerializer.writeValue(v.Key, buffer, false)
- if err != nil {
- return nil, err
- }
-
- _, err = typeSerializer.write(v.Value, buffer)
- if err != nil {
- return nil, err
- }
- // Note that as TinkerPop currently send "references" only, parent and properties will always be null
- buffer.Write(nullBytes)
- return buffer.Bytes(), nil
-}
-
-// Format: {Id}{Label}{Value}{parent}{properties}
-func vertexPropertyWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- vp := value.(*VertexProperty)
- _, err := typeSerializer.write(vp.Id, buffer)
- if err != nil {
- return nil, err
- }
-
- // Not fully qualified.
- _, err = typeSerializer.writeValue([1]string{vp.Label}, buffer, false)
- if err != nil {
- return nil, err
- }
- _, err = typeSerializer.write(vp.Value, buffer)
- if err != nil {
- return nil, err
- }
- // Note that as TinkerPop currently send "references" only, parent and properties will always be null
- buffer.Write(nullBytes)
- buffer.Write(nullBytes)
- return buffer.Bytes(), nil
-}
-
-// Format: {Labels}{Objects}
-func pathWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- p := value.(*Path)
- _, err := typeSerializer.write(p.Labels, buffer)
- if err != nil {
- return nil, err
- }
- _, err = typeSerializer.write(p.Objects, buffer)
- if err != nil {
- return nil, err
- }
- return buffer.Bytes(), nil
-}
-
-// Format: Same as List.
-// Mostly similar to listWriter with small changes
-func setWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- slice := value.(Set).ToSlice()
- return listWriter(slice, buffer, typeSerializer)
-}
-
-func dateTimeWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- t := value.(time.Time)
- err := binary.Write(buffer, binary.BigEndian, int32(t.Year()))
- if err != nil {
- return nil, err
- }
- err = binary.Write(buffer, binary.BigEndian, byte(t.Month()))
- if err != nil {
- return nil, err
- }
- err = binary.Write(buffer, binary.BigEndian, byte(t.Day()))
- if err != nil {
- return nil, err
- }
- // construct time of day in nanoseconds
- h := t.Hour()
- m := t.Minute()
- s := t.Second()
- ns := (h * 60 * 60 * 1e9) + (m * 60 * 1e9) + (s * 1e9) + t.Nanosecond()
- err = binary.Write(buffer, binary.BigEndian, int64(ns))
- if err != nil {
- return nil, err
- }
- _, os := t.Zone()
- err = binary.Write(buffer, binary.BigEndian, int32(os))
- if err != nil {
- return nil, err
- }
- return buffer.Bytes(), nil
-}
-
-func durationWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- t := value.(time.Duration)
- sec := int64(t / time.Second)
- nanos := int32(t % time.Second)
- err := binary.Write(buffer, binary.BigEndian, sec)
- if err != nil {
- return nil, err
- }
- err = binary.Write(buffer, binary.BigEndian, nanos)
- if err != nil {
- return nil, err
- }
- return buffer.Bytes(), nil
-}
-
-func enumWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- _, err := typeSerializer.write(reflect.ValueOf(value).String(), buffer)
- return buffer.Bytes(), err
-}
-
-func markerWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
- m := value.(Marker)
- err := binary.Write(buffer, binary.BigEndian, m.GetValue())
- return buffer.Bytes(), err
-}
-
-// Format: {strategy_class}{configuration}
-func traversalStrategyWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- ts := value.(*traversalStrategy)
-
- _, err := typeSerializer.writeValue(ts.name, buffer, false)
- if err != nil {
- return nil, err
- }
-
- return mapWriter(ts.configuration, buffer, typeSerializer)
-}
-
-const (
- valueFlagNull byte = 1
- valueFlagNone byte = 0
-)
-
-func (serializer *graphBinaryTypeSerializer) getType(val interface{}) (dataType, error) {
- switch val.(type) {
- case string:
- return stringType, nil
- case uint, uint64, *big.Int:
- return bigIntegerType, nil
- case int64, int, uint32:
- return longType, nil
- case int32, uint16:
- return intType, nil
- case int8, int16: // GraphBinary doesn't have a type for signed 8-bit integer, serializing int8 as Short instead.
- return shortType, nil
- case uint8:
- return byteType, nil
- case bool:
- return booleanType, nil
- case uuid.UUID:
- return uuidType, nil
- case float32:
- return floatType, nil
- case float64:
- return doubleType, nil
- case *Vertex:
- return vertexType, nil
- case *Edge:
- return edgeType, nil
- case *Property:
- return propertyType, nil
- case *VertexProperty:
- return vertexPropertyType, nil
- case *Path:
- return pathType, nil
- case Set:
- return setType, nil
- case time.Time:
- return datetimeType, nil
- case time.Duration:
- return durationType, nil
- case direction:
- return directionType, nil
- case t:
- return tType, nil
- case merge:
- return mergeType, nil
- case gType:
- return gTypeType, nil
- case *BigDecimal, BigDecimal:
- return bigDecimalType, nil
- case *ByteBuffer, ByteBuffer:
- return byteBuffer, nil
- default:
- switch reflect.TypeOf(val).Kind() {
- case reflect.Map:
- return mapType, nil
- case reflect.Array, reflect.Slice:
- // We can write an array or slice into the list dataType.
- return listType, nil
- default:
- serializer.logHandler.logf(Error, serializeDataTypeError, reflect.TypeOf(val).Name())
- return intType, newError(err0407GetSerializerToWriteUnknownTypeError, reflect.TypeOf(val).Name())
- }
- }
-}
-
-func (serializer *graphBinaryTypeSerializer) getWriter(dataType dataType) (writer, error) {
- if writer, ok := serializers[dataType]; ok {
- return writer, nil
- }
- serializer.logHandler.logf(Error, deserializeDataTypeError, int32(dataType))
- return nil, newError(err0407GetSerializerToWriteUnknownTypeError, dataType)
-}
-
-// gets the type of the serializer based on the value
-func (serializer *graphBinaryTypeSerializer) getSerializerToWrite(val interface{}) (writer, dataType, error) {
- dataType, err := serializer.getType(val)
- if err != nil {
- return nil, intType, err
- }
- writer, err := serializer.getWriter(dataType)
- if err != nil {
- return nil, intType, err
- }
-
- return writer, dataType, nil
-}
-
-// Writes an object in fully-qualified format, containing {type_code}{type_info}{value_flag}{value}.
-func (serializer *graphBinaryTypeSerializer) write(valueObject interface{}, buffer *bytes.Buffer) (interface{}, error) {
- if valueObject == nil {
- // return Object of type "unspecified object null" with the value flag set to null.
- buffer.Write(nullBytes)
- return buffer.Bytes(), nil
- }
-
- writer, dataType, err := serializer.getSerializerToWrite(valueObject)
- if err != nil {
- return nil, err
- }
- buffer.Write(dataType.getCodeBytes())
- return serializer.writeType(valueObject, buffer, writer)
-}
-
-// Writes a value without including type information.
-func (serializer *graphBinaryTypeSerializer) writeValue(value interface{}, buffer *bytes.Buffer, nullable bool) (interface{}, error) {
- if value == nil {
- if !nullable {
- serializer.logHandler.log(Error, unexpectedNull)
- return nil, newError(err0403WriteValueUnexpectedNullError)
- }
- serializer.writeValueFlagNull(buffer)
- return buffer.Bytes(), nil
- }
-
- writer, _, err := serializer.getSerializerToWrite(value)
- if err != nil {
- return nil, err
- }
- message, err := serializer.writeTypeValue(value, buffer, writer, nullable)
- if err != nil {
- return nil, err
- }
- return message, nil
-}
-
-func (serializer *graphBinaryTypeSerializer) writeValueFlagNull(buffer *bytes.Buffer) {
- buffer.WriteByte(valueFlagNull)
-}
-
-func (serializer *graphBinaryTypeSerializer) writeValueFlagNone(buffer *bytes.Buffer) {
- buffer.WriteByte(valueFlagNone)
-}
-
-// readers
-
-func readTemp(data *[]byte, i *int, len int) *[]byte {
- tmp := make([]byte, len)
- for j := 0; j < len; j++ {
- tmp[j] = (*data)[j+*i]
- }
- *i += len
- return &tmp
-}
-
-// Primitive
-func readBoolean(data *[]byte, i *int) (interface{}, error) {
- b, _ := readByte(data, i)
- return b != uint8(0), nil
-}
-
-func readByteSafe(data *[]byte, i *int) byte {
- *i++
- return (*data)[*i-1]
-}
-func readByte(data *[]byte, i *int) (interface{}, error) {
- return readByteSafe(data, i), nil
-}
-
-func readShort(data *[]byte, i *int) (interface{}, error) {
- return int16(binary.BigEndian.Uint16(*readTemp(data, i, 2))), nil
-}
-
-func readIntSafe(data *[]byte, i *int) int32 {
- return int32(binary.BigEndian.Uint32(*readTemp(data, i, 4)))
-}
-func readInt(data *[]byte, i *int) (interface{}, error) {
- return readIntSafe(data, i), nil
-}
-
-func readLongSafe(data *[]byte, i *int) int64 {
- return int64(binary.BigEndian.Uint64(*readTemp(data, i, 8)))
-}
-func readLong(data *[]byte, i *int) (interface{}, error) {
- return readLongSafe(data, i), nil
-}
-
-func readBigInt(data *[]byte, i *int) (interface{}, error) {
- sz := readIntSafe(data, i)
- b := readTemp(data, i, int(sz))
-
- var newBigInt = big.NewInt(0).SetBytes(*b)
- var one = big.NewInt(1)
- if len(*b) == 0 {
- return newBigInt, nil
- }
- // If the first bit in the first element of the byte array is a 1, we need to interpret the byte array as a two's complement representation
- if (*b)[0]&0x80 == 0x00 {
- newBigInt.SetBytes(*b)
- return newBigInt, nil
- }
- // Undo two's complement to byte array and set negative boolean to true
- length := uint((len(*b)*8)/8+1) * 8
- b2 := new(big.Int).Sub(newBigInt, new(big.Int).Lsh(one, length)).Bytes()
-
- // Strip the resulting 0xff byte at the start of array
- b2 = b2[1:]
-
- // Strip any redundant 0x00 byte at the start of array
- if b2[0] == 0x00 {
- b2 = b2[1:]
- }
- newBigInt = big.NewInt(0)
- newBigInt.SetBytes(b2)
- newBigInt.Neg(newBigInt)
- return newBigInt, nil
-}
-
-func readBigDecimal(data *[]byte, i *int) (interface{}, error) {
- bigDecimal := &BigDecimal{}
- bigDecimal.Scale = readIntSafe(data, i)
- unscaled, err := readBigInt(data, i)
- if err != nil {
- return nil, err
- }
- bigDecimal.UnscaledValue = unscaled.(*big.Int)
- return bigDecimal, nil
-}
-
-func readUint32Safe(data *[]byte, i *int) uint32 {
- return binary.BigEndian.Uint32(*readTemp(data, i, 4))
-}
-
-func readFloat(data *[]byte, i *int) (interface{}, error) {
- return math.Float32frombits(binary.BigEndian.Uint32(*readTemp(data, i, 4))), nil
-}
-
-func readDouble(data *[]byte, i *int) (interface{}, error) {
- return math.Float64frombits(binary.BigEndian.Uint64(*readTemp(data, i, 8))), nil
-}
-
-func readString(data *[]byte, i *int) (interface{}, error) {
- sz := int(readUint32Safe(data, i))
- if sz == 0 {
- return "", nil
- }
- *i += sz
- return string((*data)[*i-sz : *i]), nil
-}
-
-func readDataType(data *[]byte, i *int) dataType {
- return dataType(readByteSafe(data, i))
-}
-
-func getDefaultValue(dataType dataType) interface{} {
- switch dataType {
- case intType, bigIntegerType, longType, shortType, byteType, booleanType, floatType, doubleType:
- return 0
- case uuidType:
- return uuid.Nil
- case vertexType:
- return Vertex{}
- case edgeType:
- return Edge{}
- case propertyType:
- return Property{}
- case vertexPropertyType:
- return VertexProperty{}
- case pathType:
- return Path{}
- case setType:
- return SimpleSet{}
- case datetimeType:
- return time.Time{}
- case durationType:
- return time.Duration(0)
- default:
- return nil
- }
-}
-
-// Composite
-func readList(data *[]byte, i *int, flag byte) (interface{}, error) {
- sz := readIntSafe(data, i)
- var valList []interface{}
- if flag == 0x02 {
- for j := int32(0); j < sz; j++ {
- val, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- bulk := readLongSafe(data, i)
- for k := int64(0); k < bulk; k++ {
- valList = append(valList, val)
- }
- }
- } else {
- for j := int32(0); j < sz; j++ {
- val, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- valList = append(valList, val)
- }
- }
- return valList, nil
-}
-
-func readByteBuffer(data *[]byte, i *int) (interface{}, error) {
- r := &ByteBuffer{}
- sz := readIntSafe(data, i)
- r.Data = make([]byte, sz)
- for j := int32(0); j < sz; j++ {
- r.Data[j] = readByteSafe(data, i)
- }
- return r, nil
-}
-
-func readMap(data *[]byte, i *int) (interface{}, error) {
- sz := readUint32Safe(data, i)
- var mapData = make(map[interface{}]interface{})
- for j := uint32(0); j < sz; j++ {
- k, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- v, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- if k == nil {
- mapData[nil] = v
- } else if reflect.TypeOf(k).Comparable() {
- mapData[k] = v
- } else {
- switch reflect.TypeOf(k).Kind() {
- case reflect.Map:
- mapData[&k] = v
- default:
- mapData[fmt.Sprint(k)] = v
- }
- }
- }
- return mapData, nil
-}
-
-func readMapUnqualified(data *[]byte, i *int) (interface{}, error) {
- sz := readUint32Safe(data, i)
- var mapData = make(map[string]interface{})
- for j := uint32(0); j < sz; j++ {
- keyDataType := readDataType(data, i)
- if keyDataType != stringType {
- return nil, newError(err0703ReadMapNonStringKeyError, keyDataType)
- }
-
- // Skip nullable, key must be present
- *i++
-
- k, err := readString(data, i)
- if err != nil {
- return nil, err
- }
- mapData[k.(string)], err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- }
- return mapData, nil
-}
-
-func readSet(data *[]byte, i *int, flag byte) (interface{}, error) {
- list, err := readList(data, i, flag)
- if err != nil {
- return nil, err
- }
- return NewSimpleSet(list.([]interface{})...), nil
-}
-
-func readUuid(data *[]byte, i *int) (interface{}, error) {
- id, _ := uuid.FromBytes(*readTemp(data, i, 16))
- return id, nil
-}
-
-func dateTimeReader(data *[]byte, i *int) (interface{}, error) {
- year := readIntSafe(data, i)
- month := readByteSafe(data, i)
- day := readByteSafe(data, i)
- totalNS := readLongSafe(data, i)
- // calculate hour, minute, second, and ns from totalNS (int64) to prevent int overflow in the nanoseconds arg
- ns := totalNS % 1e9
- totalS := totalNS / 1e9
- s := totalS % 60
- totalM := totalS / 60
- m := totalM % 60
- h := totalM / 60
-
- offset := readIntSafe(data, i)
- datetime := time.Date(int(year), time.Month(month), int(day), int(h), int(m), int(s), int(ns), GetTimezoneFromOffset(int(offset)))
- return datetime, nil
-}
-
-// GetTimezoneFromOffset is a helper function to convert an offset in seconds to a time.Location
-func GetTimezoneFromOffset(offsetSeconds int) *time.Location {
- // calculate hours and minutes from seconds
- hours := offsetSeconds / 3600
- minutes := (offsetSeconds % 3600) / 60
-
- // format the timezone name in the format that go expects
- // for example: "UTC+01:00" or "UTC-05:30"
- sign := "+"
- if hours < 0 {
- sign = "-"
- hours = -hours
- minutes = -minutes
- }
- tzName := fmt.Sprintf("UTC%s%02d:%02d", sign, hours, minutes)
-
- return time.FixedZone(tzName, offsetSeconds)
-}
-
-func durationReader(data *[]byte, i *int) (interface{}, error) {
- return time.Duration(readLongSafe(data, i)*int64(time.Second) + int64(readIntSafe(data, i))), nil
-}
-
-// Graph
-
-// {fully qualified id}{unqualified label}
-func vertexReader(data *[]byte, i *int) (interface{}, error) {
- return vertexReaderReadingProperties(data, i, true)
-}
-
-// {fully qualified id}{unqualified label}{fully qualified properties}
-func vertexReaderReadingProperties(data *[]byte, i *int, readProperties bool) (interface{}, error) {
- var err error
- v := new(Vertex)
- v.Id, err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- label, err := readList(data, i, 0)
- if err != nil {
- return nil, err
- }
- v.Label = label.([]interface{})[0].(string)
- if readProperties {
- props, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- // null properties are returned as empty slices
- v.Properties = make([]interface{}, 0)
- if props != nil {
- v.Properties = props
- }
- }
- return v, nil
-}
-
-// {fully qualified id}{unqualified label}{in vertex w/o null byte}{out vertex}{unused null byte}{fully qualified properties}
-func edgeReader(data *[]byte, i *int) (interface{}, error) {
- var err error
- e := new(Edge)
- e.Id, err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- label, err := readList(data, i, 0)
- if err != nil {
- return nil, err
- }
- e.Label = label.([]interface{})[0].(string)
- v, err := vertexReaderReadingProperties(data, i, false)
- if err != nil {
- return nil, err
- }
- e.InV = *v.(*Vertex)
- v, err = vertexReaderReadingProperties(data, i, false)
- if err != nil {
- return nil, err
- }
- e.OutV = *v.(*Vertex)
- *i += 2
- props, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- // null properties are returned as empty slices
- e.Properties = make([]interface{}, 0)
- if props != nil {
- e.Properties = props
- }
- return e, nil
-}
-
-// {unqualified key}{fully qualified value}{null byte}
-func propertyReader(data *[]byte, i *int) (interface{}, error) {
- p := new(Property)
- key, err := readUnqualified(data, i, stringType, false)
- if err != nil {
- return nil, err
- }
- p.Key = key.(string)
- p.Value, err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- *i += 2
- return p, nil
-}
-
-// {fully qualified id}{unqualified label}{fully qualified value}{null byte}{null byte}
-func vertexPropertyReader(data *[]byte, i *int) (interface{}, error) {
- var err error
- vp := new(VertexProperty)
- vp.Id, err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- label, err := readList(data, i, 0)
- if err != nil {
- return nil, err
- }
- vp.Label = label.([]interface{})[0].(string)
- vp.Value, err = readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
-
- *i += 2
-
- props, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
-
- // null properties are returned as empty slices
- vp.Properties = make([]interface{}, 0)
- if props != nil {
- vp.Properties = props
- }
-
- return vp, nil
-}
-
-// {list of set of strings}{list of fully qualified objects}
-func pathReader(data *[]byte, i *int) (interface{}, error) {
- path := new(Path)
- newLabels, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- for _, param := range newLabels.([]interface{}) {
- path.Labels = append(path.Labels, param.(*SimpleSet))
- }
- objects, err := readFullyQualifiedNullable(data, i, true)
- if err != nil {
- return nil, err
- }
- path.Objects = objects.([]interface{})
- return path, err
-}
-
-// {type code (always string so ignore)}{nil code (always false so ignore)}{int32 size}{string enum}
-func enumReader(data *[]byte, i *int) (interface{}, error) {
- typeCode := readDataType(data, i)
- if typeCode != stringType {
- return nil, newError(err0406EnumReaderInvalidTypeError)
- }
- *i++
- return readString(data, i)
-}
-
-func markerReader(data *[]byte, i *int) (interface{}, error) {
- m, err := Of(readByteSafe(data, i))
- if err != nil {
- return nil, err
- }
- return m, nil
-}
-
-func readUnqualified(data *[]byte, i *int, dataTyp dataType, nullable bool) (interface{}, error) {
- if nullable && readByteSafe(data, i) == valueFlagNull {
- return getDefaultValue(dataTyp), nil
- }
- deserializer, ok := deserializers[dataTyp]
- if !ok {
- return nil, newError(err0408GetSerializerToReadUnknownTypeError, dataTyp)
- }
- return deserializer(data, i)
-}
-
-func readFullyQualifiedNullable(data *[]byte, i *int, nullable bool) (interface{}, error) {
- dataTyp := readDataType(data, i)
- if dataTyp == nullType {
- if readByteSafe(data, i) != valueFlagNull {
- return nil, newError(err0404ReadNullTypeError)
- }
- return nil, nil
- } else if nullable {
- flag := readByteSafe(data, i)
- if flag == valueFlagNull {
- return getDefaultValue(dataTyp), nil
- }
- if dataTyp == listType {
- return readList(data, i, flag)
- }
- if dataTyp == setType {
- return readSet(data, i, flag)
- }
- }
- deserializer, ok := deserializers[dataTyp]
- if !ok {
- return nil, newError(err0408GetSerializerToReadUnknownTypeError, dataTyp)
- }
-
- return deserializer(data, i)
-}
-
-// {name}{type specific payload}
-func customTypeReader(data *[]byte, i *int) (interface{}, error) {
- // we need to decrement the index by 1 to be read the 32-bit int with the size of the string
- *i = *i - 1
- customTypeName, err := readString(data, i)
- if err != nil {
- return nil, err
- }
-
- // grab a read lock for the map of deserializers, since these can be updated out-of-band
- customTypeReaderLock.RLock()
- defer customTypeReaderLock.RUnlock()
- deserializer, ok := customDeserializers[customTypeName.(string)]
- if !ok {
- return nil, newError(err0409GetSerializerToReadUnknownCustomTypeError, customTypeName)
- }
- return deserializer(data, i)
-}
diff --git a/gremlin-go/driver/graphBinaryDeserializer.go b/gremlin-go/driver/graphBinaryDeserializer.go
new file mode 100644
index 0000000000..b96ea9c53f
--- /dev/null
+++ b/gremlin-go/driver/graphBinaryDeserializer.go
@@ -0,0 +1,617 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+
+package gremlingo
+
+import (
+ "bufio"
+ "encoding/binary"
+ "fmt"
+ "io"
+ "math"
+ "math/big"
+ "reflect"
+ "time"
+
+ "github.com/google/uuid"
+)
+
+// GraphBinaryDeserializer reads GraphBinary data directly from an io.Reader,
+// enabling streaming deserialization of server responses.
+//
+// Streaming Behavior:
+// The deserializer is designed to work with HTTP chunked transfer encoding where
+// the server streams results as they become available. Key behaviors:
+//
+// 1. Blocking on partial data: When reading an object, if the underlying reader
+// doesn't have enough bytes available, the deserializer blocks until the data
+// arrives. This is handled by io.ReadFull which waits for the exact number of
+// bytes needed based on GraphBinary's self-describing format (type codes and
+// length prefixes).
+//
+// 2. Chunk boundary independence: Go's HTTP client may receive data in chunks that
+// don't align with server-sent GraphBinary object boundaries. The deserializer
+// handles this transparently - it reads exactly the bytes needed for each object,
+// blocking if necessary, regardless of how the data was chunked by the network.
+//
+// 3. Immediate object delivery: Each complete object is returned as soon as it's
+// fully read, allowing the caller to process results incrementally rather than
+// waiting for the entire response.
+//
+// The bufio.Reader wrapper provides efficient buffering without affecting the
+// streaming semantics - it simply reduces the number of underlying read syscalls.
+type GraphBinaryDeserializer struct {
+ r *bufio.Reader
+ buf [8]byte
+ err error // sticky error
+}
+
+// GraphBinary flag for bulked list/set
+const flagBulked = 0x02
+
+// NewGraphBinaryDeserializer creates a new GraphBinaryDeserializer that reads from the given io.Reader.
+// The reader is wrapped in a buffered reader for efficient reading.
+func NewGraphBinaryDeserializer(r io.Reader) *GraphBinaryDeserializer {
+ return &GraphBinaryDeserializer{r: bufio.NewReaderSize(r, 8192)}
+}
+
+func (d *GraphBinaryDeserializer) readByte() (byte, error) {
+ if d.err != nil {
+ return 0, d.err
+ }
+ b, err := d.r.ReadByte()
+ if err != nil {
+ d.err = err
+ return 0, err
+ }
+ return b, nil
+}
+
+func (d *GraphBinaryDeserializer) readBytes(n int) ([]byte, error) {
+ if d.err != nil {
+ return nil, d.err
+ }
+ buf := make([]byte, n)
+ _, err := io.ReadFull(d.r, buf)
+ if err != nil {
+ d.err = err
+ return nil, err
+ }
+ return buf, nil
+}
+
+func (d *GraphBinaryDeserializer) readInt32() (int32, error) {
+ if d.err != nil {
+ return 0, d.err
+ }
+ _, err := io.ReadFull(d.r, d.buf[:4])
+ if err != nil {
+ d.err = err
+ return 0, err
+ }
+ return int32(binary.BigEndian.Uint32(d.buf[:4])), nil
+}
+
+func (d *GraphBinaryDeserializer) readUint32() (uint32, error) {
+ if d.err != nil {
+ return 0, d.err
+ }
+ _, err := io.ReadFull(d.r, d.buf[:4])
+ if err != nil {
+ d.err = err
+ return 0, err
+ }
+ return binary.BigEndian.Uint32(d.buf[:4]), nil
+}
+
+func (d *GraphBinaryDeserializer) readInt64() (int64, error) {
+ if d.err != nil {
+ return 0, d.err
+ }
+ _, err := io.ReadFull(d.r, d.buf[:8])
+ if err != nil {
+ d.err = err
+ return 0, err
+ }
+ return int64(binary.BigEndian.Uint64(d.buf[:8])), nil
+}
+
+// ReadHeader reads and validates the GraphBinary response header.
+// This must be called before reading any objects from the stream.
+func (d *GraphBinaryDeserializer) ReadHeader() error {
+ if _, err := d.readByte(); err != nil {
+ return err
+ }
+ _, err := d.readByte()
+ return err
+}
+
+// ReadFullyQualified reads the next fully-qualified GraphBinary value from the stream.
+// Returns the deserialized object, or an error if reading fails.
+// When the end of the result stream is reached, this returns a Marker equal to EndOfStream().
+func (d *GraphBinaryDeserializer) ReadFullyQualified() (interface{}, error) {
+ dtByte, err := d.readByte()
+ if err != nil {
+ return nil, err
+ }
+ dt := dataType(dtByte)
+ if dt == nullType {
+ if _, err := d.readByte(); err != nil {
+ return nil, err
+ }
+ return nil, nil
+ }
+ flag, err := d.readByte()
+ if err != nil {
+ return nil, err
+ }
+ if flag == valueFlagNull {
+ return nil, nil
+ }
+ return d.readValue(dt, flag)
+}
+
+func (d *GraphBinaryDeserializer) readValue(dt dataType, flag byte) (interface{}, error) {
+ switch dt {
+ case intType:
+ return d.readInt32()
+ case longType:
+ return d.readInt64()
+ case stringType:
+ return d.readString()
+ case doubleType:
+ if d.err != nil {
+ return nil, d.err
+ }
+ if _, err := io.ReadFull(d.r, d.buf[:8]); err != nil {
+ d.err = err
+ return nil, err
+ }
+ return math.Float64frombits(binary.BigEndian.Uint64(d.buf[:8])), nil
+ case floatType:
+ if d.err != nil {
+ return nil, d.err
+ }
+ if _, err := io.ReadFull(d.r, d.buf[:4]); err != nil {
+ d.err = err
+ return nil, err
+ }
+ return math.Float32frombits(binary.BigEndian.Uint32(d.buf[:4])), nil
+ case booleanType:
+ b, err := d.readByte()
+ return b != 0, err
+ case byteType:
+ return d.readByte()
+ case shortType:
+ if d.err != nil {
+ return nil, d.err
+ }
+ if _, err := io.ReadFull(d.r, d.buf[:2]); err != nil {
+ d.err = err
+ return nil, err
+ }
+ return int16(binary.BigEndian.Uint16(d.buf[:2])), nil
+ case uuidType:
+ buf, err := d.readBytes(16)
+ if err != nil {
+ return nil, err
+ }
+ id, err := uuid.FromBytes(buf)
+ if err != nil {
+ return nil, err
+ }
+ return id, nil
+ case listType:
+ return d.readList(flag == flagBulked)
+ case setType:
+ list, err := d.readList(flag == flagBulked)
+ if err != nil {
+ return nil, err
+ }
+ return NewSimpleSet(list.([]interface{})...), nil
+ case mapType:
+ return d.readMap()
+ case vertexType:
+ return d.readVertex(true)
+ case edgeType:
+ return d.readEdge()
+ case pathType:
+ return d.readPath()
+ case propertyType:
+ return d.readProperty()
+ case vertexPropertyType:
+ return d.readVertexProperty()
+ case bigIntegerType:
+ return d.readBigInt()
+ case bigDecimalType:
+ return d.readBigDecimal()
+ case datetimeType:
+ return d.readDateTime()
+ case durationType:
+ return d.readDuration()
+ case markerType:
+ b, err := d.readByte()
+ if err != nil {
+ return nil, err
+ }
+ return Of(b)
+ case byteBuffer:
+ return d.readByteBuffer()
+ case tType, directionType, mergeType, gTypeType:
+ return d.readEnum()
+ default:
+ return nil, newError(err0408GetSerializerToReadUnknownTypeError, dt)
+ }
+}
+
+func (d *GraphBinaryDeserializer) readString() (string, error) {
+ length, err := d.readInt32()
+ if err != nil {
+ return "", err
+ }
+ if length == 0 {
+ return "", nil
+ }
+ buf, err := d.readBytes(int(length))
+ if err != nil {
+ return "", err
+ }
+ return string(buf), nil
+}
+
+func (d *GraphBinaryDeserializer) readList(bulked bool) (interface{}, error) {
+ length, err := d.readInt32()
+ if err != nil {
+ return nil, err
+ }
+ list := make([]interface{}, 0, length)
+ for i := int32(0); i < length; i++ {
+ val, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ if bulked {
+ bulk, err := d.readInt64()
+ if err != nil {
+ return nil, err
+ }
+ for j := int64(0); j < bulk; j++ {
+ list = append(list, val)
+ }
+ } else {
+ list = append(list, val)
+ }
+ }
+ return list, nil
+}
+
+func (d *GraphBinaryDeserializer) readMap() (interface{}, error) {
+ length, err := d.readUint32()
+ if err != nil {
+ return nil, err
+ }
+ m := make(map[interface{}]interface{}, length)
+ for i := uint32(0); i < length; i++ {
+ key, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ val, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ if key == nil {
+ m[nil] = val
+ } else if reflect.TypeOf(key).Comparable() {
+ m[key] = val
+ } else if reflect.TypeOf(key).Kind() == reflect.Map {
+ m[&key] = val
+ } else {
+ m[fmt.Sprint(key)] = val
+ }
+ }
+ return m, nil
+}
+
+func (d *GraphBinaryDeserializer) readVertex(withProps bool) (*Vertex, error) {
+ id, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ labels, err := d.readList(false)
+ if err != nil {
+ return nil, err
+ }
+ labelSlice, ok := labels.([]interface{})
+ if !ok || len(labelSlice) == 0 {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ label, ok := labelSlice[0].(string)
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ v := &Vertex{Element: Element{Id: id, Label: label}}
+ if withProps {
+ props, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ v.Properties = make([]interface{}, 0)
+ if props != nil {
+ v.Properties = props
+ }
+ }
+ return v, nil
+}
+
+func (d *GraphBinaryDeserializer) readEdge() (*Edge, error) {
+ id, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ labels, err := d.readList(false)
+ if err != nil {
+ return nil, err
+ }
+ labelSlice, ok := labels.([]interface{})
+ if !ok || len(labelSlice) == 0 {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ label, ok := labelSlice[0].(string)
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ inV, err := d.readVertex(false)
+ if err != nil {
+ return nil, err
+ }
+ outV, err := d.readVertex(false)
+ if err != nil {
+ return nil, err
+ }
+ if _, err := d.readBytes(2); err != nil {
+ return nil, err
+ }
+ props, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ e := &Edge{
+ Element: Element{Id: id, Label: label},
+ InV: *inV,
+ OutV: *outV,
+ }
+ e.Properties = make([]interface{}, 0)
+ if props != nil {
+ e.Properties = props
+ }
+ return e, nil
+}
+
+func (d *GraphBinaryDeserializer) readPath() (*Path, error) {
+ labels, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ objects, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ objectSlice, ok := objects.([]interface{})
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ path := &Path{Objects: objectSlice}
+ if labels != nil {
+ labelSlice, ok := labels.([]interface{})
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ for _, l := range labelSlice {
+ set, ok := l.(*SimpleSet)
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ path.Labels = append(path.Labels, set)
+ }
+ }
+ return path, nil
+}
+
+func (d *GraphBinaryDeserializer) readProperty() (*Property, error) {
+ key, err := d.readString()
+ if err != nil {
+ return nil, err
+ }
+ value, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ if _, err := d.readBytes(2); err != nil {
+ return nil, err
+ }
+ return &Property{Key: key, Value: value}, nil
+}
+
+func (d *GraphBinaryDeserializer) readVertexProperty() (*VertexProperty, error) {
+ id, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ labels, err := d.readList(false)
+ if err != nil {
+ return nil, err
+ }
+ labelSlice, ok := labels.([]interface{})
+ if !ok || len(labelSlice) == 0 {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ label, ok := labelSlice[0].(string)
+ if !ok {
+ return nil, newError(err0404ReadNullTypeError)
+ }
+ value, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ if _, err := d.readBytes(2); err != nil {
+ return nil, err
+ }
+ props, err := d.ReadFullyQualified()
+ if err != nil {
+ return nil, err
+ }
+ vp := &VertexProperty{
+ Element: Element{Id: id, Label: label},
+ Value: value,
+ }
+ vp.Properties = make([]interface{}, 0)
+ if props != nil {
+ vp.Properties = props
+ }
+ return vp, nil
+}
+
+func (d *GraphBinaryDeserializer) readBigInt() (*big.Int, error) {
+ length, err := d.readInt32()
+ if err != nil {
+ return nil, err
+ }
+ if length == 0 {
+ return big.NewInt(0), nil
+ }
+ b, err := d.readBytes(int(length))
+ if err != nil {
+ return nil, err
+ }
+ bi := big.NewInt(0).SetBytes(b)
+ if b[0]&0x80 != 0 {
+ one := big.NewInt(1)
+ bitLen := uint((len(b)*8)/8+1) * 8
+ bi.Sub(bi, new(big.Int).Lsh(one, bitLen))
+ }
+ return bi, nil
+}
+
+func (d *GraphBinaryDeserializer) readBigDecimal() (*BigDecimal, error) {
+ scale, err := d.readInt32()
+ if err != nil {
+ return nil, err
+ }
+ unscaled, err := d.readBigInt()
+ if err != nil {
+ return nil, err
+ }
+ return &BigDecimal{Scale: scale, UnscaledValue: unscaled}, nil
+}
+
+func (d *GraphBinaryDeserializer) readDateTime() (time.Time, error) {
+ year, err := d.readInt32()
+ if err != nil {
+ return time.Time{}, err
+ }
+ month, err := d.readByte()
+ if err != nil {
+ return time.Time{}, err
+ }
+ day, err := d.readByte()
+ if err != nil {
+ return time.Time{}, err
+ }
+ totalNS, err := d.readInt64()
+ if err != nil {
+ return time.Time{}, err
+ }
+ offset, err := d.readInt32()
+ if err != nil {
+ return time.Time{}, err
+ }
+ ns := totalNS % 1e9
+ totalS := totalNS / 1e9
+ s := totalS % 60
+ totalM := totalS / 60
+ m := totalM % 60
+ h := totalM / 60
+ return time.Date(int(year), time.Month(month), int(day), int(h), int(m), int(s), int(ns), GetTimezoneFromOffset(int(offset))), nil
+}
+
+func (d *GraphBinaryDeserializer) readDuration() (time.Duration, error) {
+ seconds, err := d.readInt64()
+ if err != nil {
+ return 0, err
+ }
+ nanos, err := d.readInt32()
+ if err != nil {
+ return 0, err
+ }
+ return time.Duration(seconds*int64(time.Second) + int64(nanos)), nil
+}
+
+func (d *GraphBinaryDeserializer) readByteBuffer() (*ByteBuffer, error) {
+ length, err := d.readInt32()
+ if err != nil {
+ return nil, err
+ }
+ data, err := d.readBytes(int(length))
+ if err != nil {
+ return nil, err
+ }
+ return &ByteBuffer{Data: data}, nil
+}
+
+func (d *GraphBinaryDeserializer) readEnum() (string, error) {
+ if _, err := d.readByte(); err != nil { // type code (string)
+ return "", err
+ }
+ if _, err := d.readByte(); err != nil { // null flag
+ return "", err
+ }
+ return d.readString()
+}
+
+// ReadStatus reads the response status after the EndOfStream marker.
+// Returns the status code, message, exception string, and any error encountered.
+// This should be called after ReadFullyQualified() returns an EndOfStream marker.
+func (d *GraphBinaryDeserializer) ReadStatus() (code uint32, message string, exception string, err error) {
+ code, err = d.readUint32()
+ if err != nil {
+ return 0, "", "", err
+ }
+ flag, err := d.readByte()
+ if err != nil {
+ return code, "", "", err
+ }
+ if flag != valueFlagNull {
+ message, err = d.readString()
+ if err != nil {
+ return code, "", "", err
+ }
+ }
+ flag, err = d.readByte()
+ if err != nil {
+ return code, message, "", err
+ }
+ if flag != valueFlagNull {
+ exception, err = d.readString()
+ if err != nil {
+ return code, message, "", err
+ }
+ }
+ return code, message, exception, nil
+}
diff --git a/gremlin-go/driver/graphBinaryDeserializer_test.go b/gremlin-go/driver/graphBinaryDeserializer_test.go
new file mode 100644
index 0000000000..cc5caef74c
--- /dev/null
+++ b/gremlin-go/driver/graphBinaryDeserializer_test.go
@@ -0,0 +1,402 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package gremlingo
+
+import (
+ "bytes"
+ "io"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+)
+
+// slowReader simulates a network stream that delivers data in chunks with delays.
+// This mimics how Go's HTTP client receives chunked transfer-encoded responses
+// where chunk boundaries don't align with GraphBinary object boundaries.
+type slowReader struct {
+ chunks [][]byte
+ delay time.Duration
+ index int
+ offset int
+ mu sync.Mutex
+}
+
+func newSlowReader(chunks [][]byte, delay time.Duration) *slowReader {
+ return &slowReader{chunks: chunks, delay: delay}
+}
+
+func (r *slowReader) Read(p []byte) (n int, err error) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.index >= len(r.chunks) {
+ return 0, io.EOF
+ }
+
+ // Simulate network delay between chunks
+ if r.offset == 0 && r.index > 0 {
+ r.mu.Unlock()
+ time.Sleep(r.delay)
+ r.mu.Lock()
+ }
+
+ chunk := r.chunks[r.index]
+ remaining := chunk[r.offset:]
+ n = copy(p, remaining)
+ r.offset += n
+
+ if r.offset >= len(chunk) {
+ r.index++
+ r.offset = 0
+ }
+
+ return n, nil
+}
+
+func TestGraphBinaryDeserializer(t *testing.T) {
+ t.Run("readInt32", func(t *testing.T) {
+ data := []byte{0x00, 0x00, 0x00, 0x2A} // 42
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ val, err := d.readInt32()
+ assert.Nil(t, err)
+ assert.Equal(t, int32(42), val)
+ })
+
+ t.Run("readInt64", func(t *testing.T) {
+ data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64} // 100
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ val, err := d.readInt64()
+ assert.Nil(t, err)
+ assert.Equal(t, int64(100), val)
+ })
+
+ t.Run("readString", func(t *testing.T) {
+ data := []byte{0x00, 0x00, 0x00, 0x05, 'h', 'e', 'l', 'l', 'o'}
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ val, err := d.readString()
+ assert.Nil(t, err)
+ assert.Equal(t, "hello", val)
+ })
+
+ t.Run("readString empty", func(t *testing.T) {
+ data := []byte{0x00, 0x00, 0x00, 0x00}
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ val, err := d.readString()
+ assert.Nil(t, err)
+ assert.Equal(t, "", val)
+ })
+
+ t.Run("error on incomplete data", func(t *testing.T) {
+ data := []byte{0x00, 0x00} // incomplete int32
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ _, err := d.readInt32()
+ assert.ErrorIs(t, err, io.ErrUnexpectedEOF)
+ })
+
+ t.Run("sticky error", func(t *testing.T) {
+ data := []byte{0x00} // too short
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+
+ _, err1 := d.readInt32()
+ assert.Error(t, err1)
+
+ // Subsequent reads should also fail
+ _, err2 := d.readInt32()
+ assert.Error(t, err2)
+ })
+}
+
+func TestStreamingChannelDelivery(t *testing.T) {
+ t.Run("results arrive incrementally via channel", func(t *testing.T) {
+ rs := newChannelResultSet()
+
+ // Simulate streaming - send results with delays
+ go func() {
+ for i := 0; i < 5; i++ {
+ time.Sleep(10 * time.Millisecond)
+ rs.Channel() <- &Result{i}
+ }
+ rs.Close()
+ }()
+
+ var times []time.Duration
+ start := time.Now()
+
+ for {
+ _, ok, _ := rs.One()
+ if !ok {
+ break
+ }
+ times = append(times, time.Since(start))
+ }
+
+ assert.Equal(t, 5, len(times))
+
+ // Results should arrive ~10ms apart, not all at once
+ for i := 1; i < len(times); i++ {
+ gap := times[i] - times[i-1]
+ assert.GreaterOrEqual(t, gap, 5*time.Millisecond,
+ "Results %d and %d arrived too close together: %v", i-1, i, gap)
+ }
+ })
+}
+
+// TestGraphBinaryDeserializerBlocksOnPartialData verifies that the deserializer correctly blocks
+// when it receives partial data, waiting for the rest of the object to arrive.
+// This simulates the real-world scenario where Go's HTTP client receives chunks
+// that don't align with server-sent GraphBinary object boundaries.
+func TestGraphBinaryDeserializerBlocksOnPartialData(t *testing.T) {
+ t.Run("blocks until complete int32 is available", func(t *testing.T) {
+ // Split a 4-byte int32 across two chunks
+ chunk1 := []byte{0x00, 0x00} // First 2 bytes
+ chunk2 := []byte{0x00, 0x2A} // Last 2 bytes (total = 42)
+
+ reader := newSlowReader([][]byte{chunk1, chunk2}, 20*time.Millisecond)
+ d := NewGraphBinaryDeserializer(reader)
+
+ start := time.Now()
+ val, err := d.readInt32()
+ elapsed := time.Since(start)
+
+ assert.Nil(t, err)
+ assert.Equal(t, int32(42), val)
+ // Should have blocked waiting for second chunk
+ assert.GreaterOrEqual(t, elapsed, 15*time.Millisecond,
+ "Should have blocked waiting for remaining bytes")
+ })
+
+ t.Run("blocks until complete string is available", func(t *testing.T) {
+ // String "hello" split across chunks:
+ // Chunk 1: length (4 bytes) + partial content
+ // Chunk 2: remaining content
+ chunk1 := []byte{0x00, 0x00, 0x00, 0x05, 'h', 'e'} // length=5, "he"
+ chunk2 := []byte{'l', 'l', 'o'} // "llo"
+
+ reader := newSlowReader([][]byte{chunk1, chunk2}, 20*time.Millisecond)
+ d := NewGraphBinaryDeserializer(reader)
+
+ start := time.Now()
+ val, err := d.readString()
+ elapsed := time.Since(start)
+
+ assert.Nil(t, err)
+ assert.Equal(t, "hello", val)
+ assert.GreaterOrEqual(t, elapsed, 15*time.Millisecond,
+ "Should have blocked waiting for remaining string bytes")
+ })
+}
+
+// TestGraphBinaryDeserializerMultipleObjects verifies that multiple GraphBinary objects
+// can be read from a stream, with each object returned as soon as it's complete.
+func TestGraphBinaryDeserializerMultipleObjects(t *testing.T) {
+ t.Run("reads multiple objects as they arrive", func(t *testing.T) {
+ // Build a stream with 3 fully-qualified int32 values
+ // Each int32: type(1) + flag(1) + value(4) = 6 bytes
+ obj1 := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x01} // int32 = 1
+ obj2 := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x02} // int32 = 2
+ obj3 := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x03} // int32 = 3
+
+ // Deliver each object as a separate chunk with delay
+ reader := newSlowReader([][]byte{obj1, obj2, obj3}, 15*time.Millisecond)
+ d := NewGraphBinaryDeserializer(reader)
+
+ var results []int32
+ var times []time.Duration
+ start := time.Now()
+
+ for i := 0; i < 3; i++ {
+ val, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ results = append(results, val.(int32))
+ times = append(times, time.Since(start))
+ }
+
+ assert.Equal(t, []int32{1, 2, 3}, results)
+
+ // Objects should arrive with delays between them
+ for i := 1; i < len(times); i++ {
+ gap := times[i] - times[i-1]
+ assert.GreaterOrEqual(t, gap, 10*time.Millisecond,
+ "Object %d should have arrived after a delay", i)
+ }
+ })
+
+ t.Run("handles object split across chunk boundary", func(t *testing.T) {
+ // First chunk: complete object + partial second object
+ // Second chunk: rest of second object + complete third object
+ chunk1 := []byte{
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, // int32 = 1 (complete)
+ 0x01, 0x00, 0x00, // partial int32 (type + flag + 2 bytes of value)
+ }
+ chunk2 := []byte{
+ 0x00, 0x00, 0x02, // rest of int32 = 2
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, // int32 = 3 (complete)
+ }
+
+ reader := newSlowReader([][]byte{chunk1, chunk2}, 20*time.Millisecond)
+ d := NewGraphBinaryDeserializer(reader)
+
+ // First object should return immediately
+ val1, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ assert.Equal(t, int32(1), val1)
+
+ // Second object should block waiting for chunk2
+ start := time.Now()
+ val2, err := d.ReadFullyQualified()
+ elapsed := time.Since(start)
+ assert.Nil(t, err)
+ assert.Equal(t, int32(2), val2)
+ assert.GreaterOrEqual(t, elapsed, 15*time.Millisecond,
+ "Should have blocked waiting for rest of object")
+
+ // Third object should return immediately (already in buffer)
+ val3, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ assert.Equal(t, int32(3), val3)
+ })
+}
+
+// TestGraphBinaryDeserializerWithEndOfStreamMarker verifies that the deserializer correctly
+// handles the EndOfStream marker and subsequent status reading.
+func TestGraphBinaryDeserializerWithEndOfStreamMarker(t *testing.T) {
+ t.Run("reads EndOfStream marker and status", func(t *testing.T) {
+ // Build a complete response:
+ // - Header (2 bytes): version + flags
+ // - One int32 result
+ // - EndOfStream marker
+ // - Status: code(4) + message(nullable) + exception(nullable)
+ data := []byte{
+ 0x81, 0x00, // Header: version byte + no bulking
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x2A, // int32 = 42
+ 0xfd, 0x00, 0x00, // Marker type + flag + value=0 (EndOfStream)
+ 0x00, 0x00, 0x00, 0xC8, // Status code = 200
+ 0x01, // Message is null
+ 0x01, // Exception is null
+ }
+
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+
+ // Read header
+ err := d.ReadHeader()
+ assert.Nil(t, err)
+
+ // Read the result
+ val, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ assert.Equal(t, int32(42), val)
+
+ // Read EndOfStream marker
+ marker, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ assert.Equal(t, EndOfStream(), marker)
+
+ // Read status
+ code, msg, exc, err := d.ReadStatus()
+ assert.Nil(t, err)
+ assert.Equal(t, uint32(200), code)
+ assert.Equal(t, "", msg)
+ assert.Equal(t, "", exc)
+ })
+
+ t.Run("reads status with message", func(t *testing.T) {
+ // Status with a message
+ data := []byte{
+ 0xfd, 0x00, 0x00, // EndOfStream marker
+ 0x00, 0x00, 0x01, 0x90, // Status code = 400
+ 0x00, // Message is not null
+ 0x00, 0x00, 0x00, 0x05, 'e', 'r', 'r', 'o', 'r', // Message = "error"
+ 0x01, // Exception is null
+ }
+
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+
+ marker, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+ assert.Equal(t, EndOfStream(), marker)
+
+ code, msg, exc, err := d.ReadStatus()
+ assert.Nil(t, err)
+ assert.Equal(t, uint32(400), code)
+ assert.Equal(t, "error", msg)
+ assert.Equal(t, "", exc)
+ })
+}
+
+// TestGraphBinaryDeserializerComplexTypes verifies streaming deserialization of complex types
+// like vertices, edges, and paths.
+func TestGraphBinaryDeserializerComplexTypes(t *testing.T) {
+ t.Run("reads vertex from stream", func(t *testing.T) {
+ // Vertex: id(int32) + labels(list of string) + properties(nullable)
+ data := []byte{
+ 0x11, 0x00, // Vertex type + flag
+ // ID: int32 = 1
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
+ // Labels: list with one string "person"
+ 0x00, 0x00, 0x00, 0x01, // list length = 1
+ 0x03, 0x00, // string type + flag
+ 0x00, 0x00, 0x00, 0x06, 'p', 'e', 'r', 's', 'o', 'n',
+ // Properties: null
+ 0xfe, 0x01,
+ }
+
+ d := NewGraphBinaryDeserializer(bytes.NewReader(data))
+ val, err := d.ReadFullyQualified()
+ assert.Nil(t, err)
+
+ v, ok := val.(*Vertex)
+ assert.True(t, ok)
+ assert.Equal(t, int32(1), v.Id)
+ assert.Equal(t, "person", v.Label)
+ })
+
+ t.Run("reads list of integers from chunked stream", func(t *testing.T) {
+ // List type split across chunks
+ chunk1 := []byte{
+ 0x09, 0x00, // List type + flag
+ 0x00, 0x00, 0x00, 0x03, // length = 3
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x0A, // int32 = 10
+ }
+ chunk2 := []byte{
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x14, // int32 = 20
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x1E, // int32 = 30
+ }
+
+ reader := newSlowReader([][]byte{chunk1, chunk2}, 15*time.Millisecond)
+ d := NewGraphBinaryDeserializer(reader)
+
+ start := time.Now()
+ val, err := d.ReadFullyQualified()
+ elapsed := time.Since(start)
+
+ assert.Nil(t, err)
+ list, ok := val.([]interface{})
+ assert.True(t, ok)
+ assert.Equal(t, 3, len(list))
+ assert.Equal(t, int32(10), list[0])
+ assert.Equal(t, int32(20), list[1])
+ assert.Equal(t, int32(30), list[2])
+
+ // Should have blocked for second chunk
+ assert.GreaterOrEqual(t, elapsed, 10*time.Millisecond)
+ })
+}
diff --git a/gremlin-go/driver/graphBinarySerializer.go b/gremlin-go/driver/graphBinarySerializer.go
new file mode 100644
index 0000000000..1150314a11
--- /dev/null
+++ b/gremlin-go/driver/graphBinarySerializer.go
@@ -0,0 +1,579 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package gremlingo
+
+import (
+ "encoding/binary"
+ "fmt"
+ "io"
+ "math/big"
+ "reflect"
+ "time"
+
+ "github.com/google/uuid"
+)
+
+// Version 1.0
+
+// dataType graphBinary types.
+type dataType uint8
+
+// dataType defined as constants.
+const (
+ customType dataType = 0x00
+ intType dataType = 0x01
+ longType dataType = 0x02
+ stringType dataType = 0x03
+ datetimeType dataType = 0x04
+ doubleType dataType = 0x07
+ floatType dataType = 0x08
+ listType dataType = 0x09
+ mapType dataType = 0x0a
+ setType dataType = 0x0b
+ uuidType dataType = 0x0c
+ edgeType dataType = 0x0d
+ pathType dataType = 0x0e
+ propertyType dataType = 0x0f
+ vertexType dataType = 0x11
+ vertexPropertyType dataType = 0x12
+ directionType dataType = 0x18
+ tType dataType = 0x20
+ bigDecimalType dataType = 0x22
+ bigIntegerType dataType = 0x23
+ byteType dataType = 0x24
+ byteBuffer dataType = 0x25
+ shortType dataType = 0x26
+ booleanType dataType = 0x27
+ mergeType dataType = 0x2e
+ gTypeType dataType = 0x30
+ durationType dataType = 0x81
+ markerType dataType = 0xfd
+ nullType dataType = 0xFE
+)
+
+var nullBytes = []byte{nullType.getCodeByte(), 0x01}
+
+func (dataType dataType) getCodeByte() byte {
+ return byte(dataType)
+}
+
+func (dataType dataType) getCodeBytes() []byte {
+ return []byte{dataType.getCodeByte()}
+}
+
+// graphBinaryTypeSerializer struct for the different types of serializers.
+type graphBinaryTypeSerializer struct {
+ logHandler *logHandler
+}
+
+func (serializer *graphBinaryTypeSerializer) writeType(value interface{}, w io.Writer, writer writer) error {
+ return serializer.writeTypeValue(value, w, writer, true)
+}
+
+func (serializer *graphBinaryTypeSerializer) writeTypeValue(value interface{}, w io.Writer, writer writer, nullable bool) error {
+ if value == nil {
+ if !nullable {
+ serializer.logHandler.log(Error, unexpectedNull)
+ return newError(err0401WriteTypeValueUnexpectedNullError)
+ }
+ return serializer.writeValueFlagNull(w)
+ }
+ if nullable {
+ if err := serializer.writeValueFlagNone(w); err != nil {
+ return err
+ }
+ }
+ return writer(value, w, serializer)
+}
+
+// Format: {length}{item_0}...{item_n}
+func listWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ v := reflect.ValueOf(value)
+ valLen := v.Len()
+ if err := binary.Write(w, binary.BigEndian, int32(valLen)); err != nil {
+ return err
+ }
+ for i := 0; i < valLen; i++ {
+ if err := typeSerializer.write(v.Index(i).Interface(), w); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// Format: {length}{value}
+func byteBufferWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ var v ByteBuffer
+ if reflect.TypeOf(value).Kind() == reflect.Ptr {
+ v = *(value.(*ByteBuffer))
+ } else {
+ v = value.(ByteBuffer)
+ }
+
+ if err := binary.Write(w, binary.BigEndian, int32(len(v.Data))); err != nil {
+ return err
+ }
+ _, err := w.Write(v.Data)
+ return err
+}
+
+// Format: {length}{item_0}...{item_n}
+// Item format: {type_code}{type_info}{value_flag}{value}
+func mapWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ if value == nil {
+ return typeSerializer.writeValue(int32(0), w, false)
+ }
+
+ v := reflect.ValueOf(value)
+ keys := v.MapKeys()
+ if err := binary.Write(w, binary.BigEndian, int32(len(keys))); err != nil {
+ return err
+ }
+ for _, k := range keys {
+ convKey := k.Convert(v.Type().Key())
+ // serialize k
+ if err := typeSerializer.write(k.Interface(), w); err != nil {
+ return err
+ }
+ // serialize v.MapIndex(c_key)
+ val := v.MapIndex(convKey)
+ if err := typeSerializer.write(val.Interface(), w); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func stringWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ str := value.(string)
+ if err := binary.Write(w, binary.BigEndian, int32(len(str))); err != nil {
+ return err
+ }
+ _, err := io.WriteString(w, str)
+ return err
+}
+
+func longWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ switch v := value.(type) {
+ case int:
+ value = int64(v)
+ case uint32:
+ value = int64(v)
+ }
+ return binary.Write(w, binary.BigEndian, value)
+}
+
+func intWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ switch v := value.(type) {
+ case uint16:
+ value = int32(v)
+ }
+ return binary.Write(w, binary.BigEndian, value.(int32))
+}
+
+func shortWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ switch v := value.(type) {
+ case int8:
+ value = int16(v)
+ }
+ return binary.Write(w, binary.BigEndian, value.(int16))
+}
+
+// Golang stores BigIntegers with big.Int types
+// it contains an unsigned representation of the number and uses a boolean to track +ve and -ve
+// getSignedBytesFromBigInt gives us the signed(two's complement) byte array that represents the unsigned byte array in
+// big.Int
+func getSignedBytesFromBigInt(n *big.Int) []byte {
+ var one = big.NewInt(1)
+ if n.Sign() == 1 {
+ // add a buffer 0x00 byte to the start of byte array if number is positive and has a 1 in its MSB
+ b := n.Bytes()
+ if b[0]&0x80 > 0 {
+ b = append([]byte{0}, b...)
+ }
+ return b
+ } else if n.Sign() == -1 {
+ // Convert Unsigned byte array to signed byte array
+ length := uint(n.BitLen()/8+1) * 8
+ b := new(big.Int).Add(n, new(big.Int).Lsh(one, length)).Bytes()
+ // Strip any redundant 0xff bytes from the front of the byte array if the following byte starts with a 1
+ if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
+ b = b[1:]
+ }
+ return b
+ }
+ return []byte{}
+}
+
+// Format: {length}{value_0}...{value_n}
+func bigIntWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ var v big.Int
+ switch val := value.(type) {
+ case uint:
+ v = *(new(big.Int).SetUint64(uint64(val)))
+ case uint64:
+ v = *(new(big.Int).SetUint64(val))
+ default:
+ if reflect.TypeOf(value).Kind() == reflect.Ptr {
+ v = *(value.(*big.Int))
+ } else {
+ v = value.(big.Int)
+ }
+ }
+ signedBytes := getSignedBytesFromBigInt(&v)
+ if err := binary.Write(w, binary.BigEndian, int32(len(signedBytes))); err != nil {
+ return err
+ }
+ _, err := w.Write(signedBytes)
+ return err
+}
+
+// Format: {scale}{unscaled_value}
+func bigDecimalWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ var v BigDecimal
+ if reflect.TypeOf(value).Kind() == reflect.Ptr {
+ v = *(value.(*BigDecimal))
+ } else {
+ v = value.(BigDecimal)
+ }
+ if err := binary.Write(w, binary.BigEndian, v.Scale); err != nil {
+ return err
+ }
+
+ return bigIntWriter(v.UnscaledValue, w, typeSerializer)
+}
+
+// Format: {Id}{Label}{properties}
+func vertexWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ v := value.(*Vertex)
+ if err := typeSerializer.write(v.Id, w); err != nil {
+ return err
+ }
+
+ // Not fully qualified.
+ if err := typeSerializer.writeValue([1]string{v.Label}, w, false); err != nil {
+ return err
+ }
+ // Note that as TinkerPop currently send "references" only, properties will always be null
+ _, err := w.Write(nullBytes)
+ return err
+}
+
+// Format: {Id}{Label}{inVId}{inVLabel}{outVId}{outVLabel}{parent}{properties}
+func edgeWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ e := value.(*Edge)
+ if err := typeSerializer.write(e.Id, w); err != nil {
+ return err
+ }
+
+ // Not fully qualified
+ if err := typeSerializer.writeValue([1]string{e.Label}, w, false); err != nil {
+ return err
+ }
+
+ // Write in-vertex
+ if err := typeSerializer.write(e.InV.Id, w); err != nil {
+ return err
+ }
+
+ // Not fully qualified.
+ if err := typeSerializer.writeValue([1]string{e.InV.Label}, w, false); err != nil {
+ return err
+ }
+ // Write out-vertex
+ if err := typeSerializer.write(e.OutV.Id, w); err != nil {
+ return err
+ }
+
+ // Not fully qualified.
+ if err := typeSerializer.writeValue([1]string{e.OutV.Label}, w, false); err != nil {
+ return err
+ }
+
+ // Note that as TinkerPop currently send "references" only, parent and properties will always be null
+ if _, err := w.Write(nullBytes); err != nil {
+ return err
+ }
+ _, err := w.Write(nullBytes)
+ return err
+}
+
+// Format: {Key}{Value}{parent}
+func propertyWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ v := value.(*Property)
+
+ // Not fully qualified.
+ if err := typeSerializer.writeValue(v.Key, w, false); err != nil {
+ return err
+ }
+
+ if err := typeSerializer.write(v.Value, w); err != nil {
+ return err
+ }
+ // Note that as TinkerPop currently send "references" only, parent and properties will always be null
+ _, err := w.Write(nullBytes)
+ return err
+}
+
+// Format: {Id}{Label}{Value}{parent}{properties}
+func vertexPropertyWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ vp := value.(*VertexProperty)
+ if err := typeSerializer.write(vp.Id, w); err != nil {
+ return err
+ }
+
+ // Not fully qualified.
+ if err := typeSerializer.writeValue([1]string{vp.Label}, w, false); err != nil {
+ return err
+ }
+ if err := typeSerializer.write(vp.Value, w); err != nil {
+ return err
+ }
+ // Note that as TinkerPop currently send "references" only, parent and properties will always be null
+ if _, err := w.Write(nullBytes); err != nil {
+ return err
+ }
+ _, err := w.Write(nullBytes)
+ return err
+}
+
+// Format: {Labels}{Objects}
+func pathWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ p := value.(*Path)
+ if err := typeSerializer.write(p.Labels, w); err != nil {
+ return err
+ }
+ return typeSerializer.write(p.Objects, w)
+}
+
+// Format: Same as List.
+// Mostly similar to listWriter with small changes
+func setWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ slice := value.(Set).ToSlice()
+ return listWriter(slice, w, typeSerializer)
+}
+
+func dateTimeWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ t := value.(time.Time)
+ if err := binary.Write(w, binary.BigEndian, int32(t.Year())); err != nil {
+ return err
+ }
+ if err := binary.Write(w, binary.BigEndian, byte(t.Month())); err != nil {
+ return err
+ }
+ if err := binary.Write(w, binary.BigEndian, byte(t.Day())); err != nil {
+ return err
+ }
+ // construct time of day in nanoseconds
+ h := t.Hour()
+ m := t.Minute()
+ s := t.Second()
+ ns := (h * 60 * 60 * 1e9) + (m * 60 * 1e9) + (s * 1e9) + t.Nanosecond()
+ if err := binary.Write(w, binary.BigEndian, int64(ns)); err != nil {
+ return err
+ }
+ _, os := t.Zone()
+ return binary.Write(w, binary.BigEndian, int32(os))
+}
+
+func durationWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ t := value.(time.Duration)
+ sec := int64(t / time.Second)
+ nanos := int32(t % time.Second)
+ if err := binary.Write(w, binary.BigEndian, sec); err != nil {
+ return err
+ }
+ return binary.Write(w, binary.BigEndian, nanos)
+}
+
+func enumWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return typeSerializer.write(reflect.ValueOf(value).String(), w)
+}
+
+func markerWriter(value interface{}, w io.Writer, _ *graphBinaryTypeSerializer) error {
+ m := value.(Marker)
+ return binary.Write(w, binary.BigEndian, m.GetValue())
+}
+
+// Format: {strategy_class}{configuration}
+func traversalStrategyWriter(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ ts := value.(*traversalStrategy)
+
+ if err := typeSerializer.writeValue(ts.name, w, false); err != nil {
+ return err
+ }
+
+ return mapWriter(ts.configuration, w, typeSerializer)
+}
+
+const (
+ valueFlagNull byte = 1
+ valueFlagNone byte = 0
+)
+
+func (serializer *graphBinaryTypeSerializer) getType(val interface{}) (dataType, error) {
+ switch val.(type) {
+ case string:
+ return stringType, nil
+ case uint, uint64, *big.Int:
+ return bigIntegerType, nil
+ case int64, int, uint32:
+ return longType, nil
+ case int32, uint16:
+ return intType, nil
+ case int8, int16: // GraphBinary doesn't have a type for signed 8-bit integer, serializing int8 as Short instead.
+ return shortType, nil
+ case uint8:
+ return byteType, nil
+ case bool:
+ return booleanType, nil
+ case uuid.UUID:
+ return uuidType, nil
+ case float32:
+ return floatType, nil
+ case float64:
+ return doubleType, nil
+ case *Vertex:
+ return vertexType, nil
+ case *Edge:
+ return edgeType, nil
+ case *Property:
+ return propertyType, nil
+ case *VertexProperty:
+ return vertexPropertyType, nil
+ case *Path:
+ return pathType, nil
+ case Set:
+ return setType, nil
+ case time.Time:
+ return datetimeType, nil
+ case time.Duration:
+ return durationType, nil
+ case direction:
+ return directionType, nil
+ case t:
+ return tType, nil
+ case merge:
+ return mergeType, nil
+ case gType:
+ return gTypeType, nil
+ case *BigDecimal, BigDecimal:
+ return bigDecimalType, nil
+ case *ByteBuffer, ByteBuffer:
+ return byteBuffer, nil
+ default:
+ switch reflect.TypeOf(val).Kind() {
+ case reflect.Map:
+ return mapType, nil
+ case reflect.Array, reflect.Slice:
+ // We can write an array or slice into the list dataType.
+ return listType, nil
+ default:
+ serializer.logHandler.logf(Error, serializeDataTypeError, reflect.TypeOf(val).Name())
+ return intType, newError(err0407GetSerializerToWriteUnknownTypeError, reflect.TypeOf(val).Name())
+ }
+ }
+}
+
+func (serializer *graphBinaryTypeSerializer) getWriter(dataType dataType) (writer, error) {
+ if writer, ok := serializers[dataType]; ok {
+ return writer, nil
+ }
+ serializer.logHandler.logf(Error, deserializeDataTypeError, int32(dataType))
+ return nil, newError(err0407GetSerializerToWriteUnknownTypeError, dataType)
+}
+
+// gets the type of the serializer based on the value
+func (serializer *graphBinaryTypeSerializer) getSerializerToWrite(val interface{}) (writer, dataType, error) {
+ dataType, err := serializer.getType(val)
+ if err != nil {
+ return nil, intType, err
+ }
+ writer, err := serializer.getWriter(dataType)
+ if err != nil {
+ return nil, intType, err
+ }
+
+ return writer, dataType, nil
+}
+
+// Writes an object in fully-qualified format, containing {type_code}{type_info}{value_flag}{value}.
+func (serializer *graphBinaryTypeSerializer) write(valueObject interface{}, w io.Writer) error {
+ if valueObject == nil {
+ // return Object of type "unspecified object null" with the value flag set to null.
+ _, err := w.Write(nullBytes)
+ return err
+ }
+
+ writer, dataType, err := serializer.getSerializerToWrite(valueObject)
+ if err != nil {
+ return err
+ }
+ if _, err := w.Write(dataType.getCodeBytes()); err != nil {
+ return err
+ }
+ return serializer.writeType(valueObject, w, writer)
+}
+
+// Writes a value without including type information.
+func (serializer *graphBinaryTypeSerializer) writeValue(value interface{}, w io.Writer, nullable bool) error {
+ if value == nil {
+ if !nullable {
+ serializer.logHandler.log(Error, unexpectedNull)
+ return newError(err0403WriteValueUnexpectedNullError)
+ }
+ return serializer.writeValueFlagNull(w)
+ }
+
+ writer, _, err := serializer.getSerializerToWrite(value)
+ if err != nil {
+ return err
+ }
+ return serializer.writeTypeValue(value, w, writer, nullable)
+}
+
+func (serializer *graphBinaryTypeSerializer) writeValueFlagNull(w io.Writer) error {
+ _, err := w.Write([]byte{valueFlagNull})
+ return err
+}
+
+func (serializer *graphBinaryTypeSerializer) writeValueFlagNone(w io.Writer) error {
+ _, err := w.Write([]byte{valueFlagNone})
+ return err
+}
+
+// GetTimezoneFromOffset is a helper function to convert an offset in seconds to a time.Location
+func GetTimezoneFromOffset(offsetSeconds int) *time.Location {
+ // calculate hours and minutes from seconds
+ hours := offsetSeconds / 3600
+ minutes := (offsetSeconds % 3600) / 60
+
+ // format the timezone name in the format that go expects
+ // for example: "UTC+01:00" or "UTC-05:30"
+ sign := "+"
+ if hours < 0 {
+ sign = "-"
+ hours = -hours
+ minutes = -minutes
+ }
+ tzName := fmt.Sprintf("UTC%s%02d:%02d", sign, hours, minutes)
+
+ return time.FixedZone(tzName, offsetSeconds)
+}
diff --git a/gremlin-go/driver/graphBinarySerializer_test.go b/gremlin-go/driver/graphBinarySerializer_test.go
new file mode 100644
index 0000000000..2b074af66b
--- /dev/null
+++ b/gremlin-go/driver/graphBinarySerializer_test.go
@@ -0,0 +1,537 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package gremlingo
+
+import (
+ "bytes"
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "math/big"
+ "reflect"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+ "golang.org/x/text/language"
+)
+
+// failingWriter is an io.Writer that fails after writing a specified number of bytes.
+// Used to test error propagation through the serialization chain.
+// Feature: serializer-writer-refactor, Property 4: Writer Error Propagation
+type failingWriter struct {
+ failAfter int // Number of bytes to write before failing
+ written int // Number of bytes written so far
+}
+
+func (w *failingWriter) Write(p []byte) (n int, err error) {
+ if w.written >= w.failAfter {
+ return 0, errors.New("simulated write failure")
+ }
+ // Calculate how many bytes we can write before failing
+ remaining := w.failAfter - w.written
+ if len(p) <= remaining {
+ w.written += len(p)
+ return len(p), nil
+ }
+ // Partial write then fail
+ w.written += remaining
+ return remaining, errors.New("simulated write failure")
+}
+
+func TestGraphBinaryV4(t *testing.T) {
+ t.Run("graphBinaryTypeSerializer tests", func(t *testing.T) {
+ serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+
+ t.Run("getType should return correct type for simple type", func(t *testing.T) {
+ res, err := serializer.getType(int32(1))
+ assert.Nil(t, err)
+ assert.Equal(t, intType, res)
+ })
+ t.Run("getType should return correct type for complex type", func(t *testing.T) {
+ res, err := serializer.getType([]byte{1, 2, 3})
+ assert.Nil(t, err)
+ assert.Equal(t, listType, res)
+ })
+ t.Run("getType should return error for missing type", func(t *testing.T) {
+ _, err := serializer.getType(Error)
+ assert.NotNil(t, err)
+ })
+
+ t.Run("getWriter should return correct func for dataType", func(t *testing.T) {
+ writer, err := serializer.getWriter(intType)
+ assert.Nil(t, err)
+ assert.Equal(t, reflect.ValueOf(intWriter).Pointer(), reflect.ValueOf(writer).Pointer())
+ })
+ t.Run("getWriter should return error for missing dataType", func(t *testing.T) {
+ _, err := serializer.getWriter(nullType)
+ assert.NotNil(t, err)
+ })
+
+ t.Run("getSerializerToWrite should return correct func for int", func(t *testing.T) {
+ writer, dataType, err := serializer.getSerializerToWrite(int32(1))
+ assert.Nil(t, err)
+ assert.Equal(t, intType, dataType)
+ assert.Equal(t, reflect.ValueOf(intWriter).Pointer(), reflect.ValueOf(writer).Pointer())
+ })
+ t.Run("getSerializerToWrite should return error for missing dataType", func(t *testing.T) {
+ _, _, err := serializer.getSerializerToWrite(nullType)
+ assert.NotNil(t, err)
+ })
+ })
+
+ t.Run("read-write tests", func(t *testing.T) {
+ t.Run("read-write string", func(t *testing.T) {
+ var buffer bytes.Buffer
+ str := "test string"
+ err := stringWriter(str, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readString()
+ assert.Nil(t, err)
+ assert.Equal(t, str, res)
+ })
+ t.Run("read-write bool", func(t *testing.T) {
+ var buffer bytes.Buffer
+ f := func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(buffer, binary.BigEndian, value.(bool))
+ }
+ err := f(false, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ b, err := d.readByte()
+ assert.Nil(t, err)
+ assert.False(t, b != 0)
+
+ buffer.Reset()
+ err = f(true, &buffer, nil)
+ assert.Nil(t, err)
+ d = NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ b, err = d.readByte()
+ assert.Nil(t, err)
+ assert.True(t, b != 0)
+ })
+ t.Run("read-write BigDecimal", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := &BigDecimal{11, big.NewInt(int64(22))}
+ err := bigDecimalWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readBigDecimal()
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write int", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := int32(123)
+ err := intWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readInt32()
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write short", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := int16(123)
+ err := shortWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ buf, err := d.readBytes(2)
+ assert.Nil(t, err)
+ res := int16(binary.BigEndian.Uint16(buf))
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write short int8", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := int8(123)
+ err := shortWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ buf, err := d.readBytes(2)
+ assert.Nil(t, err)
+ res := int16(binary.BigEndian.Uint16(buf))
+ assert.Equal(t, int16(source), res)
+ })
+ t.Run("read-write long", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := 123
+ err := longWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readInt64()
+ assert.Nil(t, err)
+ assert.Equal(t, int64(source), res)
+ })
+ t.Run("read-write bigInt", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := big.NewInt(123)
+ err := bigIntWriter(*source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readBigInt()
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write bigInt uint64", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := uint64(123)
+ err := bigIntWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readBigInt()
+ assert.Nil(t, err)
+ assert.Equal(t, new(big.Int).SetUint64(source), res)
+ })
+ t.Run("read-write bigInt uint", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := uint(123)
+ err := bigIntWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readBigInt()
+ assert.Nil(t, err)
+ assert.Equal(t, new(big.Int).SetUint64(uint64(source)), res)
+ })
+ t.Run("read-write list", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := []interface{}{int32(111), "str"}
+ serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+ err := listWriter(source, &buffer, &serializer)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readList(false)
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write byteBuffer", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := &ByteBuffer{[]byte{byte(127), byte(255)}}
+ err := byteBufferWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readByteBuffer()
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write set", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := NewSimpleSet(int32(111), "str")
+ serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+ err := setWriter(source, &buffer, &serializer)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ list, err := d.readList(false)
+ assert.Nil(t, err)
+ res := NewSimpleSet(list.([]interface{})...)
+ assert.Equal(t, source, res)
+ })
+ t.Run("read-write map", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := map[interface{}]interface{}{int32(1): "s1", "s2": int32(2), nil: nil}
+ serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+ err := mapWriter(source, &buffer, &serializer)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readMap()
+ assert.Nil(t, err)
+ assert.Equal(t, fmt.Sprintf("%v", source), fmt.Sprintf("%v", res))
+ })
+ t.Run("read incomparable map: a map value as the key", func(t *testing.T) {
+ // prepare test data
+ var buf = &bytes.Buffer{}
+ typeSerializer := &graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+ // write the size of map
+ err := binary.Write(buf, binary.BigEndian, uint32(1))
+ if err != nil {
+ t.Fatalf("Failed to write data: %v", err)
+ }
+ // write a map value as the key
+ k1 := map[string]string{"key": "value"}
+ err = typeSerializer.write(reflect.ValueOf(k1).Interface(), buf)
+ if err != nil {
+ t.Fatalf("Failed to encode data: %v", err)
+ }
+ v1 := "value1"
+ err = typeSerializer.write(reflect.ValueOf(v1).Interface(), buf)
+ if err != nil {
+ t.Fatalf("Failed to encode data: %v", err)
+ }
+
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buf.Bytes()))
+ result, err := d.readMap()
+ if err != nil {
+ t.Fatalf("readMap failed: %v", err)
+ }
+ mResult, ok := result.(map[interface{}]interface{})
+ if !ok {
+ t.Fatalf("readMap result not map[interface{}]interface{}")
+ }
+ for k, v := range mResult {
+ assert.Equal(t, reflect.Ptr, reflect.TypeOf(k).Kind())
+ assert.Equal(t, "value1", v)
+ }
+ })
+ t.Run("read incomparable map: a slice value as the key", func(t *testing.T) {
+ // prepare test data
+ var buf = &bytes.Buffer{}
+ typeSerializer := &graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+ // write the size of map
+ err := binary.Write(buf, binary.BigEndian, uint32(1))
+ if err != nil {
+ t.Fatalf("Failed to write data: %v", err)
+ }
+ // write a slice value as the key
+ k2 := []int{1, 2, 3}
+ err = typeSerializer.write(reflect.ValueOf(k2).Interface(), buf)
+ if err != nil {
+ t.Fatalf("Failed to encode data: %v", err)
+ }
+ v2 := "value2"
+ err = typeSerializer.write(reflect.ValueOf(v2).Interface(), buf)
+ if err != nil {
+ t.Fatalf("Failed to encode data: %v", err)
+ }
+
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buf.Bytes()))
+ result, err := d.readMap()
+ if err != nil {
+ t.Fatalf("readMap failed: %v", err)
+ }
+ expected := map[interface{}]interface{}{
+ "[1 2 3]": "value2",
+ }
+ if !reflect.DeepEqual(result, expected) {
+ t.Errorf("Expected %v, but got %v", expected, result)
+ }
+ })
+ t.Run("read-write time", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.Local)
+ err := dateTimeWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readDateTime()
+ assert.Nil(t, err)
+ // ISO format
+ assert.Equal(t, source.Format(time.RFC3339Nano), res.Format(time.RFC3339Nano))
+ })
+ t.Run("read-write local datetime", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.Local)
+ err := dateTimeWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readDateTime()
+ assert.Nil(t, err)
+ // ISO format
+ assert.Equal(t, source.Format(time.RFC3339Nano), res.Format(time.RFC3339Nano))
+ })
+ t.Run("read-write UTC datetime", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.UTC)
+ err := dateTimeWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readDateTime()
+ assert.Nil(t, err)
+ // ISO format
+ assert.Equal(t, source.Format(time.RFC3339Nano), res.Format(time.RFC3339Nano))
+ })
+ t.Run("read-write HST datetime", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := time.Date(2022, 5, 10, 9, 51, 34, 123456789, GetTimezoneFromOffset(-36000))
+ err := dateTimeWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ res, err := d.readDateTime()
+ assert.Nil(t, err)
+ // ISO format
+ assert.Equal(t, source.Format(time.RFC3339Nano), res.Format(time.RFC3339Nano))
+ })
+ })
+
+ t.Run("read-write marker", func(t *testing.T) {
+ var buffer bytes.Buffer
+ source := EndOfStream()
+ err := markerWriter(source, &buffer, nil)
+ assert.Nil(t, err)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(buffer.Bytes()))
+ b, err := d.readByte()
+ assert.Nil(t, err)
+ res, err := Of(b)
+ assert.Nil(t, err)
+ assert.Equal(t, source, res)
+ })
+
+}
+
+// TestWriterErrorPropagation tests that errors from io.Writer are properly propagated
+// through the serialization chain.
+// Feature: serializer-writer-refactor, Property 4: Writer Error Propagation
+// **Validates: Requirements 7.1, 7.4**
+func TestWriterErrorPropagation(t *testing.T) {
+ serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
+
+ t.Run("error propagates from stringWriter", func(t *testing.T) {
+ // String "hello" requires: 4 bytes for length + 5 bytes for content = 9 bytes
+ // Fail after 2 bytes (during length write)
+ w := &failingWriter{failAfter: 2}
+ err := stringWriter("hello", w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from stringWriter during content write", func(t *testing.T) {
+ // String "hello" requires: 4 bytes for length + 5 bytes for content = 9 bytes
+ // Fail after 6 bytes (during content write)
+ w := &failingWriter{failAfter: 6}
+ err := stringWriter("hello", w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from intWriter", func(t *testing.T) {
+ // int32 requires 4 bytes
+ // Fail after 2 bytes
+ w := &failingWriter{failAfter: 2}
+ err := intWriter(int32(42), w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from longWriter", func(t *testing.T) {
+ // int64 requires 8 bytes
+ // Fail after 4 bytes
+ w := &failingWriter{failAfter: 4}
+ err := longWriter(int64(42), w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from listWriter", func(t *testing.T) {
+ // List with one int32 element requires:
+ // 4 bytes for length + (1 byte type code + 1 byte value flag + 4 bytes int value) = 10 bytes
+ // Fail after 2 bytes (during length write)
+ w := &failingWriter{failAfter: 2}
+ err := listWriter([]interface{}{int32(1)}, w, &serializer)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from listWriter during element write", func(t *testing.T) {
+ // List with one int32 element requires:
+ // 4 bytes for length + (1 byte type code + 1 byte value flag + 4 bytes int value) = 10 bytes
+ // Fail after 6 bytes (during element write)
+ w := &failingWriter{failAfter: 6}
+ err := listWriter([]interface{}{int32(1)}, w, &serializer)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from mapWriter", func(t *testing.T) {
+ // Map with one entry requires:
+ // 4 bytes for length + key bytes + value bytes
+ // Fail after 2 bytes (during length write)
+ w := &failingWriter{failAfter: 2}
+ err := mapWriter(map[interface{}]interface{}{int32(1): "v"}, w, &serializer)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from graphBinaryTypeSerializer.write", func(t *testing.T) {
+ // Writing a fully-qualified int32 requires:
+ // 1 byte type code + 1 byte value flag + 4 bytes value = 6 bytes
+ // Fail immediately
+ w := &failingWriter{failAfter: 0}
+ err := serializer.write(int32(42), w)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from graphBinaryTypeSerializer.write during value", func(t *testing.T) {
+ // Writing a fully-qualified int32 requires:
+ // 1 byte type code + 1 byte value flag + 4 bytes value = 6 bytes
+ // Fail after 3 bytes (during value write)
+ w := &failingWriter{failAfter: 3}
+ err := serializer.write(int32(42), w)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from graphBinaryTypeSerializer.writeValue", func(t *testing.T) {
+ // Writing a non-nullable int32 value requires:
+ // 4 bytes value
+ // Fail after 2 bytes
+ w := &failingWriter{failAfter: 2}
+ err := serializer.writeValue(int32(42), w, false)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from graphBinaryTypeSerializer.writeValue nullable", func(t *testing.T) {
+ // Writing a nullable int32 value requires:
+ // 1 byte value flag + 4 bytes value = 5 bytes
+ // Fail immediately (during value flag write)
+ w := &failingWriter{failAfter: 0}
+ err := serializer.writeValue(int32(42), w, true)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from bigIntWriter", func(t *testing.T) {
+ // BigInt requires: 4 bytes for length + variable bytes for value
+ // Fail after 2 bytes (during length write)
+ w := &failingWriter{failAfter: 2}
+ err := bigIntWriter(big.NewInt(12345), w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("error propagates from dateTimeWriter", func(t *testing.T) {
+ // DateTime requires: 4 bytes year + 1 byte month + 1 byte day + 8 bytes nanoseconds + 4 bytes offset = 18 bytes
+ // Fail after 2 bytes (during year write)
+ w := &failingWriter{failAfter: 2}
+ err := dateTimeWriter(time.Now(), w, nil)
+ assert.NotNil(t, err)
+ assert.Contains(t, err.Error(), "simulated write failure")
+ })
+
+ t.Run("successful write with sufficient buffer", func(t *testing.T) {
+ // Verify that writes succeed when the writer has enough capacity
+ w := &failingWriter{failAfter: 100}
+ err := stringWriter("hello", w, nil)
+ assert.Nil(t, err)
+ // String "hello" = 4 bytes length + 5 bytes content = 9 bytes
+ assert.Equal(t, 9, w.written)
+ })
+
+ t.Run("successful write of complex type with sufficient buffer", func(t *testing.T) {
+ // Verify that complex type writes succeed when the writer has enough capacity
+ w := &failingWriter{failAfter: 100}
+ err := serializer.write([]interface{}{int32(1), "test"}, w)
+ assert.Nil(t, err)
+ // List type code (1) + value flag (1) + length (4) +
+ // int32: type code (1) + value flag (1) + value (4) +
+ // string: type code (1) + value flag (1) + length (4) + "test" (4)
+ // Total = 22 bytes
+ assert.Equal(t, 22, w.written)
+ })
+}
diff --git a/gremlin-go/driver/graphBinary_test.go b/gremlin-go/driver/graphBinary_test.go
deleted file mode 100644
index eb44538fe1..0000000000
--- a/gremlin-go/driver/graphBinary_test.go
+++ /dev/null
@@ -1,363 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements. See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership. The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License. You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied. See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package gremlingo
-
-import (
- "bytes"
- "encoding/binary"
- "fmt"
- "math/big"
- "reflect"
- "testing"
- "time"
-
- "github.com/stretchr/testify/assert"
- "golang.org/x/text/language"
-)
-
-func TestGraphBinaryV4(t *testing.T) {
- t.Run("graphBinaryTypeSerializer tests", func(t *testing.T) {
- serializer := graphBinaryTypeSerializer{newLogHandler(&defaultLogger{}, Error, language.English)}
-
- t.Run("getType should return correct type for simple type", func(t *testing.T) {
- res, err := serializer.getType(int32(1))
- assert.Nil(t, err)
- assert.Equal(t, intType, res)
- })
- t.Run("getType should return correct type for complex type", func(t *testing.T) {
- res, err := serializer.getType([]byte{1, 2, 3})
- assert.Nil(t, err)
- assert.Equal(t, listType, res)
- })
- t.Run("getType should return error for missing type", func(t *testing.T) {
- _, err := serializer.getType(Error)
- assert.NotNil(t, err)
- })
-
- t.Run("getWriter should return correct func for dataType", func(t *testing.T) {
- writer, err := serializer.getWriter(intType)
- assert.Nil(t, err)
- assert.Equal(t, reflect.ValueOf(intWriter).Pointer(), reflect.ValueOf(writer).Pointer())
- })
- t.Run("getWriter should return error for missing dataType", func(t *testing.T) {
- _, err := serializer.getWriter(nullType)
- assert.NotNil(t, err)
- })
-
- t.Run("getSerializerToWrite should return correct func for int", func(t *testing.T) {
- writer, dataType, err := serializer.getSerializerToWrite(int32(1))
- assert.Nil(t, err)
- assert.Equal(t, intType, dataType)
- assert.Equal(t, reflect.ValueOf(intWriter).Pointer(), reflect.ValueOf(writer).Pointer())
- })
- t.Run("getSerializerToWrite should return error for missing dataType", func(t *testing.T) {
- _, _, err := serializer.getSerializerToWrite(nullType)
- assert.NotNil(t, err)
- })
- })
-
- t.Run("read-write tests", func(t *testing.T) {
- t.Run("read-write string", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- str := "test string"
- buf, err := stringWriter(str, &buffer, nil)
- assert.Nil(t, err)
- res, err := readString(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, str, res)
- })
- t.Run("read-write bool", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- f := func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value.(bool))
- return buffer.Bytes(), err
- }
- data, err := f(false, &buffer, nil)
- assert.Nil(t, err)
- res, err := readBoolean(&data, &pos)
- assert.Nil(t, err)
- assert.False(t, res.(bool))
-
- data, err = f(true, &buffer, nil)
- assert.Nil(t, err)
- res, err = readBoolean(&data, &pos)
- assert.Nil(t, err)
- assert.True(t, res.(bool))
- })
- t.Run("read-write BigDecimal", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := &BigDecimal{11, big.NewInt(int64(22))}
- buf, err := bigDecimalWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readBigDecimal(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write int", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := int32(123)
- buf, err := intWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readInt(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write short", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := int16(123)
- buf, err := shortWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readShort(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write short int8", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := int8(123)
- buf, err := shortWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readShort(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, int16(source), res)
- })
- t.Run("read-write long", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := 123
- buf, err := longWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readLong(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, int64(source), res)
- })
- t.Run("read-write bigInt", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := big.NewInt(123)
- buf, err := bigIntWriter(*source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readBigInt(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write bigInt uint64", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := uint64(123)
- buf, err := bigIntWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readBigInt(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, new(big.Int).SetUint64(source), res)
- })
- t.Run("read-write bigInt uint64", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := uint(123)
- buf, err := bigIntWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readBigInt(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, new(big.Int).SetUint64(uint64(source)), res)
- })
- t.Run("read-write list", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := []interface{}{int32(111), "str"}
- buf, err := listWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readList(&buf, &pos, 0x00)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write byteBuffer", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := &ByteBuffer{[]byte{byte(127), byte(255)}}
- buf, err := byteBufferWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readByteBuffer(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write set", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := NewSimpleSet(int32(111), "str")
- buf, err := setWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readSet(&buf, &pos, 0x00)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
- t.Run("read-write map", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := map[interface{}]interface{}{1: "s1", "s2": 2, nil: nil}
- buf, err := mapWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := readMap(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, fmt.Sprintf("%v", source), fmt.Sprintf("%v", res))
- })
- t.Run("read incomparable map: a map value as the key", func(t *testing.T) {
- // prepare test data
- var buf = &bytes.Buffer{}
- typeSerializer := &graphBinaryTypeSerializer{}
- // write the size of map
- err := binary.Write(buf, binary.BigEndian, uint32(1))
- if err != nil {
- t.Fatalf("Failed to write data: %v", err)
- }
- // write a map value as the key
- k1 := map[string]string{"key": "value"}
- _, err = typeSerializer.write(reflect.ValueOf(k1).Interface(), buf)
- if err != nil {
- t.Fatalf("Failed to encode data: %v", err)
- }
- v1 := "value1"
- _, err = typeSerializer.write(reflect.ValueOf(v1).Interface(), buf)
- if err != nil {
- t.Fatalf("Failed to encode data: %v", err)
- }
-
- data := buf.Bytes()
- i := 0
- result, err := readMap(&data, &i)
- if err != nil {
- t.Fatalf("readMap failed: %v", err)
- }
- mResult, ok := result.(map[interface{}]interface{})
- if !ok {
- t.Fatalf("readMap result not map[interface{}]interface{}")
- }
- for k, v := range mResult {
- assert.Equal(t, reflect.Ptr, reflect.TypeOf(k).Kind())
- assert.Equal(t, "value1", v)
- }
- })
- t.Run("read incomparable map: a slice value as the key", func(t *testing.T) {
- // prepare test data
- var buf = &bytes.Buffer{}
- typeSerializer := &graphBinaryTypeSerializer{}
- // write the size of map
- err := binary.Write(buf, binary.BigEndian, uint32(1))
- if err != nil {
- t.Fatalf("Failed to write data: %v", err)
- }
- // write a slice value as the key
- k2 := []int{1, 2, 3}
- _, err = typeSerializer.write(reflect.ValueOf(k2).Interface(), buf)
- if err != nil {
- t.Fatalf("Failed to encode data: %v", err)
- }
- v2 := "value2"
- _, err = typeSerializer.write(reflect.ValueOf(v2).Interface(), buf)
- if err != nil {
- t.Fatalf("Failed to encode data: %v", err)
- }
-
- data := buf.Bytes()
- i := 0
- result, err := readMap(&data, &i)
- if err != nil {
- t.Fatalf("readMap failed: %v", err)
- }
- expected := map[interface{}]interface{}{
- "[1 2 3]": "value2",
- }
- if !reflect.DeepEqual(result, expected) {
- t.Errorf("Expected %v, but got %v", expected, result)
- }
- })
- t.Run("read-write time", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.Local)
- buf, err := dateTimeWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := dateTimeReader(&buf, &pos)
- assert.Nil(t, err)
- // ISO format
- assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
- })
- t.Run("read-write local datetime", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.Local)
- buf, err := dateTimeWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := dateTimeReader(&buf, &pos)
- assert.Nil(t, err)
- // ISO format
- assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
- })
- t.Run("read-write UTC datetime", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.UTC)
- buf, err := dateTimeWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := dateTimeReader(&buf, &pos)
- assert.Nil(t, err)
- // ISO format
- assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
- })
- t.Run("read-write HST datetime", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := time.Date(2022, 5, 10, 9, 51, 34, 123456789, GetTimezoneFromOffset(-36000))
- buf, err := dateTimeWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := dateTimeReader(&buf, &pos)
- assert.Nil(t, err)
- // ISO format
- assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
- })
- })
-
- t.Run("error handle tests", func(t *testing.T) {
- t.Run("test map key not string failure", func(t *testing.T) {
- i := 0
- buff := []byte{0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01}
- m, err := readMapUnqualified(&buff, &i)
- assert.Nil(t, m)
- assert.Equal(t, newError(err0703ReadMapNonStringKeyError, intType), err)
- })
- })
-
- t.Run("read-write marker", func(t *testing.T) {
- pos := 0
- var buffer bytes.Buffer
- source := EndOfStream()
- buf, err := markerWriter(source, &buffer, nil)
- assert.Nil(t, err)
- res, err := markerReader(&buf, &pos)
- assert.Nil(t, err)
- assert.Equal(t, source, res)
- })
-
-}
diff --git a/gremlin-go/driver/gremlinlang.go b/gremlin-go/driver/gremlinlang.go
index 498cda8206..cd2b724487 100644
--- a/gremlin-go/driver/gremlinlang.go
+++ b/gremlin-go/driver/gremlinlang.go
@@ -567,17 +567,3 @@ func (gl *GremlinLang) convertArgument(arg interface{}) (interface{}, error) {
}
}
}
-
-// TODO revisit and remove if necessary
-//var withOptionsMap map[any]string = map[any]string{
-// WithOptions.Tokens: "WithOptions.tokens",
-// WithOptions.None: "WithOptions.none",
-// WithOptions.Ids: "WithOptions.ids",
-// WithOptions.Labels: "WithOptions.labels",
-// WithOptions.Keys: "WithOptions.keys",
-// WithOptions.Values: "WithOptions.values",
-// WithOptions.All: "WithOptions.all",
-// WithOptions.Indexer: "WithOptions.indexer",
-// WithOptions.List: "WithOptions.list",
-// WithOptions.Map: "WithOptions.map",
-//}
diff --git a/gremlin-go/driver/httpProtocol.go b/gremlin-go/driver/httpProtocol.go
deleted file mode 100644
index 9c9bba4a91..0000000000
--- a/gremlin-go/driver/httpProtocol.go
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements. See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership. The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License. You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied. See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package gremlingo
-
-import (
- "fmt"
- "net/http"
-)
-
-// responsible for serializing and sending requests and then receiving and deserializing responses
-type httpProtocol struct {
- serializer Serializer
- logHandler *logHandler
- url string
- connSettings *connectionSettings
- httpClient *http.Client
-}
-
-func newHttpProtocol(handler *logHandler, url string, connSettings *connectionSettings) *httpProtocol {
- transport := &http.Transport{
- TLSClientConfig: connSettings.tlsConfig,
- MaxConnsPerHost: 0, // TODO
- IdleConnTimeout: 0, // TODO
- DisableCompression: !connSettings.enableCompression,
- }
-
- httpClient := http.Client{
- Transport: transport,
- Timeout: connSettings.connectionTimeout,
- }
-
- httpProt := &httpProtocol{
- serializer: newGraphBinarySerializer(handler),
- logHandler: handler,
- url: url,
- connSettings: connSettings,
- httpClient: &httpClient,
- }
- return httpProt
-}
-
-// sends a query request and returns a ResultSet that can be used to obtain query results
-func (protocol *httpProtocol) send(request *request) (ResultSet, error) {
- rs := newChannelResultSet()
- fmt.Println("Serializing request")
- bytes, err := protocol.serializer.SerializeMessage(request)
- if err != nil {
- rs.setError(err)
- rs.Close()
- return rs, err
- }
-
- // one transport per request
- transport := NewHttpTransporter(protocol.url, protocol.connSettings, protocol.httpClient, protocol.logHandler)
-
- // async send request
- transport.wg.Add(1)
- go func() {
- defer transport.wg.Done()
- err := transport.Write(bytes)
- if err != nil {
- rs.setError(err)
- rs.Close()
- }
- }()
-
- // async receive response
- transport.wg.Add(1)
- go func() {
- defer transport.wg.Done()
- msg, err := transport.Read()
- if err != nil {
- rs.setError(err)
- rs.Close()
- } else {
- err = protocol.receive(rs, msg)
- }
- transport.Close()
- }()
-
- // Wait for both async operations to complete
- transport.wg.Wait()
-
- return rs, rs.GetError()
-}
-
-// receives a binary response message, deserializes, and adds results to the ResultSet
-func (protocol *httpProtocol) receive(rs ResultSet, msg []byte) error {
- fmt.Println("Deserializing response")
- resp, err := protocol.serializer.DeserializeMessage(msg)
- if err != nil {
- protocol.logHandler.logf(Error, logErrorGeneric, "receive()", err.Error())
- rs.Close()
- return err
- }
-
- fmt.Println("Handling response")
- err = protocol.handleResponse(rs, resp)
- if err != nil {
- protocol.logHandler.logf(Error, logErrorGeneric, "receive()", err.Error())
- rs.Close()
- return err
- }
- return nil
-}
-
-// processes a deserialized response and attempts to add results to the ResultSet
-func (protocol *httpProtocol) handleResponse(rs ResultSet, response Response) error {
- fmt.Println("Handling response")
-
- statusCode, data := response.ResponseStatus.code, response.ResponseResult.Data
- if rs == nil {
- return newError(err0501ResponseHandlerResultSetNotCreatedError)
- }
-
- if statusCode == http.StatusNoContent {
- rs.addResult(&Result{make([]interface{}, 0)})
- rs.Close()
- protocol.logHandler.logf(Debug, readComplete)
- } else if statusCode == http.StatusOK {
- rs.addResult(&Result{data})
- rs.Close()
- protocol.logHandler.logf(Debug, readComplete)
- } else if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
- rs.Close()
- err := newError(err0503ResponseHandlerAuthError, response.ResponseStatus, response.ResponseResult)
- rs.setError(err)
- return err
- } else {
- rs.Close()
- err := newError(err0502ResponseHandlerReadLoopError, response.ResponseStatus, statusCode)
- rs.setError(err)
- return err
- }
- return nil
-}
-
-func (protocol *httpProtocol) close() {
- protocol.httpClient.CloseIdleConnections()
-}
diff --git a/gremlin-go/driver/httpTransporter.go b/gremlin-go/driver/httpTransporter.go
deleted file mode 100644
index 94059a19c1..0000000000
--- a/gremlin-go/driver/httpTransporter.go
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements. See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership. The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License. You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied. See the License for the
-specific language governing permissions and limitations
-under the License.
-*/
-
-package gremlingo
-
-import (
- "bytes"
- "compress/zlib"
- "errors"
- "fmt"
- "io"
- "net/http"
- "sync"
-)
-
-// TODO decide channel size when chunked response handling is implemented - for now just set to 1
-const responseChannelSizeDefault = 1
-
-// HttpTransporter responsible for sending and receiving bytes to/from the server
-type HttpTransporter struct {
- url string
- isClosed bool
- connSettings *connectionSettings
- responseChannel chan []byte // receives response bytes from the server
- httpClient *http.Client
- wg *sync.WaitGroup
- logHandler *logHandler
-}
-
-func NewHttpTransporter(url string, connSettings *connectionSettings, httpClient *http.Client, logHandler *logHandler) *HttpTransporter {
- wg := &sync.WaitGroup{}
-
- return &HttpTransporter{
- url: url,
- connSettings: connSettings,
- responseChannel: make(chan []byte, responseChannelSizeDefault),
- httpClient: httpClient,
- wg: wg,
- logHandler: logHandler,
- }
-}
-
-// Write sends bytes to the server as a POST request and sends received response bytes to the responseChannel
-func (transporter *HttpTransporter) Write(data []byte) error {
- req, err := http.NewRequest("POST", transporter.url, bytes.NewBuffer(data))
- if err != nil {
- transporter.logHandler.logf(Error, failedToSendRequest, err.Error())
- return err
- }
- req.Header.Set("content-type", graphBinaryMimeType)
- req.Header.Set("accept", graphBinaryMimeType)
- if transporter.connSettings.enableUserAgentOnConnect {
- req.Header.Set(userAgentHeader, userAgent)
- }
- if transporter.connSettings.enableCompression {
- req.Header.Set("accept-encoding", "deflate")
- }
- if transporter.connSettings.authInfo != nil {
- // Add custom headers
- if headers := transporter.connSettings.authInfo.GetHeader(); headers != nil {
- for key, values := range headers {
- for _, value := range values {
- req.Header.Add(key, value)
- }
- }
- }
-
- // Add basic auth
- if ok, username, password := transporter.connSettings.authInfo.GetBasicAuth(); ok {
- req.SetBasicAuth(username, password)
- }
- }
-
- fmt.Println("Sending request")
- resp, err := transporter.httpClient.Do(req)
- if err != nil {
- transporter.logHandler.logf(Error, failedToSendRequest, err.Error())
- return err
- }
-
- reader := resp.Body
- if resp.Header.Get("content-encoding") == "deflate" {
- reader, err = zlib.NewReader(resp.Body)
- if err != nil {
- transporter.logHandler.logf(Error, failedToReceiveResponse, err.Error())
- return err
- }
- }
-
- // TODO handle chunked encoding and send chunks to responseChannel
- all, err := io.ReadAll(reader)
- if err != nil {
- transporter.logHandler.logf(Error, failedToReceiveResponse, err.Error())
- return err
- }
- err = reader.Close()
- if err != nil {
- return err
- }
-
- // TODO for debug, remove later, and check response handling
- //str := hex.EncodeToString(all)
- //_, _ = fmt.Fprintf(os.Stdout, "Received response data : %s\n", str)
-
- fmt.Println("Sending response to responseChannel")
- transporter.responseChannel <- all
- return nil
-}
-
-// Read reads bytes from the responseChannel
-func (transporter *HttpTransporter) Read() ([]byte, error) {
- fmt.Println("Reading from responseChannel")
- msg, ok := <-transporter.responseChannel
- if !ok {
- return []byte{}, errors.New("failed to read from response channel")
- }
- return msg, nil
-}
-
-// Close closes the transporter and its corresponding responseChannel
-func (transporter *HttpTransporter) Close() {
- fmt.Println("Closing http transporter")
- if !transporter.isClosed {
- if transporter.responseChannel != nil {
- close(transporter.responseChannel)
- }
- transporter.isClosed = true
- }
-}
diff --git a/gremlin-go/driver/logger.go b/gremlin-go/driver/logger.go
index f1f8c6986a..388ce44a2c 100644
--- a/gremlin-go/driver/logger.go
+++ b/gremlin-go/driver/logger.go
@@ -109,10 +109,11 @@ const (
submitStartedString errorKey = "SUBMIT_STARTED_STRING"
failedToCloseInErrorCallback errorKey = "FAILED_TO_CLOSE_IN_ERROR_CALLBACK"
failedToWriteMessage errorKey = "FAILED_TO_WRITE_MESSAGE"
- failedToSetWriteDeadline errorKey = "FAILED_TO_SET_WRITE_DEADLINE"
failedToReceiveResponse errorKey = "FAILED_TO_RECEIVE_RESPONSE"
failedToSendRequest errorKey = "FAILED_TO_SEND_REQUEST"
logErrorGeneric errorKey = "LOG_ERROR_GENERIC"
closeDriverRemoteConnection errorKey = "CLOSE_DRIVER_REMOTE_CONNECTION"
closeClient errorKey = "CLOSE_CLIENT"
+ failedToCloseResponseBody errorKey = "FAILED_TO_CLOSE_RESPONSE_BODY"
+ failedToCloseDecompReader errorKey = "FAILED_TO_CLOSE_DECOMPRESSION_READER"
)
diff --git a/gremlin-go/driver/performance/performanceSuite.go b/gremlin-go/driver/performance/performanceSuite.go
index ba2b493b91..bcad844f67 100644
--- a/gremlin-go/driver/performance/performanceSuite.go
+++ b/gremlin-go/driver/performance/performanceSuite.go
@@ -378,10 +378,7 @@ func createConnection(host string, port, poolSize, buffersSize int) (*GraphTrave
drc, err = gremlingo.NewDriverRemoteConnection(endpoint, func(settings *DriverRemoteConnectionSettings) {
settings.LogVerbosity = GremlinWarning
settings.TraversalSource = gratefulGraphAlias
- settings.NewConnectionThreshold = threshold
settings.MaximumConcurrentConnections = poolSize
- settings.WriteBufferSize = buffersSize
- settings.ReadBufferSize = buffersSize
})
if err != nil {
diff --git a/gremlin-go/driver/request.go b/gremlin-go/driver/request.go
index 022ddc484d..752dd16dcd 100644
--- a/gremlin-go/driver/request.go
+++ b/gremlin-go/driver/request.go
@@ -28,7 +28,7 @@ type request struct {
// MakeStringRequest creates a request from a Gremlin string query for submission to a Gremlin server.
//
// This function is exposed publicly to enable alternative transport protocols (gRPC, HTTP/2, etc.)
-// to construct properly formatted requests outside the standard WebSocket client. The returned
+// to construct properly formatted requests outside the standard HTTP client. The returned
// request can then be serialized using SerializeMessage().
//
// Parameters:
diff --git a/gremlin-go/driver/resources/error-messages/en.json b/gremlin-go/driver/resources/error-messages/en.json
index cfd070af47..ecae8a1510 100644
--- a/gremlin-go/driver/resources/error-messages/en.json
+++ b/gremlin-go/driver/resources/error-messages/en.json
@@ -21,9 +21,10 @@
"E0408_GRAPH_BINARY_GETSERIALIZERTOREAD_UNKNOWN_TYPE_ERROR": "E0408: unknown data type to deserialize 0x%x",
"E0409_GRAPH_BINARY_GETSERIALIZERTOREAD_UNKNOWN_CUSTOM_TYPE_ERROR": "E0409: unknown custom data type to deserialize %s",
- "E0501_PROTOCOL_RESPONSEHANDLER_NO_RESULTSET_ON_DATA_RECEIVE":"E0501: resultSet was not created before data was received",
- "E0502_PROTOCOL_RESPONSEHANDLER_ERROR": "E0502: error handling response, error message '%+v'. statusCode: %d",
- "E0503_PROTOCOL_RESPONSEHANDLER_AUTH_ERROR":"E0503: failed to authenticate %v : %v",
+ "E0501_RESPONSE_NO_RESULTSET_ON_DATA_RECEIVE":"E0501: resultSet was not created before data was received",
+ "E0502_RESPONSE_ERROR": "E0502: error handling response, error message '%+v'. statusCode: %d",
+ "E0502_RESPONSE_READ_LOOP_ERROR": "E0502: error in read loop, error message '%v'. statusCode: %d",
+ "E0503_RESPONSE_AUTH_ERROR":"E0503: failed to authenticate %v : %v",
"E0601_RESULT_NOT_VERTEX_ERROR":"E0601: result is not a Vertex",
"E0602_RESULT_NOT_EDGE_ERROR": "E0602: result is not an Edge",
diff --git a/gremlin-go/driver/resources/logger-messages/en.json b/gremlin-go/driver/resources/logger-messages/en.json
index 82bdce46a3..f7025e837a 100644
--- a/gremlin-go/driver/resources/logger-messages/en.json
+++ b/gremlin-go/driver/resources/logger-messages/en.json
@@ -20,5 +20,7 @@
"POOL_NEW_CONNECTION_ERROR": "Falling back to least-used connection. Creating new connection due to least-used connection exceeding concurrent usage threshold failed: %s",
"POOL_INITIAL_EXCEEDS_MAXIMUM": "InitialConcurrentConnections setting %d exceeded MaximumConcurrentConnections setting %d - limiting InitialConcurrentConnections to %d.",
"FAILED_TO_RECEIVE_RESPONSE": "Failed to receive response: %s",
- "FAILED_TO_SEND_REQUEST": "Failed to send request: %s"
+ "FAILED_TO_SEND_REQUEST": "Failed to send request: %s",
+ "FAILED_TO_CLOSE_RESPONSE_BODY": "Error closing response body: %s",
+ "FAILED_TO_CLOSE_DECOMPRESSION_READER": "Error closing decompression reader: %s"
}
diff --git a/gremlin-go/driver/resultSet.go b/gremlin-go/driver/resultSet.go
index 59e22e66d1..cf1230cd53 100644
--- a/gremlin-go/driver/resultSet.go
+++ b/gremlin-go/driver/resultSet.go
@@ -152,8 +152,8 @@ func (channelResultSet *channelResultSet) All() ([]*Result, error) {
func (channelResultSet *channelResultSet) addResult(r *Result) {
channelResultSet.channelMutex.Lock()
- if r.GetType().Kind() == reflect.Array || r.GetType().Kind() == reflect.Slice {
- for _, v := range r.Data.([]interface{}) {
+ if data, ok := r.Data.([]interface{}); ok {
+ for _, v := range data {
if reflect.TypeOf(v) == reflect.TypeOf(&Traverser{}) {
for i := int64(0); i < (v.(*Traverser)).bulk; i++ {
channelResultSet.channel <- &Result{(v.(*Traverser)).value}
diff --git a/gremlin-go/driver/serializer.go b/gremlin-go/driver/serializer.go
index 68be6862ac..69b870f1c7 100644
--- a/gremlin-go/driver/serializer.go
+++ b/gremlin-go/driver/serializer.go
@@ -22,6 +22,7 @@ package gremlingo
import (
"bytes"
"encoding/binary"
+ "io"
"sync"
)
@@ -39,26 +40,28 @@ type GraphBinarySerializer struct {
}
// CustomTypeReader user provided function to deserialize custom types
+// Deprecated: Custom type deserialization is handled by GraphBinaryDeserializer
type CustomTypeReader func(data *[]byte, i *int) (interface{}, error)
-type writer func(interface{}, *bytes.Buffer, *graphBinaryTypeSerializer) ([]byte, error)
-type reader func(data *[]byte, i *int) (interface{}, error)
+type writer func(interface{}, io.Writer, *graphBinaryTypeSerializer) error
-var deserializers map[dataType]reader
var serializers map[dataType]writer
// customTypeReaderLock used to synchronize access to the customDeserializers map
+// Deprecated: Custom type deserialization is handled by GraphBinaryDeserializer
var customTypeReaderLock = sync.RWMutex{}
var customDeserializers map[string]CustomTypeReader
func init() {
initSerializers()
- initDeserializers()
+ customDeserializers = map[string]CustomTypeReader{}
}
-func newGraphBinarySerializer(handler *logHandler) Serializer {
+func newGraphBinarySerializer(handler *logHandler) *GraphBinarySerializer {
serializer := graphBinaryTypeSerializer{handler}
- return GraphBinarySerializer{&serializer}
+ return &GraphBinarySerializer{
+ ser: &serializer,
+ }
}
// TODO change for graph binary 4.0 version is finalized
@@ -66,7 +69,7 @@ const versionByte byte = 0x81
// SerializeMessage serializes a request message into GraphBinary format.
//
-// This method is part of the serializer interface and is used internally by the WebSocket driver.
+// This method is part of the serializer interface and is used internally by the HTTP driver.
// It is also exposed publicly to enable alternative transport protocols (gRPC, HTTP/2, etc.) to
// serialize requests created with MakeBytecodeRequest() or MakeStringRequest().
//
@@ -86,8 +89,8 @@ const versionByte byte = 0x81
// bytes, err := serializer.(graphBinarySerializer).SerializeMessage(&req)
// // Send bytes over custom transport
//
-// serializeMessage serializes a request message into GraphBinary.
-func (gs GraphBinarySerializer) SerializeMessage(request *request) ([]byte, error) {
+// SerializeMessage serializes a request message into GraphBinary.
+func (gs *GraphBinarySerializer) SerializeMessage(request *request) ([]byte, error) {
finalMessage, err := gs.buildMessage(request.gremlin, request.fields)
if err != nil {
return nil, err
@@ -101,11 +104,11 @@ func (gs *GraphBinarySerializer) buildMessage(gremlin string, args map[string]in
// Version
buffer.WriteByte(versionByte)
- _, err := gs.ser.writeValue(args, &buffer, false)
+ err := gs.ser.writeValue(args, &buffer, false)
if err != nil {
return nil, err
}
- _, err = gs.ser.writeValue(gremlin, &buffer, false)
+ err = gs.ser.writeValue(gremlin, &buffer, false)
if err != nil {
return nil, err
}
@@ -115,7 +118,7 @@ func (gs *GraphBinarySerializer) buildMessage(gremlin string, args map[string]in
// DeserializeMessage deserializes a GraphBinary-encoded response message.
//
-// This method is part of the serializer interface and is used internally by the WebSocket driver.
+// This method is part of the serializer interface and is used internally by the HTTP driver.
// It is also exposed publicly to enable alternative transport protocols (gRPC, HTTP/2, etc.) to
// deserialize responses received from a Gremlin server.
//
@@ -132,52 +135,48 @@ func (gs *GraphBinarySerializer) buildMessage(gremlin string, args map[string]in
// serializer := newGraphBinarySerializer(nil)
// resp, err := serializer.(graphBinarySerializer).DeserializeMessage(responseBytes)
// results := resp.responseResult.data
-func (gs GraphBinarySerializer) DeserializeMessage(message []byte) (Response, error) {
+func (gs *GraphBinarySerializer) DeserializeMessage(message []byte) (Response, error) {
var msg Response
if message == nil || len(message) == 0 {
gs.ser.logHandler.log(Error, nullInput)
return msg, newError(err0405ReadValueInvalidNullInputError)
}
- results := make([]interface{}, 0)
- //Skip version and nullable byte.
- i := 2
- // TODO temp serialization before fully streaming set-up
- for len(message) > 0 {
- n, err := readFullyQualifiedNullable(&message, &i, true)
+ d := NewGraphBinaryDeserializer(bytes.NewReader(message))
+
+ // Read header
+ if err := d.ReadHeader(); err != nil {
+ return msg, err
+ }
+
+ results := make([]interface{}, 0)
+ for {
+ n, err := d.ReadFullyQualified()
if err != nil {
return msg, err
}
- // TODO for debug, remove later
- //_, _ = fmt.Fprintf(os.Stdout, "Deserializing data : %v\n", n)
if n == EndOfStream() {
break
}
results = append(results, n)
}
- // TODO for debug, remove later
- //_, _ = fmt.Fprintf(os.Stdout, "Deserialized results : %s\n", results)
msg.ResponseResult.Data = results
- code := readUint32Safe(&message, &i)
- msg.ResponseStatus.code = code
- // TODO read status message
- msg.ResponseStatus.message = "OK"
- statusMsg, err := readUnqualified(&message, &i, stringType, true)
+
+ code, statusMsg, exception, err := d.ReadStatus()
if err != nil {
return msg, err
}
- if statusMsg != nil {
- msg.ResponseStatus.message = statusMsg.(string)
- }
- exception, err := readUnqualified(&message, &i, stringType, true)
- if err != nil {
- return msg, err
+ msg.ResponseStatus.code = code
+ msg.ResponseStatus.message = "OK"
+ if statusMsg != "" {
+ msg.ResponseStatus.message = statusMsg
}
- if exception != nil {
- msg.ResponseStatus.exception = exception.(string)
+ if exception != "" {
+ msg.ResponseStatus.exception = exception
}
+
return msg, nil
}
@@ -189,25 +188,20 @@ func initSerializers() {
longType: longWriter,
intType: intWriter,
shortType: shortWriter,
- byteType: func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value.(uint8))
- return buffer.Bytes(), err
+ byteType: func(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(w, binary.BigEndian, value.(uint8))
},
- booleanType: func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value.(bool))
- return buffer.Bytes(), err
+ booleanType: func(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(w, binary.BigEndian, value.(bool))
},
- uuidType: func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value)
- return buffer.Bytes(), err
+ uuidType: func(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(w, binary.BigEndian, value)
},
- floatType: func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value)
- return buffer.Bytes(), err
+ floatType: func(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(w, binary.BigEndian, value)
},
- doubleType: func(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
- err := binary.Write(buffer, binary.BigEndian, value)
- return buffer.Bytes(), err
+ doubleType: func(value interface{}, w io.Writer, typeSerializer *graphBinaryTypeSerializer) error {
+ return binary.Write(w, binary.BigEndian, value)
},
vertexType: vertexWriter,
edgeType: edgeWriter,
@@ -228,50 +222,8 @@ func initSerializers() {
}
}
-func initDeserializers() {
- deserializers = map[dataType]reader{
- // Primitive
- booleanType: readBoolean,
- byteType: readByte,
- shortType: readShort,
- intType: readInt,
- longType: readLong,
- bigDecimalType: readBigDecimal,
- bigIntegerType: readBigInt,
- floatType: readFloat,
- doubleType: readDouble,
- stringType: readString,
-
- // Composite
- //listType: readList,
- //setType: readSet,
- mapType: readMap,
- uuidType: readUuid,
- byteBuffer: readByteBuffer,
-
- // Date Time
- datetimeType: dateTimeReader,
- durationType: durationReader,
-
- // Graph
- vertexType: vertexReader,
- edgeType: edgeReader,
- propertyType: propertyReader,
- vertexPropertyType: vertexPropertyReader,
- pathType: pathReader,
- tType: enumReader,
- directionType: enumReader,
- gTypeType: enumReader,
-
- // Customer
- customType: customTypeReader,
-
- markerType: markerReader,
- }
- customDeserializers = map[string]CustomTypeReader{}
-}
-
// RegisterCustomTypeReader register a reader (deserializer) for a custom type
+// Deprecated: Custom type deserialization should be handled by extending GraphBinaryDeserializer
func RegisterCustomTypeReader(customTypeName string, reader CustomTypeReader) {
customTypeReaderLock.Lock()
defer customTypeReaderLock.Unlock()
@@ -279,6 +231,7 @@ func RegisterCustomTypeReader(customTypeName string, reader CustomTypeReader) {
}
// UnregisterCustomTypeReader unregister a reader (deserializer) for a custom type
+// Deprecated: Custom type deserialization should be handled by extending GraphBinaryDeserializer
func UnregisterCustomTypeReader(customTypeName string) {
customTypeReaderLock.Lock()
defer customTypeReaderLock.Unlock()
diff --git a/gremlin-go/driver/strategies_test.go b/gremlin-go/driver/strategies_test.go
index ee1435890b..2a855be2d5 100644
--- a/gremlin-go/driver/strategies_test.go
+++ b/gremlin-go/driver/strategies_test.go
@@ -27,11 +27,10 @@ import (
"github.com/stretchr/testify/assert"
)
-func getModernGraph(t *testing.T, url string, auth AuthInfoProvider, tls *tls.Config) *GraphTraversalSource {
+func getModernGraph(t *testing.T, url string, tls *tls.Config) *GraphTraversalSource {
remote, err := NewDriverRemoteConnection(url,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = tls
- settings.AuthInfo = auth
settings.TraversalSource = testServerModernGraphAlias
})
assert.Nil(t, err)
@@ -45,7 +44,7 @@ func TestStrategy(t *testing.T) {
testNoAuthUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
t.Run("Test read with ConnectiveStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(ConnectiveStrategy()).V().Count().ToList()
@@ -58,7 +57,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with OptionsStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(OptionsStrategy(map[string]interface{}{"a": "b"})).V().Count().ToList()
@@ -71,7 +70,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with PartitionStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := PartitionStrategyConfig{
@@ -90,7 +89,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with SeedStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := SeedStrategyConfig{Seed: 1}
@@ -104,7 +103,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with SubgraphStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := SubgraphStrategyConfig{
Vertices: T__.HasLabel(testLabel),
@@ -130,7 +129,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with AdjacentToIncidentStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(AdjacentToIncidentStrategy()).V().Count().ToList()
@@ -143,7 +142,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with ByModulatorOptimizationStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(ByModulatorOptimizationStrategy()).V().Count().ToList()
@@ -156,7 +155,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with CountStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(CountStrategy()).V().Count().ToList()
@@ -169,7 +168,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with EarlyLimitStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(EarlyLimitStrategy()).V().Count().ToList()
@@ -182,7 +181,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with FilterRankingStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(FilterRankingStrategy()).V().Count().ToList()
@@ -195,7 +194,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with IdentityRemovalStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(IdentityRemovalStrategy()).V().Count().ToList()
@@ -208,7 +207,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with IncidentToAdjacentStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(IncidentToAdjacentStrategy()).V().Count().ToList()
@@ -221,7 +220,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with InlineFilterStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(InlineFilterStrategy()).V().Count().ToList()
@@ -234,7 +233,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with LazyBarrierStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(LazyBarrierStrategy()).V().Count().ToList()
@@ -247,7 +246,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with MatchPredicateStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(MatchPredicateStrategy()).V().Count().ToList()
@@ -260,7 +259,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with OrderLimitStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(OrderLimitStrategy()).V().Count().ToList()
@@ -273,7 +272,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with PathProcessorStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(PathProcessorStrategy()).V().Count().ToList()
@@ -286,7 +285,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with PathRetractionStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(PathRetractionStrategy()).V().Count().ToList()
@@ -299,7 +298,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with ProductiveByStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := ProductiveByStrategyConfig{ProductiveKeys: []string{"a", "b"}}
@@ -313,7 +312,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with RepeatUnrollStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(RepeatUnrollStrategy()).V().Count().ToList()
@@ -326,7 +325,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with EdgeLabelVerificationStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := EdgeLabelVerificationStrategyConfig{
@@ -343,7 +342,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with LambdaRestrictionStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(LambdaRestrictionStrategy()).V().Count().ToList()
@@ -356,7 +355,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with TestReadOnlyStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(ReadOnlyStrategy()).V().Count().ToList()
@@ -369,7 +368,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test write with TestReadOnlyStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
promise := g.WithStrategies(ReadOnlyStrategy()).AddV("person").Property("name", "foo").Iterate()
@@ -377,7 +376,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with ReservedKeysVerificationStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
config := ReservedKeysVerificationStrategyConfig{
@@ -396,7 +395,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test read with RepeatUnrollStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithStrategies(RepeatUnrollStrategy()).V().Count().ToList()
@@ -409,7 +408,7 @@ func TestStrategy(t *testing.T) {
})
t.Run("Test without strategies MessagePassingReductionStrategy", func(t *testing.T) {
- g := getModernGraph(t, testNoAuthUrl, &AuthInfo{}, &tls.Config{})
+ g := getModernGraph(t, testNoAuthUrl, &tls.Config{})
defer g.remoteConnection.Close()
count, err := g.WithoutStrategies(MessagePassingReductionStrategy()).V().Count().ToList()
diff --git a/gremlin-go/driver/traversal.go b/gremlin-go/driver/traversal.go
index 3ac5e934f3..40e8d79785 100644
--- a/gremlin-go/driver/traversal.go
+++ b/gremlin-go/driver/traversal.go
@@ -44,7 +44,6 @@ func (t *Traversal) ToList() ([]*Result, error) {
return nil, newError(err0901ToListAnonTraversalError)
}
- // TODO update and test when connection is set up
results, err := t.remote.submitGremlinLang(t.GremlinLang)
if err != nil {
return nil, err
@@ -80,7 +79,6 @@ func (t *Traversal) Iterate() <-chan error {
t.GremlinLang.AddStep("discard")
- // TODO update and test when connection is set up
res, err := t.remote.submitGremlinLang(t.GremlinLang)
if err != nil {
r <- err
@@ -124,7 +122,6 @@ func (t *Traversal) Next() (*Result, error) {
// GetResultSet submits the traversal and returns the ResultSet.
func (t *Traversal) GetResultSet() (ResultSet, error) {
if t.results == nil {
- // TODO update and test when connection is set up
results, err := t.remote.submitGremlinLang(t.GremlinLang)
if err != nil {
return nil, err
@@ -749,33 +746,6 @@ var IO = ioconfig{
Registry: "~tinkerpop.ioconfig.registry",
}
-// TODO pending update/removal
-// Metrics holds metrics data; typically for .profile()-step analysis. Metrics may be nested. Nesting enables
-// the ability to capture explicit metrics for multiple distinct operations. Annotations are used to store
-// miscellaneous notes that might be useful to a developer when examining results, such as index coverage
-// for Steps in a Traversal.
-//type Metrics struct {
-// Id string
-// Name string
-// // the duration in nanoseconds.
-// Duration int64
-// Counts map[string]int64
-// Annotations map[string]interface{}
-// NestedMetrics []Metrics
-//}
-
-// TraversalMetrics contains the Metrics gathered for a Traversal as the result of the .profile()-step.
-//type TraversalMetrics struct {
-// // the duration in nanoseconds.
-// Duration int64
-// Metrics []Metrics
-//}
-
-// GremlinType represents the GraphBinary type Class which can be used to serialize a class.
-//type GremlinType struct {
-// Fqcn string
-//}
-
// BigDecimal represents an arbitrary-precision signed decimal number, consisting of an arbitrary precision integer
// unscaled value and a 32-bit integer scale.
type BigDecimal struct {
diff --git a/gremlin-go/driver/traversal_test.go b/gremlin-go/driver/traversal_test.go
index 1fdf1a52d9..6b97fde2cf 100644
--- a/gremlin-go/driver/traversal_test.go
+++ b/gremlin-go/driver/traversal_test.go
@@ -547,13 +547,11 @@ func TestTraversal(t *testing.T) {
func newWithOptionsConnection(t *testing.T) *GraphTraversalSource {
// No authentication integration test with graphs loaded and alias configured server
testNoAuthWithAliasUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
- testNoAuthWithAliasAuthInfo := &AuthInfo{}
testNoAuthWithAliasTlsConfig := &tls.Config{}
remote, err := NewDriverRemoteConnection(testNoAuthWithAliasUrl,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = testNoAuthWithAliasTlsConfig
- settings.AuthInfo = testNoAuthWithAliasAuthInfo
settings.TraversalSource = "gmodern"
})
assert.Nil(t, err)
@@ -561,15 +559,13 @@ func newWithOptionsConnection(t *testing.T) *GraphTraversalSource {
return Traversal_().With(remote)
}
-func newConnection(t *testing.T) *DriverRemoteConnection {
+func newTestRemoteConnection(t *testing.T) *DriverRemoteConnection {
testNoAuthWithAliasUrl := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl)
- testNoAuthWithAliasAuthInfo := &AuthInfo{}
testNoAuthWithAliasTlsConfig := &tls.Config{}
remote, err := NewDriverRemoteConnection(testNoAuthWithAliasUrl,
func(settings *DriverRemoteConnectionSettings) {
settings.TlsConfig = testNoAuthWithAliasTlsConfig
- settings.AuthInfo = testNoAuthWithAliasAuthInfo
settings.TraversalSource = "gtx"
})
assert.Nil(t, err)
diff --git a/gremlin-go/go.mod b/gremlin-go/go.mod
index c36ea3718d..169beee051 100644
--- a/gremlin-go/go.mod
+++ b/gremlin-go/go.mod
@@ -28,6 +28,20 @@ require (
)
require (
+ github.com/aws/aws-sdk-go-v2 v1.41.1 // indirect
+ github.com/aws/aws-sdk-go-v2/config v1.32.7 // indirect
+ github.com/aws/aws-sdk-go-v2/credentials v1.19.7 // indirect
+ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 // indirect
+ github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 // indirect
+ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 // indirect
+ github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 // indirect
+ github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 // indirect
+ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17 // indirect
+ github.com/aws/aws-sdk-go-v2/service/signin v1.0.5 // indirect
+ github.com/aws/aws-sdk-go-v2/service/sso v1.30.9 // indirect
+ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13 // indirect
+ github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 // indirect
+ github.com/aws/smithy-go v1.24.0 // indirect
github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect
github.com/cucumber/messages/go/v21 v21.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
diff --git a/gremlin-go/go.sum b/gremlin-go/go.sum
index 59675a75c5..cc7f1d6a7d 100644
--- a/gremlin-go/go.sum
+++ b/gremlin-go/go.sum
@@ -1,5 +1,33 @@
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
+github.com/aws/aws-sdk-go-v2 v1.41.1 h1:ABlyEARCDLN034NhxlRUSZr4l71mh+T5KAeGh6cerhU=
+github.com/aws/aws-sdk-go-v2 v1.41.1/go.mod h1:MayyLB8y+buD9hZqkCW3kX1AKq07Y5pXxtgB+rRFhz0=
+github.com/aws/aws-sdk-go-v2/config v1.32.7 h1:vxUyWGUwmkQ2g19n7JY/9YL8MfAIl7bTesIUykECXmY=
+github.com/aws/aws-sdk-go-v2/config v1.32.7/go.mod h1:2/Qm5vKUU/r7Y+zUk/Ptt2MDAEKAfUtKc1+3U1Mo3oY=
+github.com/aws/aws-sdk-go-v2/credentials v1.19.7 h1:tHK47VqqtJxOymRrNtUXN5SP/zUTvZKeLx4tH6PGQc8=
+github.com/aws/aws-sdk-go-v2/credentials v1.19.7/go.mod h1:qOZk8sPDrxhf+4Wf4oT2urYJrYt3RejHSzgAquYeppw=
+github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 h1:I0GyV8wiYrP8XpA70g1HBcQO1JlQxCMTW9npl5UbDHY=
+github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17/go.mod h1:tyw7BOl5bBe/oqvoIeECFJjMdzXoa/dfVz3QQ5lgHGA=
+github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 h1:xOLELNKGp2vsiteLsvLPwxC+mYmO6OZ8PYgiuPJzF8U=
+github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17/go.mod h1:5M5CI3D12dNOtH3/mk6minaRwI2/37ifCURZISxA/IQ=
+github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 h1:WWLqlh79iO48yLkj1v3ISRNiv+3KdQoZ6JWyfcsyQik=
+github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17/go.mod h1:EhG22vHRrvF8oXSTYStZhJc1aUgKtnJe+aOiFEV90cM=
+github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 h1:WKuaxf++XKWlHWu9ECbMlha8WOEGm0OUEZqm4K/Gcfk=
+github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4/go.mod h1:ZWy7j6v1vWGmPReu0iSGvRiise4YI5SkR3OHKTZ6Wuc=
+github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 h1:0ryTNEdJbzUCEWkVXEXoqlXV72J5keC1GvILMOuD00E=
+github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4/go.mod h1:HQ4qwNZh32C3CBeO6iJLQlgtMzqeG17ziAA/3KDJFow=
+github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17 h1:RuNSMoozM8oXlgLG/n6WLaFGoea7/CddrCfIiSA+xdY=
+github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17/go.mod h1:F2xxQ9TZz5gDWsclCtPQscGpP0VUOc8RqgFM3vDENmU=
+github.com/aws/aws-sdk-go-v2/service/signin v1.0.5 h1:VrhDvQib/i0lxvr3zqlUwLwJP4fpmpyD9wYG1vfSu+Y=
+github.com/aws/aws-sdk-go-v2/service/signin v1.0.5/go.mod h1:k029+U8SY30/3/ras4G/Fnv/b88N4mAfliNn08Dem4M=
+github.com/aws/aws-sdk-go-v2/service/sso v1.30.9 h1:v6EiMvhEYBoHABfbGB4alOYmCIrcgyPPiBE1wZAEbqk=
+github.com/aws/aws-sdk-go-v2/service/sso v1.30.9/go.mod h1:yifAsgBxgJWn3ggx70A3urX2AN49Y5sJTD1UQFlfqBw=
+github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13 h1:gd84Omyu9JLriJVCbGApcLzVR3XtmC4ZDPcAI6Ftvds=
+github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13/go.mod h1:sTGThjphYE4Ohw8vJiRStAcu3rbjtXRsdNB0TvZ5wwo=
+github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 h1:5fFjR/ToSOzB2OQ/XqWpZBmNvmP/pJ1jOWYlFDJTjRQ=
+github.com/aws/aws-sdk-go-v2/service/sts v1.41.6/go.mod h1:qgFDZQSD/Kys7nJnVqYlWKnh0SSdMjAi0uSwON4wgYQ=
+github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk=
+github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI=
github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0=