Exemplo n.º 1
0
// Add a new Broker
func (command *Command) AddBroker(r *http.Request, broker *m.Broker, done *bool) error {

	*done = false

	keyId := fmt.Sprintf("%s:%d", BROKERS, broker.Id)
	key, _ := as.NewKey(NAMESPACE, BROKERS, keyId)

	db.Delete(nil, key)

	bins := as.BinMap{
		"broker_id":   broker.Id,
		"broker_name": broker.Name,
		"credit":      broker.Credit,
	}

	policy := &as.WritePolicy{
		BasePolicy:         *as.NewPolicy(),
		RecordExistsAction: as.CREATE_ONLY,
		GenerationPolicy:   as.NONE,
		Generation:         0,
		Expiration:         0,
		SendKey:            false,
	}

	if err := db.Put(policy, key, bins); err != nil {
		return err
	}

	*done = true
	return nil
}
Exemplo n.º 2
0
// Connect to the database, and initial setup
func connectToDatabase(host string, port int) {
	var err error

	// connect to the db
	if db, err = as.NewClient(host, port); err != nil {
		panic(err)
	}

	readPolicy = as.NewPolicy()
	writePolicy = as.NewWritePolicy(0, 0)
	scanPolicy = as.NewScanPolicy()

	// index on offer_id of bids, so we can find bids on a given offer
	if _, err := db.CreateIndex(writePolicy, "test", BIDS, "idx:br:1", "offer_id", as.NUMERIC); err != nil {
		fmt.Printf("Create Index Failed: %s\n", err.Error())
	}

	// index on broker_id of bids, so we can find bids by a particular broker
	if _, err := db.CreateIndex(writePolicy, "test", BIDS, "idx:br:2", "broker_id", as.NUMERIC); err != nil {
		fmt.Printf("Create Index Failed: %s\n", err.Error())
	}

	// index on broker_id of offers, so we can find offers by a particular broker
	if _, err := db.CreateIndex(writePolicy, "test", OFFERS, "idx:br:3", "broker_id", as.NUMERIC); err != nil {
		fmt.Printf("Create Index Failed: %s\n", err.Error())
	}

	// index on ticker of prices
	if _, err := db.CreateIndex(writePolicy, "test", PRICES, "idx:br:4", "ticker", as.STRING); err != nil {
		fmt.Printf("Create Index Failed: %s\n", err.Error())
	}

}
Exemplo n.º 3
0
func loadASRecord(ns string, set string, pkey string) *as.Record {
	var err error
	key, err := as.NewKey(ns, set, pkey)
	panicOnError(err)

	policy := as.NewPolicy()
	rec, err := client.Get(policy, key)
	panicOnError(err)

	return rec
}
Exemplo n.º 4
0
func (c *client) getData(key *as.Key, result interface{}) bool {
	policy := as.NewPolicy()
	policy.Timeout = 3 * time.Second
	policy.MaxRetries = 3

	record, err := c.asClient.Get(policy, key)
	panicOnError(err)

	if record != nil {
		if data, ok := record.Bins[binDataName]; ok {
			json.Unmarshal([]byte(data.(string)), result)
			return true
		} else {
			return false
		}
	} else {
		return false
	}
}
Exemplo n.º 5
0
func NewAeroSpikeClient(cfg *Config) (*AeroSpikeClient, error) {

	clientPolicy := aerospike.NewClientPolicy()
	clientPolicy.ConnectionQueueSize = ConnectionQueueSize
	clientPolicy.LimitConnectionsToQueueSize = true
	clientPolicy.Timeout = 50 * time.Millisecond

	hosts := []*aerospike.Host{}
	for _, connStr := range cfg.AeroHostsPots {
		hostStr, portStr, err := net.SplitHostPort(connStr)
		if err != nil {
			return nil, err
		}
		port, err := strconv.Atoi(portStr)
		if err != nil {
			return nil, err
		}
		hosts = append(hosts, aerospike.NewHost(hostStr, port))
	}

	client, err := aerospike.NewClientWithPolicyAndHost(clientPolicy, hosts...)
	if err != nil {
		return nil, err
	}

	getPolicy := aerospike.NewPolicy()
	getPolicy.Timeout = time.Millisecond * 50
	getPolicy.MaxRetries = maxRetries

	out := &AeroSpikeClient{
		namespace: cfg.AeroNamespace,
		prefix:    cfg.AeroPrefix,
		getPolicy: getPolicy,
		client:    client,
	}

	return out, nil
}
Exemplo n.º 6
0
func NewDB(host string, port int, namespace string) *DB {
	return &DB{host, port, namespace, "", as.NewWritePolicy(0, 0), as.NewPolicy(), nil}
}
Exemplo n.º 7
0
package shared

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"math"
	"os"
	"runtime"

	as "github.com/aerospike/aerospike-client-go"
)

var WritePolicy = as.NewWritePolicy(0, 0)
var Policy = as.NewPolicy()

var Host = flag.String("h", "127.0.0.1", "Aerospike server seed hostnames or IP addresses")
var Port = flag.Int("p", 3000, "Aerospike server seed hostname or IP address port number.")
var Namespace = flag.String("n", "test", "Aerospike namespace.")
var Set = flag.String("s", "testset", "Aerospike set name.")
var showUsage = flag.Bool("u", false, "Show usage information.")
var Client *as.Client

func ValidateBin(key *as.Key, bin *as.Bin, record *as.Record) {
	if !bytes.Equal(record.Key.Digest(), key.Digest()) {
		log.Fatalln(fmt.Sprintf("key `%s` is not the same as key `%s`.", key, record.Key))
	}

	if record.Bins[bin.Name] != bin.Value.GetObject() {
		log.Fatalln(fmt.Sprintf("bin `%s: %v` is not the same as bin `%v`.", bin.Name, bin.Value, record.Bins[bin.Name]))