goriak - Go language driver for Riak KV
Clone or download
Permalink
Failed to load latest commit information.
examples Update examples for v3 Jan 27, 2017
.gitignore Updated .gitignore to ignore test coverage files Oct 29, 2016
LICENSE Initial commit Oct 5, 2016
README.md doc(README) update installation instructions for go module Jul 26, 2018
all_keys.go Fix issue with AllKeys() caused by riak-go-client 1.9.0 not beeing co… Mar 18, 2017
all_keys_test.go #51 - HyperLogLog WIP Feb 7, 2017
auto_map_array_test.go Fixed bug causing custom types of simple integer and string types to … Jan 19, 2017
auto_map_decode.go Spell correct Jan 27, 2017
auto_map_encode.go tst(time) fix time test that broke with Go 1.9 (#59) Nov 12, 2017
auto_map_operation.go Filter (#40) Jan 8, 2017
auto_map_test.go Hide *Command from sub command query builders Jan 27, 2017
circle.yml Fix Riak KV installation on Circle CI Feb 20, 2017
client.go #47 - Set TLSConfig Server name per server we're connecting to Feb 6, 2017
codecov.yml Update codecov.yml to use the the v2 branch as the default one Nov 5, 2016
command.go #46 - Comments Feb 15, 2017
counter.go JSON Marshal/Unmarshal for all helper types (added support for Flag, … Dec 26, 2016
counter_test.go #33 - Filters (#34) Jan 8, 2017
delete.go #51 - HyperLogLog WIP Feb 7, 2017
filter.go #43 - Renamed all command builder types so that we have a uniform sta… Jan 24, 2017
filter_test.go gofmt -s Jan 27, 2017
flag.go JSON Marshal/Unmarshal for all helper types (added support for Flag, … Dec 26, 2016
flag_test.go #43 - Converted more types to new API Jan 23, 2017
go.mod fix(module) try again (#62) Jul 26, 2018
go.sum fea(module) add Go module support (#61) Jul 26, 2018
goriak.go Spell correction Nov 5, 2016
hll_get.go #51 - HyperLogLog WIP Feb 7, 2017
hll_update.go #51 - HyperLogLog WIP Feb 7, 2017
hll_update_test.go #51 - Test for HLL AddMultiple Feb 7, 2017
json.go #46 - Fix merge issues Feb 15, 2017
json_test.go #43 - Converted more types to new API Jan 23, 2017
keys_in_index.go #49 - Added KeysInIndexRange command Feb 6, 2017
keys_in_index_test.go #49 - Added KeysInIndexRange command Feb 6, 2017
map_get.go #46 - RunMiddleware for Get and Set Feb 15, 2017
map_operation.go Hide *Command from sub command query builders Jan 27, 2017
map_operation_test.go #43 - More progress. Tests can successfully compile Jan 23, 2017
map_set.go Merge branch 'v3' into middleware Feb 15, 2017
middleware.go #46 - Fix merge issues Feb 15, 2017
middleware_test.go #46 - RunMiddleware for Get and Set Feb 15, 2017
query_consistency_test.go #51 - Tests for HLL consistency options Feb 7, 2017
query_get_value.go #46 - Fix merge issues Feb 15, 2017
query_store_value.go #46 - RunMiddleware for GetRaw and SetRaw Feb 15, 2017
raw.go #46 - Fix merge issues Feb 15, 2017
raw_test.go #43 - WIP: Split the command builder into multiple types Jan 22, 2017
register.go JSON Marshal/Unmarshal for all helper types (added support for Flag, … Dec 26, 2016
register_test.go #43 - More progress. Tests can successfully compile Jan 23, 2017
set.go Spell correct Jan 27, 2017
set_test.go #43 - And it builds again! Jan 24, 2017
siblings.go Custom Conflict Resolution (#28) Nov 25, 2016
siblings_test.go Bugfix for conflict resolution in GetRaw() Feb 9, 2017
time_test.go tst(time) fix time test that broke with Go 1.9 (#59) Nov 12, 2017

README.md

goriak Build Status codecov Go Report Card

Current version: v3.2.1.
Riak KV version: 2.0 or higher, the latest version of Riak KV is always recommended.

What is goriak?

goriak is a wrapper around riak-go-client (version 1.9.0 or newer is required) to make it easier and more friendly for developers to use Riak KV.

Installation

As a Go module (requires Go 1.11 or later):

go get github.com/zegl/goriak/v3@v3.2.3

As a Go package:

go get -u gopkg.in/zegl/goriak.v3

Maps (Riak Data Types)

The main feature of goriak is that goriak automatically can marshal/unmarshal your Go types into Riak data types.

Set (Riak Data Types)

In the example below Name will be saved as a register, and Aliases will be a set.

type User struct {
    Name    string
    Aliases []string
}

user := User {
    Name:   "Foo",
    Alises: []string{"Foo", "Bar"},
}

goriak.Bucket("bucket-name", "bucket-type").Set(user).Key("key").Run(c)

Tags

Struct tags can be used to change the name of the item, or to ignore it.

type User struct {
    Name    string   `goriak:"-"`       // Ignore field
    Aliases []string `goriak:"aliases"` // Save as "aliases" in Riak KV
}

Get (Riak Data Types)

The map can later be retreived as a whole:

var res User
goriak.Bucket("bucket-name", "bucket-type").Get("key", &res).Run(c)

Supported Go types

Go Type Riak Type
struct map
string register
[n]byte register
[]byte register
[]slice set
[]slice set
[][]byte set
map map
time.Time register
int [1] register

1: All signed and unsigned integer types are supported.

Golang map types

Supported key types: all integer types, string.
Supported value types: string, []byte.

Helper types

Some actions are more complicated then necessary with the use of the default Go types and MapOperations.

This is why goriak contains the types Counter, Set, Flag and Register. All of these types will help you performing actions such as incrementing a value, or adding/removing items.

Counters

Riak Counters is supported with the special goriak.Counter type.

Example:

type Article struct {
    Title string
    Views *goriak.Counter
}

// Get our object
var article Article
goriak.Bucket("articles", "map").Get("1-hello-world", &article).Run(con)

// Increase views by 1
err := article.Views.Increase(1).Exec(con)

// check err

Counter.Exec(con) will make a lightweight request to Riak, and the counter is the only object that will be updated.

You can also save the changes to your counter with SetMap(), this is useful if you want to change multiple counters at the same time.

Check godoc for more information.

Sets

You can chose to use goriak.Set to help you with Set related actions, such as adding and removing items. goriak.Set also has support for sending incremental actions to Riak so that you don't have to build that functionality yourself.

Example:

type Article struct {
    Title string
    Tags *goriak.Set
}

// Get our object
var article Article
goriak.Bucket("articles", "map").Get("1-hello-world", &article).Run(con)

// Add the tag "animals"
err := article.Tags.AddString("animals").Exec(con)

// check err

Check godoc for more information.

Values

Values can be automatically JSON Marshalled/Unmarshalled by using SetJSON() and GetJSON(). There is also SetRaw() and GetRaw() that works directly on []bytes.

JSON

// Set object
goriak.Bucket("bucket-name", "bucket-type").SetJSON(obj).Key("key").Run(con)

// Get object
goriak.Bucket("bucket-name", "bucket-type").GetJSON("key", &obj).Run(con)

MapOperation

There is a time in everyones life where you need to perform raw MapOperations on your Riak Data Values.

Some operations, such as RemoveFromSet requires a Context to perform the operation. A Context can be retreived from Get by setting a special context type.

type ourType struct {
    Aliases []string

    // The context from Riak will be added if the tag goriakcontext is provided
    Context []byte `goriak:"goriakcontext"`
}

// ... GetMap()

// Works with MapOperation from github.com/basho/riak-go-client
operation := goriak.NewMapOperation()
operation.AddToSet("Aliases", []byte("Baz"))

goriak.MapOperation("bucket-name", "bucket-type", "key", operation, val.Context)

Secondary Indexes

You can set secondary indexes automatically with SetJSON() by using struct tags.

Strings and all signed integer types are supported. Both as-is and in slices.

type User struct {
    Name    string `goriakindex:"nameindex_bin"`
    Aliases []string
}

Indexes can also be used in slices. If you are using a slice every value in the slice will be added to the index.

type User struct {
    Aliases []string `goriakindex:"aliasesindex_bin"`
}

When saved the next time the index will be updated.

KeysInIndex

Keys in a particular index can be retreived with KeysInIndex.

callback := func(item goriak.SecondaryIndexQueryResult) {
    // use item
}

goriak.Bucket("bucket-name", "bucket-type").
    KeysInIndex("nameindex_bin", "Value", callback).
    Run(con)

AddToIndex

An alternative way of setting Secondary Indexes is by using AddToIndex().

goriak.Bucket("bucket-name", "bucket-type").
    SetRaw(data).
    AddToIndex("indexname_bin", "value").
    Run(con)