geode_go_client

package module
v0.0.0-...-0012dc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

README

What is this?

This is the beginning of a Go client for Apache Geode. It uses Geode's new protobuf-based client protocol to communicate with locators and servers.

Using it

Get the package:

go get github.com/gemfire/geode-go-client

Write some client code:

package main

import (
    "net"
    "github.com/gemfire/geode-go-client/connector"
    geode "github.com/gemfire/geode-go-client"
    "fmt"
    "log"
)

func main() {
    var err error

    pool := connector.NewPool()
    pool.AddServer("localhost", 40404)
    // Optionally add user credentials
    pool.AddCredentials("jbloggs", "t0p53cr3t")
    
    conn := connector.NewConnector(pool)
    client := geode.NewGeodeClient(conn)
    fmt.Println("Connected....")

    // Add a primitive type
    err = client.Put("FOO", "A", 777)
    if err != nil {
        log.Fatal(err)
    }

    v, err := client.Get("FOO", "A")
    if err != nil {
        log.Fatal(err)
    }

    // Type assert so that we can use the value
    vx := v.(int32)
    fmt.Printf("Value for A: %d\n", vx)

    err = client.Remove("FOO", "A")
    if err != nil {
        log.Fatal(err)
    }
}

Arbitrary structs are converted to JSON when they are put into a region:

type MyStruct struct{
    Name string  `json:"name"`
    Age  int     `json:"age"`
}

v := &MyStruct{"Joe", 42}
client.Put("REGION", "Joe", v)

Similarly, to retrieve the data:

v := &MyStruct{}
x := client.Get("REGION", "Joe", v)

v is optional for Get() and is only used if the data being retrieved is JSON. In the above example, x (returned from Get()) ends up pointing to v and is thus redundant.

The API only supports manipulating data (get, getAll, put, putAll, size and remove). It does not support managing regions or other Geode constructs.

Note that values returned will be of type interface{}. It is thus the responsibility of the caller to type assert as appropriate.

Querying

OQL queries can be performed by creating a Query instance and then making a call depending the type of results which will be returned. For example:

q := client.Query("select count(*) from /MYREGION")
val, err := client.QueryForListResult(q)
fmt.Printf("count(*) = %d\n", val[0])

(Perhaps counterintuitively, count(*) returns its result in a single element list and not as a single result.)

When querying objects (returned as JSON) from Geode, you need to provide a reference type to the query:

type Person struct{}

q := client.Query("select * from /Employees")
q.Reference = &Person{}
people, err := q.QueryForListResult(q)

// Type assert to get a usable instance
person := people[0].(*Person)

On the servers

To enable Geode's protobuf support, locators and servers must be started with the option geode.feature-protobuf-protocol.

For example:

$ gfsh start server --name=server1 --J=-Dgeode.feature-protobuf-protocol=true

Developing

The Geode protobuf support is currently in very active development which means that this code may not work if you are running against a local Geode build.

In order to update the protobuf bindings you will need the protoc tool installed locally. Assuming you have checked out the Apache Geode repository, set the environment variable GEODE_CHECKOUT to this location and re-generate the bindings:

$ export GEODE_CHECKOUT=/Users/jbloggs/workspace/geode
$ go generate connector/protobuf.go

The protobuf.go file contains go:generate directives to do the actual work.

Testing

Tests are written in Ginkgo. There are both unit and integration tests.

Unit tests can be executed with:

$ ginkgo -r connector

Integration tests require a Geode product directory to work:

$ export GEODE_HOME=<path to the Geode product directory>
$ ginkgo -r -slowSpecThreshold 60 integration

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

A Client provides the high-level API required to interact with a Geode cluster. The API supports the following key and value types:

int
int16
int32
int64
byte
bool
float32
float64
[]byte
string
CustomEncodedValue

In order to enable the protobuf protocol, the Geode servers must be started with the property:

geode.feature-protobuf-protocol=true

func NewGeodeClient

func NewGeodeClient(c *connector.Protobuf) *Client

func (*Client) ExecuteOnGroups

func (this *Client) ExecuteOnGroups(functionId string, groups []string, functionArgs interface{}) ([]interface{}, error)

Execute a function on a list of group. This will execute on each member associated with the groups; returning a slice of results, one entry for each member.

func (*Client) ExecuteOnMembers

func (this *Client) ExecuteOnMembers(functionId string, members []string, functionArgs interface{}) ([]interface{}, error)

Execute a function on a list of members, returning a slice of results, one entry for each member.

func (*Client) ExecuteOnRegion

func (this *Client) ExecuteOnRegion(functionId, region string, functionArgs interface{}, keyFilter []interface{}) ([]interface{}, error)

Execute a function on a region. This will execute on all members hosting the region and return a slice of results; one entry for each member.

func (*Client) Get

func (this *Client) Get(region string, key interface{}, value ...interface{}) (interface{}, error)

Get an entry from a region using the specified key. It is the callers' responsibility to perform any type-assertion on the returned value. If a single, optional value is passed, the data retrieved from the region will be attempted to be unmarshalled as JSON into the supplied value.

func (*Client) GetAll

func (this *Client) GetAll(region string, keys interface{}) (map[interface{}]interface{}, map[interface{}]error, error)

GetAll returns the values of multiple keys. Keys must be passed as an array or slice. The returned values are a map of keys and values for those keys which were successfully retrieved, a map of keys and the relevant error for those keys which produced an error on retrieval and, finally, a single error which typically would be as a result of a key or value encoding error.

func (*Client) Put

func (this *Client) Put(region string, key, value interface{}) error

Put data into a region. key and value must be a supported type.

func (*Client) PutAll

func (this *Client) PutAll(region string, entries interface{}) (map[interface{}]error, error)

PutAll adds multiple key/value pairs to a single region. Entries must be in the form of a map. The returned values are either a map of individual keys and the associated error when attempting to add that key, or a single error which typically would be as a result of a key or value encoding error.

func (*Client) PutIfAbsent

func (this *Client) PutIfAbsent(region string, key, value interface{}) error

Put data into a region if the key is not present. key and value must be a supported type.

func (*Client) QueryForListResult

func (this *Client) QueryForListResult(query *Query) ([]interface{}, error)

Execute a query, returning a list of results.

func (*Client) QueryForSingleResult

func (this *Client) QueryForSingleResult(query *Query) (interface{}, error)

Execute a query, returning a single result value.

func (*Client) QueryForTableResult

func (this *Client) QueryForTableResult(query *Query) (map[string][]interface{}, error)

Execute a query, returning a map of column (or field) names and the associated values for each column.

func (*Client) Remove

func (this *Client) Remove(region string, key interface{}) error

Remove an entry for a region.

func (*Client) Size

func (this *Client) Size(region string) (int32, error)

Size returns the number of entries in a region

Directories

Path Synopsis
connectorfakes
Code generated by counterfeiter.
Code generated by counterfeiter.
Package org_apache_geode_internal_protocol_protobuf is a generated protocol buffer package.
Package org_apache_geode_internal_protocol_protobuf is a generated protocol buffer package.
v1
Package org_apache_geode_internal_protocol_protobuf_v1 is a generated protocol buffer package.
Package org_apache_geode_internal_protocol_protobuf_v1 is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL