private void Button_Click(object sender, RoutedEventArgs e) { // Establish connection the server try { AerospikeClient client = new AerospikeClient("45.55.231.46", 3000); // Create key Aerospike.Client.Key key = new Aerospike.Client.Key("test", "myset", "mykey"); // Create Bins Bin bin1 = new Bin("name", "John"); Bin bin2 = new Bin("age", 25); // Write record client.Put(null, key, bin1, bin2); // Read record Record record = client.Get(null, key); Record userRecord = client.Get(null, key); Console.WriteLine("Info:"); Console.WriteLine("Name: " + userRecord.GetValue("name")); Console.WriteLine("Age: " + userRecord.GetValue("age")); // Close connection client.Close(); } catch (AerospikeException.Connection conError) { Console.Write(conError); } }
/// <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> /// 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> /// 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."); }
/// <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> /// 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"); } }
private static void BatchReadRecords(AerospikeClient client, BatchPolicy batchPolicy) { Console.WriteLine("Batch Reads"); const int size = 1024; var keys = new Key[size]; for (var i = 0; i < keys.Length; i++) { keys[i] = new Key("test", "myset", (i + 1)); } var records = client.Get(batchPolicy, keys); Console.WriteLine("Read " + records.Length + " records"); }
private void RunReplaceExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "replacekey"); Bin bin1 = new Bin("bin1", "value1"); Bin bin2 = new Bin("bin2", "value2"); Bin bin3 = new Bin("bin3", "value3"); console.Info("Put: namespace={0} set={1} key={2} bin1={3} value1={4} bin2={5} value2={6}", key.ns, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value); client.Put(args.writePolicy, key, bin1, bin2); console.Info("Replace with: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin3.name, bin3.value); WritePolicy policy = new WritePolicy(); policy.recordExistsAction = RecordExistsAction.REPLACE; client.Put(policy, key, bin3); console.Info("Get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey); Record record = client.Get(args.policy, key); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } if (record.GetValue(bin1.name) == null) { console.Info(bin1.name + " was deleted as expected."); } else { console.Error(bin1.name + " found when it should have been deleted."); } if (record.GetValue(bin2.name) == null) { console.Info(bin2.name + " was deleted as expected."); } else { console.Error(bin2.name + " found when it should have been deleted."); } ValidateBin(key, bin3, record); }
/// <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); } }
private static void AerospikeReadMethod(long userId) { using (var client = new AC.AerospikeClient(Config.DOCKER_MACHINE_IP, 3000)) { if (!client.Connected) { Console.WriteLine("Aerospike ERROR: Connection failed!"); return; } var key = new AC.Key("test", "users", userId); var user = client.Get(null, key); if (user != null) { Console.WriteLine($"Aerospike: ID: {user.GetValue("id")}, Name: {user.GetValue("name")}, Age:{user.GetValue("age")}"); } else { Console.WriteLine("Aerospike ERROR: User record not found!"); } } }
/// <summary> /// Write/Read list of compound objects using Aerospike list type with blob entries (Bin.AsList()). /// </summary> private void TestListCompoundList(AerospikeClient client, Arguments args) { console.Info("Read/Write ArrayList<CompoundObject> using list with blob entries"); Key key = new Key(args.ns, args.set, "listkey5"); client.Delete(args.writePolicy, key); List<CompoundObject> list = new List<CompoundObject>(); list.Add(new CompoundObject("string1", 7)); list.Add(new CompoundObject("string2", 9)); list.Add(new CompoundObject("string3", 54)); Bin bin = new Bin("listbin", list); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); IList receivedList = (IList)record.GetValue(bin.name); ValidateSize(3, receivedList.Count); Validate(list[0], receivedList[0]); Validate(list[1], receivedList[1]); Validate(list[2], receivedList[2]); console.Info("Read/Write ArrayList<CompoundObject> 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); } }
private void WriteIfNotExists(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "udfkey3"); string binName = "udfbin3"; // Delete record if it already exists. client.Delete(args.writePolicy, key); // Write record only if not already exists. This should succeed. client.Execute(args.writePolicy, key, "record_example", "writeUnique", Value.Get(binName), Value.Get("first")); // Verify record written. Record record = client.Get(args.policy, key, binName); string expected = "first"; string received = (string)record.GetValue(binName); if (received != null && received.Equals(expected)) { console.Info("Record written: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } // Write record second time. This should fail. console.Info("Attempt second write."); client.Execute(args.writePolicy, key, "record_example", "writeUnique", Value.Get(binName), Value.Get("second")); // Verify record not written. record = client.Get(args.policy, key, binName); received = (string)record.GetValue(binName); if (received != null && received.Equals(expected)) { console.Info("Success. Record remained unchanged: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } }
/// <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> /// 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."); }
/// <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 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> /// 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"); }
/// <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> /// 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> /// Execute put and get on a server configured as single-bin. /// </summary> private void RunSingleBinTest(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "putgetkey"); Bin bin = new Bin("", "value"); console.Info("Single Bin Put: namespace={0} set={1} key={2} value={3}", key.ns, key.setName, key.userKey, bin.value); client.Put(args.writePolicy, key, bin); console.Info("Single Bin Get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey); Record record = client.Get(args.policy, key); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } ValidateBin(key, bin, record); }
private static void ReadSomeValuesForKey(AerospikeClient client, Policy policy, Key key) { Console.WriteLine("Read specific values (or bins) of a record"); var record = client.Get(policy, key, "name", "age"); Console.WriteLine("Read these values: " + record); Console.WriteLine(""); }
/// <summary> /// Write/Read List/HashMap combination directly instead of relying on default serializer. /// </summary> private void TestListMapCombined(AerospikeClient client, Arguments args) { console.Info("Read/Write List/HashMap"); Key key = new Key(args.ns, args.set, "listmapkey"); client.Delete(args.writePolicy, key); byte[] blob = new byte[] {3, 52, 125}; List<object> inner = new List<object>(); inner.Add("string2"); inner.Add(5); Dictionary<object, object> innerMap = new Dictionary<object, object>(); innerMap["a"] = 1; innerMap[2] = "b"; innerMap[3] = blob; innerMap["list"] = inner; List<object> list = new List<object>(); list.Add("string1"); list.Add(8); list.Add(inner); list.Add(innerMap); Bin bin = new Bin(args.GetBinName("listmapbin"), list); client.Put(args.writePolicy, key, bin); Record record = client.Get(args.policy, key, bin.name); List<object> received = (List<object>) record.GetValue(bin.name); ValidateSize(4, received.Count); Validate("string1", received[0]); // Server convert numbers to long, so must expect long. Validate(8L, received[1]); List<object> receivedInner = (List<object>)received[2]; ValidateSize(2, receivedInner.Count); Validate("string2", receivedInner[0]); Validate(5L, receivedInner[1]); Dictionary<object, object> receivedMap = (Dictionary<object, object>)received[3]; ValidateSize(4, receivedMap.Count); Validate(1L, receivedMap["a"]); Validate("b", receivedMap[2L]); Validate(blob, (byte[])receivedMap[3L]); List<object> receivedInner2 = (List<object>)receivedMap["list"]; ValidateSize(2, receivedInner2.Count); Validate("string2", receivedInner2[0]); Validate(5L, receivedInner2[1]); console.Info("Read/Write List/HashMap successful"); }
/// <summary> /// Read records with varying namespaces, bin names and read types in one batch. /// This requires Aerospike Server version >= 3.6.0. /// </summary> private void BatchReadComplex(AerospikeClient client, Arguments args, string keyPrefix, string binName) { // Batch gets into one call. // Batch allows multiple namespaces in one call, but example test environment may only have one namespace. string[] bins = new string[] {binName}; List<BatchRead> records = new List<BatchRead>(); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 1), bins)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 2), true)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 3), true)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 4), false)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 5), true)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 6), true)); records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 7), bins)); // This record should be found, but the requested bin will not be found. records.Add(new BatchRead(new Key(args.ns, args.set, keyPrefix + 8), new string[] { "binnotfound" })); // This record should not be found. records.Add(new BatchRead(new Key(args.ns, args.set, "keynotfound"), bins)); // Execute batch. client.Get(null, records); // Show results. int found = 0; foreach (BatchRead record in records) { Key key = record.key; Record rec = record.record; if (rec != null) { found++; console.Info("Record: ns={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, rec.GetValue(binName)); } else { console.Info("Record not found: ns={0} set={1} key={2} bin={3}", key.ns, key.setName, key.userKey, binName); } } if (found != 8) { console.Error("Records found mismatch. Expected %d. Received %d.", 8, found); } }
/// <summary> /// Read records in one batch. /// </summary> private void BatchReads(AerospikeClient client, Arguments args, string keyPrefix, string binName, int size) { // Batch gets into one call. Key[] keys = new Key[size]; for (int i = 0; i < size; i++) { keys[i] = new Key(args.ns, args.set, keyPrefix + (i + 1)); } Record[] records = client.Get(null, keys, binName); for (int i = 0; i < records.Length; i++) { Key key = keys[i]; Record record = records[i]; Log.Level level = Log.Level.ERROR; object value = null; if (record != null) { level = Log.Level.INFO; value = record.GetValue(binName); } console.Write(level, "Record: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, binName, value); } if (records.Length != size) { console.Error("Record size mismatch. Expected {0}. Received {1}.", size, records.Length); } }
/// <summary> /// Execute put and get on a server configured as multi-bin. This is the server default. /// </summary> private void RunMultiBinTest(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "putgetkey"); Bin bin1 = new Bin("bin1", "value1"); Bin bin2 = new Bin("bin2", "value2"); console.Info("Put: namespace={0} set={1} key={2} bin1={3} value1={4} bin2={5} value2={6}", key.ns, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value); client.Put(args.writePolicy, key, bin1, bin2); console.Info("Get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey); Record record = client.Get(args.policy, key); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } ValidateBin(key, bin1, record); ValidateBin(key, bin2, record); }
private static void ReadAllValuesForKey(AerospikeClient client, Policy policy, Key key) { Console.WriteLine("Read all bins of a record"); var record = client.Get(policy, key); Console.WriteLine("Read these values: " + record); Console.WriteLine(""); }