private long InitializeLastDatabaseEtagOnIndexCreation(TransactionOperationContext indexContext) { const string key = "LastEtag"; if (_environment.IsNew == false) { var tree = indexContext.Transaction.InnerTransaction.ReadTree(IndexSchema.LastDocumentEtagOnIndexCreationTree); var result = tree?.Read(key); return(result?.Reader.ReadLittleEndianInt64() ?? 0); } using (var queryContext = QueryOperationContext.Allocate(DocumentDatabase, _index)) using (queryContext.OpenReadTransaction()) using (Slice.From(indexContext.Allocator, key, out var slice)) { var lastDatabaseEtag = DocumentsStorage.ReadLastEtag(queryContext.Documents.Transaction.InnerTransaction); var tree = indexContext.Transaction.InnerTransaction.CreateTree(IndexSchema.LastDocumentEtagOnIndexCreationTree); tree.Add(slice, lastDatabaseEtag); return(lastDatabaseEtag); } }
public static bool ShouldReplace(Index index, ref bool?isSideBySide) { if (isSideBySide.HasValue == false) { isSideBySide = index.Name.StartsWith(Constants.Documents.Indexing.SideBySideIndexNamePrefix, StringComparison.OrdinalIgnoreCase); } if (isSideBySide == false) { return(false); } using (var context = QueryOperationContext.Allocate(index.DocumentDatabase, index)) using (index._contextPool.AllocateOperationContext(out TransactionOperationContext indexContext)) { using (indexContext.OpenReadTransaction()) using (context.OpenReadTransaction()) { indexContext.IgnoreStalenessDueToReduceOutputsToDelete = true; try { var canReplace = index.IsStale(context, indexContext) == false; if (canReplace) { isSideBySide = null; } return(canReplace); } finally { indexContext.IgnoreStalenessDueToReduceOutputsToDelete = false; } } } }