public Document Resolve(Conflict conflict)
        {
            if (RequireBaseRevision)
            {
                conflict.Base.Should().NotBeNull();
            }

            var resolved = new MutableDocument();

            if (conflict.Base != null)
            {
                foreach (var pair in conflict.Base)
                {
                    resolved.SetValue(pair.Key, pair.Value);
                }
            }

            var changed = new HashSet <string>();

            foreach (var pair in conflict.Theirs)
            {
                resolved.SetValue(pair.Key, pair.Value);
                changed.Add(pair.Key);
            }

            foreach (var pair in conflict.Mine)
            {
                if (!changed.Contains(pair.Key))
                {
                    resolved.SetValue(pair.Key, pair.Value);
                }
            }

            return(resolved);
        }
Exemple #2
0
        public void TestDbBlobToJson()
        {
            var blob = ArrayTestBlob();

            Db.SaveBlob(blob);

            var bjson = blob.ToJSON();

            JObject o             = JObject.Parse(bjson);
            var     mdictFromJObj = o.ToObject <Dictionary <string, object> >();

            using (var cbDoc = new MutableDocument("doc1")) {
                cbDoc.SetValue("dict", mdictFromJObj);
                Db.Save(cbDoc);
            }

            var doc = Db.GetDocument("doc1").GetValue("dict");

            doc.GetType().Should().Be(typeof(Blob));
            var newJson = ((Blob)doc).ToJSON();

            var bjsonD   = JsonConvert.DeserializeObject <Dictionary <string, object> >(bjson);
            var newJsonD = JsonConvert.DeserializeObject <Dictionary <string, object> >(newJson);

            foreach (var kv in bjsonD)
            {
                newJsonD[kv.Key].Should().Equals(kv.Value);
            }
        }
 private void CreateDocument(params int[] numbers)
 {
     using (var doc = new MutableDocument()) {
         doc.SetValue("numbers", numbers);
         SaveDocument(doc);
     }
 }
Exemple #4
0
        private static void UseTypedAccessors()
        {
            using (var newTask = new MutableDocument()) {
                // # tag::date-getter[]
                newTask.SetValue("createdAt", DateTimeOffset.UtcNow);
                var date = newTask.GetDate("createdAt");
                // # end::date-getter[]

                Console.WriteLine(date);
            }
        }
        private void TestDistanceFunction(IExpression distance, string testData)
        {
            var tests = JsonConvert.DeserializeObject <IList <IList <object> > >(testData);

            foreach (var t in tests)
            {
                using (var doc = new MutableDocument()) {
                    doc.SetValue("v1", t[0]);
                    doc.SetValue("v2", t[1]);
                    doc.SetValue("distance", t[2]);
                    SaveDocument(doc);
                }
            }

            using (var q = QueryBuilder.Select(SelectResult.Expression(distance), SelectResult.Property("distance"))
                           .From(DataSource.Database(Db))) {
                var numRows = VerifyQuery(q, (n, r) =>
                {
                    r.GetValue(0).Should().Be(r.GetValue(1));
                });

                numRows.Should().Be(tests.Count);
            }
        }
        public virtual string Create(T obj)
        {
            var mutableDoc = new MutableDocument();

            mutableDoc.SetString("uuid", UUID);
            mutableDoc.SetString("type", typeof(T).Name);
            Database.Save(mutableDoc);

            //Update Id
            var objwithId = Helper.Function.UpdateGenericObjectProperty(obj, mutableDoc.Id);

            mutableDoc.SetValue("value", JsonConvert.SerializeObject(objwithId));
            Database.Save(mutableDoc);
            return(mutableDoc.Id);
        }
        public virtual string Create(T obj, params KeyValuePair <string, string>[] pairs)
        {
            var mutableDoc = new MutableDocument();

            mutableDoc.SetString("uuid", UUID);
            mutableDoc.SetString("type", typeof(T).Name);
            foreach (var pair in pairs)
            {
                mutableDoc.SetString(pair.Key, pair.Value);
            }
            Database.Save(mutableDoc);

            //Update Id
            var objwithId = Helper.Function.UpdateGenericObjectProperty(obj, mutableDoc.Id);

            mutableDoc.SetValue("value", JsonConvert.SerializeObject(objwithId));
            Database.Save(mutableDoc);
            return(mutableDoc.Id);
        }
        private void SetDocument(T entity, MutableDocument mutableDocument)
        {
            var properties = ObjectToDictionaryHelper.ToDictionary(entity);

            foreach (var prop in properties)
            {
                if (prop.Value is int)
                {
                    mutableDocument.SetInt(prop.Key, (int)prop.Value);
                }
                else if (prop.Value is long)
                {
                    mutableDocument.SetLong(prop.Key, (long)prop.Value);
                }
                else if (prop.Value is bool)
                {
                    mutableDocument.SetBoolean(prop.Key, (bool)prop.Value);
                }
                else if (prop.Value is DateTimeOffset)
                {
                    if ((DateTimeOffset)prop.Value != default(DateTimeOffset))
                    {
                        mutableDocument.SetDate(prop.Key, (DateTimeOffset)prop.Value);
                    }
                }
                else if (prop.Value is double)
                {
                    mutableDocument.SetDouble(prop.Key, (double)prop.Value);
                }
                else if (prop.Value is float)
                {
                    mutableDocument.SetFloat(prop.Key, (float)prop.Value);
                }
                else if (prop.Value is string)
                {
                    mutableDocument.SetString(prop.Key, (string)prop.Value);
                }
                else
                {
                    mutableDocument.SetValue(prop.Key, prop.Value);
                }
            }
        }