Ejemplo n.º 1
0
        public void Pack_object_with_full_text_indexed_properties()
        {
            var description = TypedSchemaFactory.FromType <Home>();

            Assert.AreEqual(5, description.FullText.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"
                    }
                },
                Contacts = { "mail", "phone" }
            };

            var packed = PackedObject.Pack(home, description);

            Assert.AreEqual(7, 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 = PackedObject.PackJson(json, description);

            Assert.AreEqual(7, packed2.FullText.Length);
            Assert.IsTrue(packed2.FullText.Any(t => t.Contains("chien qui fume")));
        }
Ejemplo n.º 2
0
        internal static IEnumerable <PackedObject> LoadObjects(IDataClient @this, string jsonPath, [NotNull] string collectionName)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException(nameof(collectionName));
            }

            var json = File.ReadAllText(jsonPath);

            var array = JArray.Parse(json);

            var info = @this.GetClusterInformation();

            var schemaByName = info.Schema.ToDictionary(td => td.CollectionName.ToLower());

            if (!schemaByName.ContainsKey(collectionName.ToLower()))
            {
                throw new CacheException($"Collection {collectionName} not found");
            }

            CollectionSchema collectionSchema = schemaByName[collectionName];

            foreach (var item in array.Children <JObject>())
            {
                var cachedObject = PackedObject.PackJson(item.ToString(), collectionSchema);
                yield return(cachedObject);
            }
        }
Ejemplo n.º 3
0
        public static DataStore Reindex(DataStore old, CollectionSchema newDescription)
        {
            var result = new DataStore(newDescription, old.EvictionPolicy, old._fullTextConfig);


            result.InternalReindex(old.InternalEnumerateAsJson()
                                   .Select(json => PackedObject.PackJson(json, newDescription)));


            return(result);
        }
Ejemplo n.º 4
0
        public void Pack_json_with_missing_properties()
        {
            var json = @"{
                    'Id': 123,
                    }";

            var description = TypedSchemaFactory.FromType <AllKindsOfProperties>();

            var packed = PackedObject.PackJson(json, description);

            Assert.AreEqual(123, packed.PrimaryKey.IntValue);
        }
Ejemplo n.º 5
0
        public void Packing_a_binary_object_and_its_json_should_give_identical_results()
        {
            var today = DateTime.Today;
            var now   = DateTime.Now;

            var schema = TypedSchemaFactory.FromType(typeof(AllKindsOfProperties));

            var testObj = new AllKindsOfProperties
            {
                Id             = 15,
                ValueDate      = today,
                LastUpdate     = now,
                Nominal        = 156.32,
                Quantity       = 35,
                InstrumentName = "IRS",
                AnotherDate    = now,
                AreYouSure     = AllKindsOfProperties.Fuzzy.Maybe,
                IsDeleted      = true,
                Tags           = { "news", "science", "space", "διξ" },
                Languages      = { "en", "de", "fr" }
            };


            var packed1 = PackedObject.Pack(testObj, schema);

            var json = SerializationHelper.ObjectToJson(testObj);

            var packed2 = PackedObject.PackJson(json, schema);

            Console.WriteLine(packed1);
            Console.WriteLine(packed2);

            Assert.AreEqual(packed1, packed2); // only checks the primary key

            Assert.AreEqual(packed1.CollectionName, packed2.CollectionName);

            CollectionAssert.AreEqual(packed1.Values, packed2.Values);
            Assert.AreEqual(packed1.CollectionValues.Length, packed2.CollectionValues.Length);

            for (int i = 0; i < packed2.CollectionValues.Length; i++)
            {
                CollectionAssert.AreEqual(packed1.CollectionValues[i].Values, packed2.CollectionValues[i].Values);
            }



            CollectionAssert.AreEqual(packed1.ObjectData, packed2.ObjectData);
        }
Ejemplo n.º 6
0
        public void Compare_packing_result_for_different_methods()
        {
            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 = TypedSchemaFactory.FromType <Home>();

            //// warm up
            //var unused = PackedObject.Pack(home, desc);
            //var v1 = unused.ToString();

            var unused = PackedObject.Pack(home, desc);
            var v2     = unused.ToString();

            var json = SerializationHelper.ObjectToJson(home);

            unused = PackedObject.PackJson(json, desc);
            var v3 = unused.ToString();

            //Assert.AreEqual(v1, v2);
            Assert.AreEqual(v2, v3);
        }
Ejemplo n.º 7
0
        public void Packing_a_binary_object_and_its_json_should_give_identical_results_with_default_index_type()
        {
            var schema = TypedSchemaFactory.FromType(typeof(Order));

            var testObj = new Order
            {
                Amount      = 66.5, Date = DateTimeOffset.Now, Category = "student", ClientId = 101, ProductId = 405,
                Id          = Guid.NewGuid(),
                Quantity    = 1,
                IsDelivered = true
            };

            var description = TypedSchemaFactory.FromType <Order>();

            var typeDescription = description;

            var packed1 = PackedObject.Pack(testObj, schema);

            var json = SerializationHelper.ObjectToJson(testObj);

            var packed2 = PackedObject.PackJson(json, typeDescription);

            Console.WriteLine(packed1);
            Console.WriteLine(packed2);

            Assert.AreEqual(packed1, packed2); // only checks the primary key

            Assert.AreEqual(packed1.CollectionName, packed2.CollectionName);

            CollectionAssert.AreEqual(packed1.Values, packed2.Values);
            CollectionAssert.AreEqual(packed1.CollectionValues, packed2.CollectionValues);


            var json1 = Encoding.UTF8.GetString(packed1.ObjectData);
            var json2 = Encoding.UTF8.GetString(packed2.ObjectData);

            CollectionAssert.AreEqual(packed1.ObjectData, packed2.ObjectData);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Helper function. Enumerate all the objects in the dump
        /// </summary>
        /// <param name="path">path of the dump</param>
        /// <param name="collectionSchema"></param>
        /// <param name="shardIndex"></param>
        /// <returns></returns>
        public static IEnumerable <PackedObject> ObjectsInDump(string path, CollectionSchema collectionSchema,
                                                               int shardIndex = -1)
        {
            var fileMask = shardIndex != -1
                ? $"{collectionSchema.CollectionName}_shard{shardIndex:D4}*.txt"
                : $"{collectionSchema.CollectionName}_shard*.txt";

            var files = Directory.GetFiles(path, fileMask);

            foreach (var file in files)
            {
                var content = File.ReadAllText(file);

                var parts = content.Split(new[] { "\\-" }, StringSplitOptions.RemoveEmptyEntries)
                            .Select(txt => txt.Trim()).Where(t => !string.IsNullOrWhiteSpace(t)).ToList();

                foreach (var part in parts)
                {
                    var cachedObject = PackedObject.PackJson(part, collectionSchema);
                    yield return(cachedObject);
                }
            }
        }