public static void generateCustomerProduct(AerospikeClient client)
        {
            Random products = new Random (2727);
            Random productsPerAccount = new Random (9898);
            Random productQuantity = new Random (1919);
            for (int i = 0; i < accountTotal; i++) {

                int productsToAdd = productsPerAccount.Next (1, 150);
                string keyString = i.ToString ();
                Key cdtkey = new Key (ns, cdtSet, keyString);
                Aerospike.Helper.Collection.LargeList clist = new Aerospike.Helper.Collection.LargeList (client, null, cdtkey, cdtBinName);
                Key ldtkey = new Key (ns, ldtSet, keyString);
                LargeList llist = client.GetLargeList (null, ldtkey, ldtBinName);

                //for diagnositics
                client.Put (null, cdtkey, new Bin (keyBinName, keyString), new Bin (accBinName, keyString));
                client.Put (null, ldtkey, new Bin (keyBinName, keyString), new Bin (accBinName, keyString));

                for (int j = 0; j < productsToAdd; j++) {
                    int product = products.Next (1, productTotal);
                    int productAmount = productQuantity.Next (1, 100);
                    Value value = makeValue (product, productAmount);

                    llist.Update (value);
                    clist.Update (value);

                }
            }
        }
        /// <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");
            }
        }
Esempio n. 3
0
        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>
        /// 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));
        }
        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>
        /// 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.");
        }
 private static void AddRecords(AerospikeClient client,
         WritePolicy writePolicy)
 {
     const int size = 1024;
     for (var i = 0; i < size; i++)
     {
         var key = new Key("test", "myset", (i + 1));
         client.Put(writePolicy, key, new Bin("dots", i + " dots"));
     }
     Console.WriteLine("Added " + size + " Records");
     Console.WriteLine("");
 }
Esempio n. 8
0
        /// <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.");
            }
        }
Esempio n. 9
0
        /// <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.");
            }
        }
        private void ServerSideExists(AerospikeClient client, Arguments args)
        {
            console.Info("Write list.");
            List<int> list = new List<int>();
            list.Add(64);
            list.Add(3702);
            list.Add(-5);

            Key key = new Key(args.ns, args.set, "udfkey7");
            Bin bin = new Bin("udfbin7", list);
            client.Put(args.writePolicy, key, bin);

            ServerSideExists(client, args.writePolicy, key, bin, 3702, true);
            ServerSideExists(client, args.writePolicy, key, bin, 65, false);
        }
Esempio n. 11
0
        private static void AerospikeWriteMethod(User user)
        {
            using (var client = new AC.AerospikeClient(Config.DOCKER_MACHINE_IP, 3000))
            {
                if (!client.Connected)
                {
                    Console.WriteLine("Aerospike ERROR: Connection failed!");
                    return;
                }

                var wPolicy = new AC.WritePolicy
                {
                    recordExistsAction = AC.RecordExistsAction.UPDATE
                };

                var key     = new AC.Key("test", "users", user.Id);
                var binId   = new AC.Bin("id", user.Id);
                var binName = new AC.Bin("name", user.Name);
                var binAge  = new AC.Bin("age", user.Age);

                client.Put(wPolicy, key, binId, binName, binAge);
            }
        }
Esempio n. 12
0
 public void Write(AerospikeClient client, WritePolicy policy, string ns, string set)
 {
     Key key = new Key(ns, set, ticker);
     Bin binTicker = new Bin("ticker", ticker);
     // Double not supported directly, so convert to bytes.
     Bin binPrice = new Bin("price", BitConverter.GetBytes(price));
     client.Put(policy, key, binTicker, binPrice);
 }
Esempio n. 13
0
        public void Write(AerospikeClient client, WritePolicy policy, string ns, string set)
        {
            Key key = new Key(ns, set, accountId);

            List<object> tickers = new List<object>(positions.Count);
            MemoryStream ms = new MemoryStream(500);
            BinaryWriter writer = new BinaryWriter(ms);
            writer.Write(positions.Count);

            foreach (Position position in positions)
            {
                tickers.Add(position.GetTicker());
                position.Write(writer);
            }
            byte[] positionsBytes = ms.ToArray();

            Bin binPositions = new Bin("positions", positionsBytes);
            Bin binTickers = new Bin("tickers", tickers);

            client.Put(policy, key, binPositions, binTickers);
        }
Esempio n. 14
0
        /// <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 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.");
        }
Esempio n. 16
0
        /// <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);
        }
Esempio n. 17
0
        /// <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.");
        }
Esempio n. 18
0
        private void RunReplaceOnlyExample(AerospikeClient client, Arguments args)
        {
            Key key = new Key(args.ns, args.set, "replaceonlykey");
            Bin bin = new Bin("bin", "value");

            // Delete record if it already exists.
            client.Delete(args.writePolicy, key);

            console.Info("Replace record requiring that it exists: namespace={0} set={1} key={2}",
                key.ns, key.setName, key.userKey);

            try
            {
                WritePolicy policy = new WritePolicy();
                policy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
                client.Put(policy, key, bin);

                console.Error("Failure. This command should have resulted in an error.");
            }
            catch (AerospikeException ae)
            {
                if (ae.Result == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    console.Info("Success. Key not found error returned as expected.");
                }
                else
                {
                    throw ae;
                }
            }
        }
Esempio n. 19
0
        /// <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");
        }
Esempio n. 20
0
        /// <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.");
        }
        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.");
        }
        private void WriteRecord(AerospikeClient client, Arguments args, string userKey, string name, string password)
        {
            Key key = new Key(args.ns, args.set, userKey);
            Bin bin1 = new Bin("name", name);
            Bin bin2 = new Bin("password", password);
            console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}",
                key.ns, key.setName, key.userKey, bin1.name, bin1.value);

            client.Put(args.writePolicy, key, bin1, bin2);
        }
Esempio n. 23
0
        public static void Main(string[] args)
        {
            String ns = "test";
            String set = "demo";
            String host = "127.0.0.1";
            int port = 3000;
            const int MAX_RECORDS = 1000;
            AerospikeClient client = new AerospikeClient(host, port);
            /*
             * Create an index on list values
             */
            IndexTask creator = client.CreateIndex(null, ns, set,
                "list_value_index", "interests", IndexType.STRING,
                IndexCollectionType.LIST);
            creator.IsDone();
            Console.WriteLine("created list value index");
            /*
             * Create an index on map keys
             */
            creator = client.CreateIndex(null, ns, set,
                "map_key_index", "email", IndexType.STRING,
                IndexCollectionType.MAPKEYS);
            creator.IsDone();
            Console.WriteLine("created map keys index");
            /*
             * Create an index on map keys
             */
            creator = client.CreateIndex(null, ns, set,
                "map_value_index", "email", IndexType.STRING,
                IndexCollectionType.MAPVALUES);
            creator.IsDone();
            Console.WriteLine("created map values index");
            /*
             * Load some data
             */
            String[] possibleInterests = new String[]{"cats", "dogs", "mice", "birds", "snakes", "fish", "pigs", "cows"};
            String[] emailPostFix = new String[]{"@gmail.com", "@hotmail.com", "@yahoo.com"};
            String[] emailType = new String[]{"home", "work", "private"};
            Random rand1 = new Random();
            Random rand2 = new Random();
            for (int i = 0; i < MAX_RECORDS; i++){
            /*
             * create key
             */
                String userName = "******"+i;
                Key key = new Key(ns, set, userName);
                Bin user = new Bin("user", userName);
            /*
             * create interests
             */
                List<String> interestList = new List<String>();
                int interest_count = rand1.Next(possibleInterests.Length-1);
                for (int j = 0; j < interest_count; j++){
                    interestList.Add(possibleInterests[rand2.Next(possibleInterests.Length-1)]);
                }
                Bin interests = new Bin("interests", interestList);
            /*
             * create email addresses
             */
                Dictionary<String, String> emailAddresses = new Dictionary<String, String>();
                int email_count = rand1.Next(emailPostFix.Length-1);
                for (int j = 0; j < email_count; j++){
                    String type = emailType[rand2.Next(emailType.Length-1)];
                    String emailString = userName + emailPostFix[rand2.Next(emailPostFix.Length-1)];
                    emailAddresses.Add(type, emailString);
                }
                Bin emails = new Bin("email", emailAddresses);

                client.Put(null, key, user, interests, emails);

            }
            Console.WriteLine(String.Format("created {0} users", MAX_RECORDS));
            /*
             * Query for users interested in "cats"
             */
            Statement stmt = new Statement();
            stmt.SetNamespace(ns);
            stmt.SetSetName(set);
            stmt.SetBinNames("user", "interests");
            stmt.SetFilters(Filter.Contains("interests", IndexCollectionType.LIST, "cats"));

            RecordSet recordSet = client.Query(null, stmt);

            int recordCount = 0;
            try {
                while(recordSet.Next()){
                    Console.WriteLine(recordSet.Record);
                    recordCount++;
                }

            } finally {
                recordSet.Close();
            }
            Console.WriteLine(String.Format("Found {0} users interested in cats", recordCount));
            /*
             * Query for users with "work" email addresses.
             */
            stmt.SetBinNames("user", "email");
            stmt.SetFilters(Filter.Contains("email", IndexCollectionType.MAPKEYS, "work"));

            recordSet = client.Query(null, stmt);
            recordCount = 0;
            try {
                while(recordSet.Next()){
                    Console.WriteLine(recordSet.Record);
                    recordCount++;
                }

            } finally {
                recordSet.Close();
            }
            Console.WriteLine(String.Format("Found {0} users with work email address", recordCount));
            /*
             * Query for users with email address equal to "*****@*****.**".
             */
            stmt.SetBinNames("user", "email");
            stmt.SetFilters(Filter.Contains("email", IndexCollectionType.MAPVALUES, "*****@*****.**"));

            recordSet = client.Query(null, stmt);

            recordCount = 0;
            try {
                while(recordSet.Next()){
                    Console.WriteLine(recordSet.Record);
                    recordCount++;
                }

            } finally {
                recordSet.Close();
            }
            Console.WriteLine(String.Format("Found {0} users with  email address of [email protected]", recordCount));
        }
        private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, int size)
        {
            console.Info("Write " + size + " records.");

            for (int i = 1; i <= size; i++)
            {
                Key key = new Key(args.ns, args.set, keyPrefix + i);
                Bin bin = new Bin(binName, i);
                client.Put(args.writePolicy, key, bin);
            }
        }
        private void WriteListRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, string binName2, int size)
        {
            for (int i = 0; i < size; i++)
            {
                Key key = new Key(args.ns, args.set, keyPrefix + i);
                List<Value> mylist = new List<Value>();

                for (int jj = 0; jj < 10; ++jj)
                {

                    double plat = 0.0 + (0.01 * i);
                    double plng = 0.0 + (0.10 * jj);
                    string geoString = GeneratePoint(plat, plng);

                    mylist.Add(Value.GetAsGeoJSON(geoString));

                    double rlat = 0.0 + (0.01 * i);
                    double rlng = 0.0 - (0.10 * jj);

                    geoString = GeneratePolygon(rlat, rlng);

                    mylist.Add(Value.GetAsGeoJSON(geoString));

                }

                Bin bin = new Bin(binName, mylist);
                Bin bin2 = new Bin(binName2, "other_bin_value_" + i);
                client.Put(args.writePolicy, key, bin, bin2);
            }

            console.Info("Write " + size + " records.");
        }
        private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, int size)
        {
            for (int i = 1; i <= size; i++)
            {
                Key key = new Key(args.ns, args.set, keyPrefix + i);
                Bin bin = new Bin("l1", i);

                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, new Bin("l2", 1));
            }
        }
        private void WriteMapRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, string binName2, string valuePrefix, int size)
        {
            for (int i = 0; i < size; i++)
            {
                Key key = new Key(args.ns, args.set, keyPrefix + i);
                Dictionary<string, Value> map = new Dictionary<string, Value>();

                for (int jj = 0; jj < 10; ++jj)
                {
                    double plat = 0.0 + (0.01 * i);
                    double plng = 0.0 + (0.10 * jj);
                    string geoString = GeneratePoint(plat, plng);

                    map[valuePrefix + "pointkey_" + i + "_" + jj] = Value.GetAsGeoJSON(geoString);

                    double rlat = 0.0 + (0.01 * i);
                    double rlng = 0.0 - (0.10 * jj);

                    geoString = GeneratePolygon(rlat, rlng);

                    map[valuePrefix + "regionkey_" + i + "_" + jj] = Value.GetAsGeoJSON(geoString);

                }
                Bin bin = new Bin(binName, map);
                Bin bin2 = new Bin(binName2, "other_bin_value_" + i);
                client.Put(args.writePolicy, key, bin, bin2);
            }

            console.Info("Write " + size + " records.");
        }
Esempio n. 28
0
        /// <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);
        }
Esempio n. 29
0
        /// <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");
        }
Esempio n. 30
0
 private void updateUser(AerospikeClient client, Key userKey, WritePolicy policy, long ts, int tweetCount)
 {
     // Update tweet count and last tweet'd timestamp in the user record
     client.Put(policy, userKey, new Bin("tweetcount", tweetCount), new Bin("lasttweeted", ts));
     Console.WriteLine("INFO: The tweet count now is: " + tweetCount);
 }
        /// <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.");
        }