private List<JsonDocument> GetDocsToReplicate(IStorageActionsAccessor actions, PrefetchingBehavior prefetchingBehavior, JsonDocumentsToReplicate result, int? maxNumberOfItemsToReceiveInSingleBatch)
{
var docsToReplicate = prefetchingBehavior.GetDocumentsBatchFrom(result.LastEtag, maxNumberOfItemsToReceiveInSingleBatch);
Etag lastEtag = null;
if (docsToReplicate.Count > 0)
lastEtag = docsToReplicate[docsToReplicate.Count - 1].Etag;
var maxNumberOfTombstones = Math.Max(1024, docsToReplicate.Count);
var tombstones = actions
.Lists
.Read(Constants.RavenReplicationDocsTombstones, result.LastEtag, lastEtag, maxNumberOfTombstones + 1)
.Select(x => new JsonDocument
{
Etag = x.Etag,
Key = x.Key,
Metadata = x.Data,
DataAsJson = new RavenJObject()
})
.ToList();
var results = docsToReplicate.Concat(tombstones);
if (tombstones.Count >= maxNumberOfTombstones + 1)
{
var lastTombstoneEtag = tombstones[tombstones.Count - 1].Etag;
log.Info("Replication batch trimmed. Found more than '{0}' document tombstones. Last etag from prefetcher: '{1}'. Last tombstone etag: '{2}'.", maxNumberOfTombstones, lastEtag, lastTombstoneEtag);
results = results.Where(x => EtagUtil.IsGreaterThan(x.Etag, lastTombstoneEtag) == false);
}
results = results.OrderBy(x => x.Etag);
// can't return earlier, because we need to know if there are tombstones that need to be send
if (maxNumberOfItemsToReceiveInSingleBatch.HasValue)
results = results.Take(maxNumberOfItemsToReceiveInSingleBatch.Value);
return results.ToList();
}