/// <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);
        }
        public override void Init(int flowCount, long flowRecordCount)
        {
            string[] str    = ConnectionString.Split(';');
            string   server = str[0].Split(':')[1];
            int      port   = Int32.Parse(str[1].Split(':')[1]);
            string   ns     = str[2].Split(':')[1];
            string   set    = str[3].Split(':')[1];

            client  = new AerospikeClient(server, port);
            indexes = new LargeList[flowCount];

            Key listKey = new Key(ns, set, CollectionName);

            client.Delete(null, listKey);

            WritePolicy policy = new WritePolicy();

            policy.recordExistsAction = RecordExistsAction.REPLACE;

            for (int i = 0; i < flowCount; i++)
            {
                indexes[i] = client.GetLargeList(policy, listKey, CollectionName, null);
            }
        }
Exemple #3
0
        public void Add100IntOneByOne()
        {
            Key key = new Key(NS, SET, "100-list-test-key-int");

            client.Delete(null, key);
            var ll = new Aerospike.Helper.Collections.LargeList(client, null, key, "100-int");

            WriteIntSubElements(ll, 100);
            Assert.AreEqual(100, ll.Size());
            ll.Destroy();
            client.Delete(null, key);
        }
Exemple #4
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));
            }
        }
Exemple #5
0
        /// <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>
        /// Example functions not in use
        /// </summary>
        private void deleteTweets()
        {
            RecordSet rs = null;

            try
            {
                // Get username
                string username;
                Console.WriteLine("\nEnter username:"******"test", "users", username);
                    Record userRecord = client.Get(null, userKey);
                    if (userRecord != null)
                    {
                        WritePolicy wPolicy = new WritePolicy();
                        wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

                        string[]  bins = { "tweet" };
                        Statement stmt = new Statement();
                        stmt.SetNamespace("test");
                        stmt.SetSetName("tweets");
                        stmt.SetIndexName("username_index");
                        stmt.SetBinNames(bins);
                        stmt.SetFilters(Filter.Equal("username", username));

                        Console.WriteLine("\nDeleting " + username + "'s tweet(s):\n");

                        rs = client.Query(null, stmt);
                        while (rs.Next())
                        {
                            Record r = rs.Record;
                            Console.WriteLine(r.GetValue("tweet"));
                            client.Delete(null, rs.Key);
                        }
                        //Update tweetcount and timestamp to reflect this
                        client.Operate(wPolicy, userKey, Operation.Put(new Bin("tweetcount", 0)), Operation.Put(new Bin("lasttweeted", 0)));
                        rs.Close();
                    }
                    else
                    {
                        Console.WriteLine("ERROR: User record not found!");
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
            finally
            {
                if (rs != null)
                {
                    // Close record set
                    rs.Close();
                }
            }
        }
        public IActionResult Index()
        {
            #region Initialize
            AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

            // Initialize policy.
            WritePolicy policy = new WritePolicy();
            policy.SetTimeout(50);  // 50 millisecond timeout.
            #endregion

            #region Write Records
            // Write single value.
            Key key = new Key("test", "myset", "mykey");
            Bin bin = new Bin("mybin", "myvalue");
            client.Put(policy, key, bin);

            // Write multiple values.
            Key key2 = new Key("test", "myset", "mykey");
            Bin bin1 = new Bin("name", "John");
            Bin bin2 = new Bin("age", 25);
            client.Put(policy, key2, bin1, bin2);
            #endregion

            #region Read Records
            //Reading a Single or Multiple  Value
            Record record = client.Get(policy, key, "name");
            if (record != null)
            {
                Console.WriteLine("Got name: " + record.GetValue("name"));
            }
            Record recordMultiple = client.Get(policy, key);
            if (recordMultiple != null)
            {
                foreach (KeyValuePair <string, object> entry in recordMultiple.bins)
                {
                    Console.WriteLine("Name=" + entry.Key + " Value=" + entry.Value);
                }
            }
            #endregion

            #region Delete Record
            Key keyDel = new Key("test", "myset", "mykey");
            client.Delete(policy, keyDel);
            #endregion

            #region Batch Reads
            //Multiple records can be read in a single batch call.
            Key[] keysBach = new Key[size];
            for (int i = 0; i < 1000; i++)
            {
                keys[i] = new Key("test", "myset", (i + 1));
            }
            Record[] records = client.Get(policy, keysBach);
            #endregion



            #region Cleaning Up
            //Call Close() when all transactions are finished and the application is ready to shutdown.
            //The AerospikeClient object can no longer be called after calling Close).
            client.Close();
            #endregion


            return(View());
        }
Exemple #8
0
        /// <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);
            }
        }
Exemple #9
0
        /// <summary>
        /// Use serialized bin for row in largelist bin.
        /// </summary>
        private void RunWithSerializedBin(AerospikeClient client, Arguments args)
        {
            Key key = new Key(args.ns, args.set, "accountId");

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

            // Initialize large list operator.
            Aerospike.Client.LargeList list = client.GetLargeList(args.writePolicy, key, "trades");

            // Write trades
            Dictionary <string, Value> dict = new Dictionary <string, Value>();
            MemoryStream ms = new MemoryStream(500);

            DateTime timestamp1 = new DateTime(2014, 6, 25, 12, 18, 43);

            dict["key"] = Value.Get(timestamp1.Ticks);
            BinaryWriter writer = new BinaryWriter(ms);

            writer.Write("IBM");              // ticker
            writer.Write(100);                // qty
            writer.Write(181.82);             // price
            dict["value"] = Value.Get(ms.ToArray());
            list.Add(Value.Get(dict));

            DateTime timestamp2 = new DateTime(2014, 6, 26, 9, 33, 17);

            dict["key"] = Value.Get(timestamp2.Ticks);
            ms.SetLength(0);
            writer = new BinaryWriter(ms);
            writer.Write("GE");              // ticker
            writer.Write(500);               // qty
            writer.Write(26.36);             // price
            dict["value"] = Value.Get(ms.ToArray());
            list.Add(Value.Get(dict));

            DateTime timestamp3 = new DateTime(2014, 6, 27, 14, 40, 19);

            dict["key"] = Value.Get(timestamp3.Ticks);
            ms.SetLength(0);
            writer = new BinaryWriter(ms);
            writer.Write("AAPL");              // ticker
            writer.Write(75);                  // qty
            writer.Write(91.85);               // price
            dict["value"] = Value.Get(ms.ToArray());
            list.Add(Value.Get(dict));

            // Verify list size
            int size = list.Size();

            if (size != 3)
            {
                throw new Exception("List size mismatch. Expected 3 Received " + size);
            }

            // Filter on range of timestamps
            DateTime begin   = new DateTime(2014, 6, 26);
            DateTime end     = new DateTime(2014, 6, 28);
            IList    results = list.Range(Value.Get(begin.Ticks), Value.Get(end.Ticks));

            if (results.Count != 2)
            {
                throw new Exception("Query results size mismatch. Expected 2 Received " + results.Count);
            }

            // Verify data.
            ValidateWithSerializedBin(results, 0, timestamp2, "GE", 500, 26.36);
            ValidateWithSerializedBin(results, 1, timestamp3, "AAPL", 75, 91.85);

            console.Info("Data matched.");
        }
Exemple #10
0
        /// <summary>
        /// Use distinct sub-bins for row in largelist bin.
        /// </summary>
        private void RunWithDistinctBins(AerospikeClient client, Arguments args)
        {
            Key key = new Key(args.ns, args.set, "accountId");

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

            // Initialize large list operator.
            Aerospike.Client.LargeList list = client.GetLargeList(args.writePolicy, key, "trades");

            // Write trades
            Dictionary <string, Value> dict = new Dictionary <string, Value>();

            DateTime timestamp1 = new DateTime(2014, 6, 25, 12, 18, 43);

            dict["key"]    = Value.Get(timestamp1.Ticks);
            dict["ticker"] = Value.Get("IBM");
            dict["qty"]    = Value.Get(100);
            dict["price"]  = Value.Get(BitConverter.GetBytes(181.82));
            list.Add(Value.Get(dict));

            DateTime timestamp2 = new DateTime(2014, 6, 26, 9, 33, 17);

            dict["key"]    = Value.Get(timestamp2.Ticks);
            dict["ticker"] = Value.Get("GE");
            dict["qty"]    = Value.Get(500);
            dict["price"]  = Value.Get(BitConverter.GetBytes(26.36));
            list.Add(Value.Get(dict));

            DateTime timestamp3 = new DateTime(2014, 6, 27, 14, 40, 19);

            dict["key"]    = Value.Get(timestamp3.Ticks);
            dict["ticker"] = Value.Get("AAPL");
            dict["qty"]    = Value.Get(75);
            dict["price"]  = Value.Get(BitConverter.GetBytes(91.85));
            list.Add(Value.Get(dict));

            // Verify list size
            int size = list.Size();

            if (size != 3)
            {
                throw new Exception("List size mismatch. Expected 3 Received " + size);
            }

            // Filter on range of timestamps
            DateTime begin   = new DateTime(2014, 6, 26);
            DateTime end     = new DateTime(2014, 6, 28);
            IList    results = list.Range(Value.Get(begin.Ticks), Value.Get(end.Ticks));

            if (results.Count != 2)
            {
                throw new Exception("Query results size mismatch. Expected 2 Received " + results.Count);
            }

            // Verify data.
            ValidateWithDistinctBins(results, 0, timestamp2, "GE", 500, 26.36);
            ValidateWithDistinctBins(results, 1, timestamp3, "AAPL", 75, 91.85);

            console.Info("Data matched.");

            console.Info("Run large list scan.");
            IList rows = list.Scan();

            foreach (IDictionary row in rows)
            {
                foreach (DictionaryEntry entry in row)
                {
                    //console.Info(entry.Key.ToString());
                    //console.Info(entry.Value.ToString());
                }
            }
            console.Info("Large list scan complete.");
        }