public void InsertPerformance900KTest()
        {
            var connection = new RedisTestConnector().Connect();
            try
            {
                var proxy = new RedisAutoCompleteProxy(() => connection.GetDatabase());
                string root = "PERFORMANCETEST";
                var index = new RedisAutoCompleteIndex(proxy, root);
                index.Clear();
                var db = connection.GetDatabase();
                Assert.IsFalse(db.KeyExists(root + ":ITEMS"));
                var items = LoadAllItems();
                var sw = new Stopwatch();

                sw.Start();
                var tsk = index.Add(items);
                tsk.Wait();
                sw.Stop();

                Assert.IsTrue(sw.Elapsed.TotalSeconds < 120);
                Trace.WriteLine("INSERT ITEMS TIME: " + sw.Elapsed.ToString());
                Assert.AreEqual(items.Length, db.SortedSetLength(root + ":ITEMS"));
                sw.Restart();
                var result = index.Search("hol").Result;
                sw.Stop();

                Assert.IsTrue(result.Any());
                Trace.WriteLine("FIRST QUERY TIME: " + sw.Elapsed.ToString());
                Assert.IsTrue(sw.ElapsedMilliseconds < 500);

                sw.Restart();
                var result2 = index.Search("hol").Result;
                sw.Stop();

                Assert.IsTrue(result2.Any());

                Assert.AreEqual(result.Length, result2.Length);
                Trace.WriteLine("SECOND QUERY TIME: " + sw.Elapsed.ToString());
                Assert.IsTrue(sw.ElapsedMilliseconds < 50);

                index.Clear();
            }
            finally
            { connection.Close();  }

            //index.Clear();
        }
        public void AddItemsToIndexCreatingJSONTest()
        {
            string rootPath = "RedisAutoCompleteIndexBuilderTests";

            var mqProxy = new Mock<IRedisAutoCompleteProxy>();

            var objParam = new
            {
                index = new[] {new {wd = string.Empty, it = new string[] {}}},
                items = new[] {new {it = string.Empty, sc = default(int)}}
            };

            mqProxy.Setup(s => s.InsertItems(It.IsAny<string>(), It.IsAny<string>()))
                .Callback((string path, string json) =>
                {
                    objParam = JsonConvert.DeserializeAnonymousType(json, objParam);
                }).Returns(Task.FromResult(0));

            var index = new RedisAutoCompleteIndex(mqProxy.Object, rootPath);
            //var redis = new RedisAutoCompleteProxy(new RedisTestConnector().Connect().GetDatabase());
            //var index = new RedisAutoCompleteIndex(redis, rootPath);
            index.Clear();
            var items = new[]
            {
                new AutoCompleteItem() {Priority = 1, Text = "Hello World!", ItemKey = "Item1"},
                new AutoCompleteItem() {Priority = 2, Text = "Hello (Holland)", ItemKey = "Item2"},
            };

            index.Add(items).Wait();
            mqProxy.VerifyAll();

            var jidx = objParam.index.ToDictionary(x => x.wd, x => x.it);
            AssertDictEqual(jidx, new Dictionary<string, string[]>()
            {
                {"hello", new string[] {"Item1", "Item2"}},
                {"holland", new string[] {"Item2"}},
                {"world", new string[] {"Item1"}},
            });

            var jitems = objParam.items.ToDictionary(x => x.it, x => x.sc);
            Assert.AreEqual(2, jitems.Count);
            Assert.AreEqual(1, jitems["Item1"]);
            Assert.AreEqual(2, jitems["Item2"]);
        }
        public void IncreasePriorityTest()
        {
            var connector = new RedisTestConnector();
            var redis = connector.Connect();
            string rootPath = "RedisAutoCompleteIndexBuilderTests";

            var proxy = new RedisAutoCompleteProxy(() => redis.GetDatabase());
            //var index = new RedisAutoCompleteIndex(redis.GetDatabase(), rootPath);
            var index = new RedisAutoCompleteIndex(proxy, rootPath);

            index.Clear();
            var items = new[]
            {
                new AutoCompleteItem() {Priority = 1, Text = "Hello World!", ItemKey = "Item1"},
                new AutoCompleteItem() {Priority = 2, Text = "Hello (Holland)", ItemKey = "Item2"},
                new AutoCompleteItem() {Priority = 1, Text = "Hello Brazil!", ItemKey = "Item3"},
            };

            index.Add(items);

            index.IncreasePriority("Item3");
            var db = redis.GetDatabase();
            var item = (string)db.SortedSetRangeByScore(rootPath + ":ITEMS").First();
            Assert.AreEqual("Item3", item);
            index.Clear();
            redis.Close();
        }
        public void RemoveItemTest()
        {
            var connector = new RedisTestConnector();
            var redis = connector.Connect();
            string rootPath = "RedisAutoCompleteIndexBuilderTests";

            var proxy = new RedisAutoCompleteProxy(() => redis.GetDatabase());
            //var index = new RedisAutoCompleteIndex(redis.GetDatabase(), rootPath);
            var index = new RedisAutoCompleteIndex(proxy, rootPath);
            index.Clear();
            var items = new[]
            {
                new AutoCompleteItem() {Priority = 1, Text = "Hello World!", ItemKey = "Item1"},
                new AutoCompleteItem() {Priority = 2, Text = "Hello (Holland)", ItemKey = "Item2"},
                new AutoCompleteItem() {Priority = 1, Text = "Hello Brazil!", ItemKey = "Item3"},
            };

            index.Add(items).Wait();
            var db = redis.GetDatabase();
            var allItems = db.SortedSetRangeByScore(rootPath + ":ITEMS").Select(s => (string) s).ToArray();
            Assert.AreEqual(3, allItems.Length);
            Assert.IsTrue(allItems.Contains("Item2"));

            index.RemoveItem("Item2").Wait();

            allItems = db.SortedSetRangeByScore(rootPath + ":ITEMS").Select(s => (string)s).ToArray();
            Assert.AreEqual(2, allItems.Length);
            Assert.IsFalse(allItems.Contains("Item2"));
            index.Clear();
            redis.Close();
        }