public void AttachmentStorage_AttachmentAdded_AttachmentFeched(string requestedStorage)
        {
            using (var storage = NewTransactionalStorage(requestedStorage))
            {
                using (Stream dataStream = new MemoryStream())
                {
                    var data = RavenJObject.FromObject(new { Name = "Bar" });
                    data.WriteTo(dataStream);
                    dataStream.Position = 0;
                    storage.Batch(mutator => mutator.Attachments.AddAttachment("Foo", null, dataStream, new RavenJObject()));

                    Attachment fetchedAttachment = null;
                    storage.Batch(viewer => fetchedAttachment = viewer.Attachments.GetAttachment("Foo"));

                    Assert.NotNull(fetchedAttachment);

                    RavenJObject fetchedAttachmentData = null;
                    Assert.DoesNotThrow(() =>
                                        storage.Batch(viewer =>
                    {
                        using (var fetchedDataStream = fetchedAttachment.Data())
                            fetchedAttachmentData = fetchedDataStream.ToJObject();
                    }));
                    Assert.NotNull(fetchedAttachmentData);

                    Assert.Equal(fetchedAttachmentData.Keys, data.Keys);
                    Assert.Equal(1, fetchedAttachmentData.Count);
                    Assert.Equal(fetchedAttachmentData.Value <string>("Name"), data.Value <string>("Name"));
                }
            }
        }
Exemple #2
0
        public override bool TryResolve(string id, RavenJObject metadata, byte[] data, Attachment existingAttachment, Func <string, Attachment> getAttachment)
        {
            var existingAttachmentIsInConflict = existingAttachment.Metadata[Constants.RavenReplicationConflict] != null;
            var existingAttachmentIsDeleted    = existingAttachment.Metadata[Constants.RavenDeleteMarker] != null &&
                                                 existingAttachment.Metadata[Constants.RavenDeleteMarker].Value <bool>();

            if (existingAttachmentIsInConflict && existingAttachmentIsDeleted == false)
            {
                var conflictIds =
                    existingAttachment.Data().ToJObject().Value <RavenJArray>("Conflicts")
                    .Select(x => x.Value <string>())
                    .ToArray();

                if (conflictIds.Length == 0)
                {
                    return(false);
                }

                return
                    (conflictIds
                     .Select(getAttachment)
                     .All(doc => Historian.IsDirectChildOfCurrent(metadata, doc.Metadata)));
            }

            return(false);
        }
Exemple #3
0
        protected override bool TryResolve(string id, RavenJObject metadata, byte[] data, Attachment existingAttachment, Func <string, Attachment> getAttachment, out RavenJObject metadataToSave,
                                           out byte[] dataToSave)
        {
            metadataToSave = existingAttachment.Metadata;
            dataToSave     = existingAttachment.Data().ReadData();

            return(true);
        }
Exemple #4
0
        private void AttachmentConflictResolveTest(StraightforwardConflictResolution attachmentConflictResolution)
        {
            using (var master = GetDocumentStore())
                using (var slave = GetDocumentStore())
                {
                    SetupReplication(master, destinations: slave);

                    using (var session = slave.OpenSession())
                    {
                        session.Store(new ReplicationConfig()
                        {
                            AttachmentConflictResolution = attachmentConflictResolution
                        }, Constants.RavenReplicationConfig);

                        session.SaveChanges();
                    }

                    var local  = new byte[] { 1, 2, 3, 4 };
                    var remote = new byte[] { 3, 2, 1 };

                    slave.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(local), new RavenJObject());

                    master.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(remote), new RavenJObject());

                    master.DatabaseCommands.PutAttachment("marker", null, new MemoryStream(), new RavenJObject());

                    WaitForAttachment(slave, "marker");

                    Attachment attachment = null;
                    Assert.DoesNotThrow(() => { attachment = slave.DatabaseCommands.GetAttachment("attach/1"); });

                    switch (attachmentConflictResolution)
                    {
                    case StraightforwardConflictResolution.ResolveToLocal:
                        Assert.Equal(local, attachment.Data().ReadData());
                        break;

                    case StraightforwardConflictResolution.ResolveToRemote:
                        Assert.Equal(remote, attachment.Data().ReadData());
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("attachmentConflictResolution");
                    }
                }
        }
Exemple #5
0
        public async Task CanAddAndReadAttachmentsAsyncInEmbedded()
        {
            using (var store = NewDocumentStore())
            {
                await store.AsyncDatabaseCommands.PutAttachmentAsync("Ayende", null, new byte[] { 1, 2, 3 }, new RavenJObject());

                Attachment attachment = await store.AsyncDatabaseCommands.GetAttachmentAsync("Ayende");

                Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data().ReadData());
            }
        }
Exemple #6
0
        private Attachment ProcessAttachmentReadVetoes(string name, Attachment attachment)
        {
            if (attachment == null)
            {
                return(null);
            }

            var foundResult = false;

            foreach (var attachmentReadTriggerLazy in Database.AttachmentReadTriggers)
            {
                if (foundResult)
                {
                    break;
                }
                var attachmentReadTrigger = attachmentReadTriggerLazy.Value;
                var readVetoResult        = attachmentReadTrigger.AllowRead(name, attachment.Data(), attachment.Metadata,
                                                                            ReadOperation.Load);
                switch (readVetoResult.Veto)
                {
                case ReadVetoResult.ReadAllow.Allow:
                    break;

                case ReadVetoResult.ReadAllow.Deny:
                    attachment.Data     = () => new MemoryStream(new byte[0]);
                    attachment.Size     = 0;
                    attachment.Metadata = new RavenJObject
                    {
                        {
                            "Raven-Read-Veto",
                            new RavenJObject
                            {
                                { "Reason", readVetoResult.Reason },
                                { "Trigger", attachmentReadTrigger.ToString() }
                            }
                        }
                    };
                    foundResult = true;
                    break;

                case ReadVetoResult.ReadAllow.Ignore:
                    attachment  = null;
                    foundResult = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(readVetoResult.Veto.ToString());
                }
            }
            return(attachment);
        }
Exemple #7
0
        public void CanAddAndReadAttachments()
        {
            using (var tx = NewTransactionalStorage())
            {
                tx.Batch(accessor => accessor.Attachments.AddAttachment("Ayende", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject()));

                Attachment attachment = null;
                tx.Batch(viewer =>
                {
                    attachment = viewer.Attachments.GetAttachment("Ayende");
                });

                tx.Batch(_ => Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data().ReadData()));
            }
        }
Exemple #8
0
        public Get()
        {
            using (var store = new DocumentStore())
            {
                #region get_1_1
                Attachment attachment = store
                                        .DatabaseCommands
                                        .GetAttachment("albums/holidays/sea.jpg"); // null if does not exist

                Stream data = attachment.Data();
                #endregion
            }

            using (var store = new DocumentStore())
            {
                #region get_2_1
                AttachmentInformation[] attachments = store
                                                      .DatabaseCommands
                                                      .GetAttachments(start: 0, startEtag: Etag.Empty, pageSize: 10);
                #endregion
            }
        }
Exemple #9
0
        public void CanAddAndReadAttachmentsAfterReopen()
        {
            string dataDir = NewDataPath();

            using (var tx = NewTransactionalStorage(dataDir: dataDir))
            {
                tx.Batch(accessor => accessor.Attachments.AddAttachment("Ayende", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject()));
            }

            using (var tx = NewTransactionalStorage(dataDir: dataDir))
            {
                Attachment attachment = null;
                tx.Batch(viewer =>
                {
                    attachment = viewer.Attachments.GetAttachment("Ayende");
                });

                tx.Batch(_ =>
                {
                    Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data().ReadData());
                });
            }
        }
Exemple #10
0
        public void Can_replicate_between_two_instances()
        {
            var store1 = CreateStore();
            var store2 = CreateStore();

            TellFirstInstanceToReplicateToSecondInstance();

            store1.DatabaseCommands.PutAttachment("ayende", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject());


            Attachment attachment = null;

            for (int i = 0; i < RetriesCount; i++)
            {
                attachment = store2.DatabaseCommands.GetAttachment("ayende");
                if (attachment == null)
                {
                    Thread.Sleep(100);
                }
            }
            Assert.NotNull(attachment);
            Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data().ReadData());
        }
        private void HandleConflictedAttachment(Attachment attachment)
        {
            var attachmentDataStream = attachment.Data();
            var attachmentData       = attachmentDataStream.ToJObject();

            var conflicts = attachmentData.Value <RavenJArray>("Conflicts");

            if (conflicts == null)
            {
                return;
            }

            var currentSource = Database.TransactionalStorage.Id.ToString();

            foreach (var c in conflicts)
            {
                var conflict       = Database.GetStatic(c.Value <string>());
                var conflictSource = conflict.Metadata.Value <RavenJValue>(Constants.RavenReplicationSource).Value <string>();

                if (conflictSource != currentSource)
                {
                    continue;
                }

                this.deletedHistory.Value = new RavenJArray
                {
                    new RavenJObject
                    {
                        { Constants.RavenReplicationVersion, conflict.Metadata[Constants.RavenReplicationVersion] },
                        { Constants.RavenReplicationSource, conflict.Metadata[Constants.RavenReplicationSource] }
                    }
                };

                return;
            }
        }
		private Attachment ProcessAttachmentReadVetoes(string name, Attachment attachment)
		{
			if (attachment == null)
				return null;

			var foundResult = false;
			foreach (var attachmentReadTriggerLazy in AttachmentReadTriggers)
			{
				if (foundResult)
					break;
				var attachmentReadTrigger = attachmentReadTriggerLazy.Value;
				var readVetoResult = attachmentReadTrigger.AllowRead(name, attachment.Data(), attachment.Metadata,
																	 ReadOperation.Load);
				switch (readVetoResult.Veto)
				{
					case ReadVetoResult.ReadAllow.Allow:
						break;
					case ReadVetoResult.ReadAllow.Deny:
						attachment.Data = () => new MemoryStream(new byte[0]);
						attachment.Size = 0;
						attachment.Metadata = new RavenJObject
												{
													{
														"Raven-Read-Veto",
														new RavenJObject
															{
																{"Reason", readVetoResult.Reason},
																{"Trigger", attachmentReadTrigger.ToString()}
															}
														}
												};
						foundResult = true;
						break;
					case ReadVetoResult.ReadAllow.Ignore:
						attachment = null;
						foundResult = true;
						break;
					default:
						throw new ArgumentOutOfRangeException(readVetoResult.Veto.ToString());
				}
			}
			return attachment;
		}
Exemple #13
0
 public void UpdateAttachment(string key, Attachment attachment)
 {
     _store.DatabaseCommands.PutAttachment(key, attachment.Etag, attachment.Data(), attachment.Metadata);
 }