aerospike

package module
v0.0.0-...-86d6920 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2017 License: MIT Imports: 4 Imported by: 1

README

aerospike

An Aerospike database client where each set is called a "table" and each table has a struct assigned to it.

The main motivation for creating this client is to automatically build REST APIs from your structs using aerogo/api.

Struct fields must have a json tag if you want to save them in the database.

The lib also allows controlling Aerospike Go API directly by accessing db.Client in case you need low-level access.

API

NewDatabase
db := aerospike.NewDatabase(
	"127.0.0.1",
	3000,
	"YOUR_NAMESPACE",

	// Register structs by giving a nil pointer to each struct.
	[]interface{}{
		(*User)(nil),
		(*Post)(nil),
		(*Thread)(nil),
	},
)

This will register 3 structs: User, Post and Thread. The associated table names are automatically determined by the struct names: "User", "Post" and "Thread".

Get
obj, err := db.Get("User", "123")
user := obj.(*User)
Set
db.Set("User", "123", user)

Returns error and overwrites the object in the DB completely.

Delete
db.Delete("User", "123")

Returns error.

All
stream, err := db.All("User")
users := stream.(chan *User)

for user := range users {
	user.DoSomething()
}

Returns (interface{}, error) where the first parameter is a channel of the table's data type.

Exists
db.Exists("User", "123")

Returns (bool, error).

GetMany
objects, err := db.GetMany("User", []string{
	"123",
	"456",
	"789",
})

users := objects.([]*User)
GetMap
user, err := db.GetMap("User", "123")
firstName := user["firstName"]

Returns (map[string]interface{}, error). The map contains the data for the retrieved object.

GetObject
type MyUser struct {
	FirstName string "json:`firstName`"
}

user := &MyUser{}
db.GetObject("User", "123", user)
fmt.Println(user.FirstName)

GetObject retrieves data from the table and stores it in the provided object. Unlike db.Get() the data type doesn't need to be pre-registered as a table.

DeleteTable
db.DeleteTable("User")

Deletes all content from the given table.

Note: It seems there is a bug in the official Aerospike client as the deleted data will show up on a cold start and allocate memory again.

Type
userType := db.Type("User")

Returns the type of the table.

Types
types := db.Types()
userType := types["User"]

Returns a map[string]reflect.Type of table to type relationships. This is used in aerogo/api to automatically create a REST API from all of your struct data.

Namespace
namespace := db.Namespace()

Returns the previously registered namespace.

By Eduard Urbach

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	Client *as.Client
	// contains filtered or unexported fields
}

Database represents the Aerospike database.

func NewDatabase

func NewDatabase(host string, port int, namespace string, tables []interface{}) *Database

NewDatabase creates a new database client.

func (*Database) All

func (db *Database) All(table string) (interface{}, error)

All returns a stream of all objects in the given table.

func (*Database) Delete

func (db *Database) Delete(table string, id string) (existed bool, err error)

Delete deletes an object from the database and returns if it existed.

func (*Database) DeleteTable

func (db *Database) DeleteTable(table string) error

DeleteTable deletes all content from the given table.

func (*Database) Exists

func (db *Database) Exists(table string, id string) (bool, error)

Exists tells you if the given record exists.

func (*Database) Get

func (db *Database) Get(table string, id string) (interface{}, error)

Get retrieves an object from the table.

func (*Database) GetMany

func (db *Database) GetMany(table string, idList []string) (interface{}, error)

GetMany performs a Get request for every ID in the ID list and returns a slice of objects.

func (*Database) GetMap

func (db *Database) GetMap(table string, id string) (as.BinMap, error)

GetMap retrieves the data as a map[string]interface{}.

func (*Database) GetObject

func (db *Database) GetObject(table string, id string, obj interface{}) error

GetObject retrieves data from the table and stores it in the provided object.

func (*Database) Namespace

func (db *Database) Namespace() string

Namespace returns the name of the namespace.

func (*Database) Scan

func (db *Database) Scan(table string, channel interface{}) error

Scan writes all objects from a given table to the channel.

func (*Database) ScanMap

func (db *Database) ScanMap(table string) (chan as.BinMap, error)

ScanMap writes all objects from a given table to the channel.

func (*Database) Set

func (db *Database) Set(table string, id string, obj interface{}) error

Set sets an object's data for the given ID and erases old fields.

func (*Database) SetScanPriority

func (db *Database) SetScanPriority(priority string)

SetScanPriority sets the scan priority.

func (*Database) Type

func (db *Database) Type(table string) reflect.Type

Type returns the type of the table.

func (*Database) Types

func (db *Database) Types() map[string]reflect.Type

Types returns the types of all tables as a map.

Jump to

Keyboard shortcuts

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