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 SearchAzulTest()
        {
            string rootPath = "PROXYTEST";
            string jsonParam = @"
            {
            ""index"": [
            {""wd"":""arte"",""it"":[""Item1"",""Item2""]},
            {""wd"":""aula"",""it"":[""Item1""]},
            {""wd"":""azul"",""it"":[""Item3""]},
            {""wd"":""bola"",""it"":[""Item2""]},
            {""wd"":""casa"",""it"":[""Item2""]}
            ],
            ""items"":[
            {""it"":""Item1"",""sc"":1},
            {""it"":""Item2"",""sc"":2},
            {""it"":""Item3"",""sc"":3}
            ]
            }";
            var connection = new RedisTestConnector().Connect();
            var proxy = new RedisAutoCompleteProxy(() => connection.GetDatabase());
            proxy.Clear(rootPath);
            proxy.InsertItems(rootPath, jsonParam).Wait();

            var result = proxy.Search(rootPath, JsonConvert.SerializeObject(new { prefixes = new[] { "a" } }), 15, 10).Result;
            Assert.AreEqual(3, result.Length);
            Assert.IsTrue(result.Contains("Item1"));
            Assert.IsTrue(result.Contains("Item2"));
            Assert.IsTrue(result.Contains("Item3"));

            result = proxy.Search(rootPath, JsonConvert.SerializeObject(new { prefixes = new[] { "az" } }), 15, 10).Result;
            Assert.AreEqual(1, result.Length);
            Assert.IsTrue(result.Contains("Item3"));

            connection.Close();
        }
        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 InsertItemsInRedisTest()
        {
            string rootPath = "PROXYTEST";
            string jsonParam = @"
            {
            ""index"": [
            {""wd"":""hello"",""it"":[""Item1"",""Item2""]},
            {""wd"":""world"",""it"":[""Item1""]},
            {""wd"":""holland"",""it"":[""Item2""]}
            ],
            ""items"":[
            {""it"":""Item1"",""sc"":1},
            {""it"":""Item2"",""sc"":2}
            ]
            }";

            var connection = new RedisTestConnector().Connect();
            var db = connection.GetDatabase();
            var proxy = new RedisAutoCompleteProxy(() => db);
            proxy.Clear(rootPath);
            Assert.IsFalse(db.KeyExists("PROXYTEST:ITEMS"));
            proxy.InsertItems(rootPath, jsonParam).Wait();

            var allItems = db.SortedSetScan(rootPath + ":ITEMS").ToList();
            Assert.AreEqual(2, allItems.Count);
            Assert.AreEqual(1, allItems[0].Score);
            Assert.AreEqual("Item1", (string)(allItems[0].Element));

            Assert.AreEqual(2, allItems[1].Score);
            Assert.AreEqual("Item2", (string)(allItems[1].Element));

            var hWords = db.SortedSetRangeByScore(rootPath + ":LT:h").Select(s => (string)s).ToArray();
            Assert.AreEqual(2, hWords.Length);
            Assert.AreEqual("hello", hWords[0]);
            Assert.AreEqual("holland", hWords[1]);

            var helloItems = db.SetMembers(rootPath + ":WORDS:hello").Select(s => (string)s).OrderBy(s => s).ToArray();
            Assert.AreEqual(2, helloItems.Length);
            Assert.AreEqual("Item1", helloItems[0]);
            Assert.AreEqual("Item2", helloItems[1]);

            proxy.Clear(rootPath);
            Assert.IsFalse(db.KeyExists(rootPath + ":ITEMS"));
            connection.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();
        }
        public void SearchTest()
        {
            string rootPath = "PROXYTEST";
            string jsonParam = @"
            {
            ""index"": [
            {""wd"":""hello"",""it"":[""Item1"",""Item2""]},
            {""wd"":""world"",""it"":[""Item1""]},
            {""wd"":""holland"",""it"":[""Item2""]}
            ],
            ""items"":[
            {""it"":""Item1"",""sc"":1},
            {""it"":""Item2"",""sc"":2}
            ]
            }";
            var connection = new RedisTestConnector().Connect();
            var proxy = new RedisAutoCompleteProxy(() => connection.GetDatabase());
            proxy.Clear(rootPath);
            proxy.InsertItems(rootPath, jsonParam).Wait();

            var result = proxy.Search(rootPath,JsonConvert.SerializeObject(new {prefixes = new[] {"hol"}}), 15, 10).Result;
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("Item2", result[0]);
            connection.Close();
        }