Exemple #1
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");
            }
        }
        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());
        }
        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!");
                }
            }
        } //
Exemple #4
0
        /// <summary>
        /// Add value to list.  Fail if value's key exists and list is configured for unique keys.
        /// If value is a map, the key is identified by "key" entry.  Otherwise, the value is the key.
        /// If large list does not exist, create it.
        /// </summary>
        /// <param name="value">value to add</param>

        public void Add(Value value)
        {
            Key subKey = MakeSubKey(value);

            client.Put(this.policy, subKey, new Bin(ListElementBinName, value));

            // add the digest of the subKey to the CDT List in the Customer record
            Value digest = Value.Get(subKey.digest);

            client.Operate(this.policy, this.key, ListOperation.Append(this.binNameString, digest));
        }
Exemple #5
0
        } //queryUsersByTweetCount

        public void createTweets()
        {
            string[]    randomTweets = { "For just $1 you get a half price download of half of the song and listen to it just once.", "People tell me my body looks like a melted candle", "Come on movie! Make it start!", "Byaaaayy", "Please, please, win! Meow, meow, meow!", "Put. A. Bird. On. It.", "A weekend wasted is a weekend well spent", "Would you like to super spike your meal?", "We have a mean no-no-bring-bag up here on aisle two.", "SEEK: See, Every, EVERY, Kind... of spot", "We can order that for you. It will take a year to get there.", "If you are pregnant, have a soda.", "Hear that snap? Hear that clap?", "Follow me and I may follow you", "Which is the best cafe in Portland? Discuss...", "Portland Coffee is for closers!", "Lets get this party started!", "How about them portland blazers!", "You got school'd, yo", "I love animals", "I love my dog", "What's up Portland", "Which is the best cafe in Portland? Discuss...", "I dont always tweet, but when I do it is on Tweetaspike" };
            Random      rnd1         = new Random();
            Random      rnd2         = new Random();
            Random      rnd3         = new Random();
            Key         userKey;
            Record      userRecord;
            int         totalUsers = 1000;
            int         maxUsers   = 10000;
            int         maxTweets  = 20;
            string      username;
            long        ts      = 0;
            WritePolicy wPolicy = new WritePolicy();

            wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

            Console.WriteLine("\nCreate up to " + maxTweets + " tweets each for " + totalUsers + " users. Press any key to continue...");
            Console.ReadLine();

            for (int j = 0; j < totalUsers; j++)
            {
                // Check if user record exists
                username   = "******" + rnd3.Next(1, maxUsers);
                userKey    = new Key("test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    // create up to maxTweets random tweets for this user
                    int totalTweets = rnd1.Next(1, (maxTweets + 1));
                    for (int k = 1; k <= totalTweets; k++)
                    {
                        // Create timestamp to store along with the tweet so we can query, index and report on it
                        ts = getTimeStamp();
                        Key tweetKey = new Key("test", "tweets", username + ":" + k);
                        Bin bin1     = new Bin("tweet", randomTweets[rnd2.Next(1, randomTweets.Length)]);
                        Bin bin2     = new Bin("ts", ts);
                        Bin bin3     = new Bin("username", username);

                        client.Put(wPolicy, tweetKey, bin1, bin2, bin3);
                    }
                    if (totalTweets > 0)
                    {
                        // Update tweet count and last tweet'd timestamp in the user record
                        client.Put(wPolicy, userKey, new Bin("tweetcount", totalTweets), new Bin("lasttweeted", ts));
                        //Console.WriteLine("INFO: The tweet count now is: " + totalTweets);
                    }
                    Console.WriteLine("Wrote " + totalTweets + " tweets for " + username);
                }
            }
            Console.WriteLine("\nDone creating up to " + maxTweets + " tweets each for " + totalUsers + " users!");
        } //createTweets
        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
        public void createUser()
        {
            Console.WriteLine("\n********** Create User **********\n");

            string username;
            string password;
            string gender;
            string region;
            string interests;

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

                // Get gender
                Console.WriteLine("Select gender (f or m) for " + username + ":");
                gender = Console.ReadLine().Substring(0, 1);

                // Get region
                Console.WriteLine("Select region (north, south, east or west) for " + username + ":");
                region = Console.ReadLine().Substring(0, 1);

                // Get interests
                Console.WriteLine("Enter comma-separated interests for " + username + ":");
                interests = Console.ReadLine();

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

                Key key  = new Key("test", "users", username);
                Bin bin1 = new Bin("username", username);
                Bin bin2 = new Bin("password", password);
                Bin bin3 = new Bin("gender", gender);
                Bin bin4 = new Bin("region", region);
                Bin bin5 = new Bin("lasttweeted", 0);
                Bin bin6 = new Bin("tweetcount", 0);
                Bin bin7 = Bin.AsList("interests", interests.Split(',').ToList <object>());

                client.Put(wPolicy, key, bin1, bin2, bin3, bin4, bin5, bin6, bin7);

                Console.WriteLine("\nINFO: User record created!");
            }
        } //createUser
Exemple #8
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());
        }
Exemple #9
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);
        }
 protected override void WriteRecord(WritePolicy policy, Key key, Bin bin)
 {
     if (shared.writeLatency != null)
     {
         Stopwatch watch = Stopwatch.StartNew();
         client.Put(policy, key, bin);
         double elapsed = watch.Elapsed.TotalMilliseconds;
         OnWriteSuccess(elapsed);
     }
     else
     {
         client.Put(policy, key, bin);
         OnWriteSuccess();
     }
 }
Exemple #11
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;
                }
            }
        }
        private void WriteMapKeyRecords(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 <Value, string> map = new Dictionary <Value, string>();

                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[Value.GetAsGeoJSON(geoString)] = valuePrefix + "pointkey_" + i + "_" + jj;

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

                    geoString = GeneratePolygon(rlat, rlng);

                    map[Value.GetAsGeoJSON(geoString)] = valuePrefix + "regionkey_" + i + "_" + jj;
                }
                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.");
        }
Exemple #13
0
        /// <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 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.");
        }
Exemple #15
0
        private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName1, string binName2, int size)
        {
            console.Info("Write " + size + " records.");

            for (int i = 0; i < size; i++)
            {
                double        lng  = -122 + (0.1 * i);
                double        lat  = 37.5 + (0.1 * i);
                StringBuilder ptsb = new StringBuilder();
                ptsb.Append("{ \"type\": \"Point\", \"coordinates\": [");
                ptsb.Append(lng);
                ptsb.Append(", ");
                ptsb.Append(lat);
                ptsb.Append("] }");
                Key key  = new Key(args.ns, args.set, keyPrefix + i);
                Bin bin1 = Bin.AsGeoJSON(binName1, ptsb.ToString());
                Bin bin2;
                if (i % 7 == 0)
                {
                    bin2 = new Bin(binName2, "hospital");
                }
                else if (i % 2 == 0)
                {
                    bin2 = new Bin(binName2, "school");
                }
                else
                {
                    bin2 = new Bin(binName2, "store");
                }
                client.Put(args.writePolicy, key, bin1, bin2);
            }
        }
        /// <summary>
        /// Write/Read ArrayList<String> directly instead of relying on default serializer.
        /// </summary>
        private void TestListStrings(AerospikeClient client, Arguments args)
        {
            console.Info("Read/Write ArrayList<String>");
            Key key = new Key(args.ns, args.set, "listkey1");

            client.Delete(args.writePolicy, key);

            List <object> list = new List <object>();

            list.Add("string1");
            list.Add("string2");
            list.Add("string3");

            Bin bin = new Bin(args.GetBinName("listbin1"), list);

            client.Put(args.writePolicy, key, bin);

            Record        record       = client.Get(args.policy, key, bin.name);
            List <object> receivedList = (List <object>)record.GetValue(bin.name);

            ValidateSize(3, receivedList.Count);
            Validate("string1", receivedList[0]);
            Validate("string2", receivedList[1]);
            Validate("string3", receivedList[2]);

            console.Info("Read/Write ArrayList<String> successful.");
        }
        /// <summary>
        /// Write/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");
        }
Exemple #18
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.");
        }
        private void WriteRecords(AerospikeClient client, Arguments args, string binName, int size)
        {
            console.Info("Write " + size + " records.");

            for (int i = 1; i <= size; i++)
            {
                Key key  = new Key(args.ns, args.set, i);
                Bin bin1 = new Bin(binName, i);
                Bin bin2 = new Bin("bin2", i * 10);
                Bin bin3;

                if (i % 4 == 0)
                {
                    bin3 = new Bin("bin3", "prefix-" + i + "-suffix");
                }
                else if (i % 2 == 0)
                {
                    bin3 = new Bin("bin3", "prefix-" + i + "-SUFFIX");
                }
                else
                {
                    bin3 = new Bin("bin3", "pre-" + i + "-suf");
                }
                client.Put(args.writePolicy, key, bin1, bin2, bin3);
            }
        }
Exemple #20
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();
        }
Exemple #21
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.");
        }
Exemple #22
0
        private static void WriteValueWithTtl(AerospikeClient client)
        {
            var writePolicy = new WritePolicy {
                expiration = 2
            };

            var ttlKey = new Key("test", "myTtlSet", "myTtlKey");
            var bin    = new Bin("gender", "female");

            client.Put(writePolicy, ttlKey, bin);

            var policy = new Policy();

            CheckKeyExists(client, policy, ttlKey);
            Console.WriteLine("sleeping for 4 seconds");
            try
            {
                Thread.Sleep(4000);
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine(e + "");
            }
            CheckKeyExists(client, policy, ttlKey);
            Console.WriteLine("");
        }
        /// <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");
        }
Exemple #24
0
        public void AddOrUpdate(InstanceInfo instanceInfo)
        {
            if (instanceInfo == null)
            {
                return;
            }

            var policy = new WritePolicy()
            {
                recordExistsAction = RecordExistsAction.REPLACE
            };

            var key = new Key(_settings[Keystore.AerospikeKeys.InstanceStore.Namespace], _settings[Keystore.AerospikeKeys.InstanceStore.Set], instanceInfo.GetUniqueId());

            var regionBin   = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.RegionId);
            var zoneBin     = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.ZoneId);
            var instanceBin = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.InstanceId);

            var timestampBin = new Bin(Keystore.AerospikeKeys.InstanceStore.TimestampBin, instanceInfo.UpdateTimestamp.ToString("T"));

            using (AerospikeClient client = GetClient())
            {
                client.Put(policy, key, regionBin, zoneBin, instanceBin, timestampBin);
            }
        }
        /// <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.");
        }
        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());
        }
        /// <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.");
        }
Exemple #28
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();
        }
        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);
        }
Exemple #30
0
        public static void Run()
        {
            AerospikeClient client = new AerospikeClient("172.28.128.3", 3000);

            WritePolicy policy = new WritePolicy();

            policy.SetTimeout(50);


            Key key  = new Key("test", "MultiOps", "opkey");
            Bin bin1 = new Bin("optintbin", 7);
            Bin bin2 = new Bin("optstringbin", "string value");

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

            Bin bin3 = new Bin(bin1.name, 4);
            Bin bin4 = new Bin(bin2.name, "new string");


            Record record = client.Operate(policy, key, Operation.Add(bin3),
                                           Operation.Put(bin4), Operation.Get());


            var rec = client.Operate(policy, key,
                                     Operation.Get(),
                                     Operation.Delete());
        }