public void TouchDocument(string key, out Etag preTouchEtag, out Etag afterTouchEtag) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } var lowerKey = CreateKey(key); if (!tableStorage.Documents.Contains(Snapshot, lowerKey, writeBatch.Value)) { logger.Debug("Document with dataKey='{0}' was not found", key); preTouchEtag = null; afterTouchEtag = null; return; } var metadata = ReadDocumentMetadata(key); var newEtag = uuidGenerator.CreateSequentialUuid(UuidType.Documents); afterTouchEtag = newEtag; preTouchEtag = metadata.Etag; metadata.Etag = newEtag; WriteDocumentMetadata(metadata, shouldIgnoreConcurrencyExceptions: true); var keyByEtagIndex = tableStorage.Documents.GetIndex(Tables.Documents.Indices.KeyByEtag); keyByEtagIndex.Delete(writeBatch.Value, preTouchEtag); keyByEtagIndex.Add(writeBatch.Value, newEtag, lowerKey); etagTouches.Add(preTouchEtag, afterTouchEtag); logger.Debug("TouchDocument() - document with key = '{0}'", key); }
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, "DELETE"); var ravenJObject = ((RavenJObject)readResult.Key.CloneToken()); ravenJObject["txId"] = transactionInformation.Id.ToByteArray(); if (storage.Documents.UpdateKey(ravenJObject) == false) { throw new ConcurrencyException("PUT attempted on document '" + key + "' that is currently being modified by another transaction") { Key = key } } ; } 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); using (var stream = documentCodecs.Aggregate <Lazy <AbstractDocumentCodec>, Stream>(ms, (memoryStream, codec) => codec.Value.Encode(key, data, metadata, memoryStream))) { data.WriteTo(stream); stream.Flush(); } var newEtag = generator.CreateSequentialUuid(UuidType.DocumentTransactions); 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) { 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 AddDocumentResult InsertDocument(string key, RavenJObject data, RavenJObject metadata, bool checkForUpdates) { var ms = new MemoryStream(); metadata.WriteTo(ms); using (var stream = documentCodecs.Aggregate <Lazy <AbstractDocumentCodec>, Stream>(ms, (dataStream, codec) => codec.Value.Encode(key, data, metadata, dataStream))) { data.WriteTo(stream); stream.Flush(); } var readResult = storage.Documents.Read(new RavenJObject { { "key", key } }); var isUpdate = readResult != null; if (isUpdate && checkForUpdates == false) { throw new InvalidOperationException("Cannot insert document " + key + " because it already exists"); } Etag existingEtag = null; if (isUpdate) { existingEtag = Etag.Parse(readResult.Key.Value <byte[]>("etag")); } var newEtag = generator.CreateSequentialUuid(UuidType.Documents); var savedAt = SystemTime.UtcNow; storage.Documents.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() }, { "modified", savedAt }, { "id", GetNextDocumentId() }, { "entityName", metadata.Value <string>(Constants.RavenEntityName) } }, ms.ToArray()); IncrementDocumentCount(1); return(new AddDocumentResult { Etag = newEtag, PrevEtag = existingEtag, SavedAt = savedAt, Updated = isUpdate }); }
public void Set(string name, string key, RavenJObject data, UuidType type) { var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName); var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey); var nameKey = CreateKey(name); var nameKeySlice = (Slice)nameKey; var nameAndKeySlice = (Slice)AppendToKey(nameKey, key); string existingEtag = null; bool update = false; var read = listsByNameAndKey.Read(Snapshot, nameAndKeySlice, writeBatch.Value); if (read != null) { update = true; using (var stream = read.Reader.AsStream()) { using (var reader = new StreamReader(stream)) existingEtag = reader.ReadToEnd(); } } var etag = generator.CreateSequentialUuid(type); var internalKey = update == false?etag.ToString() : existingEtag; var internalKeyAsSlice = (Slice)internalKey; var createdAt = SystemTime.UtcNow; tableStorage.Lists.Add( writeBatch.Value, internalKeyAsSlice, new RavenJObject { { "name", name }, { "key", key }, { "etag", etag.ToByteArray() }, { "data", data }, { "createdAt", createdAt } }); if (update == false) { listsByName.MultiAdd(writeBatch.Value, nameKeySlice, internalKeyAsSlice); listsByNameAndKey.Add(writeBatch.Value, nameAndKeySlice, internalKey); } }
public void AddTask(DatabaseTask task, DateTime addedAt) { var tasksByType = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByType); var tasksByIndex = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByIndex); var tasksByIndexAndType = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByIndexAndType); var type = task.GetType().FullName; var index = task.Index; var id = generator.CreateSequentialUuid(UuidType.Tasks); var idAsString = (Slice)id.ToString(); var taskStructure = new Structure <TaskFields>(tableStorage.Tasks.Schema) .Set(TaskFields.IndexId, index) .Set(TaskFields.TaskId, id.ToByteArray()) .Set(TaskFields.AddedAt, addedAt.ToBinary()) .Set(TaskFields.Type, type) .Set(TaskFields.SerializedTask, task.AsBytes()); tableStorage.Tasks.AddStruct(writeBatch.Value, idAsString, taskStructure, 0); var indexKey = CreateKey(index); tasksByType.MultiAdd(writeBatch.Value, (Slice)CreateKey(type), idAsString); tasksByIndex.MultiAdd(writeBatch.Value, (Slice)indexKey, idAsString); tasksByIndexAndType.MultiAdd(writeBatch.Value, (Slice)AppendToKey(indexKey, type), idAsString); }
public void Set(string name, string key, RavenJObject data, UuidType type) { var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName); var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey); var etag = generator.CreateSequentialUuid(type); var etagAsString = etag.ToString(); var etagAsSlice = (Slice)etagAsString; var createdAt = SystemTime.UtcNow; tableStorage.Lists.Add( writeBatch.Value, etagAsSlice, new RavenJObject { { "name", name }, { "key", key }, { "etag", etag.ToByteArray() }, { "data", data }, { "createdAt", createdAt } }); var nameKey = CreateKey(name); listsByName.MultiAdd(writeBatch.Value, (Slice)nameKey, etagAsSlice); listsByNameAndKey.Add(writeBatch.Value, (Slice)AppendToKey(nameKey, key), etagAsString); }
public void UpdateDocumentReferences(int id, string key, HashSet <string> references) { var documentReferencesByKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByKey); var documentReferencesByRef = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByRef); var documentReferencesByView = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByView); var documentReferencesByViewAndKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByViewAndKey); var idKey = CreateKey(id); RemoveDocumentReference(() => documentReferencesByViewAndKey.MultiRead(Snapshot, AppendToKey(idKey, key)), false, CancellationToken.None); var loweredKey = (Slice)CreateKey(key); foreach (var reference in references) { var newKey = generator.CreateSequentialUuid(UuidType.DocumentReferences); var value = new Structure <DocumentReferencesFields>(tableStorage.DocumentReferences.Schema) .Set(DocumentReferencesFields.IndexId, id) .Set(DocumentReferencesFields.Key, key) .Set(DocumentReferencesFields.Reference, reference); var newKeyAsSlice = (Slice)newKey.ToString(); tableStorage.DocumentReferences.AddStruct(writeBatch.Value, newKeyAsSlice, value); documentReferencesByKey.MultiAdd(writeBatch.Value, loweredKey, newKeyAsSlice); documentReferencesByRef.MultiAdd(writeBatch.Value, (Slice)CreateKey(reference), newKeyAsSlice); documentReferencesByView.MultiAdd(writeBatch.Value, (Slice)idKey, newKeyAsSlice); documentReferencesByViewAndKey.MultiAdd(writeBatch.Value, (Slice)AppendToKey(idKey, key), newKeyAsSlice); } }
public Guid AddAttachment(string key, Guid?etag, Stream data, RavenJObject headers) { AssertValidEtag(key, etag, "PUT"); if (data == null) { var attachment = GetAttachment(key); if (attachment == null) { throw new InvalidOperationException("When adding new attachment, the attachment data must be specified"); } attachment.Metadata = headers; return(AddAttachment(key, etag, attachment.Data(), headers)); } var ms = new MemoryStream(); headers.WriteTo(ms); data.CopyTo(ms); var newEtag = generator.CreateSequentialUuid(); var result = storage.Attachments.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() } }, ms.ToArray()); if (result == false) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' while it was locked by another transaction"); } logger.Debug("Adding attachment {0}", key); return(newEtag); }
public Guid AddDocument(string key, Guid?etag, JObject data, JObject metadata) { AssertValidEtag(key, etag, "PUT", null); var ms = new MemoryStream(); metadata.WriteTo(ms); var bytes = documentCodecs.Aggregate(data.ToBytes(), (current, codec) => codec.Encode(key, data, metadata, current)); ms.Write(bytes, 0, bytes.Length); var newEtag = generator.CreateSequentialUuid(); storage.Documents.Put(new JObject { { "key", key }, { "etag", newEtag.ToByteArray() }, { "modified", DateTime.UtcNow }, { "id", GetNextDocumentId() }, { "entityName", metadata.Value <string>("Raven-Entity-Name") } }, ms.ToArray()); return(newEtag); }
public void AddTask(DatabaseTask task, DateTime addedAt) { var tasksByType = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByType); var tasksByIndex = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByIndex); var tasksByIndexAndType = tableStorage.Tasks.GetIndex(Tables.Tasks.Indices.ByIndexAndType); var type = task.GetType().FullName; var index = task.Index; var id = generator.CreateSequentialUuid(UuidType.Tasks); var idAsString = id.ToString(); tableStorage.Tasks.Add( writeBatch.Value, idAsString, new RavenJObject { { "index", index }, { "id", id.ToByteArray() }, { "time", addedAt }, { "type", type }, { "task", task.AsBytes() } }, 0); tasksByType.MultiAdd(writeBatch.Value, CreateKey(type), idAsString); tasksByIndex.MultiAdd(writeBatch.Value, CreateKey(index), idAsString); tasksByIndexAndType.MultiAdd(writeBatch.Value, CreateKey(index, type), idAsString); }
public Guid AddDocument(string key, Guid?etag, RavenJObject data, RavenJObject metadata) { var existingEtag = AssertValidEtag(key, etag, "PUT", null); var ms = new MemoryStream(); metadata.WriteTo(ms); using (var stream = documentCodecs.Aggregate <Lazy <AbstractDocumentCodec>, Stream>(ms, (dataStream, codec) => codec.Value.Encode(key, data, metadata, dataStream))) { data.WriteTo(stream); stream.Flush(); } var newEtag = generator.CreateSequentialUuid(); storage.Documents.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() }, { "modified", SystemTime.UtcNow }, { "id", GetNextDocumentId() }, { "entityName", metadata.Value <string>(Constants.RavenEntityName) } }, ms.ToArray()); documentCacher.RemoveCachedDocument(key, existingEtag); return(newEtag); }
public void EnqueueToQueue(string name, byte[] data) { storage.Queues.Put(new RavenJObject { { "name", name }, { "id", generator.CreateSequentialUuid().ToByteArray() }, { "reads", 0 } }, data); }
public void PutMappedResult(int view, string docId, string reduceKey, RavenJObject data) { var mappedResultsByViewAndDocumentId = tableStorage.MappedResults.GetIndex(Tables.MappedResults.Indices.ByViewAndDocumentId); var mappedResultsByView = tableStorage.MappedResults.GetIndex(Tables.MappedResults.Indices.ByView); var mappedResultsByViewAndReduceKey = tableStorage.MappedResults.GetIndex(Tables.MappedResults.Indices.ByViewAndReduceKey); var mappedResultsByViewAndReduceKeyAndSourceBucket = tableStorage.MappedResults.GetIndex(Tables.MappedResults.Indices.ByViewAndReduceKeyAndSourceBucket); var mappedResultsData = tableStorage.MappedResults.GetIndex(Tables.MappedResults.Indices.Data); var ms = CreateStream(); using (var stream = documentCodecs.Aggregate((Stream) new UndisposableStream(ms), (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds))) { data.WriteTo(stream); stream.Flush(); } var id = generator.CreateSequentialUuid(UuidType.MappedResults); var idAsString = id.ToString(); var bucket = IndexingUtil.MapBucket(docId); var reduceKeyHash = HashKey(reduceKey); tableStorage.MappedResults.Add( writeBatch.Value, idAsString, new RavenJObject { { "view", view }, { "reduceKey", reduceKey }, { "docId", docId }, { "etag", id.ToByteArray() }, { "bucket", bucket }, { "timestamp", SystemTime.UtcNow } }, 0); ms.Position = 0; mappedResultsData.Add(writeBatch.Value, idAsString, ms, 0); mappedResultsByViewAndDocumentId.MultiAdd(writeBatch.Value, CreateKey(view, docId), idAsString); mappedResultsByView.MultiAdd(writeBatch.Value, CreateKey(view), idAsString); mappedResultsByViewAndReduceKey.MultiAdd(writeBatch.Value, CreateKey(view, reduceKey, reduceKeyHash), idAsString); mappedResultsByViewAndReduceKeyAndSourceBucket.MultiAdd(writeBatch.Value, CreateKey(view, reduceKey, reduceKeyHash, bucket), idAsString); }
public void AddTask(Task task, DateTime addedAt) { storage.Tasks.Put(new RavenJObject { { "index", task.Index }, { "id", generator.CreateSequentialUuid().ToByteArray() }, { "time", addedAt }, { "type", task.GetType().FullName }, }, task.AsBytes()); }
public void AddTask(Task task, DateTime addedAt) { storage.Tasks.Put(new JObject { { "index", task.Index }, { "id", generator.CreateSequentialUuid().ToByteArray() }, { "time", addedAt }, { "type", task.Type }, { "mergable", task.SupportsMerging } }, task.AsBytes()); }
public Guid Enqueue(string queue, DateTime expiry, byte[] data) { var msgId = uuidGenerator.CreateSequentialUuid(); messages.Put(new JObject { { "MsgId", msgId.ToByteArray() }, { "Queue", queue }, { "Expiry", expiry } }, data); return(msgId); }
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data) { var ms = new MemoryStream(); using (var stream = documentCodecs.Aggregate((Stream)ms, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds))) { data.WriteTo(stream); } var byteArray = generator.CreateSequentialUuid().ToByteArray(); var key = new RavenJObject { { "view", view }, { "reduceKey", reduceKey }, { "docId", docId }, { "etag", byteArray }, { "bucket", IndexingUtil.MapBucket(docId) }, { "timestamp", SystemTime.UtcNow } }; storage.MappedResults.Put(key, ms.ToArray()); }
public void PutMappedResult(string view, string docId, string reduceKey, JObject data, byte[] viewAndReduceKeyHashed) { var ms = new MemoryStream(); data.WriteTo(ms); storage.MappedResults.Put(new JObject { { "view", view }, { "reduceKey", reduceKey }, { "docId", docId }, { "mapResultId", generator.CreateSequentialUuid().ToByteArray() } }, ms.ToArray()); }
public void Set(string name, string key, RavenJObject data, UuidType type) { var memoryStream = new MemoryStream(); data.WriteTo(memoryStream); storage.Lists.Put(new RavenJObject { { "name", name }, { "key", key }, { "etag", generator.CreateSequentialUuid(type).ToByteArray() } }, memoryStream.ToArray()); }
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data, byte[] viewAndReduceKeyHashed) { var ms = new MemoryStream(); data.WriteTo(ms); var byteArray = generator.CreateSequentialUuid().ToByteArray(); var key = new RavenJObject { { "view", view }, { "reduceKey", reduceKey }, { "docId", docId }, { "etag", byteArray }, { "timestamp", SystemTime.Now } }; storage.MappedResults.Put(key, ms.ToArray()); }
public void EnqueueToQueue(string name, byte[] data) { var queuesByName = tableStorage.Queues.GetIndex(Tables.Queues.Indices.ByName); var queuesData = tableStorage.Queues.GetIndex(Tables.Queues.Indices.Data); var id = generator.CreateSequentialUuid(UuidType.Queue); var key = CreateKey(name, id); tableStorage.Queues.Add(writeBatch.Value, key, new RavenJObject { { "name", name }, { "id", id.ToByteArray() }, { "reads", 0 } }, 0); queuesData.Add(writeBatch.Value, key, data, 0); queuesByName.MultiAdd(writeBatch.Value, CreateKey(name), key); }
public void UpdateDocumentReferences(int id, string key, HashSet <string> references) { var documentReferencesByKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByKey); var documentReferencesByRef = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByRef); var documentReferencesByView = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByView); var documentReferencesByViewAndKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByViewAndKey); var idKey = CreateKey(id); using (var iterator = documentReferencesByViewAndKey.MultiRead(Snapshot, (Slice)AppendToKey(idKey, key))) { if (iterator.Seek(Slice.BeforeAllKeys)) { do { RemoveDocumentReference(iterator.CurrentKey.Clone()); }while (iterator.MoveNext()); } } var loweredKey = (Slice)CreateKey(key); foreach (var reference in references) { var newKey = generator.CreateSequentialUuid(UuidType.DocumentReferences); var value = new Structure <DocumentReferencesFields>(tableStorage.DocumentReferences.Schema) .Set(DocumentReferencesFields.IndexId, id) .Set(DocumentReferencesFields.Key, key) .Set(DocumentReferencesFields.Reference, reference); var newKeyAsSlice = (Slice)newKey.ToString(); tableStorage.DocumentReferences.AddStruct(writeBatch.Value, newKeyAsSlice, value); documentReferencesByKey.MultiAdd(writeBatch.Value, loweredKey, newKeyAsSlice); documentReferencesByRef.MultiAdd(writeBatch.Value, (Slice)CreateKey(reference), newKeyAsSlice); documentReferencesByView.MultiAdd(writeBatch.Value, (Slice)idKey, newKeyAsSlice); documentReferencesByViewAndKey.MultiAdd(writeBatch.Value, (Slice)AppendToKey(idKey, key), newKeyAsSlice); } }
public void Set(string name, string key, RavenJObject data, UuidType type) { var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName); var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey); var etag = generator.CreateSequentialUuid(type); var etagAsString = etag.ToString(); tableStorage.Lists.Add( writeBatch.Value, etagAsString, new RavenJObject { { "name", name }, { "key", key }, { "etag", etag.ToByteArray() }, { "data", data } }); listsByName.MultiAdd(writeBatch.Value, CreateKey(name), etagAsString); listsByNameAndKey.Add(writeBatch.Value, CreateKey(name, key), etagAsString); }
public Guid AddAttachment(string key, Guid?etag, byte[] data, RavenJObject headers) { AssertValidEtag(key, etag, "PUT"); var ms = new MemoryStream(); headers.WriteTo(ms); ms.Write(data, 0, data.Length); var newEtag = generator.CreateSequentialUuid(); var result = storage.Attachments.Put(new RavenJObject { { "key", key }, { "etag", newEtag.ToByteArray() } }, ms.ToArray()); if (result == false) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' while it was locked by another transaction"); } logger.Debug("Adding attachment {0}", key); return(newEtag); }
public Etag AddDocumentInTransaction( string key, Etag etag, RavenJObject data, RavenJObject metadata, TransactionInformation transactionInformation, Etag committedEtag, IUuidGenerator uuidGenerator) { metadata.EnsureCannotBeChangeAndEnableSnapshotting(); data.EnsureCannotBeChangeAndEnableSnapshotting(); return(AddToTransactionState(key, etag, transactionInformation, committedEtag, new DocumentInTransactionData { Metadata = metadata, Data = data, Delete = false, Key = key, LastModified = SystemTime.UtcNow, Etag = uuidGenerator.CreateSequentialUuid(UuidType.DocumentTransactions) })); }
public void UpdateDocumentReferences(int id, string key, HashSet <string> references) { var documentReferencesByKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByKey); var documentReferencesByRef = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByRef); var documentReferencesByView = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByView); var documentReferencesByViewAndKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByViewAndKey); using (var iterator = documentReferencesByViewAndKey.MultiRead(Snapshot, CreateKey(id, key))) { if (iterator.Seek(Slice.BeforeAllKeys)) { do { RemoveDocumentReference(iterator.CurrentKey.Clone()); }while (iterator.MoveNext()); } } foreach (var reference in references) { var newKey = generator.CreateSequentialUuid(UuidType.DocumentReferences); var newKeyAsString = newKey.ToString(); var value = new RavenJObject { { "view", id }, { "key", key }, { "ref", reference } }; tableStorage.DocumentReferences.Add(writeBatch.Value, newKeyAsString, value); documentReferencesByKey.MultiAdd(writeBatch.Value, CreateKey(key), newKeyAsString); documentReferencesByRef.MultiAdd(writeBatch.Value, CreateKey(reference), newKeyAsString); documentReferencesByView.MultiAdd(writeBatch.Value, CreateKey(id), newKeyAsString); documentReferencesByViewAndKey.MultiAdd(writeBatch.Value, CreateKey(id, key), newKeyAsString); } }
public void Update(Session session, JET_DBID dbid) { Transaction tx; using (tx = new Transaction(session)) { int count = 0; const int rowsInTxCount = 100; using (var files = new Table(session, dbid, "files", OpenTableGrbit.None)) { var columnid = Api.GetColumnDictionary(session, files)["etag"]; Api.MoveBeforeFirst(session, files); while (Api.TryMoveNext(session, files)) { using (var update = new Update(session, files, JET_prep.Replace)) { Api.SetColumn(session, files, columnid, uuidGenerator.CreateSequentialUuid().TransformToValueForEsentSorting()); update.Save(); } if (count++ % rowsInTxCount == 0) { tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } } tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } using (var documents = new Table(session, dbid, "documents", OpenTableGrbit.None)) { var columnid = Api.GetColumnDictionary(session, documents)["etag"]; Api.MoveBeforeFirst(session, documents); while (Api.TryMoveNext(session, documents)) { using (var update = new Update(session, documents, JET_prep.Replace)) { Api.SetColumn(session, documents, columnid, uuidGenerator.CreateSequentialUuid().TransformToValueForEsentSorting()); update.Save(); } if (count++ % rowsInTxCount == 0) { tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } } tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } using (var indexesStats = new Table(session, dbid, "indexes_stats", OpenTableGrbit.None)) { var columnid = Api.GetColumnDictionary(session, indexesStats)["last_indexed_etag"]; Api.MoveBeforeFirst(session, indexesStats); while (Api.TryMoveNext(session, indexesStats)) { using (var update = new Update(session, indexesStats, JET_prep.Replace)) { Api.SetColumn(session, indexesStats, columnid, Guid.Empty.TransformToValueForEsentSorting()); update.Save(); } if (count++ % rowsInTxCount == 0) { tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } } tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } using (var documentsModifiedByTransaction = new Table(session, dbid, "documents_modified_by_transaction", OpenTableGrbit.None)) { var columnid = Api.GetColumnDictionary(session, documentsModifiedByTransaction)["etag"]; Api.MoveBeforeFirst(session, documentsModifiedByTransaction); while (Api.TryMoveNext(session, documentsModifiedByTransaction)) { using (var update = new Update(session, documentsModifiedByTransaction, JET_prep.Replace)) { Api.SetColumn(session, documentsModifiedByTransaction, columnid, uuidGenerator.CreateSequentialUuid().TransformToValueForEsentSorting()); update.Save(); } if (count++ % rowsInTxCount == 0) { tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } } tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } using (var mappedResults = new Table(session, dbid, "mapped_results", OpenTableGrbit.None)) { var columnid = Api.GetColumnDictionary(session, mappedResults)["etag"]; Api.MoveBeforeFirst(session, mappedResults); while (Api.TryMoveNext(session, mappedResults)) { using (var update = new Update(session, mappedResults, JET_prep.Replace)) { Api.SetColumn(session, mappedResults, columnid, uuidGenerator.CreateSequentialUuid().TransformToValueForEsentSorting()); update.Save(); } if (count++ % rowsInTxCount == 0) { tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } } tx.Commit(CommitTransactionGrbit.LazyFlush); tx.Dispose(); tx = new Transaction(session); } using (var details = new Table(session, dbid, "details", OpenTableGrbit.None)) { Api.JetMove(session, details, JET_Move.First, MoveGrbit.None); var columnids = Api.GetColumnDictionary(session, details); using (var update = new Update(session, details, JET_prep.Replace)) { Api.SetColumn(session, details, columnids["schema_version"], "3.0", Encoding.Unicode); update.Save(); } } tx.Commit(CommitTransactionGrbit.None); } }
public Etag AddAttachment(string key, Etag etag, Stream data, RavenJObject headers) { if (String.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } var loweredKey = CreateKey(key); var keyByETagIndice = attachmentsTable.GetIndex(Tables.Attachments.Indices.ByEtag); ushort?version; var isUpdate = attachmentsTable.Contains(Snapshot, loweredKey, writeBatch.Value, out version); if (isUpdate) { if (!metadataIndex.Contains(Snapshot, loweredKey, writeBatch.Value)) //precaution { throw new ApplicationException(String.Format(@"Headers for attachment with key = '{0}' were not found, but the attachment itself was found. Data corruption?", key)); } Etag existingEtag = null; if (etag != null && !IsAttachmentEtagMatch(loweredKey, etag, out existingEtag)) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag }; } if (existingEtag != null) //existingEtag can be null if etag parameter is null { keyByETagIndice.Delete(writeBatch.Value, existingEtag.ToString()); } else { var currentEtag = ReadCurrentEtag(loweredKey); keyByETagIndice.Delete(writeBatch.Value, currentEtag.ToString()); } } else { if (data == null) { throw new InvalidOperationException("When adding new attachment, the attachment data must be specified"); } if (!data.CanRead) //precaution { throw new InvalidOperationException("When adding/updating attachment, the attachment data stream must be readable"); } } var newETag = uuidGenerator.CreateSequentialUuid(UuidType.Attachments); if (data != null) { if (data.CanSeek) { data.Seek(0, SeekOrigin.Begin); attachmentsTable.Add(writeBatch.Value, loweredKey, data, version ?? 0); } else //handle streams like GzipStream { try { var tempStream = CreateStream(); data.CopyTo(tempStream); tempStream.Seek(0, SeekOrigin.Begin); attachmentsTable.Add(writeBatch.Value, loweredKey, tempStream, version ?? 0); } finally { data.Dispose(); } } } keyByETagIndice.Add(writeBatch.Value, newETag.ToString(), key); WriteAttachmentMetadata(loweredKey, newETag, headers); if (data != null && data.CanSeek) { logger.Debug("Fetched document attachment (key = '{0}', attachment size = {1})", key, data.Length); } else { logger.Debug("Fetched document attachment (key = '{0}')", key); } return(newETag); }