/// <summary> /// Drop a bin from a record. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (args.singleBin) { console.Info("Delete bin is not applicable to single bin servers."); return; } console.Info("Write multi-bin record."); Key key = new Key(args.ns, args.set, "delbinkey"); string binName1 = args.GetBinName("bin1"); string binName2 = args.GetBinName("bin2"); Bin bin1 = new Bin(binName1, "value1"); Bin bin2 = new Bin(binName2, "value2"); client.Put(args.writePolicy, key, bin1, bin2); console.Info("Delete one bin in the record."); bin1 = Bin.AsNull(binName1); // Set bin value to null to drop bin. client.Put(args.writePolicy, key, bin1); console.Info("Read record."); Record record = client.Get(args.policy, key, bin1.name, bin2.name, "bin3"); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } foreach (KeyValuePair<string, object> entry in record.bins) { console.Info("Received: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, entry.Key, entry.Value); } bool valid = true; if (record.GetValue("bin1") != null) { console.Error("bin1 still exists."); valid = false; } object v2 = record.GetValue("bin2"); if (v2 == null || !v2.Equals("value2")) { console.Error("bin2 value mismatch."); valid = false; } if (valid) { console.Info("Bin delete successful"); } }
/// <summary> /// Batch multiple gets in one call to the server. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { string keyPrefix = "batchkey"; string valuePrefix = "batchvalue"; string binName = args.GetBinName("batchbin"); int size = 8; WriteRecords(client, args, keyPrefix, binName, valuePrefix, size); BatchExists(client, args, keyPrefix, size); BatchReads(client, args, keyPrefix, binName, size); BatchReadHeaders(client, args, keyPrefix, size); try { BatchReadComplex(client, args, keyPrefix, binName); } catch (Exception ex) { // Server version may not yet support new batch protocol. Node[] nodes = client.Nodes; foreach (Node node in nodes) { if (!node.HasBatchIndex) { Log.Warn("Server does not support new batch protocol"); return; } } throw ex; } }
/// <summary> /// Write array of integers using standard C# serializer. /// </summary> public virtual void TestArray(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "serialarraykey"); // Delete record if it already exists. client.Delete(args.writePolicy, key); console.Info("Initialize array"); int[] array = new int[10000]; for (int i = 0; i < 10000; i++) { array[i] = i * i; } Bin bin = new Bin(args.GetBinName("serialbin"), (object)array); // Do a test that pushes this complex object through the serializer console.Info("Write array using serializer."); client.Put(args.writePolicy, key, bin); console.Info("Read array using serializer."); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } int[] received; try { received = (int[])record.GetValue(bin.name); } catch (Exception) { throw new Exception(string.Format("Failed to parse returned value: namespace={0} set={1} key={2} bin={3}", key.ns, key.setName, key.userKey, bin.name)); } if (received.Length != 10000) { throw new Exception(string.Format("Array length mismatch: Expected={0:D} Received={1:D}", 10000, received.Length)); } for (int i = 0; i < 10000; i++) { if (received[i] != i * i) { throw new Exception(string.Format("Mismatch: index={0:D} expected={1:D} received={2:D}", i, i * i, received[i])); } } console.Info("Read array successful."); }
public void RunSimpleExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "listkey"); string binName = args.GetBinName("listbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); IList inputList = new List<Value>(); inputList.Add(Value.Get(55)); inputList.Add(Value.Get(77)); // Write values to empty list. Record record = client.Operate(args.writePolicy, key, ListOperation.AppendItems(binName, inputList)); console.Info("Record: " + record); // Pop value from end of list and also return new size of list. record = client.Operate(args.writePolicy, key, ListOperation.Pop(binName, -1), ListOperation.Size(binName)); console.Info("Record: " + record); // There should be one result for each list operation on the same list bin. // In this case, there are two list operations (pop and size), so there // should be two results. IList list = record.GetList(binName); foreach (object value in list) { console.Info("Received: " + value); } }
/// <summary> /// Add integer values. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "addkey"); string binName = args.GetBinName("addbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Perform some adds and check results. Bin bin = new Bin(binName, 10); console.Info("Initial add will create record. Initial value is " + bin.value + '.'); client.Add(args.writePolicy, key, bin); bin = new Bin(binName, 5); console.Info("Add " + bin.value + " to existing record."); client.Add(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } // The value received from the server is an unsigned byte stream. // Convert to an integer before comparing with expected. int received = record.GetInt(bin.name); int expected = 15; if (received == expected) { console.Info("Add successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Add mismatch: Expected {0}. Received {1}.", expected, received); } // Demonstrate add and get combined. bin = new Bin(binName, 30); console.Info("Add " + bin.value + " to existing record."); record = client.Operate(args.writePolicy, key, Operation.Add(bin), Operation.Get(bin.name)); expected = 45; received = record.GetInt(bin.name); if (received == expected) { console.Info("Add successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Add mismatch: Expected {0}. Received {1}.", expected, received); } }
/// <summary> /// Asynchronously write and read a bin using alternate methods. /// </summary> public override void RunExample(AsyncClient client, Arguments args) { completed = false; Key key = new Key(args.ns, args.set, "putgetkey"); Bin bin = new Bin(args.GetBinName("putgetbin"), "value"); RunPutGet(client, args, key, bin); WaitTillComplete(); RunPutGetWithTask(client, args, key, bin); }
/// <summary> /// Apply user defined function on records that match the query filter. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasUdf) { console.Info("Query functions are not supported by the connected Aerospike server."); return; } string indexName = "qeindex1"; string keyPrefix = "qekey"; string binName1 = args.GetBinName("qebin1"); string binName2 = args.GetBinName("qebin2"); int size = 10; Register(client, args); CreateIndex(client, args, indexName, binName1); WriteRecords(client, args, keyPrefix, binName1, binName2, size); RunQueryExecute(client, args, indexName, binName1, binName2); ValidateRecords(client, args, indexName, binName1, binName2, size); client.DropIndex(args.policy, args.ns, args.set, indexName); }
/// <summary> /// Geospatial query with filter example. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasGeo) { console.Info("Geospatial functions are not supported by the connected Aerospike server."); return; } string indexName = "filterindexloc"; string keyPrefix = "filterkeyloc"; string binName1 = args.GetBinName("filterloc"); string binName2 = args.GetBinName("filteramenity"); int size = 20; Register(client, args); CreateIndex(client, args, indexName, binName1); WriteRecords(client, args, keyPrefix, binName1, binName2, size); RunQuery(client, args, indexName, binName1, binName2); client.DropIndex(args.policy, args.ns, args.set, indexName); }
/// <summary> /// Demonstrate touch command. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "touchkey"); Bin bin = new Bin(args.GetBinName("touchbin"), "touchvalue"); console.Info("Create record with 2 second expiration."); WritePolicy writePolicy = new WritePolicy(); writePolicy.expiration = 2; client.Put(writePolicy, key, bin); console.Info("Touch same record with 5 second expiration."); writePolicy.expiration = 5; Record record = client.Operate(writePolicy, key, Operation.Touch(), Operation.GetHeader()); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, null)); } if (record.expiration == 0) { throw new Exception(string.Format("Failed to get record expiration: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } console.Info("Sleep 3 seconds."); Thread.Sleep(3000); record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } console.Info("Success. Record still exists."); console.Info("Sleep 4 seconds."); Thread.Sleep(4000); record = client.Get(args.policy, key, bin.name); if (record == null) { console.Info("Success. Record expired as expected."); } else { console.Error("Found record when it should have expired."); } }
/// <summary> /// Perform operations on a list within a single bin. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasLargeDataTypes) { console.Info("Large set functions are not supported by the connected Aerospike server."); return; } Key key = new Key(args.ns, args.set, "setkey"); string binName = args.GetBinName("setbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Initialize large set operator. Aerospike.Client.LargeSet set = client.GetLargeSet(args.writePolicy, key, binName, null); // Write values. set.Add(Value.Get("setvalue1")); set.Add(Value.Get("setvalue2")); set.Add(Value.Get("setvalue3")); // Verify large set was created with default configuration. IDictionary map = set.GetConfig(); foreach (DictionaryEntry entry in map) { console.Info(entry.Key.ToString() + ',' + entry.Value); } // Remove last value. set.Remove(Value.Get("setvalue3")); int size = set.Size(); if (size != 2) { throw new Exception("Size mismatch. Expected 2 Received " + size); } string received = (string)set.Get(Value.Get("setvalue2")); string expected = "setvalue2"; if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} value={3}", key.ns, key.setName, key.userKey, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } }
/// <summary> /// Asynchronous batch examples. /// </summary> public override void RunExample(AsyncClient client, Arguments args) { this.client = client; this.args = args; this.binName = args.GetBinName("batchbin"); this.taskCount = 0; this.taskSize = 0; this.completed = false; InitializeKeys(); WriteRecords(); WaitTillComplete(); }
/// <summary> /// Write and twice read a bin value, demonstrating record expiration. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "expirekey"); Bin bin = new Bin(args.GetBinName("expirebin"), "expirevalue"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expiration=2", key.ns, key.setName, key.userKey, bin.name, bin.value); // Specify that record expires 2 seconds after it's written. WritePolicy writePolicy = new WritePolicy(); writePolicy.expiration = 2; client.Put(writePolicy, key, bin); // Read the record before it expires, showing it's there. console.Info("Get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } object received = record.GetValue(bin.name); string expected = bin.value.ToString(); if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { throw new Exception(string.Format("Expire mismatch: Expected {0}. Received {1}.", expected, received)); } // Read the record after it expires, showing it's gone. console.Info("Sleeping for 3 seconds ..."); Thread.Sleep(3 * 1000); record = client.Get(args.policy, key, bin.name); if (record == null) { console.Info("Expiry successful. Record not found."); } else { console.Error("Found record when it should have expired."); } }
/// <summary> /// Query on a secondary index with a filter and then apply an additional filter in the /// user defined function. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasUdf) { console.Info("Query functions are not supported by the connected Aerospike server."); return; } string indexName = "profileindex"; string keyPrefix = "profilekey"; string binName = args.GetBinName("name"); Register(client, args); CreateIndex(client, args, indexName, binName); WriteRecords(client, args, keyPrefix, binName); RunQuery(client, args, indexName, binName); client.DropIndex(args.policy, args.ns, args.set, indexName); }
/// <summary> /// Asynchronous Query example. /// </summary> public override void RunExample(AsyncClient client, Arguments args) { if (! args.hasUdf) { console.Info("Query functions are not supported by the connected Aerospike server."); return; } completed = false; string indexName = "asqindex"; string keyPrefix = "asqkey"; string binName = args.GetBinName("asqbin"); int size = 50; CreateIndex(client, args, indexName, binName); RunQueryExample(client, args, keyPrefix, binName, size); WaitTillComplete(); client.DropIndex(args.policy, args.ns, args.set, indexName); }
public void RunScoreExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "mapkey"); string binName = args.GetBinName("mapbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); IDictionary inputMap = new Dictionary<Value, Value>(); inputMap[Value.Get("Charlie")] = Value.Get(55); inputMap[Value.Get("Jim")] = Value.Get(98); inputMap[Value.Get("John")] = Value.Get(76); inputMap[Value.Get("Harry")] = Value.Get(82); // Write values to empty map. Record record = client.Operate(args.writePolicy, key, MapOperation.PutItems(MapPolicy.Default, binName, inputMap) ); console.Info("Record: " + record); // Increment some user scores. record = client.Operate(args.writePolicy, key, MapOperation.Increment(MapPolicy.Default, binName, Value.Get("John"), Value.Get(5)), MapOperation.Decrement(MapPolicy.Default, binName, Value.Get("Jim"), Value.Get(4)) ); console.Info("Record: " + record); // Get top two scores. record = client.Operate(args.writePolicy, key, MapOperation.GetByRankRange(binName, -2, 2, MapReturnType.KEY_VALUE) ); // There should be one result for each map operation on the same map bin. // In this case, there are two map operations (pop and size), so there // should be two results. IList results = record.GetList(binName); foreach (object value in results) { console.Info("Received: " + value); } }
private void WriteUsingUdf(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey1"); Bin bin = new Bin(args.GetBinName("udfbin1"), "string value"); client.Execute(args.writePolicy, key, "record_example", "writeBin", Value.Get(bin.name), bin.value); Record record = client.Get(args.policy, key, bin.name); string expected = bin.value.ToString(); string received = (string)record.GetValue(bin.name); if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } }
/// <summary> /// Prepend string to an existing string. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "prependkey"); string binName = args.GetBinName("prependbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); Bin bin = new Bin(binName, "World"); console.Info("Initial prepend will create record. Initial value is " + bin.value + '.'); client.Prepend(args.writePolicy, key, bin); bin = new Bin(binName, "Hello "); console.Info("Prepend \"" + bin.value + "\" to existing record."); client.Prepend(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } // The value received from the server is an unsigned byte stream. // Convert to an integer before comparing with expected. object received = record.GetValue(bin.name); string expected = "Hello World"; if (received.Equals(expected)) { console.Info("Prepend successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Prepend mismatch: Expected {0}. Received {1}.", expected, received); } }
/// <summary> /// Prepend string to an existing string. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "prependkey"); string binName = args.GetBinName("prependbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); Bin bin = new Bin(binName, "World"); console.Info("Initial prepend will create record. Initial value is " + bin.value + '.'); client.Prepend(args.writePolicy, key, bin); bin = new Bin(binName, "Hello "); console.Info("Prepend \"" + bin.value + "\" to existing record."); client.Prepend(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } // The value received from the server is an unsigned byte stream. // Convert to an integer before comparing with expected. object received = record.GetValue(bin.name); string expected = "Hello World"; if (received.Equals(expected)) { console.Info("Prepend successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Prepend mismatch: Expected {0}. Received {1}.", expected, received); } }
public void RunNestedMapCreateExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "mapkey2"); string binName = args.GetBinName("mapbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); IDictionary <Value, Value> m1 = new Dictionary <Value, Value>(); m1[Value.Get("key21")] = Value.Get(7); m1[Value.Get("key22")] = Value.Get(6); IDictionary <Value, Value> m2 = new Dictionary <Value, Value>(); m2[Value.Get("a")] = Value.Get(3); m2[Value.Get("c")] = Value.Get(5); IDictionary <Value, Value> inputMap = new Dictionary <Value, Value>(); inputMap[Value.Get("key1")] = Value.Get(m1); inputMap[Value.Get("key2")] = Value.Get(m2); // Create maps. client.Put(args.writePolicy, key, new Bin(binName, inputMap)); // Create key ordered map at "key2" only if map does not exist. // Set map value to 4 for map key "key21" inside of map key "key2". CTX ctx = CTX.MapKey(Value.Get("key2")); Record record = client.Operate(args.writePolicy, key, MapOperation.Create(binName, MapOrder.KEY_VALUE_ORDERED, ctx), MapOperation.Put(MapPolicy.Default, binName, Value.Get("b"), Value.Get(4), ctx), Operation.Get(binName) ); record = client.Get(args.policy, key); console.Info("Record: " + record); }
/// <summary> /// Operate on a map of maps. /// </summary> private void RunNestedExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "mapkey2"); string binName = args.GetBinName("mapbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); IDictionary <Value, Value> m1 = new Dictionary <Value, Value>(); m1[Value.Get("key11")] = Value.Get(9); m1[Value.Get("key12")] = Value.Get(4); IDictionary <Value, Value> m2 = new Dictionary <Value, Value>(); m2[Value.Get("key21")] = Value.Get(3); m2[Value.Get("key22")] = Value.Get(5); IDictionary <Value, Value> inputMap = new Dictionary <Value, Value>(); inputMap[Value.Get("key1")] = Value.Get(m1); inputMap[Value.Get("key2")] = Value.Get(m2); // Create maps. client.Put(args.writePolicy, key, new Bin(binName, inputMap)); // Set map value to 11 for map key "key21" inside of map key "key2" // and retrieve all maps. Record record = client.Operate(args.writePolicy, key, MapOperation.Put(MapPolicy.Default, binName, Value.Get("key21"), Value.Get(11), CTX.MapKey(Value.Get("key2"))), Operation.Get(binName) ); record = client.Get(args.policy, key); console.Info("Record: " + record); }
private void WriteBlobUsingUdf(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey6"); string binName = args.GetBinName("udfbin6"); byte[] blob; // Create packed blob using standard C# tools. using (MemoryStream ms = new MemoryStream()) { Formatter.Default.Serialize(ms, 9845); Formatter.Default.Serialize(ms, "Hello world."); blob = ms.ToArray(); } client.Execute(args.writePolicy, key, "record_example", "writeBin", Value.Get(binName), Value.Get(blob)); byte[] received = (byte[])client.Execute(args.writePolicy, key, "record_example", "readBin", Value.Get(binName)); string receivedString = Util.BytesToString(received); string expectedString = Util.BytesToString(blob); if (receivedString.Equals(expectedString)) { console.Info("Blob data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, receivedString); } else { throw new Exception(string.Format("Mismatch: expected={0} received={1}", expectedString, receivedString)); } }
/// <summary> /// Write/Read ArrayList<String> directly instead of relying on default serializer. /// </summary> private void TestListStrings(AerospikeClient client, Arguments args) { console.Info("Read/Write ArrayList<String>"); Key key = new Key(args.ns, args.set, "listkey1"); client.Delete(args.writePolicy, key); List<object> list = new List<object>(); list.Add("string1"); list.Add("string2"); list.Add("string3"); Bin bin = new Bin(args.GetBinName("listbin1"), list); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); List<object> receivedList = (List<object>) record.GetValue(bin.name); ValidateSize(3, receivedList.Count); Validate("string1", receivedList[0]); Validate("string2", receivedList[1]); Validate("string3", receivedList[2]); console.Info("Read/Write ArrayList<String> successful."); }
/// <summary> /// Exercise record generation functionality. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "genkey"); string binName = args.GetBinName("genbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Set some values for the same record. Bin bin = new Bin(binName, "genvalue1"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin); bin = new Bin(binName, "genvalue2"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin); // Retrieve record and its generation count. Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } object received = record.GetValue(bin.name); string expected = bin.value.ToString(); if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4} generation={5}", key.ns, key.setName, key.userKey, bin.name, received, record.generation); } else { throw new Exception(string.Format("Get mismatch: Expected {0}. Received {1}.", expected, received)); } // Set record and fail if it's not the expected generation. bin = new Bin(binName, "genvalue3"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expected generation={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, record.generation); WritePolicy writePolicy = new WritePolicy(); writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; writePolicy.generation = record.generation; client.Put(writePolicy, key, bin); // Set record with invalid generation and check results . bin = new Bin(binName, "genvalue4"); writePolicy.generation = 9999; console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expected generation={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, writePolicy.generation); try { client.Put(writePolicy, key, bin); throw new Exception("Should have received generation error instead of success."); } catch (AerospikeException ae) { if (ae.Result == ResultCode.GENERATION_ERROR) { console.Info("Success: Generation error returned as expected."); } else { throw new Exception(string.Format("Unexpected set return code: namespace={0} set={1} key={2} bin={3} value={4} code={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, ae.Result)); } } // Verify results. record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } received = record.GetValue(bin.name); expected = "genvalue3"; if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4} generation={5}", key.ns, key.setName, key.userKey, bin.name, received, record.generation); } else { throw new Exception(string.Format("Get mismatch: Expected {0}. Received {1}.", expected, received)); } }
/// <summary> /// Write/Read HashMap<Object,Object> directly instead of relying on default serializer. /// </summary> private void TestMapComplex(AerospikeClient client, Arguments args) { console.Info("Read/Write HashMap<Object,Object>"); Key key = new Key(args.ns, args.set, "mapkey2"); client.Delete(args.writePolicy, key); byte[] blob = new byte[] {3, 52, 125}; List<int> list = new List<int>(); list.Add(100034); list.Add(12384955); list.Add(3); list.Add(512); Dictionary<object, object> map = new Dictionary<object, object>(); map["key1"] = "string1"; map["key2"] = 2; map["key3"] = blob; map["key4"] = list; Bin bin = new Bin(args.GetBinName("mapbin2"), map); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); Dictionary<object, object> receivedMap = (Dictionary<object, object>)record.GetValue(bin.name); ValidateSize(4, receivedMap.Count); Validate("string1", receivedMap["key1"]); // Server convert numbers to long, so must expect long. Validate(2L, receivedMap["key2"]); Validate(blob, (byte[])receivedMap["key3"]); IList receivedInner = (IList)receivedMap["key4"]; ValidateSize(4, receivedInner.Count); Validate(100034L, receivedInner[0]); Validate(12384955L, receivedInner[1]); Validate(3L, receivedInner[2]); Validate(512L, receivedInner[3]); console.Info("Read/Write HashMap<Object,Object> successful"); }
/// <summary> /// Write complex object using standard C# serializer. /// </summary> public virtual void TestComplex(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "serialcomplexkey"); // Delete record if it already exists. client.Delete(args.writePolicy, key); console.Info("Initialize complex object"); List <object> inner = new List <object>(); inner.Add("string2"); inner.Add(8); Dictionary <object, object> innerMap = new Dictionary <object, object>(); innerMap["a"] = 1; innerMap[2] = "b"; innerMap["list"] = inner; List <object> list = new List <object>(); list.Add("string1"); list.Add(4); list.Add(inner); list.Add(innerMap); Bin bin = new Bin(args.GetBinName("complexbin"), (object)list); console.Info("Write complex object using serializer."); client.Put(args.writePolicy, key, bin); console.Info("Read complex object using serializer."); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } string expected = Util.ListToString(list); string received; try { object val = record.GetValue(bin.name); received = Util.ObjectToString(val); } catch (Exception) { throw new Exception(string.Format("Failed to parse returned value: namespace={0} set={1} key={2} bin={3}", key.ns, key.setName, key.userKey, bin.name)); } if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Data mismatch"); console.Error("Expected " + expected); console.Error("Received " + received); } console.Info("Read complex object successful."); }
/// <summary> /// Write/Read HashMap<String,String> directly instead of relying on default serializer. /// </summary> private void TestMapStrings(AerospikeClient client, Arguments args) { console.Info("Read/Write HashMap<String,String>"); Key key = new Key(args.ns, args.set, "mapkey1"); client.Delete(args.writePolicy, key); Dictionary<object, object> map = new Dictionary<object, object>(); map["key1"] = "string1"; map["key2"] = "string2"; map["key3"] = "string3"; Bin bin = new Bin(args.GetBinName("mapbin1"), map); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); Dictionary<object, object> receivedMap = (Dictionary<object, object>)record.GetValue(bin.name); ValidateSize(3, receivedMap.Count); Validate("string1", receivedMap["key1"]); Validate("string2", receivedMap["key2"]); Validate("string3", receivedMap["key3"]); console.Info("Read/Write HashMap<String,String> successful"); }
private void WriteUsingUdf(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey1"); Bin bin = new Bin(args.GetBinName("udfbin1"), "string value"); client.Execute(args.writePolicy, key, "record_example", "writeBin", Value.Get(bin.name), bin.value); Record record = client.Get(args.policy, key, bin.name); string expected = bin.value.ToString(); string received = (string)record.GetValue(bin.name); if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } }
public void RunSimpleExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "mapkey"); string binName = args.GetBinName("mapbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); IDictionary inputMap = new Dictionary<Value, Value>(); inputMap[Value.Get(1)] = Value.Get(55); inputMap[Value.Get(2)] = Value.Get(33); // Write values to empty map. Record record = client.Operate(args.writePolicy, key, MapOperation.PutItems(MapPolicy.Default, binName, inputMap)); console.Info("Record: " + record); // Pop value from map and also return new size of map. record = client.Operate(args.writePolicy, key, MapOperation.RemoveByKey(binName, Value.Get(1), MapReturnType.VALUE), MapOperation.Size(binName)); console.Info("Record: " + record); // There should be one result for each map operation on the same map bin. // In this case, there are two map operations (pop and size), so there // should be two results. IList results = record.GetList(binName); foreach (object value in results) { console.Info("Received: " + value); } }
private void WriteIfGenerationNotChanged(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey2"); Bin bin = new Bin(args.GetBinName("udfbin2"), "string value"); // Seed record. client.Put(args.writePolicy, key, bin); // Get record generation. long gen = (long)client.Execute(args.writePolicy, key, "record_example", "getGeneration"); // Write record if generation has not changed. client.Execute(args.writePolicy, key, "record_example", "writeIfGenerationNotChanged", Value.Get(bin.name), bin.value, Value.Get(gen)); console.Info("Record written."); }
/// <summary> /// Write complex object using standard C# serializer. /// </summary> public virtual void TestComplex(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "serialcomplexkey"); // Delete record if it already exists. client.Delete(args.writePolicy, key); console.Info("Initialize complex object"); List<object> inner = new List<object>(); inner.Add("string2"); inner.Add(8); Dictionary<object, object> innerMap = new Dictionary<object, object>(); innerMap["a"] = 1; innerMap[2] = "b"; innerMap["list"] = inner; List<object> list = new List<object>(); list.Add("string1"); list.Add(4); list.Add(inner); list.Add(innerMap); Bin bin = new Bin(args.GetBinName("complexbin"), (object)list); console.Info("Write complex object using serializer."); client.Put(args.writePolicy, key, bin); console.Info("Read complex object using serializer."); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } string expected = Util.ListToString(list); string received; try { object val = record.GetValue(bin.name); received = Util.ObjectToString(val); } catch (Exception) { throw new Exception(string.Format("Failed to parse returned value: namespace={0} set={1} key={2} bin={3}", key.ns, key.setName, key.userKey, bin.name)); } if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { console.Error("Data mismatch"); console.Error("Expected " + expected); console.Error("Received " + received); } console.Info("Read complex object successful."); }
/// <summary> /// Exercise record generation functionality. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "genkey"); string binName = args.GetBinName("genbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Set some values for the same record. Bin bin = new Bin(binName, "genvalue1"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin); bin = new Bin(binName, "genvalue2"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin); // Retrieve record and its generation count. Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } object received = record.GetValue(bin.name); string expected = bin.value.ToString(); if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4} generation={5}", key.ns, key.setName, key.userKey, bin.name, received, record.generation); } else { throw new Exception(string.Format("Get mismatch: Expected {0}. Received {1}.", expected, received)); } // Set record and fail if it's not the expected generation. bin = new Bin(binName, "genvalue3"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expected generation={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, record.generation); WritePolicy writePolicy = new WritePolicy(); writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; writePolicy.generation = record.generation; client.Put(writePolicy, key, bin); // Set record with invalid generation and check results . bin = new Bin(binName, "genvalue4"); writePolicy.generation = 9999; console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expected generation={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, writePolicy.generation); try { client.Put(writePolicy, key, bin); throw new Exception("Should have received generation error instead of success."); } catch (AerospikeException ae) { if (ae.Result == ResultCode.GENERATION_ERROR) { console.Info("Success: Generation error returned as expected."); } else { throw new Exception(string.Format("Unexpected set return code: namespace={0} set={1} key={2} bin={3} value={4} code={5}", key.ns, key.setName, key.userKey, bin.name, bin.value, ae.Result)); } } // Verify results. record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } received = record.GetValue(bin.name); expected = "genvalue3"; if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4} generation={5}", key.ns, key.setName, key.userKey, bin.name, received, record.generation); } else { throw new Exception(string.Format("Get mismatch: Expected {0}. Received {1}.", expected, received)); } }
/// <summary> /// Simple examples of large list functionality. /// </summary> private void RunSimpleExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "setkey"); string binName = args.GetBinName("ListBin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Initialize large set operator. Aerospike.Client.LargeList llist = client.GetLargeList(args.writePolicy, key, binName); string orig1 = "llistValue1"; string orig2 = "llistValue2"; string orig3 = "llistValue3"; // Write values. llist.Add(Value.Get(orig1)); llist.Add(Value.Get(orig2)); llist.Add(Value.Get(orig3)); IDictionary map = llist.GetConfig(); foreach (DictionaryEntry entry in map) { console.Info(entry.Key.ToString() + ',' + entry.Value); } IList rangeList = llist.Range(Value.Get(orig2), Value.Get(orig3)); if (rangeList == null) { throw new Exception("Range returned null."); } if (rangeList.Count != 2) { throw new Exception("Range Size mismatch. Expected 2 Received " + rangeList.Count); } string v2 = (string) rangeList[0]; string v3 = (string) rangeList[1]; if (v2.Equals(orig2) && v3.Equals(orig3)) { console.Info("Range Query matched: v2=" + orig2 + " v3=" + orig3); } else { throw new Exception("Range Content mismatch. Expected (" + orig2 + ":" + orig3 + ") Received (" + v2 + ":" + v3 + ")"); } // Remove last value. llist.Remove(Value.Get(orig3)); int size = llist.Size(); if (size != 2) { throw new Exception("Size mismatch. Expected 2 Received " + size); } IList listReceived = llist.Find(Value.Get(orig2)); string expected = orig2; if (listReceived == null) { console.Error("Data mismatch: Expected " + expected + " Received null"); return; } string stringReceived = (string) listReceived[0]; if (stringReceived != null && stringReceived.Equals(expected)) { console.Info("Data matched: namespace=" + key.ns + " set=" + key.setName + " key=" + key.userKey + " value=" + stringReceived); } else { console.Error("Data mismatch: Expected " + expected + " Received " + stringReceived); } }
/// <summary> /// Write list object using standard C# serializer. /// </summary> public virtual void TestList(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "seriallistkey"); // Delete record if it already exists. client.Delete(args.writePolicy, key); console.Info("Initialize list"); List<string> list = new List<string>(); list.Add("string1"); list.Add("string2"); list.Add("string3"); Bin bin = new Bin(args.GetBinName("serialbin"), (object)list); console.Info("Write list using serializer."); client.Put(args.writePolicy, key, bin); console.Info("Read list using serializer."); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } List<string> received; try { received = (List<string>)record.GetValue(bin.name); } catch (Exception e) { throw new Exception(string.Format("Failed to parse returned value: namespace={0} set={1} key={2} bin={3}", key.ns, key.setName, key.userKey, bin.name), e); } if (received.Count != 3) { throw new Exception(string.Format("Array length mismatch: Expected={0:D} Received={1:D}", 3, received.Count)); } for (int i = 0; i < received.Count; i++) { string expected = "string" + (i + 1); if (!received[i].Equals(expected)) { object obj = received[i]; throw new Exception(string.Format("Mismatch: index={0:D} expected={1} received={2}", i, expected, obj)); } } console.Info("Read list successful."); }
private void WriteUsingUdfAsync(AsyncClient client, Arguments args) { Key key = new Key(args.ns, args.set, "audfkey1"); Bin bin = new Bin(args.GetBinName("audfbin1"), "string value"); console.Info("Write with udf: namespace=" + key.ns + " set=" + key.setName + " key=" + key.userKey + " value=" + bin.value); client.Execute(null, new WriteHandler(this, client, key, bin.name), key, "record_example", "writeBin", Value.Get(bin.name), bin.value); }
private void WriteListMapUsingUdf(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey5"); List<object> inner = new List<object>(); inner.Add("string2"); inner.Add(8L); Dictionary<object, object> innerMap = new Dictionary<object, object>(); innerMap["a"] = 1L; innerMap[2L] = "b"; innerMap["list"] = inner; List<object> list = new List<object>(); list.Add("string1"); list.Add(4L); list.Add(inner); list.Add(innerMap); string binName = args.GetBinName("udfbin5"); client.Execute(args.writePolicy, key, "record_example", "writeBin", Value.Get(binName), Value.Get(list)); object received = client.Execute(args.writePolicy, key, "record_example", "readBin", Value.Get(binName)); string receivedString = Util.ListToString((List<object>)received); string expectedString = Util.ListToString(list); if (receivedString.Equals(expectedString)) { console.Info("UDF data matched: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, received); } else { console.Error("UDF data mismatch"); console.Error("Expected " + list); console.Error("Received " + received); } }
/// <summary> /// Write/Read ArrayList<Object> directly instead of relying on default serializer. /// </summary> private void TestListComplex(AerospikeClient client, Arguments args) { console.Info("Read/Write ArrayList<Object>"); Key key = new Key(args.ns, args.set, "listkey2"); client.Delete(args.writePolicy, key); byte[] blob = new byte[] {3, 52, 125}; List<object> list = new List<object>(); list.Add("string1"); list.Add(2); list.Add(blob); Bin bin = new Bin(args.GetBinName("listbin2"), list); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); List<object> receivedList = (List<object>) record.GetValue(bin.name); ValidateSize(3, receivedList.Count); Validate("string1", receivedList[0]); // Server convert numbers to long, so must expect long. Validate(2L, receivedList[1]); Validate(blob, (byte[])receivedList[2]); console.Info("Read/Write ArrayList<Object> successful."); }
/// <summary> /// Simple examples of large list functionality. /// </summary> private void RunSimpleExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "setkey"); string binName = args.GetBinName("ListBin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Initialize large set operator. Aerospike.Client.LargeList llist = client.GetLargeList(args.writePolicy, key, binName); string orig1 = "llistValue1"; string orig2 = "llistValue2"; string orig3 = "llistValue3"; // Write values. llist.Add(Value.Get(orig1)); llist.Add(Value.Get(orig2)); llist.Add(Value.Get(orig3)); IDictionary map = llist.GetConfig(); foreach (DictionaryEntry entry in map) { console.Info(entry.Key.ToString() + ',' + entry.Value); } IList rangeList = llist.Range(Value.Get(orig2), Value.Get(orig3)); if (rangeList == null) { throw new Exception("Range returned null."); } if (rangeList.Count != 2) { throw new Exception("Range Size mismatch. Expected 2 Received " + rangeList.Count); } string v2 = (string)rangeList[0]; string v3 = (string)rangeList[1]; if (v2.Equals(orig2) && v3.Equals(orig3)) { console.Info("Range Query matched: v2=" + orig2 + " v3=" + orig3); } else { throw new Exception("Range Content mismatch. Expected (" + orig2 + ":" + orig3 + ") Received (" + v2 + ":" + v3 + ")"); } // Remove last value. llist.Remove(Value.Get(orig3)); int size = llist.Size(); if (size != 2) { throw new Exception("Size mismatch. Expected 2 Received " + size); } IList listReceived = llist.Find(Value.Get(orig2)); string expected = orig2; if (listReceived == null) { console.Error("Data mismatch: Expected " + expected + " Received null"); return; } string stringReceived = (string)listReceived[0]; if (stringReceived != null && stringReceived.Equals(expected)) { console.Info("Data matched: namespace=" + key.ns + " set=" + key.setName + " key=" + key.userKey + " value=" + stringReceived); } else { console.Error("Data mismatch: Expected " + expected + " Received " + stringReceived); } }