public PutResult Put(string key, Guid? etag, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
{
log.Debug("Putting a document with key: {0} and etag {1}", key, etag);
if (string.IsNullOrEmpty(key))
{
// we no longer sort by the key, so it doesn't matter
// that the key is no longer sequential
key = Guid.NewGuid().ToString();
}
RemoveReservedProperties(document);
RemoveReservedProperties(metadata);
Guid newEtag = Guid.Empty;
lock (putSerialLock)
{
TransactionalStorage.Batch(actions =>
{
if (key.EndsWith("/"))
{
key += GetNextIdentityValueWithoutOverritingOnExistingDocuments(key, actions, transactionInformation);
}
if (transactionInformation == null)
{
AssertPutOperationNotVetoed(key, metadata, document, transactionInformation);
PutTriggers.Apply(trigger => trigger.OnPut(key, document, metadata, transactionInformation));
newEtag = actions.Documents.AddDocument(key, etag, document, metadata);
// We detect this by using the etags
// AddIndexingTask(actions, metadata, () => new IndexDocumentsTask { Keys = new[] { key } });
PutTriggers.Apply(trigger => trigger.AfterPut(key, document, metadata, newEtag, transactionInformation));
}
else
{
newEtag = actions.Transactions.AddDocumentInTransaction(key, etag,
document, metadata, transactionInformation);
}
workContext.ShouldNotifyAboutWork();
});
}
TransactionalStorage
.ExecuteImmediatelyOrRegisterForSyncronization(() => PutTriggers.Apply(trigger => trigger.AfterCommit(key, document, metadata, newEtag)));
return new PutResult
{
Key = key,
ETag = newEtag
};
}