Exemple #1
0
        public Attachment GetAttachment(string key)
        {
            Api.JetSetCurrentIndex(session, Files, "by_name");
            Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
            if (Api.TrySeek(session, Files, SeekGrbit.SeekEQ) == false)
            {
                return(null);
            }

            var metadata = Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["metadata"], Encoding.Unicode);

            return(new Attachment
            {
                Key = key,
                Data = () =>
                {
                    StorageActionsAccessor storageActionsAccessor = transactionalStorage.GetCurrentBatch();
                    var documentStorageActions = ((DocumentStorageActions)storageActionsAccessor.Attachments);
                    return documentStorageActions.GetAttachmentStream(key);
                },
                Size = (int)GetAttachmentStream(key).Length,
                Etag = Etag.Parse(Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"])),
                Metadata = RavenJObject.Parse(metadata)
            });
        }
Exemple #2
0
 public GeneralStorageActions(TableStorage storage, Reference <WriteBatch> writeBatch, Reference <SnapshotReader> snapshot, IBufferPool bufferPool, StorageActionsAccessor storageActionsAccessor)
     : base(snapshot, bufferPool)
 {
     this.storage                = storage;
     this.writeBatch             = writeBatch;
     this.snapshot               = snapshot;
     this.storageActionsAccessor = storageActionsAccessor;
 }
        public GeneralStorageActions(TableStorage storage, Reference<WriteBatch> writeBatch, Reference<SnapshotReader> snapshot, IBufferPool bufferPool, StorageActionsAccessor storageActionsAccessor)
            : base(snapshot, bufferPool)
        {
            this.storage = storage;
            this.writeBatch = writeBatch;
            this.snapshot = snapshot;
	        this.storageActionsAccessor = storageActionsAccessor;
        }
        private Action ExecuteBatch(Action <IStorageActionsAccessor> action)
        {
            var snapshotRef   = new Reference <SnapshotReader>();
            var writeBatchRef = new Reference <WriteBatch>();

            try
            {
                snapshotRef.Value   = tableStorage.CreateSnapshot();
                writeBatchRef.Value = new WriteBatch {
                    DisposeAfterWrite = false
                };                                                                  // prevent from disposing after write to allow read from batch OnStorageCommit
                var storageActionsAccessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                                                                        documentCacher, writeBatchRef, snapshotRef,
                                                                        tableStorage, this, bufferPool);

                if (disableBatchNesting.Value == null)
                {
                    current.Value = storageActionsAccessor;
                }

                action(storageActionsAccessor);
                storageActionsAccessor.SaveAllTasks();
                storageActionsAccessor.ExecuteBeforeStorageCommit();

                tableStorage.Write(writeBatchRef.Value);

                try
                {
                    return(storageActionsAccessor.ExecuteOnStorageCommit);
                }
                finally
                {
                    storageActionsAccessor.ExecuteAfterStorageCommit();
                }
            }
            finally
            {
                if (snapshotRef.Value != null)
                {
                    snapshotRef.Value.Dispose();
                }

                if (writeBatchRef.Value != null)
                {
                    writeBatchRef.Value.Dispose();
                }
            }
        }
        public IStorageActionsAccessor CreateAccessor()
        {
            var snapshotReference = new Reference <SnapshotReader> {
                Value = tableStorage.CreateSnapshot()
            };
            var writeBatchReference = new Reference <WriteBatch> {
                Value = new WriteBatch()
            };

            var accessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                                                      documentCacher, writeBatchReference, snapshotReference, tableStorage, this, bufferPool);

            accessor.OnDispose += () =>
            {
                var exceptionAggregator = new ExceptionAggregator("Could not properly dispose StorageActionsAccessor");

                exceptionAggregator.Execute(() => snapshotReference.Value.Dispose());
                exceptionAggregator.Execute(() => writeBatchReference.Value.Dispose());

                exceptionAggregator.ThrowIfNeeded();
            };

            return(accessor);
        }
        private IStorageActionsAccessor ExecuteBatch(Action<IStorageActionsAccessor> action)
        {
            var snapshotRef = new Reference<SnapshotReader>();
            var writeBatchRef = new Reference<WriteBatch>();
            try
            {
                snapshotRef.Value = tableStorage.CreateSnapshot();
                writeBatchRef.Value = new WriteBatch { DisposeAfterWrite = false }; // prevent from disposing after write to allow read from batch OnStorageCommit
                var storageActionsAccessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                                                                        documentCacher, writeBatchRef, snapshotRef,
                                                                        tableStorage, this, bufferPool);

                if (disableBatchNesting.Value == null)
                    current.Value = storageActionsAccessor;

                action(storageActionsAccessor);
                storageActionsAccessor.SaveAllTasks();

                tableStorage.Write(writeBatchRef.Value);

                storageActionsAccessor.ExecuteOnStorageCommit();

                return storageActionsAccessor;
            }
            finally
            {
                if (snapshotRef.Value != null)
                    snapshotRef.Value.Dispose();

                if (writeBatchRef.Value != null)
                    writeBatchRef.Value.Dispose();
            }
        }
		public IStorageActionsAccessor CreateAccessor()
		{
		    var snapshotReference = new Reference<SnapshotReader> { Value = tableStorage.CreateSnapshot() };
			var writeBatchReference = new Reference<WriteBatch> { Value = new WriteBatch() };
			
			var accessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                    documentCacher, writeBatchReference, snapshotReference, tableStorage, this, bufferPool);
			accessor.OnDispose += () =>
			{
				var exceptionAggregator = new ExceptionAggregator("Could not properly dispose StorageActionsAccessor");

				exceptionAggregator.Execute(() => snapshotReference.Value.Dispose());
				exceptionAggregator.Execute(() => writeBatchReference.Value.Dispose());

				exceptionAggregator.ThrowIfNeeded();
			};

			return accessor;
		}
        private Action ExecuteBatch(Action <IStorageActionsAccessor> action)
        {
            var snapshotRef   = new Reference <SnapshotReader>();
            var writeBatchRef = new Reference <WriteBatch>();

            var errorInUserAction = false;

            try
            {
                snapshotRef.Value   = tableStorage.CreateSnapshot();
                writeBatchRef.Value = new WriteBatch {
                    DisposeAfterWrite = false
                };                                                                // prevent from disposing after write to allow read from batch OnStorageCommit
                var storageActionsAccessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                                                                        documentCacher, writeBatchRef, snapshotRef,
                                                                        tableStorage, this, bufferPool);

                if (disableBatchNesting.Value == null)
                {
                    current.Value = storageActionsAccessor;
                }

                errorInUserAction = true;
                action(storageActionsAccessor);
                errorInUserAction = false;

                storageActionsAccessor.SaveAllTasks();
                storageActionsAccessor.ExecuteBeforeStorageCommit();

                tableStorage.Write(writeBatchRef.Value);

                try
                {
                    return(storageActionsAccessor.ExecuteOnStorageCommit);
                }
                finally
                {
                    storageActionsAccessor.ExecuteAfterStorageCommit();
                }
            }
            catch (Exception e)
            {
                var exception = e;
                var ae        = e as AggregateException;
                if (ae != null)
                {
                    exception = ae.ExtractSingleInnerException();
                }

                Exception _;
                if (TransactionalStorageHelper.IsVoronOutOfMemoryException(exception) ||
                    TransactionalStorageHelper.IsWriteConflict(e, out _))
                {
                    throw;
                }

                if (errorInUserAction == false)
                {
                    Log.ErrorException("Failed to execute transaction. Most likely something is really wrong here.", e);
                }

                throw;
            }
            finally
            {
                if (snapshotRef.Value != null)
                {
                    snapshotRef.Value.Dispose();
                }

                if (writeBatchRef.Value != null)
                {
                    writeBatchRef.Value.Dispose();
                }
            }
        }