RazorDB.KeyValueStore.CleanIndex C# (CSharp) Method

CleanIndex() public method

public CleanIndex ( string indexName ) : void
indexName string
return void
        public void CleanIndex(string indexName) {
            KeyValueStore indexStore = GetSecondaryIndex(indexName);
            var allValueStoreItems = new HashSet<ByteArray>(this.Enumerate().Select(item => new ByteArray(item.Key)));
            foreach (var indexItem in indexStore.Enumerate()) {
                byte[] itemKey = KeyValueStore.ItemKeyFromIndex(indexItem);
                if (!allValueStoreItems.Contains(new ByteArray(itemKey))) {
                    indexStore.Delete(indexItem.Key);
                }
            }
        }

Usage Example

Ejemplo n.º 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());
            }
        }