goriak

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2016 License: MIT Imports: 6 Imported by: 0

README

goriak Build Status codecov

Current version: v1.0.0.
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 to make it easier and more friendly for developers to use Riak KV.

Installation

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

Maps

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

SetMap

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.SetMap("bucket-name", "bucket-type", "key", user)

GetMap

The map can later be retreived as a whole:

var res User
goriak.GetMap("bucket-name", "bucket-type", "key", &res)

MapOperation

There is a time in everyones life where you need to perform raw MapOperations.

Some operations, such as RemoveFromSet requires a Context to perform the operation. A Context can be retreived from GetMap 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)

Supported Go types

Go Type Riak Type
struct map
string register
[n]byte register
[]byte register
[]slice set
[]slice set
[][]byte set
map map
Golang map types

Supported key types: int, int8, int16, int32, int64, string.
Supported value types: string, []byte.

Helper types

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

This is why goriak contains the types goriak.Counter and goriak.Set. Both 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
con.GetMap("articles", "map", "1-hello-world", &article)

// 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
con.GetMap("articles", "map", "1-hello-world", &article)

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

// check err

Check godoc for more information.

Values

Values are automatically JSON encoded and decoded.

SetValue

goriak.SetValue("bucket-name", "bucket-type", "key", obj)

GetValue

goriak.GetValue("bucket-name", "bucket-type", "key", &obj)

Secondary Indexes

You can set secondary indexes on Values with SetValue by using struct tags.

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

When saved the next time the index will be updated.

Keys in a particular index can be retreived with KeysInIndex.

goriak.KeysInIndex("bucket-name", "bucket-type", "nameindex_bin", "Value")

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"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMapOperation

func NewMapOperation() riak.MapOperation

NewMapOperation returns a new riak.MapOperation that you can for advanced Riak operations

Types

type Client

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

func NewGoriak

func NewGoriak(host string) (*Client, error)

func (*Client) AllKeys

func (c *Client) AllKeys(bucket, bucketType string) ([]string, error)

func (*Client) Delete

func (c *Client) Delete(bucket, bucketType, key string) error

func (*Client) GetJSON

func (c *Client) GetJSON(bucket, bucketType, key string, value interface{}) (err error, isNotFound bool)

GetJSON is the same as GetRaw, but with automatic JSON unmarshalling

func (*Client) GetMap

func (c *Client) GetMap(bucket, bucketType, key string, output interface{}) (err error, isNotFound bool)

GetMap fetches data from Riak and decodes the result into your Go datatype. See SetMap for more info.

func (*Client) GetRaw

func (c *Client) GetRaw(bucket, bucketType, key string) (raw []byte, err error, isNotFound bool)

GetRaw retuns the raw []byte array that is stored in Riak without any modifications

func (*Client) KeysInIndex

func (c *Client) KeysInIndex(bucket, bucketType, indexName, indexValue string, limit uint32) ([]string, error)

func (*Client) MapOperation

func (c *Client) MapOperation(bucket, bucketType, key string, op riak.MapOperation, context []byte) error

func (*Client) SetJSON

func (c *Client) SetJSON(bucket, bucketType, key string, value interface{}) error

SetJSON saves value as key in the bucket bucket/bucketType Values can automatically be added to indexes with the struct tag goriakindex

func (*Client) SetMap

func (c *Client) SetMap(bucket, bucketType, key string, input interface{}) error

SetMap automatically converts your Go datatype to the equivalent type in Riak

| Go Type | Riak Type | |------------|-----------| | `struct` | map | | `string` | register | | `[n]byte` | register | | `[]byte` | register | | `[]slice` | set | | `[]slice` | set | | `[][]byte` | set | | `map` | map |

func (*Client) SetRaw

func (c *Client) SetRaw(bucket, bucketType, key string, data []byte, opt *Options) error

SetRaw saves the data in Riak directly without any modifications

type Counter

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

Counter is a wapper to handle Riak Counters Counter needs to be initialized by GetMap() to fully function

func NewCounter

func NewCounter() *Counter

NewCounter returns a partial Counter Counters returned by NewCounter() can only be updated with SetMap(). Counter.Exec() will not work on counters returned by NewCounter()

func (*Counter) Exec

func (c *Counter) Exec(client *Client) error

Exec saves changes made to the Counter to Riak Exec only works on Counters initialized by GetMap()

func (*Counter) Increase

func (c *Counter) Increase(i int64) *Counter

Increase the value in the Counter by i The value in Counter.Value() will be updated directly Increase() will not save the changes to Riak directly

func (*Counter) Value

func (c *Counter) Value() int64

Value returns the value in the Counter

type Options

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

func (*Options) AddToIndex

func (o *Options) AddToIndex(key, value string) *Options

type Set added in v1.1.0

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

Set is a special type to make it easier to work with Riak Sets in Go.

func NewSet added in v1.1.0

func NewSet() *Set

NewSet returnes a new and empty Set. Sets returned from NewSet() can not be used with Set.Exec()

func (*Set) Add added in v1.1.0

func (s *Set) Add(add []byte) *Set

Add adds an item to the direct value of the Set. Save the changes to Riak with Set.Exec() or SetMap().

func (*Set) AddString added in v1.1.0

func (s *Set) AddString(add string) *Set

AddString is a shortcut to Add

func (*Set) Exec added in v1.1.0

func (s *Set) Exec(client *Client) error

Exec executes the diff created by Add() and Remove(), and saves the data to Riak

func (*Set) Remove added in v1.1.0

func (s *Set) Remove(remove []byte) *Set

Remove deletes an item to the direct value of the Set. Save the changes to Riak with Set.Exec() or SetMap().

func (*Set) RemoveString added in v1.1.0

func (s *Set) RemoveString(remove string) *Set

RemoveString is a shortcut to Remove

func (*Set) Strings added in v1.1.0

func (s *Set) Strings() []string

Strings returns the same data as Value(), but encoded as strings

func (*Set) Value added in v1.1.0

func (s *Set) Value() [][]byte

Value returnes the raw values from the Set

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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