Exemplo n.º 1
0
        static void storeFlatData(List <Person> pList)
        {
            int i = 0;

            foreach (Person p in pList)
            {
                Key   key      = new Key("test", FlatDataSet, "" + i++);
                Bin[] binArray = new Bin[21];
                binArray[0]  = new Bin("Id", p.Id);
                binArray[1]  = new Bin("Name", p.Name);
                binArray[2]  = new Bin("Line1", p.Line1);
                binArray[3]  = new Bin("Line2", p.Line2);
                binArray[4]  = new Bin("Age", p.Age);
                binArray[5]  = new Bin("Salary", p.Salary);
                binArray[6]  = new Bin("Bal7", p.Bal7);
                binArray[7]  = new Bin("Bal8", p.Bal8);
                binArray[8]  = new Bin("Bal9", p.Bal9);
                binArray[9]  = new Bin("Bal10", p.Bal9);
                binArray[10] = new Bin("Bal11", p.Bal9);
                binArray[11] = new Bin("Bal12", p.Bal9);
                binArray[12] = new Bin("Bal13", p.Bal9);
                binArray[13] = new Bin("Bal14", p.Bal9);
                binArray[14] = new Bin("Bal15", p.Bal9);
                binArray[15] = new Bin("Bal16", p.Bal9);
                binArray[16] = new Bin("Bal17", p.Bal9);
                binArray[17] = new Bin("Bal18", p.Bal9);
                binArray[18] = new Bin("Bal19", p.Bal9);
                binArray[19] = new Bin("Bal20", p.Bal9);
                binArray[20] = new Bin("Bal21", p.Bal9);
                client.Put(null, key, binArray);

                Record r = client.Get(null, key);
                //Console.WriteLine("Person 2 = " + r.GetString("Name"));
            }
        }
        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 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());
        }
Exemplo n.º 3
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.");
            }
        }
Exemplo n.º 4
0
        public void createTweet()
        {
            Console.WriteLine("\n********** Create Tweet **********\n");

            Record userRecord = null;
            Key    userKey    = null;
            Key    tweetKey   = null;

            // Get username
            string username;

            Console.WriteLine("\nEnter username:"******"test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    int nextTweetCount = int.Parse(userRecord.GetValue("tweetcount").ToString()) + 1;

                    // Get tweet
                    string tweet;
                    Console.WriteLine("Enter tweet for " + username + ":");
                    tweet = Console.ReadLine();

                    // Write record
                    WritePolicy wPolicy = new WritePolicy();
                    wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

                    // Create timestamp to store along with the tweet so we can query, index and report on it
                    long ts = getTimeStamp();

                    tweetKey = new Key("test", "tweets", username + ":" + nextTweetCount);
                    Bin bin1 = new Bin("tweet", tweet);
                    Bin bin2 = new Bin("ts", ts);
                    Bin bin3 = new Bin("username", username);

                    client.Put(wPolicy, tweetKey, bin1, bin2, bin3);
                    Console.WriteLine("\nINFO: Tweet record created!");

                    // Update tweet count and last tweet'd timestamp in the user record
                    updateUser(client, userKey, wPolicy, ts, nextTweetCount);
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
        } //createTweet
        /// <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.");
            }
        }
Exemplo n.º 6
0
        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 Cleaning Up

            #endregion
            client.Close();


            return(View());
        }
 protected override void ReadRecord(Policy policy, Key key, string binName)
 {
     if (shared.readLatency != null)
     {
         Stopwatch watch   = Stopwatch.StartNew();
         Record    record  = client.Get(policy, key, binName);
         double    elapsed = watch.Elapsed.TotalMilliseconds;
         OnReadSuccess(elapsed);
     }
     else
     {
         Record record = client.Get(policy, key, binName);
         OnReadSuccess();
     }
 }
Exemplo n.º 8
0
        public static void Run2()
        {
            AerospikeClient client = new AerospikeClient("172.28.128.3", 3000);
            // Initialize policy.
            WritePolicy policy = new WritePolicy();

            policy.SetTimeout(50);  // 50 millisecond timeout


            // Write single value.
            Key key = new Key("test", "myset", "mykey");
            Bin bin = new Bin("mybin", "myvalue");

            client.Put(policy, key, bin);



            Record record = client.Get(policy, key, "mybin");

            if (record != null)
            {
                Console.WriteLine("Got name: " + record.GetValue("mybin"));
            }

            client.Close();
        }
Exemplo n.º 9
0
        private string CheckInDb(string phrase)
        {
//            var _client = new MongoClient();
//            var _database = _client.GetDatabase("SignLanguage");
//            var _collection = _database.GetCollection<PhraseSynonymModel>("PhraseSynonyms");
            AerospikeClient client = new AerospikeClient(configuration["AppSettings:AerospikeClient"], 3000);
            Policy          policy = new Policy();
            Key             key    = new Key("sign-language", "PhraseSynonyms", phrase);
            Record          record = client.Get(policy, key);

            if (record != null)
            {
                foreach (KeyValuePair <string, object> entry in record.bins)
                {
                    if (entry.Key == "Synonym")
                    {
                        return(entry.Value.ToString());
                    }
                }
            }

//            if (_collection.CountDocuments(x=>x.Simplified == phrase.Trim())>0)
//            {
//                return _collection.Find(x => x.Simplified == phrase.Trim()).FirstOrDefault().Synonym.Trim();
//            }

            return(null);
        }
        public IActionResult Index()
        {
            AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

            // Initialize policy.
            WritePolicy policy = new WritePolicy();

            policy.SetTimeout(50);  // 50 millisecond timeout.

            // Write single or multiple values.
            Key key  = new Key("test", "myset", "mykey");
            Bin bin1 = new Bin("name", "John");
            Bin bin2 = new Bin("age", 25);

            client.Put(policy, key, bin1, bin2);

            Record record = client.Get(policy, key, "name");

            if (record != null)
            {
                Console.WriteLine("Got name: " + record.GetValue("name"));
            }



            client.Close();


            return(View());
        }
Exemplo n.º 11
0
        /// <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.");
        }
Exemplo n.º 12
0
        /// <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");
        }
Exemplo n.º 13
0
        public static void Run()
        {
            AerospikeClient client = new AerospikeClient("172.28.128.3", 3000);
            // Initialize policy.
            WritePolicy policy = new WritePolicy();

            policy.SetTimeout(50);  // 50 millisecond timeout


            Key key  = new Key("test", "MultiSet", "mykey");
            Bin bin1 = new Bin("name", "John");
            Bin bin2 = new Bin("age", 25);

            client.Put(policy, key, bin1, bin2);


            Record record = client.Get(policy, key);

            if (record != null)
            {
                foreach (KeyValuePair <string, object> entry in record.bins)
                {
                    Console.WriteLine("Name=" + entry.Key + " Value=" + entry.Value);
                }
            }

            client.Close();
        }
Exemplo n.º 14
0
        /// <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.");
        }
Exemplo n.º 15
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");
        }
Exemplo n.º 16
0
        /// <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.");
        }
Exemplo n.º 17
0
        /// <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);
            }
        }
Exemplo n.º 18
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.");
        }
Exemplo n.º 19
0
        public void RunNestedListCreateExample(AerospikeClient client, Arguments args)
        {
            Key    key     = new Key(args.ns, args.set, "mapkey3");
            string binName = args.GetBinName("mapbin");

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

            IList <Value> l1 = new List <Value>();

            l1.Add(Value.Get(7));
            l1.Add(Value.Get(9));
            l1.Add(Value.Get(5));

            IDictionary <Value, Value> inputMap = new Dictionary <Value, Value>();

            inputMap[Value.Get("key1")] = Value.Get(l1);

            // Create maps.
            client.Put(args.writePolicy, key, new Bin(binName, inputMap));

            // Create ordered list at map's "key2" only if list does not exist.
            // Append 2,1 to ordered list.
            CTX    ctx    = CTX.MapKey(Value.Get("key2"));
            Record record = client.Operate(args.writePolicy, key,
                                           ListOperation.Create(binName, ListOrder.ORDERED, false, ctx),
                                           ListOperation.Append(binName, Value.Get(2), ctx),
                                           ListOperation.Append(binName, Value.Get(1), ctx),
                                           Operation.Get(binName)
                                           );

            record = client.Get(args.policy, key);
            console.Info("Record: " + record);
        }
Exemplo n.º 20
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.");
        }
Exemplo n.º 21
0
        /// <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);
            }
        }
Exemplo n.º 22
0
        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("");
        }
Exemplo n.º 23
0
        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("");
        }
Exemplo n.º 24
0
        /// <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");
            }
        }
Exemplo n.º 25
0
        public void GetBenchmark()
        {
            for (int x = 0; x < TestObjectCount; x++)
            {
                string testKey = $"{IncrementalKeyBase}-{x}";
                Key    key     = new Key(Namespace, Set, testKey);
                Record record  = _client.Get(null, key);

                if (record != null)
                {
                    foreach (KeyValuePair <string, object> product in record.bins)
                    {
                        var prod = (string)product.Value;
                        var deserializedProdct = JsonConvert.DeserializeObject <ProductJson>(prod);
                    }
                }
            }
        }
Exemplo n.º 26
0
        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);
            }
        }
Exemplo n.º 27
0
        public void createTweet()
        {
            Console.WriteLine("\n********** Create Tweet **********\n");
            Record userRecord = null;
            Key    userKey    = null;
            Key    tweetKey   = null;

            // Get username
            string username;

            Console.WriteLine("\nEnter username:"******"test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    int nextTweetCount = int.Parse(userRecord.GetValue("tweetcount").ToString()) + 1;

                    // Get tweet
                    string tweet;
                    Console.WriteLine("Enter tweet for " + username + ":");
                    tweet = Console.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        nextTweetCount = int.Parse(userRecord.GetValue("tweetcount").ToString()) + 1;
                        // Write record
                        WritePolicy wPolicy = new WritePolicy();
                        //wPolicy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
                        wPolicy.SetTimeout(500);
                        // Create timestamp to store along with the tweet so we can query, index and report on it
                        long ts = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();//DateTime.Now.ToString("yyyyMMddHHmmssffff"); //getTimeStamp();

                        tweetKey = new Key("test", "tweets", username + ":" + nextTweetCount);
                        Bin bin1 = new Bin("tweet", tweet + i.ToString());
                        Bin bin2 = new Bin("ts", ts);
                        Bin bin3 = new Bin("username", username);

                        client.Put(wPolicy, tweetKey, bin1, bin2, bin3);
                        Console.WriteLine("\nINFO: Tweet record created!");

                        // Update tweet count and last tweet'd timestamp in the user record
                        client.Put(wPolicy, userKey, new Bin("tweetcount", nextTweetCount), new Bin("lasttweeted", ts));
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
        } //
Exemplo n.º 28
0
        } //createUser

        public void getUser()
        {
            Record userRecord = null;
            Key    userKey    = null;

            // Get username
            string username;

            Console.WriteLine("\nEnter username:"******"test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    // TODO: Output user record to the console
                    // Remember to convert list into comma-separated interests before outputting it.
                    // Exercise K2
                    Console.WriteLine("\nINFO: User record read successfully! Here are the details:\n");
                    Console.WriteLine("username:     "******"username"));
                    Console.WriteLine("password:     "******"password"));
                    Console.WriteLine("gender:       " + userRecord.GetValue("gender"));
                    Console.WriteLine("region:       " + userRecord.GetValue("region"));
                    Console.WriteLine("tweetcount:   " + userRecord.GetValue("tweetcount"));
                    List <object> interests = (List <object>)userRecord.GetValue("interests");
                    Console.WriteLine("interests:    " + interests.Aggregate((x, y) => x + "," + y));
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
            else
            {
                Console.WriteLine("ERROR: Invalid user name.");
            }
        } //getUser
Exemplo n.º 29
0
        private List <byte[]> SubRecordList(Key key)
        {
            Record record = client.Get(this.policy, key, binNameString);

            if (record != null)
            {
                return((List <byte[]>)record.GetList(binNameString));
            }
            return(null);
        }
Exemplo n.º 30
0
        } //getUserTweets

        public void updatePasswordUsingUDF()
        {
            Record userRecord = null;
            Key    userKey    = null;

            // Get username
            string username;

            Console.WriteLine("\nEnter username:"******"test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    //Get new password
                    string password;
                    Console.WriteLine("Enter new password for " + username + ":");
                    password = Console.ReadLine();

                    // TODO: Update userRecord using UDF
                    // Exercise R2
                    // NOTE: UDF registration has been included here for convenience and to demonstrate the syntax.
                    // NOTE: The recommended way of registering UDFs in production env is via AQL
                    string luaDirectory = @"..\..\udf";
                    LuaConfig.PackagePath = luaDirectory + @"\?.lua";
                    string       filename = "updateUserPwd.lua";
                    string       path     = Path.Combine(luaDirectory, filename);
                    RegisterTask rt       = client.Register(null, path, filename, Language.LUA);
                    rt.Wait();

                    // TODO: Execute the UDF updatePassword.lua
                    // Exercise R2
                    string updatedPassword = client.Execute(null, userKey, "updateUserPwd", "updatePassword", Value.Get(password)).ToString();

                    // TODO: Output the updated passord returned by the UDF
                    // Exercise R2
                    Console.WriteLine("\nINFO: The password has been set to: " + updatedPassword);
                }
                else
                {
                    Console.WriteLine("\nERROR: User record not found.");
                }
            }
            else
            {
                Console.WriteLine("\nERROR: Invalid user name.");
            }
        } //updatePasswordUsingUDF