Home < Blog < New Go language driver available for Volt Active Data

New Go language driver available for Volt Active Data

5 min read

I’m happy to introduce a fully featured, high-performance Volt Active Data client for the go language. The golang client is both feature and performance comparable to the Volt Active Data Java client.

Support of golang Standard Library

The golang standard library provides support for SQL database drivers. This support is represented in two of the standard library’s packages, ‘database/sql’ and ‘database/sql/driver’. The first of these packages, ‘database/sql’, represents the user-facing API for a golang SQL driver. User code is typically written against this API.

The second of these packages, ‘database/sql/driver’, is an API that Volt Active Data has implemented to provide this driver. This implementation supports the Volt Active Data wire protocol to communicate with Volt Active Data server instances.

A set of examples is included with the golang driver. One of these, ‘examples/hello-world/driver_hello_world.go’, demonstrates use of the Volt Active Data golang client using the standard ‘database/sql’ API. No Volt Active Data proprietary api is used. The import of the Volt Active Data client:

import _ "github.com/Volt Active Data/voltdb-client-go/voltdbclient"

allows the Volt Active Data client to register itself. Then, a call to ‘Open’:

db, err := sql.Open("voltdb", "localhost:21212")

returns a db/sql.DB object. This object represents a Volt Active Data connection or pool of connections. From this point the standard API can be used.

Volt Active Data-specific API

The Volt Active Data golang client also provides an alternative, Volt Active Data-specific API. It’s challenging to express all the features of a Volt Active Data client using golang’s standard ‘database/sql’ API. In particular, there’s no simple way to express asynchronous procedure invocations using the golang standard library API. Asynchronous requests typically perform significantly better than synchronous requests, so the Volt Active Data-specific API exposes additional behavior.

The Volt Active Data-specific driver API implements the interfaces present in ‘database/sql/driver’. Fundamentally, this is the same code that is executed when the standard ‘database/sql’ API is used. The distinction is that not all of the Volt Active Data API is available through the standard ‘database/sql’ API; the Volt Active Data driver implementation is a superset of this API.

Two simple examples are provided that demonstrate use of the Volt Active Data-specific API, ‘examples/hello-world/sync_hello_world.go’ and ‘examples/hello-world/async_hello_world.go’. The first demonstrates use of the synchronous API and the second demonstrates use of the asynchronous API.

In both examples, a connection to the Volt Active Data server is acquired with the statement:

conn, err := voltdbclient.OpenConn("localhost:21212")

Here, ‘conn’ is a reference to the ‘voltdbclient.Conn’ type. The behavior of this type is documented in the Volt Active Data client go documentation. Note this type implements the golang standard library interface ‘database/sql/driver.Conn’, providing both an implementation of the standard library type and additional Volt Active Data specific behavior.

The synchronous example, ‘sync_hello_world.go’, then does a synchronous procedure invocation:

rows, err := conn.Query("@AdHoc", driver.Value{"select * from HELLOWORLD where DIALECT = ?", "French"})

This invocation returns an instance of the standard library type ‘driver.Rows’. ‘driver.Rows’ is an interface; the implementing type is ‘voltdbclient.VoltRows’. The go doc for this type, ‘voltdbclient.VoltRows’, demonstrates that it provides both the ‘driver.Rows’ interface and additional Volt Active Data-specific behavior.

The asynchronous example, ‘examples/hello-world/async_hello_world.go’, demonstrates use of an asynchronous procedure invocation:

err := conn.QueryAsync(resCons, "HELLOWORLD.select", driver.Value{key})

This API is not part of the golang standard library. For asynchronous calls, an implementation of the Volt Active Data interface ‘voltdbclient.AsyncResponseConsumer’ needs to be provided. This type will consume the results of the asynchronous procedure invocation when they become available.

Conclusion

The Volt Active Data golang client was developed with a number of goals in mind. First, the client is comparable to the Java client in both feature set and in performance.

Also, the golang client implements the golang standard library ‘database/sql/driver’. This means the client can be used through the standard golang API, ‘database/sql’. It also means the ‘database/sql/driver’ interface can be coded against directly.

In addition, interfaces implemented by the Volt Active Data golang client try to be semantically consistent with the golang standard library. For example, the go doc for the type ‘database/sql.DB’ indicates this object is thread safe. For the Volt Active Data-specific API, the corresponding object is voltdbclient.Conn; this type also is thread safe.

Finally, the golang Volt Active Data client provides behavior beyond that specified by the standard golang libraries. This additional behavior is provided through an API that should be familiar to users of the Volt Active Data Java client.

The go client can be installed with ‘go get’ – find it at ‘go get github.com/Volt Active Data/voltdb-client-go’.

Please try the golang driver, and let us know what you think. And remember if you provide a code contribution to Volt Active Data (thank you in advance), don’t forget to sign our CLA.