示例#1
0
        public void DomainDescription()
        {
            var evalResult = _client.IsComplete <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa");

            Assert.IsFalse(evalResult);


            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.PutOne(item2);

            evalResult = _client.IsComplete <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa");
            Assert.IsFalse(evalResult);

            var description = new DomainDescription(OrQuery.Empty <CacheableTypeOk>());

            //describe the domain as complete
            _client.DeclareDomain(description);

            evalResult = _client.IsComplete <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa");
            Assert.IsTrue(evalResult);
        }
示例#2
0
        public void TtlEviction()
        {
            _client.ConfigEviction(nameof(CacheableTypeOk), EvictionType.TimeToLive, 0, 0, 1000);


            // this one should be evicted because it will expire
            var itemToBeEvicted = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(itemToBeEvicted, false);

            // this one should not be evicted because even if it will expire it is marked as "exclude from eviction"
            var itemToKeepForever = new CacheableTypeOk(15, 1002, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(itemToKeepForever, true);

            Thread.Sleep(1500);

            // this item should not be evicted because it has not expired
            var itemNotToBeEvicted = new CacheableTypeOk(0, 1000, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(itemNotToBeEvicted, false);

            IList <CacheableTypeOk> itemsInAaa = _client.GetMany <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa").ToList();

            Assert.AreEqual(2, itemsInAaa.Count);
            Assert.IsFalse(itemsInAaa.Any(i => i.PrimaryKey == 1), "only item 1 should have been evicted");
        }
示例#3
0
        public void RemoveAndGetMany()
        {
            //add two items (2 and 3 as primary keys)
            var item1 = new CacheableTypeOk(2, 1002, "AHA", new DateTime(2010, 10, 01), 55);

            _dataStore.InternalAddNew(CachedObject.Pack(item1), false);

            var item2 = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 01), 55);

            _dataStore.InternalAddNew(CachedObject.Pack(item2), false);

            //this get one should return the first item
            var cachedItem1 = _dataStore.InternalGetOne(_typeDescription.MakePrimaryKeyValue(2));

            Assert.IsTrue(cachedItem1.PrimaryKey.Equals(2));

            //this GetMany() should return the two items
            var result = _dataStore.InternalGetMany(_typeDescription.MakeIndexKeyValue("indexkeyfolder", "AHA"));

            Assert.AreEqual(result.Count, 2);

            //remove the first item
            _dataStore.RemoveByPrimaryKey(_typeDescription.MakePrimaryKeyValue(2));

            //it should not be there any more
            var shouldBeNull = _dataStore.InternalGetOne(_typeDescription.MakePrimaryKeyValue(2));

            Assert.IsNull(shouldBeNull);

            //this GetMany() should return one item
            result = _dataStore.InternalGetMany(_typeDescription.MakeIndexKeyValue("indexkeyfolder", "AHA"));
            Assert.AreEqual(result.Count, 1);
        }
        public void TimeToLive()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var policy = new TtlEvictionPolicy(TimeSpan.FromSeconds(1));

            for (var i = 0; i < 10; i++)
            {
                var item   = new CacheableTypeOk(i, i + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
                var packed = PackedObject.Pack(item, schema);

                policy.AddItem(packed);
            }

            Assert.IsFalse(policy.IsEvictionRequired);

            Thread.Sleep(1010);

            var toRemove = policy.DoEviction();

            Assert.AreEqual(10, toRemove.Count);

            var item11   = new CacheableTypeOk(11, 11 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed11 = PackedObject.Pack(item11, schema);

            policy.AddItem(packed11);
            toRemove = policy.DoEviction();
            Assert.AreEqual(0, toRemove.Count);

            Thread.Sleep(1010);

            toRemove = policy.DoEviction();
            Assert.AreEqual(1, toRemove.Count);
        }
        public void Truncate()
        {
            //add two new items
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.Put(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.Put(item2);

            IList <CacheableTypeOk> itemsInAaa = _client.GetMany <CacheableTypeOk>("IndexKeyFolder == aaa").ToList();

            Assert.AreEqual(itemsInAaa.Count, 2);

            var  desc = _client.GetServerDescription();
            long hits = desc.DataStoreInfoByFullName[typeof(CacheableTypeOk).FullName].HitCount;

            Assert.AreEqual(hits, 1);

            _client.Truncate <CacheableTypeOk>();

            desc = _client.GetServerDescription();
            hits = desc.DataStoreInfoByFullName[typeof(CacheableTypeOk).FullName].HitCount;
            long count = desc.DataStoreInfoByFullName[typeof(CacheableTypeOk).FullName].Count;

            Assert.AreEqual(hits, 0);
            Assert.AreEqual(count, 0);
        }
示例#6
0
        public void Truncate()
        {
            //add two new items
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.PutOne(item2);

            IList <CacheableTypeOk> itemsInAaa =
                _client.GetMany <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa").ToList();

            Assert.AreEqual(itemsInAaa.Count, 2);

            var collectionName = typeof(CacheableTypeOk).Name ?? throw new InvalidOperationException();

            var desc = _client.GetServerDescription();
            var hits = desc.DataStoreInfoByFullName[collectionName].HitCount;

            Assert.AreEqual(hits, 1);

            _client.Truncate(collectionName);

            desc = _client.GetServerDescription();
            hits = desc.DataStoreInfoByFullName[collectionName].HitCount;
            var count = desc.DataStoreInfoByFullName[collectionName].Count;

            Assert.AreEqual(hits, 0);
            Assert.AreEqual(count, 0);
        }
        public void MultiThreadedDataAccess()
        {
            //first load data
            for (int i = 0; i < 1000; i++)
            {
                var item = new CacheableTypeOk(i, 10000 + i, "aaa", new DateTime(2010, 10, 10), 1500 + i);
                _client.Put(item);
            }


            _requestsFinished = new Semaphore(0, 100);

            // 100 parallel requests
            for (int i = 0; i < 100; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    //reload both items by folder name
                    IList <CacheableTypeOk> items =
                        _client.GetMany <CacheableTypeOk>("IndexKeyValue < 1700")
                        .ToList();
                    Assert.AreEqual(items.Count, 200);

                    _requestsFinished.Release();
                });
            }


            for (int i = 0; i < 100; i++)
            {
                _requestsFinished.WaitOne();
            }
        }
示例#8
0
        public void SerializationWithCompression()
        {
            CacheableTypeOk item1 = new CacheableTypeOk(1, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            var             desc  = ClientSideTypeDescription.RegisterType <CacheableTypeOk>().AsTypeDescription;

            desc.UseCompression = true;
            byte[]          b1            = SerializationHelper.ObjectToBytes(item1, SerializationMode.Json, desc);
            CacheableTypeOk item1Reloaded =
                SerializationHelper.ObjectFromBytes <CacheableTypeOk>(b1, SerializationMode.Json, true);

            Assert.IsNotNull(item1Reloaded);

            using (MemoryStream ms = new MemoryStream())
            {
                SerializationHelper.ObjectToStream(item1, ms, SerializationMode.Json, true);
                ms.Seek(0, SeekOrigin.Begin);
                item1Reloaded =
                    SerializationHelper.ObjectFromStream <CacheableTypeOk>(ms, SerializationMode.Json, true);
                Assert.IsNotNull(item1Reloaded);
                ms.Seek(0, SeekOrigin.Begin);
                item1Reloaded =
                    SerializationHelper.ObjectFromStream <CacheableTypeOk>(ms, SerializationMode.Json, true);
                Assert.IsNotNull(item1Reloaded);
            }
        }
示例#9
0
        public void DomainDescription()
        {
            var builder    = new QueryBuilder(typeof(CacheableTypeOk));
            var evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));

            Assert.IsFalse(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 0);

            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.Put(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.Put(item2);

            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsFalse(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 2);

            var description = new DomainDescription(OrQuery.Empty <CacheableTypeOk>());

            //describe the domain as complete
            _client.DeclareDomain(description);
            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsTrue(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 2);
        }
示例#10
0
        public void DataAccess()
        {
            //add two new items
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.PutOne(item2);

            _client.DeclareDomain <CacheableTypeOk>(x => x.IndexKeyFolder == "aaa");

            var eval = _client.IsComplete <CacheableTypeOk>(x => x.IndexKeyFolder == "aaa");

            Assert.IsTrue(eval); //domain is complete


            eval = _client.IsComplete <CacheableTypeOk>(x => x.IndexKeyFolder == "bbb");
            Assert.IsFalse(eval); //domain is incomplete


            //reload the first one by primary key
            var item1Reloaded = _client.GetOne <CacheableTypeOk>(i => i.PrimaryKey == 1);

            Assert.AreEqual(item1, item1Reloaded);

            //reload both items by folder name
            var itemsInAaa = _client.GetMany <CacheableTypeOk>(x => x.IndexKeyFolder == "aaa").ToList();

            Assert.AreEqual(2, itemsInAaa.Count);

            //change the folder of the first item and put it back into the cache
            item1.IndexKeyFolder = "bbb";
            _client.PutOne(item1);

            //now it should be only one item left in aaa
            itemsInAaa = _client.GetMany <CacheableTypeOk>(x => x.IndexKeyFolder == "aaa").ToList();
            Assert.AreEqual(1, itemsInAaa.Count);

            //get both of them again by date
            var allItems = _client.GetMany <CacheableTypeOk>(x => x.IndexKeyDate == new DateTime(2010, 10, 10)).ToList();

            Assert.AreEqual(allItems.Count, 2);

            //get both of them using an In query

            var folders = new[] { "aaa", "bbb" };

            allItems = _client.GetMany <CacheableTypeOk>(x => folders.Contains(x.IndexKeyFolder)).ToList();
            Assert.AreEqual(allItems.Count, 2);

            //remove the first one
            _client.RemoveMany <CacheableTypeOk>(x => x.PrimaryKey == 1);

            //the previous query should now return only one item
            allItems = _client.GetMany <CacheableTypeOk>(x => folders.Contains(x.IndexKeyFolder)).ToList();
            Assert.AreEqual(allItems.Count, 1);
        }
示例#11
0
        public void StreamUnstreamMessagesOneByOne()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var qbuilder = new QueryBuilder(typeof(CacheableTypeOk));

            var put  = new PutRequest(typeof(CacheableTypeOk));
            var item = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            var typeDescription =
                TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            put.Items.Add(PackedObject.Pack(item, schema));

            var remove = new RemoveRequest(typeof(CacheableTypeOk), new KeyValue(1, schema.PrimaryKeyField));

            var register = new RegisterTypeRequest(typeDescription);

            using (var stream = new MemoryStream())
            {
                //request
                Streamer.ToStream(stream, new GetRequest(qbuilder.FromSql("select from CacheableTypeOk where IndexKeyValue > 1000")));
                Streamer.ToStream(stream, put);
                Streamer.ToStream(stream, remove);
                Streamer.ToStream(stream, register);

                //response
                Streamer.ToStream(stream, new NullResponse());
                Streamer.ToStream(stream, new ExceptionResponse(new Exception("fake exception")));
                Streamer.ToStream(stream, new ServerDescriptionResponse());

                stream.Seek(0, SeekOrigin.Begin);
                object reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is GetRequest);

                //request
                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is PutRequest);

                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is RemoveRequest);

                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is RegisterTypeRequest);

                ////response
                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is NullResponse);

                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is ExceptionResponse);

                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is ServerDescriptionResponse);
            }
        }
        public void StreamAvailableObjectsWithCriteria()
        {
            //add two new items
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.Put(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.Put(item2);

            var item3 = new CacheableTypeOk(3, 1003, "bbb", new DateTime(2010, 10, 10), 1600);

            _client.Put(item3);

            //ask for items 1 2 3 4 (1 2 should be returned and 3 4 not found)
            var items = new List <KeyValue>
            {
                new KeyValue(1,
                             new KeyInfo(KeyDataType.IntKey, KeyType.Primary,
                                         "PrimaryKey")),
                new KeyValue(2,
                             new KeyInfo(KeyDataType.IntKey, KeyType.Primary,
                                         "PrimaryKey")),
                new KeyValue(3,
                             new KeyInfo(KeyDataType.IntKey, KeyType.Primary,
                                         "PrimaryKey")),
                new KeyValue(4,
                             new KeyInfo(KeyDataType.IntKey, KeyType.Primary,
                                         "PrimaryKey"))
            };

            var   builder     = new QueryBuilder(typeof(CacheableTypeOk));
            Query folderIsAaa = builder.MakeAtomicQuery("IndexKeyFolder", "aaa");

            var wait     = new ManualResetEvent(false);
            var found    = new List <CacheableTypeOk>();
            var notFound = _client.GetAvailableItems(items, folderIsAaa,
                                                     delegate(CacheableTypeOk item, int currentItem, int totalItems)
            {
                found.Add(item);
                if (currentItem == totalItems)
                {
                    wait.Set();
                }
            }, delegate { });

            wait.WaitOne();
            Assert.AreEqual(notFound.Count, 2);
            Assert.AreEqual(notFound[0], 3);
            Assert.AreEqual(notFound[1], 4);
            Assert.AreEqual(found.Count, 2);
            Assert.AreEqual(found[0].PrimaryKey, 1);
            Assert.AreEqual(found[1].PrimaryKey, 2);
        }
示例#13
0
        public void StreamUnstreamMessagesOneByOne()
        {
            QueryBuilder qbuilder = new QueryBuilder(typeof(CacheableTypeOk));

            PutRequest      put  = new PutRequest(typeof(CacheableTypeOk));
            CacheableTypeOk item = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            TypeDescription typeDescription =
                ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)).AsTypeDescription;

            put.Items.Add(CachedObject.Pack(item));

            RemoveRequest remove = new RemoveRequest(typeof(CacheableTypeOk), typeDescription.MakePrimaryKeyValue(1));

            RegisterTypeRequest register = new RegisterTypeRequest(typeDescription);

            using (MemoryStream stream = new MemoryStream())
            {
                //request
                Streamer.ToStream(stream, new GetRequest(qbuilder.GetManyWhere("IndexKeyValue > 1000")));
                Streamer.ToStream(stream, put);
                Streamer.ToStream(stream, remove);
                Streamer.ToStream(stream, register);

                //response
                Streamer.ToStream(stream, new NullResponse());
                Streamer.ToStream(stream, new ExceptionResponse(new Exception("fake exception")));
                Streamer.ToStream(stream, new ServerDescriptionResponse());

                stream.Seek(0, SeekOrigin.Begin);
                object reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is GetRequest);

                //request
                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is PutRequest);

                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is RemoveRequest);

                reloaded = Streamer.FromStream <Request>(stream);
                Assert.IsTrue(reloaded is RegisterTypeRequest);

                ////response
                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is NullResponse);

                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is ExceptionResponse);

                reloaded = Streamer.FromStream <Response>(stream);
                Assert.IsTrue(reloaded is ServerDescriptionResponse);
            }
        }
        private static CacheableTypeOk GetObject1()
        {
            var object1 = new CacheableTypeOk
            {
                UniqueKey      = 1,
                PrimaryKey     = 11,
                IndexKeyValue  = 15,
                IndexKeyDate   = new DateTime(2009, 10, 25),
                IndexKeyFolder = "FOL"
            };

            return(object1);
        }
示例#15
0
        public void StreamUnstreamOneCacheable()
        {
            CacheableTypeOk item1 = new CacheableTypeOk(1, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            CachedObject it1 = CachedObject.Pack(item1);

            using (MemoryStream stream = new MemoryStream())
            {
                Streamer.ToStream(stream, it1);

                stream.Seek(0, SeekOrigin.Begin);

                object reloaded = Streamer.FromStream <CacheableTypeOk>(stream);
                Assert.IsTrue(reloaded is CacheableTypeOk);
            }
        }
示例#16
0
        public void FeedDataToCache()
        {
            var items = new List <CacheableTypeOk>();

            for (var i = 0; i < 113; i++)
            {
                var item1 = new CacheableTypeOk(i, 1000 + i, "aaa", new DateTime(2010, 10, 10), 1500);
                items.Add(item1);
            }

            _aggregator.PutMany(items, true);

            var itemsReloaded = _aggregator.GetMany <CacheableTypeOk>(x => x.IndexKeyFolder == "aaa").ToList();

            Assert.AreEqual(113, itemsReloaded.Count);
        }
示例#17
0
        public void StreamManyUnstreamOne()
        {
            var item = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            var desc = TypedSchemaFactory.FromType <CacheableTypeOk>();

            using (var stream = new MemoryStream())
            {
                Streamer.ToStream(stream, item, desc);
                stream.Seek(0, SeekOrigin.Begin);

                var itemReloaded = Streamer.FromStream <CacheableTypeOk>(stream);
                Assert.IsNotNull(itemReloaded);
                Assert.AreEqual(itemReloaded, item);
            }
        }
        public void Import_export_data_to_json()
        {
            //add some data
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.Put(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.Put(item2);

            var item3 = new CacheableTypeOk(3, 1003, "bbb", new DateTime(2010, 10, 10), 1600);

            _client.Put(item3);


            var parser     = new CommandLineParser(_client.GetClusterInformation());
            var selectInto = parser.Parse("select CacheableTypeOk where IndexKeyFolder=aaa into export_test.json");

            selectInto.TryExecute(_client);

            FileAssert.Exists("export_test.json");

            var exported = DumpHelper.LoadObjects("export_test.json", _client).ToList();

            Assert.AreEqual(2, exported.Count);

            var json = File.ReadAllText("export_test.json");

            json = json.Replace("aaa", "abc");
            File.WriteAllText("export_test.json", json);

            var import = parser.Parse("import export_test.json");

            import.TryExecute(_client);

            _logger.Reset();
            var cmd = _parser.Parse("count CacheableTypeOk where IndexKeyFolder=abc");

            Assert.IsTrue(cmd.CanExecute);
            Assert.IsNotNull(cmd.TryExecute(_client));
            var response = _logger.Buffer;

            var items = ExtractOne(@"\s*found\s*([0-9]*?)\s*items", response);

            Assert.AreEqual(items, "2");
        }
        public void LessRecentlyUsedRemoveItem()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var policy = new LruEvictionPolicy(10, 2);

            for (var i = 0; i < 9; i++)
            {
                var item   = new CacheableTypeOk(i, i + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
                var packed = PackedObject.Pack(item, schema);

                policy.AddItem(packed);
            }

            Assert.IsFalse(policy.IsEvictionRequired);
            var toRemove = policy.DoEviction();

            Assert.AreEqual(0, toRemove.Count);

            var item1   = new CacheableTypeOk(1, 1 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed1 = PackedObject.Pack(item1, schema);

            policy.TryRemove(packed1);


            var item10   = new CacheableTypeOk(10, 10 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed10 = PackedObject.Pack(item10, schema);

            policy.AddItem(packed10);


            // as one item was removed explicitly the eviction should not be triggered yet
            toRemove = policy.DoEviction();
            Assert.AreEqual(0, toRemove.Count);

            var item11   = new CacheableTypeOk(11, 11 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed11 = PackedObject.Pack(item11, schema);

            policy.AddItem(packed11);
            // now the eviction should be triggered
            toRemove = policy.DoEviction();
            Assert.AreEqual(2, toRemove.Count);

            // the explicitly removed item should not be in the list
            Assert.IsFalse(toRemove.Any(i => i == packed1));
        }
        public void DomainDescription()
        {
            var builder    = new QueryBuilder(typeof(CacheableTypeOk));
            var evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));

            Assert.IsFalse(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 0);

            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.Put(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.Put(item2);

            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsFalse(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 2);

            var description = new DomainDescription(typeof(CacheableTypeOk))
            {
                IsFullyLoaded = true
            };

            //describe the domain as complete
            _client.DeclareDomain(description, DomainDeclarationAction.Set);
            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsTrue(evalResult.Key);
            Assert.AreEqual(evalResult.Value, 2);

            //remove the completeness declaration
            _client.DeclareDomain(description, DomainDeclarationAction.Remove);
            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsFalse(evalResult.Key);

            description.AddOrReplace(builder.MakeAtomicQuery("IndexKeyFolder", "aaa"));
            _client.DeclareDomain(description, DomainDeclarationAction.Add);
            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsTrue(evalResult.Key);

            _client.DeclareDomain(description, DomainDeclarationAction.Remove);
            evalResult = _client.EvalQuery(builder.GetMany("IndexKeyFolder=aaa"));
            Assert.IsFalse(evalResult.Key);
        }
示例#21
0
        public void FeedDataToCache()
        {
            var items = new List <CacheableTypeOk>();

            for (int i = 0; i < 113; i++)
            {
                var item1 = new CacheableTypeOk(i, 1000 + i, "aaa", new DateTime(2010, 10, 10), 1500);
                items.Add(item1);
            }


            _client.FeedMany(items, true);

            IList <CacheableTypeOk> itemsReloaded =
                _client.GetManyWhere <CacheableTypeOk>("IndexKeyFolder == aaa").ToList();

            Assert.AreEqual(itemsReloaded.Count, 113);
        }
示例#22
0
        public void StreamManyUnstreamOneCacheable()
        {
            CacheableTypeOk     item        = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            CachedObject        it          = CachedObject.Pack(item);
            List <CachedObject> oneItemList = new List <CachedObject>();

            oneItemList.Add(it);

            using (MemoryStream stream = new MemoryStream())
            {
                Streamer.ToStreamMany(stream, oneItemList);
                stream.Seek(0, SeekOrigin.Begin);

                CacheableTypeOk itemReloaded = Streamer.FromStream <CacheableTypeOk>(stream);
                Assert.IsNotNull(itemReloaded);
                Assert.AreEqual(itemReloaded, item);
            }
        }
示例#23
0
        public void StreamUnstreamManyCacheable()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var items = new List <PackedObject>(3);

            var item1 = new CacheableTypeOk(1, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            var it1   = PackedObject.Pack(item1, schema);

            items.Add(it1);

            var item2 = new CacheableTypeOk(2, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            var it2   = PackedObject.Pack(item2, schema);

            items.Add(it2);

            var item3 = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            var it3   = PackedObject.Pack(item3, schema);

            items.Add(it3);

            using (var stream = new MemoryStream())
            {
                Streamer.ToStreamMany(stream, items, new int[0], null);

                stream.Seek(0, SeekOrigin.Begin);

                var itemsReceived = 0;
                Streamer.FromStream(stream,
                                    delegate(CacheableTypeOk data, int currentItem, int totalItems)
                {
                    Assert.IsTrue(currentItem > 0);
                    Assert.IsTrue(currentItem <= totalItems);

                    itemsReceived++;

                    Assert.AreEqual(itemsReceived, data.PrimaryKey);
                },
                                    delegate { Assert.Fail(); });

                Assert.AreEqual(itemsReceived, 3);
            }
        }
示例#24
0
        public void StreamUnstreamOneCacheable()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var item1 = new CacheableTypeOk(1, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            var it1 = PackedObject.Pack(item1, schema);

            using var stream = new MemoryStream();
            Streamer.ToStream(stream, it1);

            stream.Seek(0, SeekOrigin.Begin);

            var reloaded = Streamer.FromStream <PackedObject>(stream);
            var original = PackedObject.Unpack <CacheableTypeOk>(reloaded);


            Assert.IsTrue(original is CacheableTypeOk);
        }
示例#25
0
        public void StreamManyUnstreamOne()
        {
            CacheableTypeOk        item        = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            List <CacheableTypeOk> oneItemList = new List <CacheableTypeOk>();

            oneItemList.Add(item);

            var desc = ClientSideTypeDescription.RegisterType <CacheableTypeOk>().AsTypeDescription;

            using (MemoryStream stream = new MemoryStream())
            {
                Streamer.ToStream(stream, item, desc);
                stream.Seek(0, SeekOrigin.Begin);

                CacheableTypeOk itemReloaded = Streamer.FromStream <CacheableTypeOk>(stream);
                Assert.IsNotNull(itemReloaded);
                Assert.AreEqual(itemReloaded, item);
            }
        }
示例#26
0
        public void StreamUnstreamOneCacheable()
        {
            var item1 = new CacheableTypeOk(1, 1003, "AHA", new DateTime(2010, 10, 02), 8);

            var it1 = CachedObject.Pack(item1);

            using (var stream = new MemoryStream())
            {
                Streamer.ToStream(stream, it1);

                stream.Seek(0, SeekOrigin.Begin);

                var reloaded = Streamer.FromStream <CachedObject>(stream);
                var original = CachedObject.Unpack <CacheableTypeOk>(reloaded);


                Assert.IsTrue(original is CacheableTypeOk);
            }
        }
        public void LessRecentlyUsed()
        {
            var schema = TypedSchemaFactory.FromType(typeof(CacheableTypeOk));

            var policy = new LruEvictionPolicy(10, 2);

            for (var i = 0; i < 100; i++)
            {
                var item   = new CacheableTypeOk(i, i + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
                var packed = PackedObject.Pack(item, schema);

                policy.AddItem(packed);
            }

            Assert.IsTrue(policy.IsEvictionRequired);
            var toRemove = policy.DoEviction();

            Assert.AreEqual(92, toRemove.Count);

            var item93   = new CacheableTypeOk(93, 93 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed93 = PackedObject.Pack(item93, schema);

            // check that the 93rd item was not removed
            Assert.IsFalse(toRemove.Any(i => i == packed93));
            policy.Touch(packed93);

            var item100   = new CacheableTypeOk(100, 100 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed100 = PackedObject.Pack(item100, schema);

            var item101   = new CacheableTypeOk(101, 101 + 1000, "aaa", new DateTime(2010, 10, 10), 1500);
            var packed101 = PackedObject.Pack(item101, schema);


            policy.AddItem(packed100);
            policy.AddItem(packed101);

            toRemove = policy.DoEviction();

            Assert.AreEqual(2, toRemove.Count);

            // item 93 was not removed because it was recently used (the call to Touch)
            Assert.IsFalse(toRemove.Any(i => i == packed93));
        }
示例#28
0
        public void MultiThreadedDataAccess()
        {
            //first load data
            for (var i = 0; i < 1000; i++)
            {
                var item = new CacheableTypeOk(i, 10000 + i, "aaa", new DateTime(2010, 10, 10), 1500 + i);
                _client.PutOne(item);
            }


            // 100 parallel requests
            Parallel.For(0, 10, i =>
            {
                IList <CacheableTypeOk> items =
                    _client.GetMany <CacheableTypeOk>(i => i.IndexKeyValue < 1700)
                    .ToList();
                Assert.AreEqual(items.Count, 200);
            });
        }
示例#29
0
        public void GetManyOnOrderedIndexes()
        {
            var item = new CacheableTypeOk(1, 1001, "AHA", new DateTime(2010, 10, 01), 9);

            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            item = new CacheableTypeOk(2, 1002, "AHA", new DateTime(2010, 10, 01), 8);
            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            item = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8);
            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            item = new CacheableTypeOk(4, 1004, "BBB", new DateTime(2010, 9, 01), 5);
            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            item = new CacheableTypeOk(5, 1005, "BBB", new DateTime(2010, 10, 01), 4);
            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            item = new CacheableTypeOk(6, 1006, "BBA", new DateTime(2010, 10, 01), 1);
            _dataStore.InternalAddNew(CachedObject.Pack(item), false);

            var result =
                _dataStore.InternalGetMany(
                    _typeDescription.MakeIndexKeyValue("IndexKeyDate", new DateTime(2010, 10, 01)),
                    QueryOperator.Le);

            Assert.AreEqual(result.Count, 5);

            result = _dataStore.InternalGetMany(_typeDescription.MakeIndexKeyValue("IndexKeyValue", 8),
                                                QueryOperator.Ge);
            Assert.AreEqual(result.Count, 3);

            _dataStore.RemoveByPrimaryKey(_typeDescription.MakePrimaryKeyValue(2));

            result = _dataStore.InternalGetMany(
                _typeDescription.MakeIndexKeyValue("IndexKeyDate", new DateTime(2010, 10, 01)),
                QueryOperator.Le);
            Assert.AreEqual(result.Count, 4);

            result = _dataStore.InternalGetMany(_typeDescription.MakeIndexKeyValue("IndexKeyValue", 8),
                                                QueryOperator.Ge);
            Assert.AreEqual(result.Count, 2);
        }
示例#30
0
        public void FeedDataToCacheUsingSessions()
        {
            var session = _client.BeginFeed <CacheableTypeOk>(10, false);

            for (int i = 0; i < 113; i++)
            {
                var item1 = new CacheableTypeOk(i, 1000 + i, "aaa", new DateTime(2010, 10, 10), 1500);
                _client.Add(session, item1);
            }


            _client.EndFeed <CacheableTypeOk>(session);

            Thread.Sleep(300);

            IList <CacheableTypeOk> itemsReloaded =
                _client.GetManyWhere <CacheableTypeOk>("IndexKeyFolder == aaa").ToList();

            Assert.AreEqual(itemsReloaded.Count, 113);
        }