示例#1
0
        public void IndexClean()
        {
            string path = Path.GetFullPath("TestData\\IndexClean");

            using (var db = new KeyValueStore(path)) {
                db.Truncate();
                db.Manifest.Logger = msg => Console.WriteLine(msg);

                db.Set(Encoding.UTF8.GetBytes("KeyA"), Encoding.UTF8.GetBytes("ValueA:1"), new Dictionary <string, byte[]> {
                    { "Idx", Encoding.UTF8.GetBytes("1") }
                });
                db.Set(Encoding.UTF8.GetBytes("KeyB"), Encoding.UTF8.GetBytes("ValueB:2"), new Dictionary <string, byte[]> {
                    { "Idx", Encoding.UTF8.GetBytes("2") }
                });
                db.Set(Encoding.UTF8.GetBytes("KeyC"), Encoding.UTF8.GetBytes("ValueC:3"), new Dictionary <string, byte[]> {
                    { "Idx", Encoding.UTF8.GetBytes("3") }
                });

                var lookupValue = db.Find("Idx", Encoding.UTF8.GetBytes("3")).Single();
                Assert.AreEqual("ValueC:3", Encoding.UTF8.GetString(lookupValue.Value));
                Assert.AreEqual("KeyC", Encoding.UTF8.GetString(lookupValue.Key));

                db.Delete(Encoding.UTF8.GetBytes("KeyC"));
            }

            // Open the index directly and confirm that the lookup key is still there
            using (var db = new KeyValueStore(Path.Combine(path, "Idx"))) Assert.AreEqual(3, db.Enumerate().Count());

            using (var db = new KeyValueStore(path)) db.CleanIndex("Idx");

            // Open the index directly and confirm that the lookup key is now gone
            using (var db = new KeyValueStore(Path.Combine(path, "Idx"))) Assert.AreEqual(2, db.Enumerate().Count());
        }
示例#2
0
        public void ShouldBeAbleToDelete()
        {
            var version           = Kvs.Overwrite(Key, RandomValue, DefaultTransaction);
            var newDeletedVersion = Kvs.Delete(Key, version, DefaultTransaction);

            var isExceptionRaised = false;

            try
            {
                Kvs.Read(Key);
            }
            catch (ArgumentException)
            {
                isExceptionRaised = true;
            }

            Assert.AreEqual(isExceptionRaised, true);
        }
示例#3
0
        public void RemoveDeletedValuesFromIndex()
        {
            string path  = Path.GetFullPath("TestData\\RemoveDeletedValuesFromIndex");
            var    timer = new Stopwatch();

            using (var db = new KeyValueStore(path)) {
                db.Truncate();
                int totalSize = 0;
                db.Manifest.Logger = msg => Console.WriteLine(msg);

                var indexed   = new SortedDictionary <string, byte[]>();
                int num_items = 1000;
                timer.Start();
                for (int i = 0; i < num_items; i++)
                {
                    indexed["Mod"] = BitConverter.GetBytes(i % 100);
                    db.Set(BitConverter.GetBytes(i), BitConverter.GetBytes(i), indexed);
                    totalSize += 8 + 4;
                }
                timer.Stop();

                Console.WriteLine("Wrote data (with indexing) at a throughput of {0} MB/s", (double)totalSize / timer.Elapsed.TotalSeconds / (1024.0 * 1024.0));

                timer.Reset();
                timer.Start();
                var ctModZeros = db.Find("Mod", BitConverter.GetBytes((int)0)).Count();
                timer.Stop();
                Assert.AreEqual(10, ctModZeros);
                Console.WriteLine("Scanned index at a throughput of {0} items/s", (double)ctModZeros / timer.Elapsed.TotalSeconds);
            }

            // Open the index directly and see if the data is there
            using (var db = new KeyValueStore(Path.Combine(path, "Mod"))) {
                int num_vals = db.EnumerateFromKey(BitConverter.GetBytes((int)0)).Count(pair => pair.Key.Take(4).All(b => b == 0));

                Assert.AreEqual(10, num_vals);
            }

            // Re-open the main key-value store and delete the value at 30
            using (var db = new KeyValueStore(path)) {
                db.Delete(BitConverter.GetBytes(200));

                // Clean the data from the index
                db.RemoveFromIndex(BitConverter.GetBytes(200), new Dictionary <string, byte[]> {
                    { "Mod", BitConverter.GetBytes(200 % 100) }
                });
            }

            // Open the index again directly and confirm that the lookup key is gone now as well
            using (var db = new KeyValueStore(Path.Combine(path, "Mod"))) {
                int num_vals = db.EnumerateFromKey(BitConverter.GetBytes((int)0)).Count(pair => pair.Key.Take(4).All(b => b == 0));

                Assert.AreEqual(9, num_vals);
            }
        }
示例#4
0
 public void DeleteMany()
 {
     KeyBytes[] nonExistentKeys = Enumerable.Range(0, 10)
                                  .Select(_ => NewRandomKey())
                                  .ToArray();
     KeyBytes[] keys = PreStoredDataKeys
                       .Concat(PreStoredDataKeys.Take(PreStoredDataCount / 2))
                       .Concat(nonExistentKeys)
                       .ToArray();
     KeyValueStore.Delete(keys);
     Assert.All(keys, k => Assert.False(KeyValueStore.Exists(k)));
 }
示例#5
0
        public void Delete()
        {
            foreach (KeyBytes key in PreStoredDataKeys)
            {
                KeyValueStore.Delete(key);
                Assert.False(KeyValueStore.Exists(key));
            }

            KeyBytes nonExistent = NewRandomKey();

            KeyValueStore.Delete(nonExistent);
            Assert.False(KeyValueStore.Exists(nonExistent));
        }
示例#6
0
        static void Main(string[] args)
        {
            #region Alpha
            Console.WriteLine("===================================================");
            Console.WriteLine("Key Value Store Basic");
            Console.WriteLine("===================================================");
            Console.WriteLine();
            var dictionary = new KeyValueStore <string, int>();

            Console.WriteLine("Get a non existing key and check value");
            var result = dictionary.Get("a");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"Value for key A is { ConvertToString(result) }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Set a keyvalue pair with Key==A and Value==10");
            Console.WriteLine();
            dictionary.Set("a", 10);

            result = dictionary.Get("a");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"The value for key A is { ConvertToString(result) }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Set a keyvalue pair with Key==B and Value==10");
            Console.WriteLine();
            dictionary.Set("b", 10);

            var cnt = dictionary.Count(10);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Number of times the value 10 occured was { cnt }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Delete Key C");
            dictionary.Delete("c");
            Console.WriteLine($"Delete Key B");
            dictionary.Delete("b");
            Console.WriteLine();

            cnt = dictionary.Count(10);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Number of times the value 10 occured was { cnt }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Update key A value to 20");
            Console.WriteLine();
            dictionary.Set("a", 20);

            Console.ForegroundColor = ConsoleColor.Green;
            cnt = dictionary.Count(10);
            Console.WriteLine($"Number of times 10 occured was { cnt }");
            cnt = dictionary.Count(20);
            Console.WriteLine($"Number of times 20 occured was { cnt }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine(dictionary.End());

            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.Black;

            Console.WriteLine("===================================================");
            Console.WriteLine("===================================================");
            Console.WriteLine();
            #endregion

            #region Beta
            //Console.WriteLine("===================================================");
            //Console.WriteLine("Key Value Store Transaction");
            //Console.WriteLine("===================================================");
            //Console.WriteLine();

            //var mainDictionary = new KeyValueStore<string, int>();

            //using (var scope = new TransactionService<string, int>(mainDictionary))
            //{
            //    Console.WriteLine("Begin TransAction");
            //    scope.Begin();

            //    Console.WriteLine("Set a keyvalue pair with Key==A and Value==10");
            //    scope.CurrentStore.Set("a", 10);

            //    Console.WriteLine("Try Commit Transaction");
            //    Console.WriteLine();
            //    IsTransactionSuccesful(scope.Committed());
            //}

            //var transActionResult = mainDictionary.Get("a");
            //Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine($"The value for key A is { ConvertToString(transActionResult) }");
            //Console.WriteLine();

            //Console.ForegroundColor = ConsoleColor.White;
            //Console.BackgroundColor = ConsoleColor.Black;
            //Console.WriteLine("===================================================");
            //Console.WriteLine("===================================================");
            //Console.WriteLine();
            #endregion

            #region Charlie
            //Console.WriteLine("===================================================");
            //Console.WriteLine("Key Value Store Transaction");
            //Console.WriteLine("===================================================");
            //Console.WriteLine();

            //var mainDictionary = new KeyValueStore<string, int>();

            //using (var scope = new TransactionService<string, int>(mainDictionary))
            //{
            //    Console.WriteLine("Begin TransAction");
            //    scope.Begin();

            //    Console.WriteLine("Set a keyvalue pair with Key==A and Value==20");
            //    scope.CurrentStore.Set("a", 20);

            //    Console.WriteLine("Try Rollback Transaction");
            //    Console.WriteLine();
            //    IsTransactionSuccesful(scope.RollBack()); //Can also just not do anything as using will invoke dispose at end and will auto rollback with no commit
            //}

            //var transActionResult = mainDictionary.Get("a");
            //Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine($"The value for key A is { ConvertToString(transActionResult) }");
            //Console.WriteLine();

            //Console.ForegroundColor = ConsoleColor.White;
            //Console.BackgroundColor = ConsoleColor.Black;
            //Console.WriteLine("===================================================");
            //Console.WriteLine("===================================================");
            //Console.WriteLine();
            #endregion

            #region Delta
            Console.WriteLine("===================================================");
            Console.WriteLine("Key Value Store Transaction");
            Console.WriteLine("===================================================");
            Console.WriteLine();

            var mainDictionary = new KeyValueStore <string, int>();

            using (var scope = new TransactionService <string, int>(mainDictionary))
            {
                Console.WriteLine("Begin Transaction");
                scope.Begin();

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Set a keyvalue pair with Key==A and Value==20");
                Console.WriteLine();
                scope.CurrentStore.Set("a", 20);

                using (var transaction = new TransactionService <string, int>(scope.MainStore))
                {
                    Console.WriteLine("Begin 2nd Transaction");
                    transaction.Begin();

                    Console.WriteLine("Set a keyvalue pair with Key==A and Value==50");
                    Console.WriteLine();
                    transaction.CurrentStore.Set("a", 50);

                    var value = transaction.CurrentStore.Get("a");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine($"The value for key A is { ConvertToString(value) }");
                    Console.WriteLine();

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"Try Rollback Transaction 2");
                    IsTransactionSuccesful(transaction.RollBack()); //will auto dispose with no commit at end of using
                }

                Console.ForegroundColor = ConsoleColor.Green;
                var val = scope.CurrentStore.Get("a");
                Console.WriteLine($"The value for key A is { ConvertToString(val) }");
                Console.WriteLine();

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine($"Try Commit Transaction");
                IsTransactionSuccesful(scope.Committed());
            }

            var transActionResult = mainDictionary.Get("a");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"The value for key A is { ConvertToString(transActionResult) }");
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.WriteLine("===================================================");
            Console.WriteLine("===================================================");
            Console.WriteLine();
            #endregion

            #region November
            //Console.WriteLine("===================================================");
            //Console.WriteLine("Key Value Store Transaction");
            //Console.WriteLine("===================================================");
            //Console.WriteLine();

            //var mainDictionary = new KeyValueStore<string, int>();

            //using (var scope = new TransactionService<string, int>(mainDictionary))
            //{
            //    Console.ForegroundColor = ConsoleColor.White;
            //    Console.WriteLine("Set a keyvalue pair with Key==A and Value==20");
            //    Console.WriteLine();
            //    scope.CurrentStore?.Set("a", 20);

            //    Console.ForegroundColor = ConsoleColor.White;
            //    Console.WriteLine($"Try Commit Transaction");

            //    IsTransactionSuccesful(scope.Committed());
            //}

            #endregion

            Console.ReadKey();
        }
        /// <summary>
        /// Deletes the specified key-value item.
        /// </summary>
        /// <param name="nameSpace"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Delete(string nameSpace, string key)
        {
            string nameSpaceKey = string.Format(keyFormat, nameSpace, key);

            return(KeyValueStore.Delete(nameSpaceKey));
        }