コード例 #1
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeIfNotExists(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey3")
	binName := "udfbin3"

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

	// Write record only if not already exists. This should succeed.
	client.Execute(shared.WritePolicy, key, "record_example", "writeUnique", as.NewValue(binName), as.NewValue("first"))

	// Verify record written.
	record, err := client.Get(shared.Policy, key, binName)
	shared.PanicOnError(err)
	expected := "first"
	received := record.Bins[binName].(string)

	if received == expected {
		log.Printf("Record written: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), binName, received)
	} else {
		log.Printf("Data mismatch: Expected %s. Received %s.", expected, received)
	}

	// Write record second time. This should fail.
	log.Printf("Attempt second write.")
	client.Execute(shared.WritePolicy, key, "record_example", "writeUnique", as.NewValue(binName), as.NewValue("second"))

	// Verify record not written.
	record, err = client.Get(shared.Policy, key, binName)
	shared.PanicOnError(err)
	received = record.Bins[binName].(string)

	if received == expected {
		log.Printf("Success. Record remained unchanged: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), binName, received)
	} else {
		log.Printf("Data mismatch: Expected %s. Received %s.", expected, received)
	}
}
コード例 #2
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeUsingUdf(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey1")
	bin := as.NewBin("udfbin1", "string value")

	client.Execute(shared.WritePolicy, key, "record_example", "writeBin", as.NewValue(bin.Name), bin.Value)

	record, err := client.Get(shared.Policy, key, bin.Name)
	shared.PanicOnError(err)
	expected := bin.Value.String()
	received := record.Bins[bin.Name].(string)

	if received == expected {
		log.Printf("Data matched: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), bin.Name, received)
	} else {
		log.Printf("Data mismatch: Expected %s. Received %s.", expected, received)
	}
}
コード例 #3
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeWithValidation(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey4")
	binName := "udfbin4"

	// Lua function writeWithValidation accepts number between 1 and 10.
	// Write record with valid value.
	log.Printf("Write with valid value.")
	client.Execute(shared.WritePolicy, key, "record_example", "writeWithValidation", as.NewValue(binName), as.NewValue(4))

	// Write record with invalid value.
	log.Printf("Write with invalid value.")

	_, err := client.Execute(shared.WritePolicy, key, "record_example", "writeWithValidation", as.NewValue(binName), as.NewValue(11))
	if err == nil {
		log.Printf("UDF should not have succeeded!")
	} else {
		log.Printf("Success. UDF resulted in exception as expected.")
	}
}
コード例 #4
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeBlobUsingUdf(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey6")
	binName := "udfbin6"

	// Create packed blob using standard java tools.
	dos := bytes.Buffer{}
	// dos.Write(9845)
	dos.WriteString("Hello world.")
	blob := dos.Bytes()

	client.Execute(shared.WritePolicy, key, "record_example", "writeBin", as.NewValue(binName), as.NewValue(blob))
	received, err := client.Execute(shared.WritePolicy, key, "record_example", "readBin", as.NewValue(binName))
	shared.PanicOnError(err)

	if bytes.Equal(blob, received.([]byte)) {
		log.Printf("Blob data matched: namespace=%s set=%s key=%s bin=%v value=%v",
			key.Namespace(), key.SetName(), key.Value(), binName, received)
	} else {
		log.Fatalf(
			"Mismatch: expected=%v received=%v", blob, received)
	}
}
コード例 #5
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeListMapUsingUdf(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey5")

	inner := []interface{}{"string2", int64(8)}
	innerMap := map[interface{}]interface{}{"a": int64(1), int64(2): "b", "list": inner}
	list := []interface{}{"string1", int64(4), inner, innerMap}

	binName := "udfbin5"

	client.Execute(shared.WritePolicy, key, "record_example", "writeBin", as.NewValue(binName), as.NewValue(list))

	received, err := client.Execute(shared.WritePolicy, key, "record_example", "readBin", as.NewValue(binName))
	shared.PanicOnError(err)

	if testEq(received.([]interface{}), list) {
		log.Printf("UDF data matched: namespace=%s set=%s key=%s bin=%s value=%s",
			key.Namespace(), key.SetName(), key.Value(), binName, received)
	} else {
		log.Println("UDF data mismatch")
		log.Println("Expected ", list)
		log.Println("Received ", received)
	}
}
コード例 #6
0
ファイル: udf.go プロジェクト: archsaber/aerospike-client-go
func writeIfGenerationNotChanged(client *as.Client) {
	key, _ := as.NewKey(*shared.Namespace, *shared.Set, "udfkey2")
	bin := as.NewBin("udfbin2", "string value")

	// Seed record.
	client.PutBins(shared.WritePolicy, key, bin)

	// Get record generation.
	gen, err := client.Execute(shared.WritePolicy, key, "record_example", "getGeneration")
	shared.PanicOnError(err)

	// Write record if generation has not changed.
	client.Execute(shared.WritePolicy, key, "record_example", "writeIfGenerationNotChanged", as.NewValue(bin.Name), bin.Value, as.NewValue(gen))
	log.Printf("Record written.")
}