Пример #1
0
        /// <summary>
        /// Gets the value for the specified name
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public object GetValue(string name)
        {
            if (name == Constants.DocumentIdFieldName)
            {
                return(GetDocumentId());
            }
            RavenJToken value;

            if (inner.TryGetValue(name, out value))
            {
                return(TransformToValue(value));
            }
            if (name.StartsWith("_"))
            {
                if (inner.TryGetValue(name.Substring(1), out value))
                {
                    return(TransformToValue(value));
                }
            }
            if (name == "Id")
            {
                return(GetDocumentId());
            }
            if (name == "Inner")
            {
                return(inner);
            }
            return(new DynamicNullObject());
        }
        private bool TryParseFeature(RavenJObject obj, out object result)
        {
            string typeString;

            if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "feature")
            {
                RavenJToken geometry;
                object      geo;
                if (obj.TryGetValue("geometry", out geometry) && TryParseGeometry((RavenJObject)geometry, out geo))
                {
                    RavenJToken prop;
                    Dictionary <string, object> pr = null;
                    if (obj.TryGetValue("properties", out prop) && prop is RavenJObject)
                    {
                        var props = (RavenJObject)prop;
                        if (props.Count > 0)
                        {
                            pr = Enumerable.ToDictionary(props, x => x.Key, x => SantizeRavenJObjects(x.Value));
                        }
                    }

                    object      id = null;
                    RavenJToken idToken;
                    if (obj.TryGetValue("id", out idToken))
                    {
                        id = SantizeRavenJObjects(idToken);
                    }

                    result = _shapeConverter.ToFeature(geo, id, pr);
                    return(true);
                }
            }
            result = null;
            return(false);
        }
Пример #3
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (!metadata.ContainsKey("IsRoutable") || !metadata.Value<bool>("IsRoutable")) {
                return;
            }

            RavenJToken parentIdToken;
            RavenJToken slugToken;
            if (document.TryGetValue("ParentId", out parentIdToken) && document.TryGetValue("Slug", out slugToken)) {
                var parentId = parentIdToken.Value<string>();
                var slug = slugToken.Value<string>();

                string parentPath = null;
                if (!String.IsNullOrEmpty(parentId)) {
                    var parent = Database.Get(parentId, transactionInformation);
                    parentPath = parent.DataAsJson["Path"].Value<string>();
                }

                if (String.IsNullOrEmpty(parentPath)) {
                    document["Path"] = slug;
                } else {
                    document["Path"] = parentPath + "/" + slug;
                }
            }

            base.OnPut(key, document, metadata, transactionInformation);
        }
Пример #4
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (!metadata.ContainsKey("IsRoutable") || !metadata.Value <bool>("IsRoutable"))
            {
                return;
            }

            RavenJToken parentIdToken;
            RavenJToken slugToken;

            if (document.TryGetValue("ParentId", out parentIdToken) && document.TryGetValue("Slug", out slugToken))
            {
                var parentId = parentIdToken.Value <string>();
                var slug     = slugToken.Value <string>();

                string parentPath = null;
                if (!String.IsNullOrEmpty(parentId))
                {
                    var parent = Database.Get(parentId, transactionInformation);
                    parentPath = parent.DataAsJson["Path"].Value <string>();
                }

                if (String.IsNullOrEmpty(parentPath))
                {
                    document["Path"] = slug;
                }
                else
                {
                    document["Path"] = parentPath + "/" + slug;
                }
            }


            base.OnPut(key, document, metadata, transactionInformation);
        }
Пример #5
0
        /// <summary>
        /// Gets the value for the specified name
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public virtual object GetValue(string name)
        {
            if (name == Constants.Indexing.Fields.DocumentIdFieldName)
            {
                return(GetRootParentOrSelf().GetDocumentId());
            }
            RavenJToken value;

            if (inner.TryGetValue(name, out value))
            {
                return(TransformToValue(value));
            }
            if (name.StartsWith("_"))
            {
                if (inner.TryGetValue(name.Substring(1), out value))
                {
                    return(TransformToValue(value));
                }
            }
            if (name == "Id")
            {
                return(GetRootParentOrSelf().GetDocumentId());
            }
            if (name == "Inner")
            {
                return(inner);
            }
            if (name == "Count" || name == "Count()")
            {
                return(inner.Count);
            }
            return(DynamicNullObject.Null);
        }
        private static void MergeReplicationHistories(string documentId, RavenJObject origin, RavenJObject external, ref RavenJObject result)
        {
            result = (RavenJObject)origin.CloneToken();
            RavenJToken originHistory;
            RavenJToken externalHisotry;
            var         originHasHistory   = origin.TryGetValue(Constants.RavenReplicationHistory, out originHistory);
            var         externalHasHistory = external.TryGetValue(Constants.RavenReplicationHistory, out externalHisotry);
            RavenJToken externalVersion;
            RavenJToken externalSource;

            //we are going to lose the external source and version if we don't add them here
            if (external.TryGetValue(Constants.RavenReplicationVersion, out externalVersion) &&
                external.TryGetValue(Constants.RavenReplicationSource, out externalSource))
            {
                if (externalHasHistory)
                {
                    externalHisotry = externalHisotry.CloneToken();
                }
                else
                {
                    externalHisotry = new RavenJArray();
                }
                var historyEntry = new RavenJObject();
                historyEntry[Constants.RavenReplicationVersion] = externalVersion;
                historyEntry[Constants.RavenReplicationSource]  = externalSource;
                ((RavenJArray)externalHisotry).Add(historyEntry);
                externalHasHistory = true;
            }
            RavenJArray mergedHistory = null;

            //need to merge histories
            if (originHasHistory)
            {
                mergedHistory = Historian.MergeReplicationHistories((RavenJArray)originHistory, (RavenJArray)externalHisotry, documentId);
                result[Constants.RavenReplicationMergedHistory] = true;
            }
            else if (externalHasHistory)
            {
                //this might be a snapshot if somehow there was an history but no version or source
                mergedHistory = (RavenJArray)(externalHisotry.IsSnapshot? externalHisotry.CloneToken(): externalHisotry);
            }

            //if the original has history and the external didn't we already cloned it.
            if (mergedHistory != null)
            {
                result[Constants.RavenReplicationHistory] = mergedHistory;
            }
        }
        /// <summary>
        /// Runs shallow equal on the metadata while ignoring keys starting with '@'
        /// And replication related properties like replication
        /// </summary>
        /// <param name="documentId"></param>
        /// <param name="origin"></param>
        /// <param name="external"></param>
        /// <param name="result">The output metadata incase the metadata are equal</param>
        /// <returns></returns>
        protected static bool CheckIfMetadataIsEqualEnoughForReplicationAndMergeHistorires(
            string documentId,
            RavenJObject origin,
            RavenJObject external,
            out RavenJObject result)
        {
            result = null;
            var keysToCheck = new HashSet <string>(external.Keys.Where(k => !k.StartsWith("@") && !IgnoreProperties.Contains(k)));

            foreach (var key in origin.Keys.Where(k => !k.StartsWith("@") && !IgnoreProperties.Contains(k)))
            {
                var         originVal = origin[key];
                RavenJToken externalVal;
                if (external.TryGetValue(key, out externalVal) == false)
                {
                    return(false);
                }
                if (!RavenJTokenEqualityComparer.Default.Equals(originVal, externalVal))
                {
                    return(false);
                }
                keysToCheck.Remove(key);
            }
            if (keysToCheck.Any())
            {
                return(false);
            }
            //If we got here the metadata is the same, need to merge histories
            MergeReplicationHistories(documentId, origin, external, ref result);
            return(true);
        }
        public override void OnPut(string key,
                                   RavenJObject document,
                                   RavenJObject metadata,
                                   TransactionInformation transactionInformation)
        {
            Contract.Assume(!string.IsNullOrWhiteSpace(key));

            RavenJToken versionToken;

            if (!document.TryGetValue("Revision", out versionToken) || key.Contains(RevisionSegment))
            {
                return;
            }

            var newRevision     = versionToken.Value <int>();
            var currentRevision = metadata.ContainsKey(RavenDocumentRevision) ? metadata[RavenDocumentRevision].Value <int>() : 0;

            metadata[RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");

            //if we have a higher revision number than the existing then put a new revision
            if (newRevision > currentRevision)
            {
                metadata[RavenDocumentRevision] = RavenJToken.FromObject(versionToken.Value <int>());
                metadata.__ExternalState[RavenDocumentRevision] = metadata[RavenDocumentRevision];
            }
        }
		public override ReadVetoResult AllowRead(string key, Stream data, RavenJObject metadata, ReadOperation operation)
		{
			RavenJToken value;
			if (metadata.TryGetValue("Raven-Delete-Marker", out value))
				return ReadVetoResult.Ignore;
			return ReadVetoResult.Allowed;
		}
Пример #10
0
        /// <summary>
        /// Called when [put].
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="document">The document.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="transactionInformation">The transaction information.</param>
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/",true,CultureInfo.InvariantCulture)) // we don't deal with system documents
                return;

            if (TriggerContext.IsInTriggerContext)
                return;

            using (TriggerContext.Enter()) {
                var meta = document["MetaData"] as RavenJObject;
                if(meta != null) {
                    var slug = meta.Value<string>("Slug");
                    RavenJToken parent;
                    if (document.TryGetValue("Parent", out parent) && parent.Type != JTokenType.Null) {
                        var parentId = parent.Value<string>("Id");
                        var parentDocument = Database.Get(parentId, transactionInformation);
                        var parentUrl = parentDocument.DataAsJson.Value<JObject>("Metadata").Value<string>("Url");
                        if (parentUrl != null) {
                            meta["Url"] = string.Format("{0}/{1}", parentUrl, slug);
                            base.OnPut(key, document, metadata, transactionInformation);
                            return;
                        }
                    }
                    meta["Url"] = slug;
                }
            }
            base.OnPut(key, document, metadata, transactionInformation);
        }
Пример #11
0
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (string.IsNullOrEmpty(Database.Name) == false && Database.Name != Constants.SystemDatabase)
            {
                return;
            }

            if (key.StartsWith(Constants.Database.Prefix, StringComparison.OrdinalIgnoreCase) == false)
            {
                return;
            }

            RavenJObject settings;
            RavenJToken  value;

            if (jsonReplicationDocument.TryGetValue("Settings", out value) == false)
            {
                jsonReplicationDocument["Settings"] = settings = new RavenJObject();
            }
            else
            {
                settings = (RavenJObject)value;
            }

            EnsureQuotasBundleActivated(settings);
            EnsureVoronIsSetAsStorageEngineAndIsRunningInMemory(settings);
        }
        private bool TryParseFeatureCollection(RavenJObject obj, out object result)
        {
            result = null;
            string typeString;

            if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "featurecollection")
            {
                RavenJToken feats;
                if (obj.TryGetValue("features", out feats))
                {
                    var features = feats as RavenJArray;
                    if (features != null)
                    {
                        var temp = new object[features.Length];
                        for (var index = 0; index < features.Length; index++)
                        {
                            var geometry = features[index];
                            if (!TryParseFeature((RavenJObject)geometry, out temp[index]))
                            {
                                return(false);
                            }
                        }
                        result = _shapeConverter.ToFeatureCollection(temp);
                        return(true);
                    }
                }
            }
            return(false);
        }
        private bool TryParseGeometryCollection(RavenJObject obj, out object result)
        {
            result = null;
            RavenJToken geom;

            if (obj.TryGetValue("geometries", out geom))
            {
                var geometries = geom as RavenJArray;

                if (geometries != null)
                {
                    var temp = new object[geometries.Length];
                    for (var index = 0; index < geometries.Length; index++)
                    {
                        var geometry = geometries[index];
                        if (!TryParseGeometry((RavenJObject)geometry, out temp[index]))
                        {
                            return(false);
                        }
                    }
                    result = _shapeConverter.ToGeometryCollection(temp);
                    return(true);
                }
            }
            return(false);
        }
Пример #14
0
        private static void SetValueInDocumentForColumn(RavenJObject document, string column, string value)
        {
            RavenJObject targetObject   = document;
            string       targetProperty = column;

            if (targetProperty.Contains('.'))
            {
                var innerProps = column.Split('.');
                for (int i = 0; i < innerProps.Length - 1; i++)
                {
                    RavenJToken innerObject;
                    targetProperty = innerProps[i];

                    if (targetObject.TryGetValue(targetProperty, out innerObject) == false)
                    {
                        targetObject[targetProperty] = innerObject = new RavenJObject();
                    }

                    targetObject = (RavenJObject)innerObject;
                }

                targetProperty = innerProps[innerProps.Length - 1];
            }

            targetObject[targetProperty] = ParseToken(value);
        }
Пример #15
0
        /// <summary>
        /// Called when [put].
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="document">The document.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="transactionInformation">The transaction information.</param>
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/", true, CultureInfo.InvariantCulture)) // we don't deal with system documents
            {
                return;
            }

            if (TriggerContext.IsInTriggerContext)
            {
                return;
            }

            using (TriggerContext.Enter()) {
                var meta = document["MetaData"] as RavenJObject;
                if (meta != null)
                {
                    var         slug = meta.Value <string>("Slug");
                    RavenJToken parent;
                    if (document.TryGetValue("Parent", out parent) && parent.Type != JTokenType.Null)
                    {
                        var parentId       = parent.Value <string>("Id");
                        var parentDocument = Database.Get(parentId, transactionInformation);
                        var parentUrl      = parentDocument.DataAsJson.Value <JObject>("Metadata").Value <string>("Url");
                        if (parentUrl != null)
                        {
                            meta["Url"] = string.Format("{0}/{1}", parentUrl, slug);
                            base.OnPut(key, document, metadata, transactionInformation);
                            return;
                        }
                    }
                    meta["Url"] = slug;
                }
            }
            base.OnPut(key, document, metadata, transactionInformation);
        }
Пример #16
0
        internal static RavenJToken HandleMetadataChanges(RavenJObject metadata, SmugglerDataFormat format)
        {
            if (metadata == null)
            {
                return(null);
            }

            if (format != SmugglerDataFormat.V4)
            {
                return(metadata);
            }

            metadata.Remove(Metadata.Etag);
            metadata.Remove(Metadata.LastModified);
            metadata.Remove(Metadata.IndexScore);

            RavenJToken collection;

            if (metadata.TryGetValue(Metadata.Collection, out collection))
            {
                metadata.Remove(Metadata.Collection);
                metadata.Add(Constants.RavenEntityName, collection);
            }

            return(metadata);
        }
Пример #17
0
            private void GetPropertiesAndWriteCsvHeader(RavenJObject result, out bool includeId)
            {
                includeId  = false;
                properties = DocumentHelpers.GetPropertiesFromJObject(result,
                                                                      parentPropertyPath: "",
                                                                      includeNestedProperties: true,
                                                                      includeMetadata: false,
                                                                      excludeParentPropertyNames: true).ToList();

                RavenJToken token;

                if (result.TryGetValue("@metadata", out token))
                {
                    var metadata = token as RavenJObject;
                    if (metadata != null)
                    {
                        if (metadata.TryGetValue("@id", out token))
                        {
                            OutputCsvValue("@id");
                            writer.Write(',');

                            includeId = true;
                        }
                    }
                }

                foreach (var property in properties)
                {
                    OutputCsvValue(property);
                    writer.Write(',');
                }
                writer.WriteLine();
            }
		private static void EnsureQuotasBundleActivated(RavenJObject settings)
		{
			RavenJToken value;
			if (settings.TryGetValue(Constants.ActiveBundles, out value) == false)
				settings[Constants.ActiveBundles] = value = new RavenJValue(string.Empty);

			var activeBundles = value.Value<string>();
			var bundles = activeBundles.GetSemicolonSeparatedValues();

			if (bundles.Contains("Quotas", StringComparer.OrdinalIgnoreCase) == false)
				bundles.Add("Quotas");

			int hardLimitInKb;
			if (int.TryParse(ConfigurationManager.AppSettings["Raven/Bundles/LiveTest/Quotas/Size/HardLimitInKB"], out hardLimitInKb) == false) 
				hardLimitInKb = QuotasHardLimitInKb;

			int softMarginInKb;
			if (int.TryParse(ConfigurationManager.AppSettings["Raven/Bundles/LiveTest/Quotas/Size/SoftLimitInKB"], out softMarginInKb) == false)
				softMarginInKb = QuotasSoftMarginInKb;

			settings[Constants.ActiveBundles] = string.Join(";", bundles);
			settings[Constants.SizeHardLimitInKB] = hardLimitInKb;
			settings[Constants.SizeSoftLimitInKB] = softMarginInKb;
			settings[Constants.DocsHardLimit] = null;
			settings[Constants.DocsSoftLimit] = null;
		}
		private bool TryParseFeatureCollection(RavenJObject obj, out object result)
		{
			result = null;
			string typeString;
			if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "featurecollection")
			{
				RavenJToken feats;
				if (obj.TryGetValue("features", out feats))
				{
					var features = feats as RavenJArray;
					if (features != null)
					{
						var temp = new object[features.Length];
						for (var index = 0; index < features.Length; index++)
						{
							var geometry = features[index];
							if (!TryParseFeature((RavenJObject)geometry, out temp[index]))
								return false;
						}
						result = _shapeConverter.ToFeatureCollection(temp);
						return true;
					}
				}
			}
			return false;
		}
Пример #20
0
        private bool TryParseGeometryCollection(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken geom;

            if (obj.TryGetValue("geometries", out geom))
            {
                var geometries = geom as RavenJArray;

                if (geometries != null)
                {
                    builder.Append("GEOMETRYCOLLECTION (");
                    for (var index = 0; index < geometries.Length; index++)
                    {
                        if (index > 0)
                        {
                            builder.Append(", ");
                        }
                        var geometry = geometries[index];
                        if (!TryParseGeometry((RavenJObject)geometry, builder))
                        {
                            return(false);
                        }
                    }
                    builder.Append(")");
                    return(true);
                }
            }
            return(false);
        }
Пример #21
0
        private static Document CreateDocument(string lowerKey, RavenJObject metadata)
        {
            var doc = new Document();

            doc.Add(new Field("__key", lowerKey, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

            var fileName = Path.GetFileName(lowerKey);

            Debug.Assert(fileName != null);
            doc.Add(new Field("__fileName", fileName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the file name is used to allow searches that start with wildcards
            char[] revFileName = fileName.ToCharArray();
            Array.Reverse(revFileName);
            doc.Add(new Field("__rfileName", new string(revFileName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            int level         = 0;
            var directoryName = RavenFileNameHelper.RavenDirectory(Path.GetDirectoryName(lowerKey));


            doc.Add(new Field("__directory", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the directory is used to allow searches that start with wildcards
            char[] revDirectory = directoryName.ToCharArray();
            Array.Reverse(revDirectory);
            doc.Add(new Field("__rdirectory", new string(revDirectory), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            do
            {
                level += 1;

                directoryName = RavenFileNameHelper.RavenDirectory(directoryName);

                doc.Add(new Field("__directoryName", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                // the reversed version of the directory is used to allow searches that start with wildcards
                char[] revDirectoryName = directoryName.ToCharArray();
                Array.Reverse(revDirectoryName);
                doc.Add(new Field("__rdirectoryName", new string(revDirectoryName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

                directoryName = Path.GetDirectoryName(directoryName);
            }while (directoryName != null);

            doc.Add(new Field("__modified", DateTime.UtcNow.ToString(DateIndexFormat, CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("__level", level.ToString(CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            RavenJToken contentLen;

            if (metadata.TryGetValue("Content-Length", out contentLen))
            {
                long len;
                if (long.TryParse(contentLen.Value <string>(), out len))
                {
                    doc.Add(new Field("__size", len.ToString("D20"), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                    doc.Add(new NumericField("__size_numeric", Field.Store.NO, true).SetLongValue(len));
                }
            }

            return(doc);
        }
Пример #22
0
        public override ReadVetoResult AllowRead(string key, Stream data, RavenJObject metadata, ReadOperation operation)
        {
            RavenJToken value;

            if (metadata.TryGetValue("Raven-Delete-Marker", out value))
            {
                return(ReadVetoResult.Ignore);
            }
            return(ReadVetoResult.Allowed);
        }
		public override ReadVetoResult AllowRead(string key, RavenJObject metadata, ReadOperation operation,
												 TransactionInformation transactionInformation)
		{
			if(metadata == null)
				return ReadVetoResult.Allowed; // this is a projection, it is allowed
			RavenJToken value;
			if (metadata.TryGetValue("Raven-Delete-Marker", out value))
				return ReadVetoResult.Ignore;
			return ReadVetoResult.Allowed;
		}
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            // leave raven system docs alone
            if (key.StartsWith("Raven/"))
                return;

            RavenJToken lastModified;
            if (!metadata.TryGetValue("Last-Modified", out lastModified))
                metadata.Add("Created", SystemTime.UtcNow);
        }
Пример #25
0
        private bool TryParseTypeString(RavenJObject obj, out string result)
        {
			RavenJToken type = null;
            if (obj != null)
                obj.TryGetValue("type", out type);
                
            result = ((RavenJValue) type).Value as string;
			
            return type != null;
        }
Пример #26
0
		//private bool TryParseFeatureCollection(RavenJObject obj, StringBuilder result)
		//{
		//	result = null;
		//	string typeString;
		//	if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "featurecollection")
		//	{
		//		RavenJToken feats = null;
		//		if (obj.TryGetValue("features", out feats))
		//		{
		//			var features = feats as RavenJArray;
		//			if (features != null)
		//			{
		//				var temp = new object[features.Length];
		//				for (var index = 0; index < features.Length; index++)
		//				{
		//					var geometry = features[index];
		//					if (!TryParseFeature((RavenJObject) geometry, out temp[index]))
		//						return false;
		//				}
		//				result = new FeatureCollection(temp.Cast<Feature>());
		//				return true;
		//			}
		//		}
		//	}
		//	return false;
		//}

		private bool TryParseFeature(RavenJObject obj, StringBuilder builder)
		{
			string typeString;
			if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "feature")
			{
				RavenJToken geometry;
				if (obj.TryGetValue("geometry", out geometry) && TryParseGeometry((RavenJObject) geometry, builder))
					return true;
			}
			return false;
		}
Пример #27
0
        public void Write(RavenJObject result)
        {
            if (properties == null)
            {
                GetPropertiesAndWriteCsvHeader(result, out doIncludeId);
                Debug.Assert(properties != null);
            }

            if (doIncludeId)
            {
                RavenJToken token;
                if (result.TryGetValue("@metadata", out token))
                {
                    var metadata = token as RavenJObject;
                    if (metadata != null)
                    {
                        if (metadata.TryGetValue("@id", out token))
                        {
                            csvWriter.WriteField(token.Value <string>());
                        }
                    }
                }
            }

            foreach (var property in properties)
            {
                var token = result.SelectToken(property);
                if (token == null)
                {
                    csvWriter.WriteField(null);
                    continue;
                }

                switch (token.Type)
                {
                case JTokenType.Null:
                    csvWriter.WriteField(null);
                    break;

                case JTokenType.Array:
                case JTokenType.Object:
                    csvWriter.WriteField(token.ToString(Formatting.None));
                    break;

                default:
                    csvWriter.WriteField(token.Value <string>());
                    break;
                }
            }

            csvWriter.NextRecord();
        }
        public object GetValue(string fieldName)
        {
            Guard.NotNull("fieldName", fieldName);

            RavenJToken token;

            if (IsInternalField(fieldName) || !data.TryGetValue(fieldName, out token))
            {
                throw CommonErrors.DataItemFieldNotFound(fieldName);
            }

            return(GetValue(token));
        }
Пример #29
0
        private bool TryParseTypeString(RavenJObject obj, out string result)
        {
            RavenJToken type = null;

            if (obj != null)
            {
                obj.TryGetValue("type", out type);
            }

            result = ((RavenJValue)type).Value as string;

            return(type != null);
        }
            private static RavenJObject CreateJsonDocumentFromLuceneDocument(Document document)
            {
                var field = document.GetField(Constants.ReduceValueFieldName);
                if (field != null)
                    return RavenJObject.Parse(field.StringValue);

                var ravenJObject = new RavenJObject();

                var fields = document.GetFields();
                var arrayMarkers = fields
                    .Where(x => x.Name.EndsWith("_IsArray"))
                    .Select(x => x.Name)
                    .ToList();

                foreach (var fieldable in fields)
                {
                    var stringValue = GetStringValue(fieldable);
                    var isArrayMarker = fieldable.Name.EndsWith("_IsArray");
                    var isArray = !isArrayMarker && arrayMarkers.Contains(fieldable.Name + "_IsArray");

                    RavenJToken token;
                    var isJson = RavenJToken.TryParse(stringValue, out token);

                    RavenJToken value;
                    if (ravenJObject.TryGetValue(fieldable.Name, out value) == false)
                    {
                        if (isArray)
                            ravenJObject[fieldable.Name] = new RavenJArray { isJson ? token : stringValue };
                        else if (isArrayMarker)
                        {
                            var fieldName = fieldable.Name.Substring(0, fieldable.Name.Length - 8);
                            ravenJObject[fieldable.Name] = isJson ? token : stringValue;
                            ravenJObject[fieldName] = new RavenJArray();
                        }
                        else
                            ravenJObject[fieldable.Name] = isJson ? token : stringValue;
                    }
                    else
                    {
                        var ravenJArray = value as RavenJArray;
                        if (ravenJArray != null)
                            ravenJArray.Add(isJson ? token : stringValue);
                        else
                        {
                            ravenJArray = new RavenJArray { value, isJson ? token : stringValue };
                            ravenJObject[fieldable.Name] = ravenJArray;
                        }
                    }
                }
                return ravenJObject;
            }
Пример #31
0
        //private bool TryParseFeatureCollection(RavenJObject obj, StringBuilder result)
        //{
        //	result = null;
        //	string typeString;
        //	if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "featurecollection")
        //	{
        //		RavenJToken feats = null;
        //		if (obj.TryGetValue("features", out feats))
        //		{
        //			var features = feats as RavenJArray;
        //			if (features != null)
        //			{
        //				var temp = new object[features.Length];
        //				for (var index = 0; index < features.Length; index++)
        //				{
        //					var geometry = features[index];
        //					if (!TryParseFeature((RavenJObject) geometry, out temp[index]))
        //						return false;
        //				}
        //				result = new FeatureCollection(temp.Cast<Feature>());
        //				return true;
        //			}
        //		}
        //	}
        //	return false;
        //}

        private bool TryParseFeature(RavenJObject obj, StringBuilder builder)
        {
            string typeString;

            if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "feature")
            {
                RavenJToken geometry;
                if (obj.TryGetValue("geometry", out geometry) && TryParseGeometry((RavenJObject)geometry, builder))
                {
                    return(true);
                }
            }
            return(false);
        }
		private bool TryParseTypeString(RavenJObject obj, out string result)
		{
			RavenJToken type = null;
			if (obj != null)
				obj.TryGetValue("type", out type);

			var value = type as RavenJValue;
			if (value != null)
				result = value.Value as string;
			else
				result = null;

			return type != null;
		}
Пример #33
0
        public override ReadVetoResult AllowRead(string key, RavenJObject metadata, ReadOperation operation)
        {
            if (metadata == null)
            {
                return(ReadVetoResult.Allowed); // this is a projection, it is allowed
            }
            RavenJToken value;

            if (metadata.TryGetValue("Raven-Delete-Marker", out value))
            {
                return(ReadVetoResult.Ignore);
            }
            return(ReadVetoResult.Allowed);
        }
Пример #34
0
        private bool TryParseMultiPolygon(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken coord;

            if (obj.TryGetValue("coordinates", out coord))
            {
                var coordinates = coord as RavenJArray;
                builder.Append("MULTIPOLYGON (");
                if (coordinates != null && TryParseCoordinateArrayArrayArray(coordinates, builder))
                {
                    builder.Append(")");
                    return(true);
                }
            }
            return(false);
        }
Пример #35
0
        private bool TryParseLineString(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken coord;

            if (obj.TryGetValue("coordinates", out coord))
            {
                var coordinates = coord as RavenJArray;
                builder.Append("LINESTRING (");
                if (coordinates != null && TryParseCoordinateArray(coordinates, builder))
                {
                    builder.Append(")");
                    return(true);
                }
            }
            return(false);
        }
        private bool TryParseMultiPolygon(RavenJObject obj, out object result)
        {
            RavenJToken coord;

            if (obj.TryGetValue("coordinates", out coord))
            {
                var coordinates = coord as RavenJArray;
                CoordinateInfo[][][] co;
                if (coordinates != null && TryParseCoordinateArrayArrayArray(coordinates, out co))
                {
                    result = _shapeConverter.ToMultiPolygon(co);
                    return(true);
                }
            }
            result = null;
            return(false);
        }
		public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			if (string.IsNullOrEmpty(Database.Name) == false && Database.Name != Constants.SystemDatabase)
				return;

			if (key.StartsWith(Constants.Database.Prefix, StringComparison.OrdinalIgnoreCase) == false)
				return;

			RavenJObject settings;
			RavenJToken value;
			if (document.TryGetValue("Settings", out value) == false)
				document["Settings"] = settings = new RavenJObject();
			else
				settings = (RavenJObject)value;

			EnsureQuotasBundleActivated(settings);
			EnsureVoronIsSetAsStorageEngineAndIsRunningInMemory(settings);
		}
Пример #38
0
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            // leave raven system docs alone
            if (key.StartsWith("Raven/"))
            {
                return(false);
            }

            RavenJToken lastModified;

            if (!metadata.TryGetValue(Constants.LastModified, out lastModified))
            {
                metadata.Add("Created", SystemTime.UtcNow);
                return(true);
            }

            return(false);
        }
Пример #39
0
        /// <summary>
        /// Updates the children.
        /// </summary>
        /// <param name="parentKey">The parent key.</param>
        /// <param name="parentDocument">The parent document.</param>
        /// <param name="transactionInformation">The transaction information.</param>
        public void UpdateChildren(string parentKey, RavenJObject parentDocument, TransactionInformation transactionInformation)
        {
            var childrenQuery = new IndexQuery
            {
                Query = "Id:" + parentKey
            };

            var queryResult = Database.Query("Documents/ByParent", childrenQuery);

            if (queryResult.Results.Count > 0)
            {
                RavenJToken parentMetaData;
                parentDocument.TryGetValue("Metadata", out parentMetaData);

                var parentUrl = parentMetaData.Value <string>("Url");

                foreach (var result in queryResult.Results)
                {
                    var metadataJObject = result.Value <RavenJObject>("@metadata");
                    if (metadataJObject != null)
                    {
                        var metaData = result["MetaData"] as RavenJObject;
                        if (metaData != null)
                        {
                            var slug = metaData.Value <string>("Slug");

                            if (string.IsNullOrEmpty(parentUrl))
                            {
                                metaData["Url"] = slug;
                            }
                            else
                            {
                                metaData["Url"] = string.Format("{0}/{1}", parentUrl, slug);
                            }
                        }
                        var childEtag = metadataJObject.Value <string>("@etag");
                        var childId   = metadataJObject.Value <string>("@id");

                        Database.Put(childId, Guid.Parse(childEtag), result, metadataJObject, transactionInformation);
                        UpdateChildren(childId, result, transactionInformation);
                    }
                }
            }
        }
        public override void OnPut(
            string key,
            RavenJObject document,
            RavenJObject metadata,
            TransactionInformation transactionInformation)
        {
            Contract.Assume(!string.IsNullOrWhiteSpace(key));
            RavenJToken versionToken;
            if (!document.TryGetValue("Revision", out versionToken) || key.Contains(RevisionSegment))
            {
                return;
            }
            using (Database.DisableAllTriggersForCurrentThread())
            {
                var revisionCopy = new RavenJObject(document);
                var revisionKey  = $"{key}{RevisionSegment}{versionToken.Value<int>()}";

                Database.TransactionalStorage.Batch(storage => 
                    storage.Documents.AddDocument(revisionKey, null, revisionCopy, metadata));
            }
        }
Пример #41
0
        private void GetPropertiesAndWriteCsvHeader(RavenJObject result, out bool includeId)
        {
            includeId  = false;
            properties = DocumentHelpers.GetPropertiesFromJObject(result,
                                                                  parentPropertyPath: "",
                                                                  includeNestedProperties: true,
                                                                  includeMetadata: false,
                                                                  excludeParentPropertyNames: true).ToList();

            if (customColumns != null && customColumns.Length > 0)
            {
                // since user defined custom CSV columns filter list generated using GetPropertiesFromJObject
                // we interate over customColumns instead of properties to maintain columns order requested by user
                properties = customColumns
                             .SelectMany(c => properties.Where(p => p.StartsWith(c)))
                             .ToList();
            }

            RavenJToken token;

            if (result.TryGetValue("@metadata", out token))
            {
                var metadata = token as RavenJObject;
                if (metadata != null)
                {
                    if (metadata.TryGetValue("@id", out token))
                    {
                        csvWriter.WriteField("@id");
                        includeId = true;
                    }
                }
            }

            foreach (var property in properties)
            {
                csvWriter.WriteField(property);
            }

            csvWriter.NextRecord();
        }
Пример #42
0
        public override void OnPut(string key,
                                   RavenJObject document,
                                   RavenJObject metadata,
                                   TransactionInformation transactionInformation)
        {
            Contract.Assume(!string.IsNullOrWhiteSpace(key));
            RavenJToken versionToken;

            if (!document.TryGetValue("Revision", out versionToken) || key.Contains(RevisionSegment))
            {
                return;
            }
            using (Database.DisableAllTriggersForCurrentThread())
            {
                var revisionCopy = new RavenJObject(document);

                var revisionkey = key + RevisionSegment + versionToken.Value <int>();

                Database.TransactionalStorage.Batch(
                    storage => storage.Documents.AddDocument(revisionkey, null, revisionCopy, metadata));
            }
        }
        public override void OnPut(string key,
                                   RavenJObject document,
                                   RavenJObject metadata,
                                   TransactionInformation transactionInformation)
        {
            Contract.Assume(!string.IsNullOrWhiteSpace(key));

            RavenJToken versionToken;
            if (!document.TryGetValue("Revision", out versionToken) || key.Contains(RevisionSegment))
                return;

            var newRevision = versionToken.Value<int>();
            var currentRevision = metadata.ContainsKey(RavenDocumentRevision) ? metadata[RavenDocumentRevision].Value<int>() : 0;

            metadata[RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");

            //if we have a higher revision number than the existing then put a new revision
            if (newRevision > currentRevision)
            {
                metadata[RavenDocumentRevision] = RavenJToken.FromObject(versionToken.Value<int>());
                metadata.__ExternalState[RavenDocumentRevision] = metadata[RavenDocumentRevision];
            }
        }
Пример #44
0
        public Engine CheckoutScript(Func<ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request, RavenJObject customFunctions)
        {
            CachedResult value;
            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);
            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return context;
                }
            }
            var result = createEngine(request);

            RavenJToken functions;
            if (customFunctions != null && customFunctions.TryGetValue("Functions", out functions))

                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", functions), new ParserOptions { Source = "customFunctions.js" });
            var cachedResult = new CachedResult
            {
                Usage = 1,
                Queue = new ConcurrentQueue<Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return existing;
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                    .Where(x => x.Value != null)
                    .OrderByDescending(x => x.Value.Usage)
                    .ThenBy(x => x.Value.Timestamp)
                    .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    if (Equals(source.Key, request))
                        continue; // we don't want to remove the one we just added
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return result;
        }
Пример #45
0
        private static Document CreateDocument(string lowerKey, RavenJObject metadata)
        {
            var doc = new Document();
            doc.Add(new Field("__key", lowerKey, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

            var fileName = Path.GetFileName(lowerKey);
            Debug.Assert(fileName != null);
            doc.Add(new Field("__fileName", fileName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the file name is used to allow searches that start with wildcards
            char[] revFileName = fileName.ToCharArray();
            Array.Reverse(revFileName);
            doc.Add(new Field("__rfileName", new string(revFileName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            int level = 0;
            var directoryName = Path.GetDirectoryName(lowerKey);
            do
            {
                level += 1;
                directoryName = (string.IsNullOrEmpty(directoryName) ? "" : directoryName.Replace("\\", "/"));
                if (directoryName.StartsWith("/") == false)
                    directoryName = "/" + directoryName;
                doc.Add(new Field("__directory", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                directoryName = Path.GetDirectoryName(directoryName);
            } while (directoryName != null);
            doc.Add(new Field("__modified", DateTime.UtcNow.ToString(DateIndexFormat, CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("__level", level.ToString(CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            
            RavenJToken contentLen;
            if ( metadata.TryGetValue("Content-Length", out contentLen))
            {
                long len;
                if (long.TryParse(contentLen.Value<string>(), out len))
                {
                    doc.Add(new Field("__size", len.ToString("D20"), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                    doc.Add(new NumericField("__size_numeric", Field.Store.NO, true).SetLongValue(len));
                }
            }

            return doc;
        }
Пример #46
0
        /// <summary>
        /// Updates the children.
        /// </summary>
        /// <param name="parentKey">The parent key.</param>
        /// <param name="parentDocument">The parent document.</param>
        /// <param name="transactionInformation">The transaction information.</param>
        public void UpdateChildren(string parentKey, RavenJObject parentDocument, TransactionInformation transactionInformation)
        {
            var childrenQuery = new IndexQuery
            {
                Query = "Id:" + parentKey
            };

            var queryResult = Database.Query("Documents/ByParent", childrenQuery);

            if (queryResult.Results.Count > 0) {

                RavenJToken parentMetaData;
                parentDocument.TryGetValue("Metadata", out parentMetaData);

                var parentUrl = parentMetaData.Value<string>("Url");

                foreach (var result in queryResult.Results) {

                    var metadataJObject = result.Value<RavenJObject>("@metadata");
                    if (metadataJObject != null) {

                        var metaData = result["MetaData"] as RavenJObject;
                        if(metaData != null) {
                            var slug = metaData.Value<string>("Slug");

                            if(string.IsNullOrEmpty(parentUrl)) {
                                metaData["Url"] = slug;
                            }
                            else {
                                metaData["Url"] = string.Format("{0}/{1}", parentUrl, slug);
                            }
                        }
                        var childEtag = metadataJObject.Value<string>("@etag");
                        var childId = metadataJObject.Value<string>("@id");

                        Database.Put(childId, Guid.Parse(childEtag), result, metadataJObject, transactionInformation);
                        UpdateChildren(childId,result,transactionInformation);
                    }
                }
            }
        }
		private bool TryParsePoint(RavenJObject obj, out object result)
		{
			result = null;
			RavenJToken coord;
			if (obj.TryGetValue("coordinates", out coord))
			{
				var coordinates = coord as RavenJArray;

				if (coordinates == null || coordinates.Length < 2)
					return false;

				CoordinateInfo coordinate;
				if (TryParseCoordinate(coordinates, out coordinate))
				{
					result = _shapeConverter.ToPoint(coordinate);
					return true;
				}
			}
			return false;
		}
Пример #48
0
        private static Document CreateDocument(string lowerKey, RavenJObject metadata)
        {
            var doc = new Document();
            doc.Add(new Field("__key", lowerKey, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            
            var fileName = Path.GetFileName(lowerKey);            
            Debug.Assert(fileName != null);
            doc.Add(new Field("__fileName", fileName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the file name is used to allow searches that start with wildcards
            char[] revFileName = fileName.ToCharArray();
            Array.Reverse(revFileName);
            doc.Add(new Field("__rfileName", new string(revFileName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));                        

            int level = 0;
            var directoryName = RavenFileNameHelper.RavenDirectory(FileSystemPathExtentions.GetDirectoryName(lowerKey));


            doc.Add(new Field("__directory", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the directory is used to allow searches that start with wildcards
            char[] revDirectory = directoryName.ToCharArray();
            Array.Reverse(revDirectory);
            doc.Add(new Field("__rdirectory", new string(revDirectory), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            do
            {
                level += 1;

                directoryName = RavenFileNameHelper.RavenDirectory(directoryName);

                doc.Add(new Field("__directoryName", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                // the reversed version of the directory is used to allow searches that start with wildcards
                char[] revDirectoryName = directoryName.ToCharArray();
                Array.Reverse(revDirectoryName);
                doc.Add(new Field("__rdirectoryName", new string(revDirectoryName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

                directoryName = FileSystemPathExtentions.GetDirectoryName(directoryName);
            }
            while (directoryName != null );

            doc.Add(new Field("__modified", DateTime.UtcNow.ToString(DateIndexFormat, CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            var value = metadata.Value<string>(Constants.CreationDate);
            if (value != null)
                doc.Add(new Field("__created", value, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("__level", level.ToString(CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            
            RavenJToken contentLen;
            if ( metadata.TryGetValue("Content-Length", out contentLen))
            {
                long len;
                if (long.TryParse(contentLen.Value<string>(), out len))
                {
                    doc.Add(new Field("__size", len.ToString("D20"), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                    doc.Add(new NumericField("__size_numeric", Field.Store.NO, true).SetLongValue(len));
                }
            }

            return doc;
        }
Пример #49
0
        private bool TryParseMultiPolygon(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken coord;
	        if (obj.TryGetValue("coordinates", out coord))
	        {
				var coordinates = coord as RavenJArray;
				builder.Append("MULTIPOLYGON (");
				if (coordinates != null && TryParseCoordinateArrayArrayArray(coordinates, builder))
				{
					builder.Append(")");
					return true;
				}
	        }
            return false;
        }
Пример #50
0
        private bool TryParseGeometryCollection(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken geom;
			if (obj.TryGetValue("geometries", out geom))
	        {
				var geometries = geom as RavenJArray;

		        if (geometries != null)
		        {
					builder.Append("GEOMETRYCOLLECTION (");
			        for (var index = 0; index < geometries.Length; index++)
					{
						if (index > 0)
							builder.Append(", ");
				        var geometry = geometries[index];
						if (!TryParseGeometry((RavenJObject)geometry, builder))
					        return false;
			        }
					builder.Append(")");
			        return true;
		        }
	        }
	        return false;
        }
Пример #51
0
        private bool TryParsePoint(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken coord;
			if (obj.TryGetValue("coordinates", out coord))
			{
				var coordinates = coord as RavenJArray;

				if (coordinates == null || coordinates.Length < 2)
					return false;

				builder.Append("POINT (");
				if (TryParseCoordinate(coordinates, builder))
				{
					builder.Append(")");
					return true;
				}
			}
            return false;
        }
Пример #52
0
        private bool TryParseLineString(RavenJObject obj, StringBuilder builder)
        {
            RavenJToken coord;
	        if (obj.TryGetValue("coordinates", out coord))
	        {
				var coordinates = coord as RavenJArray;
				builder.Append("LINESTRING (");
				if (coordinates != null && TryParseCoordinateArray(coordinates, builder))
				{
					builder.Append(")");
					return true;
				}
	        }
            return false;
        }
		private bool TryParseGeometryCollection(RavenJObject obj, out object result)
		{
			result = null;
			RavenJToken geom;
			if (obj.TryGetValue("geometries", out geom))
			{
				var geometries = geom as RavenJArray;

				if (geometries != null)
				{
					var temp = new object[geometries.Length];
					for (var index = 0; index < geometries.Length; index++)
					{
						var geometry = geometries[index];
						if (!TryParseGeometry((RavenJObject)geometry, out temp[index]))
							return false;
					}
					result = _shapeConverter.ToGeometryCollection(temp);
					return true;
				}
			}
			return false;
		}
		private bool TryParseMultiPolygon(RavenJObject obj, out object result)
		{
			RavenJToken coord;
			if (obj.TryGetValue("coordinates", out coord))
			{
				var coordinates = coord as RavenJArray;
				CoordinateInfo[][][] co;
				if (coordinates != null && TryParseCoordinateArrayArrayArray(coordinates, out co))
				{
					result = _shapeConverter.ToMultiPolygon(co);
					return true;
				}
			}
			result = null;
			return false;
		}
		private bool TryParseFeature(RavenJObject obj, out object result)
		{
			string typeString;
			if (TryParseTypeString(obj, out typeString) && typeString.ToLowerInvariant() == "feature")
			{
				RavenJToken geometry;
				object geo;
				if (obj.TryGetValue("geometry", out geometry) && TryParseGeometry((RavenJObject)geometry, out geo))
				{
					RavenJToken prop;
					Dictionary<string, object> pr = null;
					if (obj.TryGetValue("properties", out prop) && prop is RavenJObject)
					{
						var props = (RavenJObject)prop;
						if (props.Count > 0)
						{
							pr = Enumerable.ToDictionary(props, x => x.Key, x => SantizeRavenJObjects(x.Value));
						}
					}

					object id = null;
					RavenJToken idToken;
					if (obj.TryGetValue("id", out idToken))
					{
						id = SantizeRavenJObjects(idToken);
					}

					result = _shapeConverter.ToFeature(geo, id, pr);
					return true;
				}
			}
			result = null;
			return false;
		}
            private static RavenJObject CreateJsonDocumentFromLuceneDocument(Document document)
            {
                var field = document.GetField(Constants.ReduceValueFieldName);
                if (field != null)
                    return RavenJObject.Parse(field.StringValue);

                var ravenJObject = new RavenJObject();

                foreach (var fieldable in document.GetFields())
                {
                    var stringValue = GetStringValue(fieldable);
                    RavenJToken value;
                    if (ravenJObject.TryGetValue(fieldable.Name, out value) == false)
                    {
                        ravenJObject[fieldable.Name] = stringValue;
                    }
                    else
                    {
                        var ravenJArray = value as RavenJArray;
                        if (ravenJArray != null)
                        {
                            ravenJArray.Add(stringValue);
                        }
                        else
                        {
                            ravenJArray = new RavenJArray { value, stringValue };
                            ravenJObject[fieldable.Name] = ravenJArray;
                        }
                    }
                }
                return ravenJObject;
            }
Пример #57
0
			public void Write(RavenJObject result)
			{
				if (properties == null)
				{
					GetPropertiesAndWriteCsvHeader(result, out doIncludeId);
					Debug.Assert(properties != null);
				}

                if ( doIncludeId )
                {
                    RavenJToken token;
                    if (result.TryGetValue("@metadata", out token))
                    {
                        var metadata = token as RavenJObject;
                        if (metadata != null)
                        {
                            if (metadata.TryGetValue("@id", out token))
                            {
                                OutputCsvValue(token.Value<string>());                               
                            }
                            writer.Write(',');
                        }
                    }
                }

				foreach (var property in properties)
				{
					var token = result.SelectToken(property);
					if (token != null)
					{
						switch (token.Type)
						{
							case JTokenType.Null:
								break;

							case JTokenType.Array:
							case JTokenType.Object:
                                OutputCsvValue(token.ToString(Formatting.None));
								break;

							default:
                                OutputCsvValue(token.Value<string>());
								break;
						}
					}

					writer.Write(',');
				}

				writer.WriteLine();
			}
Пример #58
0
            private void GetPropertiesAndWriteCsvHeader(RavenJObject result, out bool includeId)
			{
                includeId = false;
				properties = DocumentHelpers.GetPropertiesFromJObject(result,
					parentPropertyPath: "",
					includeNestedProperties: true,
					includeMetadata: false,
					excludeParentPropertyNames: true).ToList();

                RavenJToken token;
                if (result.TryGetValue("@metadata", out token))
                {
                    var metadata = token as RavenJObject;
                    if (metadata != null)
                    {
                        if (metadata.TryGetValue("@id", out token))
                        {
                            OutputCsvValue("@id");
                            writer.Write(',');

                            includeId = true;
                        }
                    }
                }

				foreach (var property in properties)
				{
                    OutputCsvValue(property);
                    writer.Write(',');
				}
				writer.WriteLine();
			}
            private static RavenJObject CreateJsonDocumentFromLuceneDocument(Document document)
            {
                var field = document.GetField(Constants.ReduceValueFieldName);
                if (field != null)
                    return RavenJObject.Parse(field.StringValue);

                var ravenJObject = new RavenJObject();

                var fields = document.GetFields();
                var arrayMarkers = fields
                    .Where(x => x.Name.EndsWith("_IsArray"))
                    .Select(x => x.Name)
                    .ToList();

                foreach (var fieldable in fields)
                {
                    var stringValue = GetStringValue(fieldable);
                    var isArrayMarker = fieldable.Name.EndsWith("_IsArray");
                    var isArray = !isArrayMarker && arrayMarkers.Contains(fieldable.Name + "_IsArray");

                    RavenJToken token;
                    var isJson = RavenJToken.TryParse(stringValue, out token);

                    RavenJToken value;
                    if (ravenJObject.TryGetValue(fieldable.Name, out value) == false)
                    {
                        if (isArray)
                            ravenJObject[fieldable.Name] = new RavenJArray { isJson ? token : stringValue };
                        else if (isArrayMarker)
                        {
                            var fieldName = fieldable.Name.Substring(0, fieldable.Name.Length - 8);
                            ravenJObject[fieldable.Name] = isJson ? token : stringValue;
                            ravenJObject[fieldName] = new RavenJArray();
                        }
                        else
                            ravenJObject[fieldable.Name] = isJson ? token : stringValue;
                    }
                    else
                    {
                        var ravenJArray = value as RavenJArray;
                        if (ravenJArray != null)
                            ravenJArray.Add(isJson ? token : stringValue);
                        else
                        {
                            ravenJArray = new RavenJArray { value, isJson ? token : stringValue };
                            ravenJObject[fieldable.Name] = ravenJArray;
                        }
                    }
                }
                return ravenJObject;
            }