public void SetUp() { _typeDescription = ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)).AsTypeDescription; _dataStore = new DataStore(_typeDescription, new NullEvictionPolicy()); }
public static void AddExplicitTypeDescription(Type type, ClientSideTypeDescription typeDescription) { lock (TypeDescriptions) { TypeDescriptions[type] = typeDescription; } }
/// <summary> /// The only constructor is internal to prevent explicit instantiation /// </summary> /// <param name="description"> </param> internal TypeDescription(ClientSideTypeDescription description) { PrimaryKeyField = description.PrimaryKeyField.AsKeyInfo; _uniqueKeyFields = new List <KeyInfo>(); _indexFields = new List <KeyInfo>(); _listFields = new List <KeyInfo>(); foreach (var uniqueKeyField in description.UniqueKeyFields) { _uniqueKeyFields.Add(uniqueKeyField.AsKeyInfo); } foreach (var indexField in description.IndexFields) { _indexFields.Add(indexField.AsKeyInfo); } foreach (var indexField in description.ListFields) { _listFields.Add(indexField.AsKeyInfo); } FullTypeName = description.FullTypeName; TypeName = description.TypeName; UseCompression = description.UseCompression; _fullText = new List <KeyInfo>(); foreach (var fullTextIndex in description.FullTextIndexed) { FullText.Add(fullTextIndex.AsKeyInfo); } }
private static OrderedIndex populate(params int[] valueKeys) { //register the type to get a valid TypeDescription //the type description is used to create CachedObjects from objects of the registered type var typeDescription = ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); KeyInfo valueKey = null; foreach (var keyInfo in typeDescription.IndexFields) { if (keyInfo.Name == "IndexKeyValue") { valueKey = keyInfo.AsKeyInfo; } } Assert.IsNotNull(valueKey); var index = new OrderedIndex(valueKey); for (var i = 0; i < valueKeys.Length; i++) { index.Put(CachedObject.Pack(new CacheableTypeOk(i, 106, "A", DateTime.Now, valueKeys[i]))); } return(index); }
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); } }
public void Pack_object_with_full_text_indexed_properties() { var description = ClientSideTypeDescription.RegisterType <Home>(); Assert.AreEqual(3, description.FullTextIndexed.Count); var home = new Home { Address = "14 rue du chien qui fume", Bathrooms = 2, Rooms = 4, PriceInEuros = 200, CountryCode = "FR", Comments = { new Comment { Text = "Wonderful place", User = "******" }, new Comment { Text = "Very nice apartment" } } }; var packed = CachedObject.Pack(home); Assert.AreEqual(5, packed.FullText.Length); Assert.IsTrue(packed.FullText.Any(t => t.Contains("chien qui fume"))); // now pack the same object as json var json = SerializationHelper.ObjectToJson(home); var packed2 = CachedObject.PackJson(json, description.AsTypeDescription); Assert.AreEqual(5, packed2.FullText.Length); Assert.IsTrue(packed2.FullText.Any(t => t.Contains("chien qui fume"))); }
internal DataSource(ICacheClient client, ClientSideTypeDescription typeDescription) : base(CreateParser(), new QueryExecutor(client, typeDescription.AsTypeDescription)) { _client = client; _typeDescription = typeDescription; }
public DataSource <T> DataSource <T>() { ClientSideTypeDescription typeDescription = TypeDescriptionsCache.GetDescription(typeof(T)); typeDescription = Client.RegisterType(typeof(T), typeDescription); return(new DataSource <T>(Client, typeDescription)); }
public void Test_subset_on_queries() { var datastore = new DataSource <TradeLike>(null, ClientSideTypeDescription.RegisterType <TradeLike>()); { var q1 = datastore.PredicateToQuery(t => t.ValueDate == DateTime.Today); var q2 = datastore.PredicateToQuery(t => t.ValueDate >= DateTime.Today.AddDays(-10)); Assert.IsTrue(q1.IsSubsetOf(q2)); Assert.IsFalse(q2.IsSubsetOf(q1)); } { var q1 = datastore.PredicateToQuery(t => t.ValueDate == DateTime.Today && t.Folder == "EUR12"); var q2 = datastore.PredicateToQuery(t => t.ValueDate >= DateTime.Today.AddDays(-10)); Assert.IsTrue(q1.IsSubsetOf(q2)); Assert.IsFalse(q2.IsSubsetOf(q1)); } { var q1 = datastore.PredicateToQuery(t => t.Folder == "EUR12"); var q2 = datastore.PredicateToQuery(t => t.Folder == "EUR12" || t.Folder == "EUR11"); Assert.IsTrue(q1.IsSubsetOf(q2)); Assert.IsFalse(q2.IsSubsetOf(q1)); } { var q1 = datastore.PredicateToQuery(t => t.Folder == "EUR12"); var q2 = datastore.PredicateToQuery(t => t.Folder == "EUR12" || t.Folder == "EUR11" && t.ValueDate == DateTime.Today); Assert.IsTrue(q1.IsSubsetOf(q2)); Assert.IsFalse(q2.IsSubsetOf(q1)); } { var q1 = datastore.PredicateToQuery(t => t.Folder == "EUR11"); var q2 = datastore.PredicateToQuery(t => t.Folder == "EUR12" || t.Folder == "EUR11" && t.ValueDate == DateTime.Today); Assert.IsFalse(q1.IsSubsetOf(q2)); } { var q1 = datastore.PredicateToQuery(t => t.Folder == "EUR11" && t.ValueDate == DateTime.Today); var q2 = datastore.PredicateToQuery(t => t.Folder == "EUR12" || t.Folder == "EUR11" && t.ValueDate > DateTime.Today.AddDays(-10)); Assert.IsTrue(q1.IsSubsetOf(q2)); Assert.IsFalse(q2.IsSubsetOf(q1)); // every query is a subset of an empty query Assert.IsTrue(q1.IsSubsetOf(OrQuery.Empty <TradeLike>())); Assert.IsTrue(q2.IsSubsetOf(OrQuery.Empty <TradeLike>())); } }
public void FixtureSetUp() { ClientSideTypeDescription clientTypeDescription = ClientSideTypeDescription.RegisterType(typeof(TradeLike)); var sd = new ServerDescriptionResponse { KnownTypesByFullName = { { clientTypeDescription.FullTypeName, clientTypeDescription.AsTypeDescription } } }; _serverDescription = new ClusterInformation(new[] { sd }); }
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); } }
public void TestTypeOk() { var typeDescription = ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); Assert.IsNotNull(typeDescription.PrimaryKeyField); Assert.AreEqual(typeDescription.FullTypeName, typeof(CacheableTypeOk).FullName); Assert.AreEqual(typeDescription.TypeName, typeof(CacheableTypeOk).Name); Assert.AreEqual(typeDescription.UniqueKeysCount, 1); Assert.AreEqual(typeDescription.IndexCount, 4); }
public static ClientSideTypeDescription GetDescription(Type type) { lock (TypeDescriptions) { if (TypeDescriptions.TryGetValue(type, out var description)) { return(description); } description = ClientSideTypeDescription.RegisterType(type); TypeDescriptions.Add(type, description); return(description); } }
public void Packing_a_binary_object_and_its_json_should_give_identical_results() { var today = DateTime.Today; var now = DateTime.Now; var testObj = new AllKindsOfProperties { Id = 15, ValueDate = today, LastUpdate = now, Nominal = 156.32, Quantity = 35, InstrumentName = "IRS", AnotherDate = now, AreYouSure = Fuzzy.Maybe, IsDeleted = true, Tags = { "news", "science", "space", "διξ" }, Languages = { "en", "de", "fr" } }; var description = ClientSideTypeDescription.RegisterType <AllKindsOfProperties>(); var typeDescription = description.AsTypeDescription; var packed1 = CachedObject.Pack(testObj); var json = SerializationHelper.ObjectToJson(testObj); var packed2 = CachedObject.PackJson(json, typeDescription); Console.WriteLine(packed1); Console.WriteLine(packed2); Assert.AreEqual(packed1, packed2); // only checks the primary key Assert.AreEqual(packed1.FullTypeName, packed2.FullTypeName); CollectionAssert.AreEqual(packed1.UniqueKeys, packed2.UniqueKeys); CollectionAssert.AreEqual(packed1.IndexKeys, packed2.IndexKeys); CollectionAssert.AreEqual(packed1.ListIndexKeys, packed2.ListIndexKeys); var json1 = Encoding.UTF8.GetString(packed1.ObjectData); var json2 = Encoding.UTF8.GetString(packed2.ObjectData); CollectionAssert.AreEqual(packed1.ObjectData, packed2.ObjectData); }
public void RunBeforeAnyTests() { Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory; Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory); var config = new ClientConfig(); config.LoadFromFile("inprocess_config.xml"); foreach (var typeDescription in config.TypeDescriptions) { if (typeDescription.Value.FullTypeName.Contains("Trade")) { _typeDescription = ClientSideTypeDescription.RegisterType(typeof(Trade), typeDescription.Value); } } }
public void TestPackObject() { var object1 = GetObject1(); ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); var cached = CachedObject.Pack(object1); Assert.IsNotNull(cached); Assert.IsNotNull(cached.PrimaryKey); Assert.AreEqual(cached.PrimaryKey, 11); Assert.IsNotNull(cached.UniqueKeys); Assert.AreEqual(cached.UniqueKeys.Length, 1); Assert.AreEqual(cached.UniqueKeys[0], 1); Assert.IsNotNull(cached.IndexKeys); Assert.AreEqual(cached.IndexKeys.Length, 4); foreach (var key in cached.IndexKeys) { if (key.KeyName == "IndexKeyDate") { Assert.AreEqual(key, new DateTime(2009, 10, 25).Ticks); Assert.AreEqual(key.KeyDataType, KeyDataType.IntKey); } if (key.KeyName == "IndexKeyValue") { Assert.AreEqual(key, 15); Assert.AreEqual(key.KeyDataType, KeyDataType.IntKey); } if (key.KeyDataType == KeyDataType.StringKey) { Assert.AreEqual(key, "FOL"); Assert.AreEqual(key.KeyName, "IndexKeyFolder"); } } var fromCache = CachedObject.Unpack <CacheableTypeOk>(cached); Assert.AreEqual(object1, fromCache); }
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); } }
public void Expression_tree_processing() { var homes = new DataSource <Home>(null, ClientSideTypeDescription.RegisterType <Home>()); var towns = new[] { "Paris", "Nice" }; // not supposed to be optimal; improving coverage (constant at left + extension at root level) var query = homes.PredicateToQuery(h => towns.Contains(h.Town) || "Toronto" == h.Town); Assert.AreEqual(2, query.Elements.Count); Assert.AreEqual(QueryOperator.In, query.Elements.First().Elements.Single().Operator); Assert.AreEqual("Toronto", query.Elements.Last().Elements.Single().Value.ToString()); // check reversed "Contains" at root query = homes.PredicateToQuery(h => h.AvailableDates.Contains(DateTime.Today) || h.Town == "Nowhere"); Assert.AreEqual(2, query.Elements.Count); Assert.AreEqual(QueryOperator.In, query.Elements.First().Elements.Single().Operator); }
public void TestTypeDescriptionIsSerializable() { var typeDescription = ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); var serializableDescription = typeDescription.AsTypeDescription; using (var stream = new MemoryStream()) { SerializationHelper.ObjectToStream(serializableDescription, stream, SerializationMode.ProtocolBuffers, false); stream.Seek(0, SeekOrigin.Begin); var deserializedDescription = SerializationHelper.ObjectFromStream <TypeDescription>(stream, SerializationMode .ProtocolBuffers, false); Assert.IsNotNull(deserializedDescription); Assert.AreEqual(serializableDescription, deserializedDescription); Assert.AreEqual(serializableDescription.GetHashCode(), deserializedDescription.GetHashCode()); } }
public void CompareTypedAndUntypedPacking() { var obj = GetObject1(); var description = ClientSideTypeDescription.RegisterType <CacheableTypeOk>(); var packed1 = CachedObject.Pack(obj, description); var packed2 = CachedObject.Pack(obj, description.AsTypeDescription); Assert.AreEqual(packed1, packed2); const int iterations = 100000; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < iterations; i++) { packed1 = CachedObject.Pack(obj, description); } Console.WriteLine($"{iterations} iterations with reflexion took {watch.ElapsedMilliseconds} ms"); var desc = description.AsTypeDescription; watch.Restart(); for (int i = 0; i < iterations; i++) { packed2 = CachedObject.Pack(obj, desc); } Console.WriteLine($"{iterations} iterations with json packing took {watch.ElapsedMilliseconds} ms"); }
public void StreamOneUnstreamMany() { CacheableTypeOk item = new CacheableTypeOk(3, 1003, "AHA", new DateTime(2010, 10, 02), 8); var desc = ClientSideTypeDescription.RegisterType <CacheableTypeOk>().AsTypeDescription; using (MemoryStream stream = new MemoryStream()) { Streamer.ToStream(stream, item, desc); stream.Seek(0, SeekOrigin.Begin); int itemsReceived = 0; Streamer.FromStream <CacheableTypeOk>(stream, delegate(CacheableTypeOk data, int currentItem, int totalItems) { Assert.IsTrue(currentItem > 0); Assert.IsTrue(currentItem <= totalItems); itemsReceived++; }, delegate { Assert.Fail(); }); Assert.AreEqual(itemsReceived, 1); } }
/// <summary> /// Initialize for a specified cacheable data type /// </summary> /// <param name="type"></param> public QueryBuilder(Type type) { var clientTypeDescription = ClientSideTypeDescription.RegisterType(type); _typeDescription = clientTypeDescription.AsTypeDescription; }
public void Lesser() { //register the type to get a valid TypeDescription //the type description is used to create CachedObjects from objects of the registered type var typeDescription = ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); KeyInfo valueKey = null; foreach (var keyInfo in typeDescription.IndexFields) { if (keyInfo.Name == "IndexKeyValue") { valueKey = keyInfo.AsKeyInfo; } } Assert.IsNotNull(valueKey); //fill in order { var indexByValue = new OrderedIndex(valueKey); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(1, 101, "A", DateTime.Now, 1))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(2, 102, "A", DateTime.Now, 4))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(3, 103, "A", DateTime.Now, 6))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(4, 104, "A", DateTime.Now, 13))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(5, 105, "A", DateTime.Now, 14))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(6, 106, "A", DateTime.Now, 80))); checkLE(indexByValue); checkLS(indexByValue); } //fill out of order { var indexByValue = new OrderedIndex(valueKey); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(6, 106, "A", DateTime.Now, 80))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(1, 101, "A", DateTime.Now, 1))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(4, 104, "A", DateTime.Now, 13))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(2, 102, "A", DateTime.Now, 4))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(5, 105, "A", DateTime.Now, 14))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(3, 103, "A", DateTime.Now, 6))); checkLE(indexByValue); checkLS(indexByValue); } //fill out of order transactional { var indexByValue = new OrderedIndex(valueKey); indexByValue.BeginFill(); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(6, 106, "A", DateTime.Now, 80))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(1, 101, "A", DateTime.Now, 1))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(4, 104, "A", DateTime.Now, 13))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(2, 102, "A", DateTime.Now, 4))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(5, 105, "A", DateTime.Now, 14))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(3, 103, "A", DateTime.Now, 6))); indexByValue.EndFill(); checkLE(indexByValue); checkLS(indexByValue); } //all equals { var indexByValue = new OrderedIndex(valueKey); var keyType = new KeyInfo(KeyDataType.IntKey, KeyType.ScalarIndex, "test", true); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(6, 106, "A", DateTime.Now, 45))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(1, 101, "A", DateTime.Now, 45))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(4, 104, "A", DateTime.Now, 45))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(2, 102, "A", DateTime.Now, 45))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(5, 105, "A", DateTime.Now, 45))); indexByValue.Put(CachedObject.Pack(new CacheableTypeOk(3, 103, "A", DateTime.Now, 45))); //value not fount (too small) var result1 = indexByValue.GetMany(MakeIntValue(12, keyType), QueryOperator.Le); Assert.AreEqual(result1.Count, 0); result1 = indexByValue.GetMany(MakeIntValue(12, keyType), QueryOperator.Lt); Assert.AreEqual(result1.Count, 0); //value not found (too big) result1 = indexByValue.GetMany(MakeIntValue(50, keyType), QueryOperator.Le); Assert.AreEqual(result1.Count, 6); result1 = indexByValue.GetMany(MakeIntValue(50, keyType), QueryOperator.Lt); Assert.AreEqual(result1.Count, 6); //value found (all match the index key ) result1 = indexByValue.GetMany(MakeIntValue(45, keyType), QueryOperator.Le); Assert.AreEqual(result1.Count, 6); //not found (Lt) result1 = indexByValue.GetMany(MakeIntValue(45, keyType), QueryOperator.Lt); Assert.AreEqual(result1.Count, 0); } }
public void TestPackedObjectIsSerializable() { ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); }
public void TestFixtureSetUp() { ClientSideTypeDescription.RegisterType(typeof(TradeLike)); }
public void SerializeCachedObjectUsingProtocolBuffers() { ClientSideTypeDescription.RegisterType(typeof(TradeLike)); Random randGen = new Random(); //to byte array for (int i = 0; i < 5000; i++) { TradeLike obj = new TradeLike(1, 1001, "aaa", new DateTime(2009, 10, 10), 1); CachedObject packed = CachedObject.Pack(obj).Metadata; byte[] data = SerializationHelper.ObjectToBytes(packed, SerializationMode.ProtocolBuffers, null); CachedObject reloaded = SerializationHelper.ObjectFromBytes <CachedObject>(data, SerializationMode.ProtocolBuffers, false); Assert.AreEqual(reloaded.IndexKeys[2], packed.IndexKeys[2]); Console.WriteLine(reloaded); } //to stream MemoryStream stream = new MemoryStream(); List <CachedObject> items = new List <CachedObject>(); for (int i = 0; i < 1000; i++) { TradeLike obj = new TradeLike(1, 1001, "aaa", new DateTime(2009, 10, 10), randGen.Next(1000)); CachedObject packed = CachedObject.Pack(obj).Metadata; items.Add(packed); } List <CachedObject> itemsReloaded = new List <CachedObject>(); Streamer.ToStreamGeneric(stream, items); stream.Seek(0, SeekOrigin.Begin); ManualResetEvent evt = new ManualResetEvent(false); Streamer.FromStream(stream, delegate(CachedObject item, int i, int totalItems) { itemsReloaded.Add(item); if (i == totalItems) { evt.Set(); } }, delegate { /* ignore exceptions */ }); evt.WaitOne(); for (int i = 0; i < 1000; i++) { Assert.AreEqual(itemsReloaded[i].IndexKeys[2], items[i].IndexKeys[2]); } }
public void TestKoNoPrimaryKey() { Assert.Throws <NotSupportedException>(() => ClientSideTypeDescription.RegisterType(typeof(CacheableTypeKo))); }
public void Init() { ClientSideTypeDescription.RegisterType(typeof(CacheableTypeOk)); }
public void DataAccess() { //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); //reload the first one by primary key var item1Reloaded = _client.GetOne <CacheableTypeOk>(1); Assert.AreEqual(item1, item1Reloaded); //try to load nonexistent object by primary key var itemNull = _client.GetOne <CacheableTypeOk>("UniqueKey", 2055); Assert.IsNull(itemNull); //try to load nonexistent object by unique key itemNull = _client.GetOne <CacheableTypeOk>(55); Assert.IsNull(itemNull); //reload both items by folder name IList <CacheableTypeOk> itemsInAaa = _client.GetMany <CacheableTypeOk>("IndexKeyFolder == aaa").ToList(); Assert.AreEqual(itemsInAaa.Count, 2); //change the folder of the first item and put it back into the cache item1.IndexKeyFolder = "bbb"; _client.Put(item1); //now it should be only one item left in aaa itemsInAaa = _client.GetMany <CacheableTypeOk>("IndexKeyFolder == aaa").ToList(); Assert.AreEqual(itemsInAaa.Count, 1); //get both of them again by date IList <CacheableTypeOk> allItems = _client.GetMany <CacheableTypeOk>($"IndexKeyDate == {new DateTime(2010, 10, 10).Ticks}").ToList(); Assert.AreEqual(allItems.Count, 2); var desc = ClientSideTypeDescription.RegisterType <CacheableTypeOk>().AsTypeDescription; //get object descriptions var qb = new QueryBuilder(typeof(CacheableTypeOk)); var itemDescriptions = _client.GetObjectDescriptions(qb.GetManyWhere($"IndexKeyDate == {new DateTime(2010, 10, 10).Ticks}")); Assert.AreEqual(itemDescriptions.Count, 2); Assert.AreEqual(itemDescriptions[0].PrimaryKey, desc.MakePrimaryKeyValue(1)); Assert.AreEqual(itemDescriptions[1].PrimaryKey, desc.MakePrimaryKeyValue(2)); //get both of them using an In query var builder = new QueryBuilder(typeof(CacheableTypeOk)); var q = builder.In("indexkeyfolder", "aaa", "bbb"); allItems = _client.GetMany <CacheableTypeOk>(q).ToList(); Assert.AreEqual(allItems.Count, 2); //remove the first one _client.Remove <CacheableTypeOk>(1); //removing non existent item should throw exception Assert.Throws <CacheException>(() => _client.Remove <CacheableTypeOk>(46546)); //the previous query should now return only one item allItems = _client.GetMany <CacheableTypeOk>(q).ToList(); Assert.AreEqual(allItems.Count, 1); //COUNT should also return 1 int count = _client.EvalQuery(q).Value; Assert.AreEqual(count, 1); }
public void Test_packing_performance() { var home = new Home { Address = "14 rue du chien qui fume", Bathrooms = 2, Rooms = 4, PriceInEuros = 200, CountryCode = "FR", Comments = { new Comment { Text = "Wonderful place", User = "******" }, new Comment { Text = "Very nice apartment" } } }; var desc = ClientSideTypeDescription.RegisterType <Home>(); const int objects = 10_000; { // warm up var unused = CachedObject.Pack(home, desc); var json = unused.AsJson(); var reloaded = CachedObject.Unpack <Home>(unused); var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < objects; i++) { var packed = CachedObject.Pack(home, desc); reloaded = CachedObject.Unpack <Home>(unused); } watch.Stop(); Console.WriteLine($"Packing + unpacking {objects} objects took {watch.ElapsedMilliseconds} ms"); } { // warm up desc.UseCompression = true; var unused = CachedObject.Pack(home, desc); var reloaded = CachedObject.Unpack <Home>(unused); var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < objects; i++) { var packed = CachedObject.Pack(home, desc); reloaded = CachedObject.Unpack <Home>(unused); } watch.Stop(); Console.WriteLine($"Packing + unpacking {objects} objects with compression took {watch.ElapsedMilliseconds} ms"); } }