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> /// Demonstrate multiple operations on a single record in one call. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { // Write initial record. Key key = new Key(args.ns, args.set, "opkey"); Bin bin1 = new Bin("optintbin", 7); Bin bin2 = new Bin("optstringbin", "string value"); console.Info("Put: namespace={0} set={1} key={2} binname1={3} binvalue1={4} binname1={5} binvalue1={6}", key.ns, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value); client.Put(args.writePolicy, key, bin1, bin2); // Add integer, write new string and read record. Bin bin3 = new Bin(bin1.name, 4); Bin bin4 = new Bin(bin2.name, "new string"); console.Info("Add: " + bin3.value); console.Info("Write: " + bin4.value); console.Info("Read:"); Record record = client.Operate(args.writePolicy, key, Operation.Add(bin3), Operation.Put(bin4), Operation.Get()); 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, record, bin3.name, 11L, record.GetValue(bin3.name)); ValidateBin(key, record, bin4.name, bin4.value.ToString(), record.GetValue(bin4.name)); }
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); } }
/// <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."); } }
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); } }
/// <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); } }
public static void WriteEventToAerospike(string date, AerospikeClient client, string account, string product, string txType) { /* Event record schema Key: account::date::sequence Bin: keyBinName - contains string same as key (diagnostic only) Bin: accBinName - contains string: account Bin: dayBinName - contains string: day Bin: seqBinName - contains integer sequence number Bin: listBinName - contains a CDT list of events CDT list element: product,txType */ int seq = 1; int eventSize = 0; string accDayString = account + "::" + date; string keyString = accDayString + "::" + seq; string eventString = product + "," + txType; Key key = new Key (ns, seqSet, keyString); /* * get the size of the event list in first record */ Record record = client.Operate (null, key, ListOperation.Size (listBinName), Operation.Get (seqBinName)); if (record != null) { eventSize = record.GetInt (listBinName); if (eventSize == 30) { record = client.Operate (null, key, Operation.Add (new Bin (seqBinName, 1)), Operation.Get (seqBinName)); seq = record.GetInt (seqBinName); keyString = account + "::" + date + "::" + seq; key = new Key (ns, seqSet, keyString); } } client.Operate (null, key, Operation.Put (new Bin (keyBinName, keyString)), Operation.Put (new Bin (accBinName, account)), Operation.Put (new Bin (dayBinName, date)), Operation.Put (new Bin (seqBinName, seq)), ListOperation.Append (listBinName, Value.Get (eventString))); }
private void updateUserUsingOperate(AerospikeClient client, Key userKey, WritePolicy policy, long ts) { Record record = client.Operate(policy, userKey, Operation.Add(new Bin("tweetcount", 1)), Operation.Put(new Bin("lasttweeted", ts)), Operation.Get()); Console.WriteLine("INFO: The tweet count now is: " + record.GetValue("tweetcount")); }
private static void MultiOps(AerospikeClient client, WritePolicy writePolicy, Key key) { Console.WriteLine("Multiops"); var bin1 = new Bin("optintbin", 7); var bin2 = new Bin("optstringbin", "string value"); client.Put(writePolicy, key, bin1, bin2); var bin3 = new Bin(bin1.name, 4); var bin4 = new Bin(bin2.name, "new string"); var record = client.Operate(writePolicy, key, Operation.Add(bin3), Operation.Put(bin4), Operation.Get()); Console.WriteLine("Record: " + record); }