예제 #1
0
		public void CloningTestsStoresValues()
		{
			var f = new RavenJObject();
			f.Add("test", "Test");
			f.Add("2nd", "second");
			Assert.True(f.Count == 2);
		}
예제 #2
0
        public void can_update_a_doc_within_transaction_scope()
        {
            using (var documentStore = NewDocumentStore(requestedStorage: "esent"))
            {
                EnsureDtcIsSupported(documentStore);

                var id1 = Guid.NewGuid();
                RavenJObject dummy = null;

                using (TransactionScope trnx = new TransactionScope())
                {
                    using (var session = documentStore.OpenSession())
                    {
                        dummy = new RavenJObject();
                        dummy.Add("Name", "This is the object content");
                        dummy.Add("Id", RavenJToken.FromObject(id1));
                        session.Store(dummy);
                        session.SaveChanges();

                    }
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(dummy);
                        session.SaveChanges();
                    }
                    trnx.Complete();
                }
            }
        }
예제 #3
0
        public static RavenJToken HandleConflictDocuments(RavenJObject metadata)
        {
            if (metadata == null)
                return null;

            if (metadata.ContainsKey(Constants.RavenReplicationConflictDocument))
                metadata.Add(Constants.RavenReplicationConflictDocumentForcePut, true);

            if (metadata.ContainsKey(Constants.RavenReplicationConflict))
                metadata.Add(Constants.RavenReplicationConflictSkipResolution, true);

            return metadata;
        }
예제 #4
0
		public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
		{
			int count = 0;
			foreach (var partition in rows.Partition(Constants.BatchSize))
			{
				var cmds = new List<ICommandData>();
				foreach (var votesForPost in partition.GroupBy(row => row["PostId"]))
				{
					var votes = new List<RavenJToken>();
					foreach (var row in votesForPost)
					{
						var vote = new RavenJObject(new[]
						{
							new KeyValuePair<string, RavenJToken>("VoteTypeId", new RavenJValue(row["VoteTypeId"])),
							new KeyValuePair<string, RavenJToken>("CreationDate", new RavenJValue(row["CreationDate"]))
						});

						switch ((long) row["VoteTypeId"])
						{
							case 5L:
								vote.Add("UserId", new RavenJValue("users/" + row["UserId"]));
								break;
							case 9L:
								vote.Add("BountyAmount", new RavenJValue(row["BountyAmount"]));
								break;
						}
						votes.Add(vote);
					}
					cmds.Add(new PatchCommandData
					{
						Key = "posts/" + votesForPost.Key,
						Patches = new[]
						{
							new PatchRequest
							{
								Name = "Votes",
								Type = PatchCommandType.Set,
								Value = new RavenJArray(votes)
							},
						}
					});
				}
				count++;

				WriteCommandsTo("Votes #" + count.ToString("00000") + ".json", cmds);

			}
			yield break;
		}
예제 #5
0
		public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
		{
			int count = 0;
			foreach (var partitionedRows in rows.Partition(Constants.BatchSize))
			{
				List<PutCommandData> commands = new List<PutCommandData>();

				foreach(var row in partitionedRows)
				{
					RavenJObject obj = new RavenJObject();
					
					foreach(object key in row.Keys)
					{
						obj.Add((string)key, RavenJToken.FromObject(row[key]));
					}
					 
					commands.Add(new PutCommandData()
					{
						Document = obj,
						Metadata = new RavenJObject
						{
							{"Raven-Entity-Name", new RavenJValue(collection)}
						},
						Key = generateKey(obj)
					});
				}
								
				count++;
				WriteCommandsTo(collection + " #" + count.ToString("00000") + ".json", commands);

			}
			yield break;
		}
        protected RavenSessionStateTest()
        {
            MockDocumentSession = new Mock<IDocumentSession>();
            MockDocumentStore = new Mock<IDocumentStore>();
            MockSyncAdvancedSessionOperation = new Mock<ISyncAdvancedSessionOperation>();

            MockDocumentSession.SetupGet(cmd => cmd.Advanced).Returns(MockSyncAdvancedSessionOperation.Object);
            MockDocumentStore.Setup(cmd => cmd.OpenSession()).Returns(MockDocumentSession.Object);

            Subject = new RavenSessionStateStoreProvider{ApplicationName = ApplicationName, SessionStateConfig = new SessionStateSection
                {
                    Timeout = Timeout
                }};
            Subject.Initialize("", new NameValueCollection(), MockDocumentStore.Object);
            if (PreExistingSessionStateDocument != null)
            {
                PersistedSessionStateDocument = PreExistingSessionStateDocument.ShallowCopy();
            }
            MockDocumentSession.Setup(cmd => cmd.Load<SessionStateDocument>(It.IsAny<string>()))
                    .Returns(PersistedSessionStateDocument);
            RavenJObject ravenJObject = new RavenJObject();
            ravenJObject.Add("Raven-Expiration-Date", null);
            MockSyncAdvancedSessionOperation.Setup(cmd => cmd.GetMetadataFor(It.IsAny<SessionStateDocument>()))
                .Returns(ravenJObject);
        }
예제 #7
0
        public static RavenJToken DisableVersioning(RavenJObject metadata)
        {
            if (metadata != null)
                metadata.Add(Constants.RavenIgnoreVersioning, true);

            return metadata;
        }
예제 #8
0
 /// <summary>
 /// Translates this instance to a Json object.
 /// </summary>
 /// <returns>RavenJObject representing the command.</returns>
 public RavenJObject ToJson()
 {
     var ret = new RavenJObject
             {
                 {"Key", Key},
                 {"Method", Method},
                 {"Patch", new RavenJObject
                 {
                     { "Script", Patch.Script },
                     { "Values", RavenJObject.FromObject(Patch.Values)}
                 }},
                 {"DebugMode", DebugMode},
                 {"AdditionalData", AdditionalData},
                 {"Metadata", Metadata}
             };
     if (Etag != null)
         ret.Add("Etag", Etag.ToString());
     if (PatchIfMissing != null)
     {
         ret.Add("PatchIfMissing", new RavenJObject
                 {
                     { "Script", PatchIfMissing.Script },
                     { "Values", RavenJObject.FromObject(PatchIfMissing.Values)}
                 });
     }
     return ret;
 }
예제 #9
0
		/// <summary>
		/// Translates this instance to a Json object.
		/// </summary>
		/// <returns>RavenJObject representing the command.</returns>
		public RavenJObject ToJson()
		{
			var ret = new RavenJObject
			       	{
						{"Key", Key},
						{"Method", Method},
						{"Patches", new RavenJArray(Patches.Select(x => x.ToJson()))},
						{"Metadata", Metadata},
						{"AdditionalData", AdditionalData},
						{"SkipPatchIfEtagMismatch", SkipPatchIfEtagMismatch}
					};
			if (Etag != null)
				ret.Add("Etag", Etag.ToString());
			if (PatchesIfMissing != null && PatchesIfMissing.Length > 0)
				ret.Add("PatchesIfMissing", new RavenJArray(PatchesIfMissing.Select(x => x.ToJson())));
			return ret;
		}
예제 #10
0
		/// <summary>
		/// Translate this instance to json
		/// </summary>
		public RavenJObject ToJson()
		{
			var jObject = new RavenJObject
			              	{
			              		{"Type", new RavenJValue(Type.ToString())},
			              		{"Value", Value},
			              		{"Name", new RavenJValue(Name)}
			              	};
			if (Position != null)
				jObject.Add("Position", new RavenJValue(Position.Value));
			if (Nested != null)
				jObject.Add("Nested",  new RavenJArray(Nested.Select(x => x.ToJson())));
			if (AllPositions != null)
				jObject.Add("AllPositions", new RavenJValue(AllPositions.Value));
			if (PrevVal != null)
				jObject.Add("PrevVal", PrevVal);
			return jObject;
		}
 public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
 {
     var document = entityInstance as IDocument;
     if (document == null) {
         return false;
     }
     metadata.Add("IsRoutable", true);
     return true;
 }
 public ReleaseItemExclusive_when_lock_id_matches()
 {
     RavenJObject ravenJObject = new RavenJObject();
     ravenJObject.Add("Raven-Expiration-Date", null);
     MockSyncAdvancedSessionOperation.Setup(cmd => cmd.GetMetadataFor(It.IsAny<SessionStateDocument>()))
         .Returns(ravenJObject);
     //call ReleaseItemExclusive with matching lockId
        Subject.ReleaseItemExclusive(null, SessionId, LockIdExisting);
        ExpiryExisting = DateTime.UtcNow;
 }
예제 #13
0
파일: Read.cs 프로젝트: jamesfarrer/docs
        public override void OnRead(string key, RavenJObject document, RavenJObject metadata, ReadOperation operation, TransactionInformation transactionInformation)
        {
            var linkName = metadata.Value<string>("Raven-Link-Name");
            var link = metadata.Value<string>("Raven-Link");
            if (link == null)
                return;

            var linkedDocument = Database.Get(link, transactionInformation);
            document.Add(linkName, linkedDocument.ToJson());
        }
        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);
        }
예제 #15
0
		public void Store(object entity, string id)
		{
			var metadata = new RavenJObject();

			var tag = documentStore.Conventions.GetTypeTagName(entity.GetType());
			if (tag != null)
				metadata.Add(Constants.RavenEntityName, tag);

			var data = entityToJson.ConvertEntityToJson(id, entity, metadata);
			operation.Write(id, metadata, data);
		}
예제 #16
0
		/// <summary>
		/// Translate this instance to a Json object.
		/// </summary>
		public RavenJObject ToJson()
		{
			var ret = new RavenJObject
			       	{
						{"Key", Key},
						{"Method", Method},
						{"Patches", new RavenJArray(Patches.Select(x => x.ToJson()))}
			       	};
			if (Etag != null)
				ret.Add("Etag", Etag.ToString());
			return ret;
		}
        private string SaveConflictedItem(string id, RavenJObject metadata, TExternal incoming, Etag existingEtag)
        {
            metadata[Constants.RavenReplicationConflictDocument] = true;
            var newDocumentConflictId = id + "/conflicts/" + GetReplicationIdentifier(metadata);

            metadata.Add(Constants.RavenReplicationConflict, RavenJToken.FromObject(true));
            AddWithoutConflict(
                newDocumentConflictId,
                null, // we explicitly want to overwrite a document if it already exists, since it  is known uniuque by the key
                metadata,
                incoming);
            return(newDocumentConflictId);
        }
예제 #18
0
		/// <summary>
		/// Translate this instance to a Json object.
		/// </summary>
		public RavenJObject ToJson()
		{
			var ret = new RavenJObject
			          	{
			          		{"Key", Key},
							{"Method", Method},
							{"Document", Document},
							{"Metadata", Metadata}
			          	};
			if (Etag != null)
				ret.Add("Etag", Etag.ToString());
			return ret;
		}
예제 #19
0
        /// <summary>
        /// Translates this instance to a Json object.
        /// </summary>
        /// <returns>RavenJObject representing the command.</returns>
        public RavenJObject ToJson()
        {
            var ret = new RavenJObject
            {
                { "Key", Key },
                { "Method", Method },
                { "Patches", new RavenJArray(Patches.Select(x => x.ToJson())) },
                { "Metadata", Metadata },
                { "AdditionalData", AdditionalData },
                { "SkipPatchIfEtagMismatch", SkipPatchIfEtagMismatch }
            };

            if (Etag != null)
            {
                ret.Add("Etag", Etag.ToString());
            }
            if (PatchesIfMissing != null && PatchesIfMissing.Length > 0)
            {
                ret.Add("PatchesIfMissing", new RavenJArray(PatchesIfMissing.Select(x => x.ToJson())));
            }
            return(ret);
        }
예제 #20
0
        private void StoreInternal(object entity, Guid?etag, string id, bool forceConcurrencyCheck)
        {
            if (null == entity)
            {
                throw new ArgumentNullException("entity");
            }

            DocumentMetadata value;

            if (entitiesAndMetadata.TryGetValue(entity, out value))
            {
                value.ETag = etag ?? value.ETag;
                value.ForceConcurrencyCheck = forceConcurrencyCheck;
                return;
            }

            if (id == null)
            {
                if (GenerateDocumentKeysOnStore)
                {
                    id = GenerateEntityIdOnTheClient.GenerateDocumentKeyForStorage(entity);
                }
                else
                {
                    RememberEntityForDocumentKeyGeneration(entity);
                }
            }
            else
            {
                // Store it back into the Id field so the client has access to to it
                GenerateEntityIdOnTheClient.TrySetIdentity(entity, id);
            }

            // we make the check here even if we just generated the key
            // users can override the key generation behavior, and we need
            // to detect if they generate duplicates.
            AssertNoNonUniqueInstance(entity, id);

            var metadata = new RavenJObject();
            var tag      = documentStore.Conventions.GetTypeTagName(entity.GetType());

            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }
            if (id != null)
            {
                knownMissingIds.Remove(id);
            }
            StoreEntityInUnitOfWork(id, entity, etag, metadata, forceConcurrencyCheck);
        }
예제 #21
0
            public override void OnRead(string key, RavenJObject document, RavenJObject metadata, ReadOperation operation, TransactionInformation transactionInformation)
            {
                var linkName = metadata.Value <string>("Raven-Link-Name");
                var link     = metadata.Value <string>("Raven-Link");

                if (link == null)
                {
                    return;
                }

                var linkedDocument = Database.Get(link, transactionInformation);

                document.Add(linkName, linkedDocument.ToJson());
            }
예제 #22
0
 /// <summary>
 /// Translates this instance to a Json object.
 /// </summary>
 /// <returns>RavenJObject representing the command.</returns>
 public RavenJObject ToJson()
 {
     var ret = new RavenJObject
     {
         {"Key", Key},
         {"Method", Method},
         {"Document", Document},
         {"Metadata", Metadata},
         {"AdditionalData", AdditionalData}
     };
     if (Etag != null)
         ret.Add("Etag", Etag.ToString());
     return ret;
 }
예제 #23
0
            private static void ReplaceValues(RavenJObject target, RavenJObject source)
            {
                string[] targetKeys = target.Keys.ToArray();

                foreach (string key in targetKeys)
                {
                    target.Remove(key);
                }

                foreach (string key in source.Keys)
                {
                    target.Add(key, source[key]);
                }
            }
예제 #24
0
		public void Store(object entity, string id)
		{
			var metadata = new RavenJObject();

			var tag = documentStore.Conventions.GetDynamicTagName(entity);
			if (tag != null)
				metadata.Add(Constants.RavenEntityName, tag);

			var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

			OnBeforeEntityInsert(id, data, metadata);

			Operation.Write(id, metadata, data);
		}
예제 #25
0
            public Task ImportAsync()
            {
                var batch = new List <RavenJObject>();

                while (enumerator.MoveNext())
                {
                    var record   = enumerator.Current;
                    var document = new RavenJObject();
                    var id       = Guid.NewGuid().ToString("N");
                    foreach (var column in header.Values)
                    {
                        if (string.Compare("id", column, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            id = record[column];
                        }
                        else
                        {
                            document[column] = record[column];
                        }
                    }

                    var metadata = new RavenJObject {
                        { "Raven-Entity-Name", entity }
                    };
                    document.Add("@metadata", metadata);
                    metadata.Add("@id", id);

                    batch.Add(document);

                    if (batch.Count >= BatchSize)
                    {
                        return(FlushBatch(batch)
                               .ContinueWith(t => t.IsCompleted ? ImportAsync() : t)
                               .Unwrap());
                    }
                }

                if (batch.Count > 0)
                {
                    return(FlushBatch(batch)
                           .ContinueWith(t => t.IsCompleted ? ImportAsync() : t)
                           .Unwrap());
                }

                output(String.Format("Imported {0:#,#;;0} documents in {1:#,#;;0} ms", totalCount, sw.ElapsedMilliseconds));
                taskModel.TaskStatus = TaskStatus.Ended;

                return(new CompletedTask());
            }
        public void Initialize()
        {
            MockDocumentStore = new Mock<IDocumentStore>();
            MockHostingProvider = new Mock<IHostingProvider>();
            MockDocumentSession = new Mock<IDocumentSession>();
            MockAdvancedSessionOperation = new Mock<ISyncAdvancedSessionOperation>();

            MockDocumentStore.Setup(cmd => cmd.OpenSession()).Returns(MockDocumentSession.Object);
            MockDocumentSession.SetupGet(cmd => cmd.Advanced).Returns(MockAdvancedSessionOperation.Object);

            RavenJObject ravenJObject = new RavenJObject();
            ravenJObject.Add("Raven-Expiration-Date", null);
            MockAdvancedSessionOperation.Setup(cmd => cmd.GetMetadataFor(It.IsAny<SessionStateDocument>()))
                .Returns(ravenJObject);
        }
예제 #27
0
        /// <summary>
        /// Translate this instance to a Json object.
        /// </summary>
        public RavenJObject ToJson()
        {
            var ret = new RavenJObject
            {
                { "Key", Key },
                { "Method", Method },
                { "Patches", new RavenJArray(Patches.Select(x => x.ToJson())) }
            };

            if (Etag != null)
            {
                ret.Add("Etag", Etag.ToString());
            }
            return(ret);
        }
예제 #28
0
        /// <summary>
        /// Translates this instance to a Json object.
        /// </summary>
        /// <returns>RavenJObject representing the command.</returns>
        public RavenJObject ToJson()
        {
            var ret = new RavenJObject
            {
                { "Key", Id },
                { "Method", Method },
                { "Document", Document },
                { "AdditionalData", AdditionalData }
            };

            if (Etag != null)
            {
                ret.Add("Etag", Etag);
            }
            return(ret);
        }
예제 #29
0
        public async Task StoreAsync(object entity, string id)
        {
            var metadata = new RavenJObject();
            var tag      = documentStore.Conventions.GetDynamicTagName(entity);

            if (tag != null)
            {
                metadata.Add(Constants.Headers.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            await Operation.WriteAsync(id, metadata, data).ConfigureAwait(false);
        }
예제 #30
0
        /// <summary>
        /// Translate this instance to a Json object.
        /// </summary>
        public RavenJObject ToJson()
        {
            var ret = new RavenJObject
            {
                { "Key", Key },
                { "Method", Method },
                { "Document", Document },
                { "Metadata", Metadata }
            };

            if (Etag != null)
            {
                ret.Add("Etag", Etag.ToString());
            }
            return(ret);
        }
        private static RavenJObject BuildSkipDocument()
        {
            var metadata = new RavenJObject();

            metadata.Add("@id", Constants.BulkImportHeartbeatDocKey);
            var skipDoc = new JsonDocument
            {
                Key        = Constants.BulkImportHeartbeatDocKey,
                DataAsJson = RavenJObject.FromObject(new
                {
                    LastHearbeatSent = SystemTime.UtcNow
                }),
                Metadata = metadata
            };

            return(skipDoc.ToJson());
        }
예제 #32
0
        public void Store(object entity, string id)
        {
            var metadata = new RavenJObject();

            var tag = documentStore.Conventions.GetTypeTagName(entity.GetType());

            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            operation.Write(id, metadata, data);
        }
예제 #33
0
		public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
		{
			if (metadata[Constants.EnsureUniqueConstraints] != null)
			{
				return true;
			}

			var type = entityInstance.GetType();

			var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

			if (properties != null)
			{
				metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x => x.Name).ToArray()));
			}

			return true;
		}
예제 #34
0
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            if (metadata[Constants.EnsureUniqueConstraints] != null)
            {
                return(true);
            }

            var type = entityInstance.GetType();

            var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

            if (properties != null)
            {
                metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x => x.Name).ToArray()));
            }

            return(true);
        }
예제 #35
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);
        }
		public bool BeforeStore(string key, object entityInstance, RavenJObject metadata)
		{
			if (metadata[Constants.EnsureUniqueConstraints] != null)
			{
				return true;
			}

			var type = entityInstance.GetType();

			string[] properties;
			if (!TypeProperties.TryGetValue(type, out properties))
			{
				properties = TypeProperties.GetOrAdd(type, type.GetProperties().Where(p => Attribute.IsDefined(p, typeof(UniqueConstraintAttribute))).Select(x => x.Name).ToArray());
			}

			metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties));

			return true;
		}
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata)
        {
            if (metadata[Constants.EnsureUniqueConstraints] != null)
            {
                return(true);
            }

            var type = entityInstance.GetType();

            string[] properties;
            if (!TypeProperties.TryGetValue(type, out properties))
            {
                properties = TypeProperties.GetOrAdd(type, type.GetProperties().Where(p => Attribute.IsDefined(p, typeof(UniqueConstraintAttribute))).Select(x => x.Name).ToArray());
            }

            metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties));

            return(true);
        }
예제 #38
0
        private void ImportWorksheet(string worksheetName)
        {
            SetDocumentKeyPrefix(worksheetName);
            IList<string> columns = _excelQueryFactory.GetColumnNames(worksheetName).ToList();

            foreach (Row row in _excelQueryFactory.Worksheet(worksheetName))
            {
                RavenJObject doc = new RavenJObject();
                foreach (string column in columns)
                {
                    if (column.ToLower() == "id")
                        doc["Id"] = string.Format("{0}/{1}", worksheetName, row[column]);
                    else
                        doc.Add(column, new RavenJValue(row[column]));
                }
                _session.Store(doc);
                _session.Advanced.GetMetadataFor(doc)[Constants.RavenEntityName] = worksheetName;
            }
            _session.SaveChanges();
        }
예제 #39
0
        /// <summary>
        /// Translate this instance to a Json object.
        /// </summary>
        public RavenJObject ToJson()
        {
            var ret = new RavenJObject
            {
                { "Key", Key },
                { "Method", Method },
                { "Patch", new RavenJObject
                  {
                      { "Script", Patch.Script },
                      { "Values", RavenJObject.FromObject(Patch.Values) }
                  } },
                { "DebugMode", DebugMode },
                { "AdditionalData", AdditionalData }
            };

            if (Etag != null)
            {
                ret.Add("Etag", Etag.ToString());
            }
            return(ret);
        }
		public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
		{
			if (metadata[Constants.EnsureUniqueConstraints] != null)
			{
				return true;
			}

			var type = entityInstance.GetType();

			var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

			if (properties != null)
			{
                metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x =>
                    {
                        var att = ((UniqueConstraintAttribute) Attribute.GetCustomAttribute(x, typeof (UniqueConstraintAttribute)));
                        return RavenJObject.FromObject(new { x.Name, att.CaseInsensitive });
                    })));
			}

			return true;
		}
예제 #41
0
        public void Store(object entity, string id)
        {
            if (Operation.IsAborted)
            {
                throw new InvalidOperationException("Bulk insert has been aborted or the operation was timed out");
            }

            var metadata = new RavenJObject();

            var tag = documentStore.Conventions.GetDynamicTagName(entity);

            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            Operation.Write(id, metadata, data);
        }
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            if (metadata[Constants.EnsureUniqueConstraints] != null)
            {
                return(true);
            }

            var type = entityInstance.GetType();

            var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

            if (properties != null)
            {
                metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x =>
                {
                    var att = ((UniqueConstraintAttribute)Attribute.GetCustomAttribute(x, typeof(UniqueConstraintAttribute)));
                    return(RavenJObject.FromObject(new { x.Name, att.CaseInsensitive }));
                })));
            }

            return(true);
        }
예제 #43
0
		/// <summary>
		/// Filters the headers from unwanted headers
		/// </summary>
		/// <param name="self">The self.</param>
		/// <param name="isServerDocument">if set to <c>true</c> [is server document].</param>
		/// <returns></returns>public static RavenJObject FilterHeaders(this System.Collections.Specialized.NameValueCollection self, bool isServerDocument)
		public static RavenJObject FilterHeaders(this IDictionary<string, IList<string>> self, bool isServerDocument)
		  {
			  var metadata = new RavenJObject();
			foreach (var header in self)
			{
				if (header.Key.StartsWith("Temp"))
					continue;
				if (HeadersToIgnoreClient.Contains(header.Key))
					continue;
				if(isServerDocument && HeadersToIgnoreServerDocument.Contains(header.Key))
					continue;
				var values = header.Value;
				var headerName = CaptureHeaderName(header.Key);
				if (values.Count == 1)
					metadata.Add(headerName, GetValue(values[0]));
				else
					metadata.Add(headerName, new RavenJArray(values.Select(GetValue)));
			}
			return metadata;
		}
예제 #44
0
        public async Task CanSearchByNumericMetadataFields()
        {
            using (var store = NewStore())
            {
                using (var session = store.OpenAsyncSession())
                {
                    var metadata = new RavenJObject();

                    metadata.Add("int", 5);
                    metadata.Add("long", 5L);
                    metadata.Add("float", 5.0f);
                    metadata.Add("double", 5.0);

                    metadata.Add("uint", 5u);
                    metadata.Add("ulong", 5UL);
                    metadata.Add("short", (short)5);
                    metadata.Add("ushort", (ushort)5);
                    metadata.Add("decimal", 5m);

                    session.RegisterUpload("test-1.file", CreateRandomFileStream(10), metadata);

                    var metadata2 = new RavenJObject();

                    metadata2.Add("int", 10);
                    metadata2.Add("long", 10L);
                    metadata2.Add("float", 10.0f);
                    metadata2.Add("double", 10.0);

                    metadata2.Add("uint", 10u);
                    metadata2.Add("ulong", 10UL);
                    metadata2.Add("short", (short)10);
                    metadata2.Add("ushort", (ushort)10);
                    metadata2.Add("decimal", 10m);

                    session.RegisterUpload("test-2.file", CreateRandomFileStream(10), metadata2);

                    await session.SaveChangesAsync();
                }

                var metadataKeys = new[]
                {
                    "int", "long", "float", "double", "uint", "ulong", "short", "ushort", "decimal"
                };

                foreach (var key in metadataKeys)
                {
                    using (var session = store.OpenAsyncSession())
                    {
                        Assert.Equal(1, (await session.Query().WhereEquals(key, 5).ToListAsync()).Count);
                        Assert.Equal(1, (await session.Query().WhereGreaterThan(key, 5).ToListAsync()).Count);
                        Assert.Equal(2, (await session.Query().WhereGreaterThanOrEqual(key, 5).ToListAsync()).Count);
                        Assert.Equal(1, (await session.Query().WhereLessThan(key, 10).ToListAsync()).Count);
                        Assert.Equal(2, (await session.Query().WhereLessThanOrEqual(key, 10).ToListAsync()).Count);
                        Assert.Equal(0, (await session.Query().WhereBetween(key, 5, 10).ToListAsync()).Count);
                        Assert.Equal(1, (await session.Query().WhereBetween(key, 0, 10).ToListAsync()).Count);
                        Assert.Equal(1, (await session.Query().WhereBetween(key, 5, 20).ToListAsync()).Count);
                        Assert.Equal(2, (await session.Query().WhereBetweenOrEqual(key, 5, 10).ToListAsync()).Count);
                    }
                }
            }
        }
예제 #45
0
파일: RavenDb_1977.cs 프로젝트: mow/ravendb
        public void CanDetectDeleteMetadataChanges()
        {
            using (var store = NewDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new UserData
                    {
                        Id     = 123,
                        Name   = "user1",
                        Salary = 12.5,
                        Date   = DateTime.Now
                    }, "UserDatas/1");
                    session.Store(new UserData
                    {
                        Id     = 1234,
                        Name   = "user2",
                        Salary = 12.51,
                        Date   = new DateTime(2014, 1, 1)
                    }, "UserDatas/2");
                    session.Store(new UserData
                    {
                        Id     = 1235,
                        Name   = "user3",
                        Salary = 12.45,
                        Date   = new DateTime(2014, 1, 2)
                    }, "UserDatas/3");
                    session.SaveChanges();
                }

                const string propName1 = "test1";
                const string propName2 = "test2";

                using (var session = store.OpenSession())
                {
                    var          userdata1 = session.Load <UserData>("UserDatas/1");
                    RavenJObject metadata  = session.Advanced.GetMetadataFor(userdata1);
                    metadata.Add(propName1, null);
                    metadata.Add(propName2, null);
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    var          userdata1 = session.Load <UserData>("UserDatas/1");
                    RavenJObject metadata  = session.Advanced.GetMetadataFor(userdata1);

                    RavenJToken value;
                    metadata.TryGetValue(propName1, out value);
                    Assert.NotNull(value);
                    metadata.TryGetValue(propName2, out value);
                    Assert.NotNull(value);

                    metadata.Remove(propName1);
                    metadata.Remove(propName2);

                    IDictionary <string, DocumentsChanges[]> changes3 = session.Advanced.WhatChanged();
                    Assert.Equal(1, changes3.Count);
                    Assert.True(changes3.ContainsKey("UserDatas/1"));

                    DocumentsChanges[] data3;
                    changes3.TryGetValue("UserDatas/1", out data3);
                    Assert.NotNull(data3);
                    Assert.Equal(2, data3.Length);
                    session.SaveChanges();
                }
            }
        }
예제 #46
0
            public ReplicationStatisticsRecorderScope(string name, RavenJObject record)
            {
                this.record = record;
                records = new RavenJArray();

                record.Add("Name", name);
                record.Add("Records", records);

                watch = Stopwatch.StartNew();
            }
예제 #47
0
파일: Index.cs 프로젝트: jon-adams/ravendb
		public static RavenJObject CreateDocumentFromFields(Document document, FieldsToFetch fieldsToFetch)
		{
			var documentFromFields = new RavenJObject();
			var fields = fieldsToFetch.Fields;
			if (fieldsToFetch.FetchAllStoredFields)
				fields = fields.Concat(document.GetFields().Select(x => x.Name));


			var q = fields
				.Distinct()
				.SelectMany(name => document.GetFields(name) ?? new Field[0])
				.Where(x => x != null)
				.Where(
					x =>
					x.Name.EndsWith("_IsArray") == false &&
					x.Name.EndsWith("_Range") == false &&
					x.Name.EndsWith("_ConvertToJson") == false)
				.Select(fld => CreateProperty(fld, document))
				.GroupBy(x => x.Key)
				.Select(g =>
				{
					if (g.Count() == 1 && document.GetField(g.Key + "_IsArray") == null)
					{
						return g.First();
					}
					var ravenJTokens = g.Select(x => x.Value).ToArray();
					return new KeyValuePair<string, RavenJToken>(g.Key, new RavenJArray((IEnumerable)ravenJTokens));
				});
			foreach (var keyValuePair in q)
			{
				documentFromFields.Add(keyValuePair.Key, keyValuePair.Value);
			}
			return documentFromFields;
		}
        public RavenJToken DisableVersioning(RavenJObject metadata)
        {
            metadata.Add(Constants.RavenIgnoreVersioning, true);

            return(metadata);
        }
예제 #49
0
        private void ReplicateAttachment(IStorageActionsAccessor actions, string id, RavenJObject metadata, byte[] data, Guid lastEtag, string src)
        {
            var existingAttachment = actions.Attachments.GetAttachment(id);

            if (existingAttachment == null)
            {
                log.Debug("New attachment {0} replicated successfully from {1}", id, src);
                actions.Attachments.AddAttachment(id, Guid.Empty, data, metadata);
                return;
            }

            var existingDocumentIsInConflict = existingAttachment.Metadata[ReplicationConstants.RavenReplicationConflict] != null;

            if (existingDocumentIsInConflict == false &&                                // if the current document is not in conflict, we can continue without having to keep conflict semantics
                (IsDirectChildOfCurrentAttachment(existingAttachment, metadata)))       // this update is direct child of the existing doc, so we are fine with overwriting this
            {
                log.Debug("Existing document {0} replicated successfully from {1}", id, src);
                actions.Attachments.AddAttachment(id, null, data, metadata);
                return;
            }

            if (ReplicationConflictResolvers.Any(replicationConflictResolver => replicationConflictResolver.TryResolve(id, metadata, data, existingAttachment)))
            {
                actions.Attachments.AddAttachment(id, null, data, metadata);
                return;
            }

            var newDocumentConflictId = id + "/conflicts/" +
                                        metadata.Value <string>(ReplicationConstants.RavenReplicationSource) +
                                        "/" + lastEtag;

            metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
            actions.Attachments.AddAttachment(newDocumentConflictId, null, data, metadata);

            if (existingDocumentIsInConflict)             // the existing document is in conflict
            {
                log.Debug("Conflicted document {0} has a new version from {1}, adding to conflicted documents", id, src);

                // just update the current doc with the new conflict document
                existingAttachment.Metadata.Value <RavenJArray>("Conflicts").Add(RavenJToken.FromObject(newDocumentConflictId));
                actions.Attachments.AddAttachment(id, existingAttachment.Etag, existingAttachment.Data, existingAttachment.Metadata);
                return;
            }
            log.Debug("Existing document {0} is in conflict with replicated version from {1}, marking document as conflicted", id, src);

            // we have a new conflict
            // move the existing doc to a conflict and create a conflict document
            var existingDocumentConflictId = id + "/conflicts/" + Database.TransactionalStorage.Id + "/" + existingAttachment.Etag;

            existingAttachment.Metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
            actions.Attachments.AddAttachment(existingDocumentConflictId, null, existingAttachment.Data, existingAttachment.Metadata);
            actions.Attachments.AddAttachment(id, null,
                                              new RavenJObject
            {
                { "Conflicts", new RavenJArray(existingDocumentConflictId, newDocumentConflictId) }
            }.ToBytes(),
                                              new RavenJObject
            {
                { ReplicationConstants.RavenReplicationConflict, true },
                { "@Http-Status-Code", 409 },
                { "@Http-Status-Description", "Conflict" }
            });
        }
        private void ReplicateAttachment(IStorageActionsAccessor actions, string id, RavenJObject metadata, byte[] data, Guid lastEtag, string src)
        {
            var existingAttachment = actions.Attachments.GetAttachment(id);

            if (existingAttachment == null)
            {
                log.Debug("New attachment {0} replicated successfully from {1}", id, src);
                actions.Attachments.AddAttachment(id, Guid.Empty, new MemoryStream(data), metadata);
                return;
            }

            // we just got the same version from the same source - request playback again?
            // at any rate, not an error, moving on
            if (existingAttachment.Metadata.Value <string>(ReplicationConstants.RavenReplicationSource) == metadata.Value <string>(ReplicationConstants.RavenReplicationSource) &&
                existingAttachment.Metadata.Value <int>(ReplicationConstants.RavenReplicationVersion) == metadata.Value <int>(ReplicationConstants.RavenReplicationVersion))
            {
                return;
            }

            var existingDocumentIsInConflict = existingAttachment.Metadata[ReplicationConstants.RavenReplicationConflict] != null;

            if (existingDocumentIsInConflict == false &&                                // if the current document is not in conflict, we can continue without having to keep conflict semantics
                (IsDirectChildOfCurrentAttachment(existingAttachment, metadata)))       // this update is direct child of the existing doc, so we are fine with overwriting this
            {
                log.Debug("Existing document {0} replicated successfully from {1}", id, src);
                actions.Attachments.AddAttachment(id, null, new MemoryStream(data), metadata);
                return;
            }

            if (ReplicationConflictResolvers.Any(replicationConflictResolver => replicationConflictResolver.TryResolve(id, metadata, data, existingAttachment)))
            {
                actions.Attachments.AddAttachment(id, null, new MemoryStream(data), metadata);
                return;
            }

            var newDocumentConflictId = id + "/conflicts/" + HashReplicationIdentifier(metadata, lastEtag);

            metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
            actions.Attachments.AddAttachment(newDocumentConflictId, null, new MemoryStream(data), metadata);

            if (existingDocumentIsInConflict)             // the existing document is in conflict
            {
                log.Debug("Conflicted document {0} has a new version from {1}, adding to conflicted documents", id, src);

                // just update the current doc with the new conflict document
                var conflictArray = existingAttachment.Metadata.Value <RavenJArray>("Conflicts");
                if (conflictArray == null)
                {
                    existingAttachment.Metadata["Conflicts"] = conflictArray = new RavenJArray();
                }

                conflictArray.Add(RavenJToken.FromObject(newDocumentConflictId));
                actions.Attachments.AddAttachment(id, existingAttachment.Etag, existingAttachment.Data(), existingAttachment.Metadata);
                return;
            }
            log.Debug("Existing document {0} is in conflict with replicated version from {1}, marking document as conflicted", id, src);

            // we have a new conflict
            // move the existing doc to a conflict and create a conflict document
            var existingDocumentConflictId = id + "/conflicts/" + HashReplicationIdentifier(existingAttachment.Etag);

            existingAttachment.Metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
            actions.Attachments.AddAttachment(existingDocumentConflictId, null, existingAttachment.Data(), existingAttachment.Metadata);
            var conflictAttachment = new RavenJObject
            {
                { "Conflicts", new RavenJArray(existingDocumentConflictId, newDocumentConflictId) }
            };
            var memoryStream = new MemoryStream();

            conflictAttachment.WriteTo(memoryStream);
            memoryStream.Position = 0;
            actions.Attachments.AddAttachment(id, null,
                                              memoryStream,
                                              new RavenJObject
            {
                { ReplicationConstants.RavenReplicationConflict, true },
                { "@Http-Status-Code", 409 },
                { "@Http-Status-Description", "Conflict" }
            });
        }
예제 #51
0
        public async Task <HttpResponseMessage> LoadCsvFile()
        {
            const int csvImportBatchSize = 512;

            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new Exception(); // divided by zero
            }
            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider).ConfigureAwait(false);

            foreach (var file in provider.Contents)
            {
                var filename = file.Headers.ContentDisposition.FileName.Trim('\"');

                using (var stream = await file.ReadAsStreamAsync().ConfigureAwait(false))
                    using (var textReader = new StreamReader(stream))
                        using (var csvReader = new CsvReader(textReader))
                        {
                            csvReader.Configuration.Delimiter = ",";

                            if (csvReader.Read() == false)
                            {
                                return(GetEmptyMessage());
                            }

                            var headers = csvReader.FieldHeaders;
                            var entity  =
                                Inflector.Pluralize(CSharpClassName.ConvertToValidClassName(Path.GetFileNameWithoutExtension(filename)));
                            if (entity.Length > 0 && char.IsLower(entity[0]))
                            {
                                entity = char.ToUpper(entity[0]) + entity.Substring(1);
                            }

                            var totalCount = 0;
                            var batch      = new List <RavenJObject>();

                            var validColumnIndexes = headers.Select((h, i) => new { Header = h, Index = i })
                                                     .Where(x => x.Header.StartsWith("@") == false)
                                                     .Select(s => s.Index)
                                                     .ToArray();

                            batch.Clear();
                            do
                            {
                                var          record   = csvReader.CurrentRecord;
                                var          document = new RavenJObject();
                                string       id       = null;
                                RavenJObject metadata = null;
                                foreach (var index in validColumnIndexes)
                                {
                                    var column = headers[index];
                                    if (string.IsNullOrEmpty(column))
                                    {
                                        continue;
                                    }
                                    var value = record[index];

                                    if (string.Equals("id", column, StringComparison.OrdinalIgnoreCase))
                                    {
                                        id = value;
                                    }
                                    else if (string.Equals(Constants.RavenEntityName, column, StringComparison.OrdinalIgnoreCase))
                                    {
                                        metadata = metadata ?? new RavenJObject();
                                        metadata[Constants.RavenEntityName] = value;
                                        id = id ?? value + "/";
                                    }
                                    else if (string.Equals(Constants.RavenClrType, column, StringComparison.OrdinalIgnoreCase))
                                    {
                                        metadata = metadata ?? new RavenJObject();
                                        metadata[Constants.RavenClrType] = value;
                                        id = id ?? value + "/";
                                    }
                                    else
                                    {
                                        SetValueInDocumentForColumn(document, column, value);
                                    }
                                }

                                metadata = metadata ?? new RavenJObject {
                                    { "Raven-Entity-Name", entity }
                                };
                                document.Add("@metadata", metadata);
                                metadata.Add("@id", id ?? Guid.NewGuid().ToString());

                                batch.Add(document);
                                totalCount++;

                                if (batch.Count >= csvImportBatchSize)
                                {
                                    await FlushBatch(batch).ConfigureAwait(false);

                                    batch.Clear();
                                }
                            } while (csvReader.Read());

                            if (batch.Count > 0)
                            {
                                await FlushBatch(batch).ConfigureAwait(false);
                            }
                        }
            }

            return(GetEmptyMessage());
        }
예제 #52
0
		private static RavenJObject CreateDocumentFromFields(Document document, IEnumerable<string> fieldsToFetch)
		{
			var documentFromFields = new RavenJObject();
			var q = fieldsToFetch
				.SelectMany(name => document.GetFields(name) ?? new Field[0])
				.Where(x => x != null)
				.Where(
					x =>
					x.Name().EndsWith("_IsArray") == false && 
					x.Name().EndsWith("_Range") == false &&
					x.Name().EndsWith("_ConvertToJson") == false)
				.Select(fld => CreateProperty(fld, document))
				.GroupBy(x => x.Key)
				.Select(g =>
				{
					if (g.Count() == 1 && document.GetField(g.Key + "_IsArray") == null)
					{
						return g.First();
					}
					return new KeyValuePair<string, RavenJToken>(g.Key, new RavenJArray(g.Select(x => x.Value)));
				});
			foreach (var keyValuePair in q)
			{
				documentFromFields.Add(keyValuePair.Key, keyValuePair.Value);
			}
			return documentFromFields;
		}
예제 #53
0
            public Task ImportAsync()
            {
                if (hadError)
                {
                    return(new CompletedTask());
                }
                var batch   = new List <RavenJObject>();
                var columns = header.Values.Where(x => x.StartsWith("@") == false).ToArray();

                while (enumerator.MoveNext())
                {
                    var          record   = enumerator.Current;
                    var          document = new RavenJObject();
                    string       id       = null;
                    RavenJObject metadata = null;
                    foreach (var column in columns)
                    {
                        if (string.IsNullOrEmpty(column))
                        {
                            continue;
                        }

                        try
                        {
                            if (string.Equals("id", column, StringComparison.InvariantCultureIgnoreCase))
                            {
                                id = record[column];
                            }
                            else if (string.Equals("Raven-Entity-Name", column, StringComparison.InvariantCultureIgnoreCase))
                            {
                                metadata = new RavenJObject {
                                    { "Raven-Entity-Name", record[column] }
                                };
                                id = id ?? record[column] + "/";
                            }
                            else
                            {
                                document[column] = SetValueInDocument(record[column]);
                            }
                        }
                        catch (Exception e)
                        {
                            taskModel.ReportError(e);
                            taskModel.ReportError("Import not completed");
                            taskModel.TaskStatus = TaskStatus.Ended;
                            return(new CompletedTask());
                        }
                    }

                    metadata = metadata ?? new RavenJObject {
                        { "Raven-Entity-Name", entity }
                    };
                    document.Add("@metadata", metadata);
                    metadata.Add("@id", id ?? Guid.NewGuid().ToString());

                    batch.Add(document);

                    if (batch.Count >= BatchSize)
                    {
                        return(FlushBatch(batch)
                               .ContinueWith(t =>
                        {
                            if (t.IsFaulted)
                            {
                                Infrastructure.Execute.OnTheUI(() =>
                                {
                                    taskModel.ReportError(t.Exception);
                                    taskModel.ReportError("Import not completed");
                                    taskModel.TaskStatus = TaskStatus.Ended;
                                });

                                return t;
                            }
                            return t.IsCompleted ? ImportAsync() : t;
                        })
                               .Unwrap());
                    }
                }

                if (batch.Count > 0)
                {
                    return(FlushBatch(batch)
                           .ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            Infrastructure.Execute.OnTheUI(() =>
                            {
                                taskModel.ReportError(t.Exception);
                                taskModel.ReportError("Import not completed");
                                taskModel.TaskStatus = TaskStatus.Ended;
                            });

                            return t;
                        }
                        return t.IsCompleted ? ImportAsync() : t;
                    })
                           .Unwrap());
                }

                output(String.Format("Imported {0:#,#;;0} documents in {1:#,#;;0} ms", totalCount, sw.ElapsedMilliseconds));
                taskModel.TaskStatus = TaskStatus.Ended;

                return(new CompletedTask());
            }
예제 #54
0
        private static RavenJObject DisableVersioning(RavenJObject metadata)
        {
            metadata?.Add(Constants.RavenIgnoreVersioning, true);

            return(metadata);
        }
        public RavenJObject DisableVersioning(RavenJObject metadata)
        {
            if (metadata != null)
            {
                metadata.Add(Constants.RavenIgnoreVersioning, true);
            }

            return metadata;
        }
예제 #56
0
        private void StoreInternal(object entity, Guid?etag, string id)
        {
            if (null == entity)
            {
                throw new ArgumentNullException("entity");
            }

            if (entitiesAndMetadata.ContainsKey(entity))
            {
                return;
            }

            if (id == null)
            {
#if !NET_3_5
                if (entity is IDynamicMetaObjectProvider)
                {
                    if (TryGetIdFromDynamic(entity, out id) == false)
                    {
                        id = Conventions.DocumentKeyGenerator(entity);

                        if (id != null)
                        {
                            // Store it back into the Id field so the client has access to to it
                            TrySetIdOnynamic(entity, id);
                        }
                    }
                }
                else
#endif
                {
                    id = GetOrGenerateDocumentKey(entity);

                    TrySetIdentity(entity, id);
                }
            }
            else
            {
                // Store it back into the Id field so the client has access to to it
                TrySetIdentity(entity, id);
            }

            // we make the check here even if we just generated the key
            // users can override the key generation behavior, and we need
            // to detect if they generate duplicates.
            AssertNoNonUniqueInstance(entity, id);

            var tag      = documentStore.Conventions.GetTypeTagName(entity.GetType());
            var metadata = new RavenJObject();
            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }
            entitiesAndMetadata.Add(entity, new DocumentMetadata
            {
                Key              = id,
                Metadata         = metadata,
                OriginalMetadata = new RavenJObject(),
                ETag             = etag,
                OriginalValue    = new RavenJObject()
            });
            if (id != null)
            {
                entitiesByKey[id] = entity;
            }
        }
예제 #57
0
        public async Task <HttpResponseMessage> LoadCsvFile()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new Exception(); // divided by zero
            }
            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (var file in provider.Contents)
            {
                var filename = file.Headers.ContentDisposition.FileName.Trim('\"');

                var stream = await file.ReadAsStreamAsync();

                using (var csvReader = new TextFieldParser(stream))
                {
                    csvReader.SetDelimiters(",");
                    var headers = csvReader.ReadFields();
                    var entity  =
                        Inflector.Pluralize(CSharpClassName.ConvertToValidClassName(Path.GetFileNameWithoutExtension(filename)));
                    if (entity.Length > 0 && char.IsLower(entity[0]))
                    {
                        entity = char.ToUpper(entity[0]) + entity.Substring(1);
                    }

                    var totalCount = 0;
                    var batch      = new List <RavenJObject>();
                    var columns    = headers.Where(x => x.StartsWith("@") == false).ToArray();

                    batch.Clear();
                    while (csvReader.EndOfData == false)
                    {
                        var          record   = csvReader.ReadFields();
                        var          document = new RavenJObject();
                        string       id       = null;
                        RavenJObject metadata = null;
                        for (int index = 0; index < columns.Length; index++)
                        {
                            var column = columns[index];
                            if (string.IsNullOrEmpty(column))
                            {
                                continue;
                            }

                            if (string.Equals("id", column, StringComparison.OrdinalIgnoreCase))
                            {
                                id = record[index];
                            }
                            else if (string.Equals(Constants.RavenEntityName, column, StringComparison.OrdinalIgnoreCase))
                            {
                                metadata = metadata ?? new RavenJObject();
                                metadata[Constants.RavenEntityName] = record[index];
                                id = id ?? record[index] + "/";
                            }
                            else if (string.Equals(Constants.RavenClrType, column, StringComparison.OrdinalIgnoreCase))
                            {
                                metadata = metadata ?? new RavenJObject();
                                metadata[Constants.RavenClrType] = record[index];
                                id = id ?? record[index] + "/";
                            }
                            else
                            {
                                document[column] = SetValueInDocument(record[index]);
                            }
                        }

                        metadata = metadata ?? new RavenJObject {
                            { "Raven-Entity-Name", entity }
                        };
                        document.Add("@metadata", metadata);
                        metadata.Add("@id", id ?? Guid.NewGuid().ToString());

                        batch.Add(document);
                        totalCount++;

                        if (batch.Count >= CsvImportBatchSize)
                        {
                            await FlushBatch(batch);

                            batch.Clear();
                        }
                    }

                    if (batch.Count > 0)
                    {
                        await FlushBatch(batch);
                    }
                }
            }

            return(GetEmptyMessage());
        }
예제 #58
0
	    public async Task<HttpResponseMessage> LoadCsvFile()
	    {

            if (!Request.Content.IsMimeMultipartContent())
                throw new Exception(); // divided by zero

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (var file in provider.Contents)
            {
                var filename = file.Headers.ContentDisposition.FileName.Trim('\"');

                var stream = await file.ReadAsStreamAsync();

                using (var csvReader = new TextFieldParser(stream))
                {
	                csvReader.SetDelimiters(",");
                    var headers = csvReader.ReadFields();
                    var entity =
                        Inflector.Pluralize(CSharpClassName.ConvertToValidClassName(Path.GetFileNameWithoutExtension(filename)));
                    if (entity.Length > 0 && char.IsLower(entity[0]))
                        entity = char.ToUpper(entity[0]) + entity.Substring(1);

                    var totalCount = 0;
                    var batch = new List<RavenJObject>();
                    var columns = headers.Where(x => x.StartsWith("@") == false).ToArray();

                    batch.Clear();
	                while (csvReader.EndOfData == false)
	                {
		                var record = csvReader.ReadFields();
                        var document = new RavenJObject();
                        string id = null;
                        RavenJObject metadata = null;
		                for (int index = 0; index < columns.Length; index++)
		                {
			                var column = columns[index];
			                if (string.IsNullOrEmpty(column))
				                continue;

			                if (string.Equals("id", column, StringComparison.OrdinalIgnoreCase))
			                {
								id = record[index];
			                }
			                else if (string.Equals(Constants.RavenEntityName, column, StringComparison.OrdinalIgnoreCase))
			                {
				                metadata = metadata ?? new RavenJObject();
								metadata[Constants.RavenEntityName] = record[index];
								id = id ?? record[index] + "/";
			                }
			                else if (string.Equals(Constants.RavenClrType, column, StringComparison.OrdinalIgnoreCase))
			                {
				                metadata = metadata ?? new RavenJObject();
								metadata[Constants.RavenClrType] = record[index];
								id = id ?? record[index] + "/";
			                }
			                else
			                {
								document[column] = SetValueInDocument(record[index]);
			                }
		                }

		                metadata = metadata ?? new RavenJObject { { "Raven-Entity-Name", entity } };
                        document.Add("@metadata", metadata);
                        metadata.Add("@id", id ?? Guid.NewGuid().ToString());

                        batch.Add(document);
                        totalCount++;

                        if (batch.Count >= CsvImportBatchSize)
                        {
                            await FlushBatch(batch);
                            batch.Clear();
                        }
                    }

                    if (batch.Count > 0)
                    {
                        await FlushBatch(batch);
                    }
                }

            }

            return GetEmptyMessage();
	    }
예제 #59
0
        public void Run(ISagaInfo sagaInfo)
        {
            PrintHelper.PrintFunctionCallDetails("PopulateSagaUniqueIdentityRecords", sagaInfo);
            var documentStore = new DocumentStore
            {
                Url             = sagaInfo.RavenUrl,
                DefaultDatabase = sagaInfo.DatabaseName
            };

            documentStore.Initialize();

            var       existingSagaUniqueIdentityUniqueValues = DocumentHelper.GetExistingSagaUniqueIdentityUniqueValues(documentStore, sagaInfo);
            var       commandsList   = new List <ICommandData>();
            var       dictionarytest = new Dictionary <string, int>();
            const int pageSize       = 100;

            for (var start = 0; ; start += pageSize)
            {
                var sagaDocuments = documentStore.DatabaseCommands.GetDocuments(start, pageSize);
                foreach (var document in sagaDocuments)
                {
                    if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && DocumentHelper.HasServicebusUniqueValue(document))
                    {
                        var body        = document.DataAsJson;
                        var uniqueValue = body.Value <string>(sagaInfo.UniqueField);
                        if (existingSagaUniqueIdentityUniqueValues.Exists(x => x == uniqueValue))
                        {
                            PrintHelper.PrintMessage(string.Format("Skip uniquevalue {0} because it already exists", uniqueValue));
                            continue; // Already has SagaUniqueIdentity record
                        }
                        var sagaId = document.Key.Replace(sagaInfo.SagaKeyPrefix + "/", "");

                        var documentBody = new RavenJObject();
                        documentBody.Add("SagaId", sagaId);
                        documentBody.Add("UniqueValue", uniqueValue);
                        documentBody.Add("SagaDocId", document.Key);

                        var documentMetadata = new RavenJObject();
                        documentMetadata.Add("Content-Type", "application/json; charset=utf-8");
                        documentMetadata.Add("Raven-Entity-Name", "SagaUniqueIdentity");
                        documentMetadata.Add("Raven-Clr-Type",
                                             "NServiceBus.SagaPersisters.Raven.SagaUniqueIdentity, NServiceBus.Core");

                        ICommandData command = new PutCommandData
                        {
                            Key      = DocumentHelper.GenerateUniqueIdentityRecordId(uniqueValue, sagaInfo),
                            Document = documentBody,
                            Metadata = documentMetadata,
                        };

                        if (dictionarytest.ContainsKey(uniqueValue))
                        {
                            dictionarytest[uniqueValue] += 1;
                        }
                        else
                        {
                            dictionarytest[uniqueValue] = 1;
                        }

                        commandsList.Add(command);
                    }
                }

                if (sagaDocuments.Length < pageSize)
                {
                    break;
                }
            }
            PrintHelper.PrintMessage(string.Format("Attempt to insert {0} SagaUniqueIdentity documents", commandsList.Count));
            foreach (var item in dictionarytest.Keys)
            {
                if (dictionarytest[item] > 1)
                {
                    PrintHelper.PrintMessage("the following has duplicate " + item + " : " + dictionarytest[item]);
                }
            }
            var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray());

            PrintHelper.PrintResults("PopulateSagaUniqueIdentityRecords", results);
        }
        private void ReplicateDocument(IStorageActionsAccessor actions, string id, RavenJObject metadata, RavenJObject document, string src)
        {
            var existingDoc = actions.Documents.DocumentByKey(id, null);
            if (existingDoc == null)
            {
                log.DebugFormat("New document {0} replicated successfully from {1}", id, src);
				actions.Documents.AddDocument(id, Guid.Empty, document, metadata);
                return;
            }
            
            var existingDocumentIsInConflict = existingDoc.Metadata[ReplicationConstants.RavenReplicationConflict] != null;
            if (existingDocumentIsInConflict == false &&                    // if the current document is not in conflict, we can continue without having to keep conflict semantics
                (IsDirectChildOfCurrentDocument(existingDoc, metadata))) // this update is direct child of the existing doc, so we are fine with overwriting this
            {
                log.DebugFormat("Existing document {0} replicated successfully from {1}", id, src);
				actions.Documents.AddDocument(id, null, document, metadata);
                return;
            }


        	var newDocumentConflictId = id + "/conflicts/" +
        	                            metadata.Value<string>(ReplicationConstants.RavenReplicationSource) + "/" +
        	                            metadata.Value<string>("@etag");
            metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
			actions.Documents.AddDocument(newDocumentConflictId, null, document, metadata);

            if (existingDocumentIsInConflict) // the existing document is in conflict
            {
                log.DebugFormat("Conflicted document {0} has a new version from {1}, adding to conflicted documents", id, src);
                
                // just update the current doc with the new conflict document
                existingDoc.DataAsJson.Value<RavenJArray>("Conflicts").Add(RavenJToken.FromObject(newDocumentConflictId));
				actions.Documents.AddDocument(id, existingDoc.Etag, existingDoc.DataAsJson, existingDoc.Metadata);
                return;
            }
            log.DebugFormat("Existing document {0} is in conflict with replicated version from {1}, marking document as conflicted", id, src);
                
            // we have a new conflict
            // move the existing doc to a conflict and create a conflict document
        	var existingDocumentConflictId = id + "/conflicts/" + Database.TransactionalStorage.Id + "/" + existingDoc.Etag;
            
            existingDoc.Metadata.Add(ReplicationConstants.RavenReplicationConflict, RavenJToken.FromObject(true));
			actions.Documents.AddDocument(existingDocumentConflictId, null, existingDoc.DataAsJson, existingDoc.Metadata);
        	actions.Documents.AddDocument(id, null,
        	                              new RavenJObject
        	                              {
        	                              	{
        	                              	"Conflicts", new RavenJArray(existingDocumentConflictId, newDocumentConflictId)
        	                              	}
        	                              },
        	                              new RavenJObject
        	                              {
        	                              	{ReplicationConstants.RavenReplicationConflict, true},
        	                              	{"@Http-Status-Code", 409},
        	                              	{"@Http-Status-Description", "Conflict"}
        	                              });
        }