Exemple #1
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())
	}

}
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	// defer client.Close()
	client, err = as.NewClient("172.31.53.227", 3000)
	fmt.Println(client)
	panicOnError(err)

	writePolicy = as.NewWritePolicy(0, 0)
	ch = make(chan Event, 1)
	key, err := as.NewKey("test", "totals", "test")
	panicOnError(err)
	// define some bins with data
	bins := as.BinMap{
		"value": 100,
	}
	err = client.Put(nil, key, bins)
	panicOnError(err)

	time.AfterFunc(1*time.Second, func() {
		for i := 1; i < 24; i++ {
			fmt.Println("starting worker %d", i)
			go worker(ch)
		}
	})

	http.HandleFunc("/vote", voteHandler)
	http.HandleFunc("/loaderio-35df9c4fffde902e3b0e3e0115816d82.html", validationHandler)
	http.ListenAndServe(":80", nil)
}
Exemple #3
0
func (c *client) lockKey(key *as.Key) error {
	writePolicy := as.NewWritePolicy(0, -1)
	writePolicy.RecordExistsAction = as.CREATE_ONLY

	lockBin := as.NewBin(binLockName, "true")

	return c.asClient.PutBins(writePolicy, key, lockBin)
}
Exemple #4
0
// NewEnv stores a new env.
func NewEnv(as *aerospike.Client, env string, description string) error {
	key, err := envKey(env)
	if err != nil {
		return err
	}
	wp := aerospike.NewWritePolicy(0, 0)
	wp.RecordExistsAction = aerospike.CREATE_ONLY
	return as.PutObject(wp, key, &EnvData{Description: description})
}
Exemple #5
0
func (c *client) writeData(key *as.Key, result interface{}, expire time.Duration) {
	writePolicy := as.NewWritePolicy(0, int32(expire.Seconds()))

	value, err := json.Marshal(result)
	panicOnError(err)

	lockBin := as.NewBin(binLockName, nil)
	dataBin := as.NewBin(binDataName, string(value))

	err = c.asClient.PutBins(writePolicy, key, lockBin, dataBin)
	panicOnError(err)
}
Exemple #6
0
func main() {
	// flags
	host := flag.String("h", "127.0.0.1", "host")
	port := flag.Int("p", 3000, "port")
	del := flag.Bool("del", false, "delete?")
	flag.Parse()

	// connect to Aerospike
	if h, found := os.LookupEnv("AEROSPIKE_PORT_3000_TCP_ADDR"); found {
		*host = h
	}
	cl, err := asc.NewClient(*host, *port)
	panicOnErr(err)

	key, err := asc.NewKey("test", "aerospike", "key")
	panicOnErr(err)

	// define some bins with data
	bins := asc.BinMap{
		"bin1": 42,
		"bin2": "elephant",
		"bin3": []interface{}{"Go", 2009},
	}

	// write the bins
	wp := asc.NewWritePolicy(0, 0)
	wp.SendKey = true // also send the key itself
	err = cl.Put(wp, key, bins)
	panicOnErr(err)

	rec, err := cl.Get(nil, key)
	panicOnErr(err)

	_, _ = pp.Printf("key: %v\nbins: %v\n", *rec.Key, rec.Bins)

	// scan all data
	rs, err := cl.ScanAll(nil, "test", "aerospike")
	panicOnErr(err)
	defer rs.Close()

	for r := range rs.Results() {
		_, _ = pp.Println(*r.Record.Key, r.Record.Bins)
	}

	if *del {
		existed, err := cl.Delete(nil, key)
		panicOnErr(err)
		fmt.Printf("Record existed before delete? %v\n", existed)
	}
}
Exemple #7
0
func runExample(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "touchkey")
	bin := as.NewBin("touchbin", "touchvalue")

	log.Printf("Create record with 2 second expiration.")
	writePolicy := as.NewWritePolicy(0, 2)
	client.PutBins(writePolicy, key, bin)

	log.Printf("Touch same record with 5 second expiration.")
	writePolicy.Expiration = 5
	record, err := client.Operate(writePolicy, key, as.TouchOp(), as.GetHeaderOp())

	if record == nil {
		log.Fatalf(
			"Failed to get: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, nil)
	}

	if record.Expiration == 0 {
		log.Fatalf(
			"Failed to get record expiration: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	log.Printf("Sleep 3 seconds.")
	time.Sleep(3 * time.Second)

	record, err = client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)

	if record == nil {
		log.Fatalf(
			"Failed to get: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	log.Printf("Success. Record still exists.")
	log.Printf("Sleep 4 seconds.")
	time.Sleep(4 * time.Second)

	record, err = client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)

	if record == nil {
		log.Printf("Success. Record expired as expected.")
	} else {
		log.Fatalf("Found record when it should have expired.")
	}
}
Exemple #8
0
/**
 * Write and twice read a non-expiring tuple using the new "NoExpire" value (-1).
 * This example is most effective when the Default Namespace Time To Live (TTL)
 * is set to a small value, such as 5 seconds.  When we sleep beyond that
 * time, we show that the NoExpire TTL flag actually works.
 */
func noExpireExample(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "expirekey")
	bin := as.NewBin("expirebin", "noexpirevalue")

	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=NoExpire",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value)

	// Specify that record NEVER expires.
	// The "Never Expire" value is -1, or 0xFFFFFFFF.
	writePolicy := as.NewWritePolicy(0, 2)
	writePolicy.Expiration = math.MaxUint32
	client.PutBins(writePolicy, key, bin)

	// Read the record, showing it is there.
	log.Printf("Get: namespace=%s set=%s key=%s",
		key.Namespace(), key.SetName(), key.Value())

	record, err := client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)
	if record == nil {
		log.Fatalf(
			"Failed to get record: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	received := record.Bins[bin.Name]
	expected := bin.Value.String()
	if received == expected {
		log.Printf("Get record successful: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, received)
	} else {
		log.Fatalf("Expire record mismatch: Expected %s. Received %s.",
			expected, received)
	}

	// Read this Record after the Default Expiration, showing it is still there.
	// We should have set the Namespace TTL at 5 sec.
	log.Printf("Sleeping for 10 seconds ...")
	time.Sleep(10 * time.Second)
	record, err = client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)

	if record == nil {
		log.Fatalf("Record expired and should NOT have.")
	} else {
		log.Printf("Found Record (correctly) after default TTL.")
	}
}
// putEntry store data to cache
func (as *AeroSpikeClient) putEntry(key *AeroSpikeKey, data IEntryData, ttl time.Duration) {

	aKey, _ := as.createKey(key)

	policy := aerospike.NewWritePolicy(0, int32(ttl.Seconds()))
	policy.MaxRetries = maxRetries

	bins := data.Export()
	bins["tags"] = key.Tags
	bins["id"] = key.Pk

	if err := as.client.Put(policy, aKey, bins); err != nil {
		log.Printf("PutExternal error: %s", err)
	}

	return
}
Exemple #10
0
// UpdateService updates the data associated with a service.
func UpdateService(
	as *aerospike.Client, env string, service string, description string,
	isPublic bool, liveCodeVersion int64) (
	err error) {
	key, err := serviceKey(env, service)
	if err != nil {
		return err
	}
	wp := aerospike.NewWritePolicy(0, 0)
	wp.RecordExistsAction = aerospike.UPDATE_ONLY
	return as.PutBins(
		wp, key,
		aerospike.NewBin("Desc", description),
		aerospike.NewBin("IsPub", btoi(isPublic)),
		aerospike.NewBin("LiveVer", liveCodeVersion),
	)
}
Exemple #11
0
/**
 * Write and twice read an expiration record.
 */
func expireExample(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "expirekey ")
	bin := as.NewBin("expirebin", "expirevalue")

	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=2",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value)

	// Specify that record expires 2 seconds after it's written.
	writePolicy := as.NewWritePolicy(0, 2)
	client.PutBins(writePolicy, key, bin)

	// Read the record before it expires, showing it is there.
	log.Printf("Get: namespace=%s set=%s key=%s",
		key.Namespace(), key.SetName(), key.Value())

	record, err := client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)
	if record == nil {
		log.Fatalf(
			"Failed to get record: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	received := record.Bins[bin.Name]
	expected := bin.Value.String()
	if received == expected {
		log.Printf("Get record successful: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, received)
	} else {
		log.Fatalf("Expire record mismatch: Expected %s. Received %s.",
			expected, received)
	}

	// Read the record after it expires, showing it's gone.
	log.Printf("Sleeping for 3 seconds ...")
	time.Sleep(3 * time.Second)
	record, err = client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)
	if record == nil {
		log.Printf("Expiry of record successful. Record not found.")
	} else {
		log.Fatalf("Found record when it should have expired.")
	}
}
Exemple #12
0
func runReplaceExample(client *as.Client) {
	key, err := as.NewKey(*shared.Namespace, *shared.Set, "replacekey")
	shared.PanicOnError(err)
	bin1 := as.NewBin("bin1", "value1")
	bin2 := as.NewBin("bin2", "value2")
	bin3 := as.NewBin("bin3", "value3")

	log.Printf("Put: namespace=%s set=%s key=%s bin1=%s value1=%s bin2=%s value2=%s",
		key.Namespace(), key.SetName(), key.Value(), bin1.Name, bin1.Value, bin2.Name, bin2.Value)

	client.PutBins(shared.WritePolicy, key, bin1, bin2)

	log.Printf("Replace with: namespace=%s set=%s key=%s bin=%s value=%s",
		key.Namespace(), key.SetName(), key.Value(), bin3.Name, bin3.Value)

	wpolicy := as.NewWritePolicy(0, 0)
	wpolicy.RecordExistsAction = as.REPLACE
	client.PutBins(wpolicy, key, bin3)

	log.Printf("Get: namespace=%s set=%s key=%s", key.Namespace(), key.SetName(), key.Value())

	record, err := client.Get(shared.Policy, key)
	shared.PanicOnError(err)

	if record == nil {
		log.Fatalf(
			"Failed to get: namespace=%s set=%s key=%s", key.Namespace(), key.SetName(), key.Value())
	}

	if record.Bins[bin1.Name] == nil {
		log.Printf(bin1.Name + " was deleted as expected.")
	} else {
		log.Fatalln(bin1.Name + " found when it should have been deleted.")
	}

	if record.Bins[bin2.Name] == nil {
		log.Printf(bin2.Name + " was deleted as expected.")
	} else {
		log.Fatalln(bin2.Name + " found when it should have been deleted.")
	}
	shared.ValidateBin(key, bin3, record)
}
Exemple #13
0
// NewService stores a new service.
func NewService(
	as *aerospike.Client, env string, service string,
	description string, isPublic bool) error {
	key, err := serviceKey(env, service)
	if err != nil {
		return err
	}
	wp := aerospike.NewWritePolicy(0, 0)
	wp.RecordExistsAction = aerospike.CREATE_ONLY
	return as.PutObject(wp, key, &ServiceData{
		Env: env,
		// TODO: This is not guaranteed to be unique. Should figure out a way
		//       to generate this uniquely.
		UniqueID:        leverutil.RandomHostName(),
		Description:     description,
		NextCodeVersion: 0,
		LiveCodeVersion: 0,
		IsPublic:        isPublic,
	})
}
Exemple #14
0
func runReplaceOnlyExample(client *as.Client) {
	key, err := as.NewKey(*shared.Namespace, *shared.Set, "replaceonlykey")
	shared.PanicOnError(err)
	bin := as.NewBin("bin", "value")

	// Delete record if it already exists.
	client.Delete(shared.WritePolicy, key)

	log.Printf("Replace record requiring that it exists: namespace=%s set=%s key=%s",
		key.Namespace(), key.SetName(), key.Value())

	wpolicy := as.NewWritePolicy(0, 0)
	wpolicy.RecordExistsAction = as.REPLACE_ONLY
	err = client.PutBins(wpolicy, key, bin)
	if ae, ok := err.(ast.AerospikeError); ok && ae.ResultCode() == ast.KEY_NOT_FOUND_ERROR {
		log.Printf("Success. `Not found` error returned as expected.")
	} else {
		log.Fatalln("Failure. This command should have resulted in an error.")
	}
}
func runExample(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "genkey")
	binName := "genbin"

	// Delete record if it already exists.
	client.Delete(shared.WritePolicy, key)

	// Set some values for the same record.
	bin := as.NewBin(binName, "genvalue1")
	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value)

	client.PutBins(shared.WritePolicy, key, bin)

	bin = as.NewBin(binName, "genvalue2")
	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value)

	client.PutBins(shared.WritePolicy, key, bin)

	// Retrieve record and its generation count.
	record, err := client.Get(shared.Policy, key, bin.Name)

	if record == nil {
		log.Fatalf(
			"Failed to get: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	received := record.Bins[bin.Name]
	expected := bin.Value.String()

	if received == expected {
		log.Printf("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, received, record.Generation)
	} else {
		log.Fatalf("Get mismatch: Expected %s. Received %s.",
			expected, received)
	}

	// Set record and fail if it's not the expected generation.
	bin = as.NewBin(binName, "genvalue3")
	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value, record.Generation)

	writePolicy := as.NewWritePolicy(0, 2)
	writePolicy.GenerationPolicy = as.EXPECT_GEN_EQUAL
	writePolicy.Generation = int32(record.Generation)
	client.PutBins(writePolicy, key, bin)

	// Set record with invalid generation and check results .
	bin = as.NewBin(binName, "genvalue4")
	writePolicy.Generation = 9999
	log.Printf("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d",
		key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value, writePolicy.Generation)

	err = client.PutBins(writePolicy, key, bin)
	if err != nil {
		if ae, ok := err.(ast.AerospikeError); ok && ae.ResultCode() == ast.GENERATION_ERROR {
			shared.PanicOnError(errors.New("Should have received generation error instead of success."))
		}
		log.Printf("Success: Generation error returned as expected.")
	} else {
		log.Fatalf(
			"Unexpected set return code: namespace=%s set=%s key=%s bin=%s value=%s code=%s",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, bin.Value, err)
	}

	// Verify results.
	record, err = client.Get(shared.Policy, key, bin.Name)

	if record == nil {
		log.Fatalf(
			"Failed to get: namespace=%s set=%s key=%s",
			key.Namespace(), key.SetName(), key.Value())
	}

	received = record.Bins[bin.Name]
	expected = "genvalue3"

	if received == expected {
		log.Printf("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, received, record.Generation)
	} else {
		log.Fatalf("Get mismatch: Expected %s. Received %s.",
			expected, received)
	}
}
Exemple #16
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() {
Exemple #17
0
func NewDB(host string, port int, namespace string) *DB {
	return &DB{host, port, namespace, "", as.NewWritePolicy(0, 0), as.NewPolicy(), nil}
}