public Guid AddDocument(string key, Guid?etag, RavenJObject data, RavenJObject metadata) { AssertValidEtag(key, etag, "PUT", null); var ms = new MemoryStream(); metadata.WriteTo(ms); var bytes = documentCodecs.Aggregate(data.ToBytes(), (current, codec) => codec.Value.Encode(key, data, metadata, current)); ms.Write(bytes, 0, bytes.Length); var newEtag = generator.CreateSequentialUuid(); storage.Documents.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() }, { "modified", DateTime.UtcNow }, { "id", GetNextDocumentId() }, { "entityName", metadata.Value <string>(Constants.RavenEntityName) } }, ms.ToArray()); return(newEtag); }
public Guid AddDocumentInTransaction(string key, Guid?etag, RavenJObject data, RavenJObject metadata, TransactionInformation transactionInformation) { var readResult = storage.Documents.Read(new RavenJObject { { "key", key } }); if (readResult != null) // update { StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation); AssertValidEtag(key, readResult, storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } }), etag); ((RavenJObject)readResult.Key)["txId"] = transactionInformation.Id.ToByteArray(); if (storage.Documents.UpdateKey(readResult.Key) == false) { throw new ConcurrencyException("PUT attempted on document '" + key + "' that is currently being modified by another transaction"); } } else { readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } }); StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation); } storage.Transactions.UpdateKey(new RavenJObject { { "txId", transactionInformation.Id.ToByteArray() }, { "timeout", SystemTime.UtcNow.Add(transactionInformation.Timeout) } }); var ms = new MemoryStream(); metadata.WriteTo(ms); var dataBytes = documentCodecs.Aggregate(data.ToBytes(), (bytes, codec) => codec.Encode(key, data, metadata, bytes)); ms.Write(dataBytes, 0, dataBytes.Length); var newEtag = generator.CreateSequentialUuid(); storage.DocumentsModifiedByTransactions.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() }, { "modified", SystemTime.UtcNow }, { "txId", transactionInformation.Id.ToByteArray() } }, ms.ToArray()); return(newEtag); }
public Guid AddDocumentInTransaction(string key, Guid?etag, RavenJObject data, RavenJObject metadata, TransactionInformation transactionInformation) { Api.JetSetCurrentIndex(session, Documents, "by_key"); Api.MakeKey(session, Documents, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Documents, SeekGrbit.SeekEQ); if (isUpdate) { EnsureNotLockedByTransaction(key, transactionInformation.Id); EnsureDocumentEtagMatchInTransaction(key, etag); using (var update = new Update(session, Documents, JET_prep.Replace)) { Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["locked_by_transaction"], transactionInformation.Id.ToByteArray()); update.Save(); } } else { EnsureDocumentIsNotCreatedInAnotherTransaction(key, transactionInformation.Id); } EnsureTransactionExists(transactionInformation); Guid newEtag = uuidGenerator.CreateSequentialUuid(); Api.JetSetCurrentIndex(session, DocumentsModifiedByTransactions, "by_key"); Api.MakeKey(session, DocumentsModifiedByTransactions, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdateInTransaction = Api.TrySeek(session, DocumentsModifiedByTransactions, SeekGrbit.SeekEQ); var bytes = documentCodecs.Aggregate(data.ToBytes(), (current, codec) => codec.Encode(key, data, metadata, current)); using (var update = new Update(session, DocumentsModifiedByTransactions, isUpdateInTransaction ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["key"], key, Encoding.Unicode); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["data"], bytes); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["etag"], newEtag.TransformToValueForEsentSorting()); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["metadata"], metadata.ToBytes()); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["last_modified"], SystemTime.UtcNow); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["delete_document"], false); Api.SetColumn(session, DocumentsModifiedByTransactions, tableColumnsCache.DocumentsModifiedByTransactionsColumns["locked_by_transaction"], transactionInformation.Id.ToByteArray()); update.Save(); } logger.Debug("Inserted a new document with key '{0}', update: {1}, in transaction: {2}", key, isUpdate, transactionInformation.Id); return(newEtag); }
public Guid AddDocument(string key, Guid?etag, RavenJObject data, RavenJObject metadata) { if (key != null && Encoding.Unicode.GetByteCount(key) >= 255) { throw new ArgumentException("The key must be a maximum of 255 bytes in unicode, 127 characters, key is: " + key, "key"); } Guid existingEtag; Api.JetSetCurrentIndex(session, Documents, "by_key"); Api.MakeKey(session, Documents, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Documents, SeekGrbit.SeekEQ); if (isUpdate) { EnsureNotLockedByTransaction(key, null); existingEtag = EnsureDocumentEtagMatch(key, etag, "PUT"); } else { existingEtag = Guid.Empty; EnsureDocumentIsNotCreatedInAnotherTransaction(key, Guid.NewGuid()); if (Api.TryMoveFirst(session, Details)) { Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["document_count"], 1); } } Guid newEtag = uuidGenerator.CreateSequentialUuid(); var bytes = documentCodecs.Aggregate(data.ToBytes(), (current, codec) => codec.Encode(key, data, metadata, current)); using (var update = new Update(session, Documents, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["key"], key, Encoding.Unicode); Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["data"], bytes); Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["etag"], newEtag.TransformToValueForEsentSorting()); Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["last_modified"], SystemTime.UtcNow); Api.SetColumn(session, Documents, tableColumnsCache.DocumentsColumns["metadata"], metadata.ToBytes()); update.Save(); } logger.Debug("Inserted a new document with key '{0}', update: {1}, ", key, isUpdate); cacher.RemoveCachedDocument(key, newEtag); return(newEtag); }
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data, byte[] viewAndReduceKeyHashed) { Guid etag = uuidGenerator.CreateSequentialUuid(); using (var update = new Update(session, MappedResults, JET_prep.Insert)) { Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["view"], view, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["document_key"], docId, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key"], reduceKey, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key_and_view_hashed"], viewAndReduceKeyHashed); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["data"], data.ToBytes()); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["etag"], etag.TransformToValueForEsentSorting()); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["timestamp"], DateTime.Now); update.Save(); } }
public Guid AddDocumentInTransaction(string key, Guid? etag, RavenJObject data, RavenJObject metadata, TransactionInformation transactionInformation) { var readResult = storage.Documents.Read(new RavenJObject {{"key", key}}); if (readResult != null) // update { StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation); AssertValidEtag(key, readResult, storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } }), etag); ((RavenJObject)readResult.Key)["txId"] = transactionInformation.Id.ToByteArray(); if (storage.Documents.UpdateKey(readResult.Key) == false) throw new ConcurrencyException("PUT attempted on document '" + key + "' that is currently being modified by another transaction"); } else { readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } }); StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation); } storage.Transactions.UpdateKey(new RavenJObject { {"txId", transactionInformation.Id.ToByteArray()}, {"timeout", SystemTime.UtcNow.Add(transactionInformation.Timeout)} }); var ms = new MemoryStream(); metadata.WriteTo(ms); var dataBytes = documentCodecs.Aggregate(data.ToBytes(), (bytes, codec) => codec.Encode(key, data, metadata, bytes)); ms.Write(dataBytes, 0, dataBytes.Length); var newEtag = generator.CreateSequentialUuid(); storage.DocumentsModifiedByTransactions.Put(new RavenJObject { {"key", key}, {"etag", newEtag.ToByteArray()}, {"modified", SystemTime.UtcNow}, {"txId", transactionInformation.Id.ToByteArray()} }, ms.ToArray()); return newEtag; }