Example #1
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.")
	}
}
Example #2
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 = -1
	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.")
	}
}
Example #3
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.")
	}
}
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 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)

	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)
}
Example #6
0
package shared

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

	as "github.com/THE108/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() {
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)
	}
}