Skip to content

Commit

Permalink
RavenDB-21900 When query has no load, use small cache to avoid persis…
Browse files Browse the repository at this point in the history
…ting document too long.
  • Loading branch information
maciejaszyk authored and arekpalinski committed May 10, 2024
1 parent aeb6c7d commit 55736cc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/Raven.Server/Documents/DocumentsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ public IEnumerable<TDocument> GetDocuments<TDocument>(DocumentsOperationContext
listOfIds.Add(slice);
}

return GetDocuments(context, listOfIds, start, take);
return GetDocuments<TDocument>(context, listOfIds, start, take);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -1049,7 +1049,7 @@ public IEnumerable<TDocument> GetDocumentsForCollection<TDocument>(DocumentsOper
where TDocument : Document, new()
{
// we'll fetch all documents and do the filtering here since we must check the collection name
foreach (var doc in GetDocuments<TDocument>(context, ids, start, int.MaxValue, totalCount))
foreach (var doc in GetDocuments<TDocument>(context, ids, start, int.MaxValue))
{
if (collection == Constants.Documents.Collections.AllDocumentsCollection)
{
Expand Down
58 changes: 4 additions & 54 deletions src/Raven.Server/Documents/Queries/CollectionQueryEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ private void CountDocumentsInEnumerator(bool countQuery)

if (_fieldsToFetch.IsProjection)
{
RetrieverInput retrieverInput = new(null, QueryResultRetrieverBase.ZeroScore, null);
var result = _resultsRetriever.GetProjectionFromDocument(_inner.Current, ref retrieverInput, _fieldsToFetch, _context, _token);
if (result.List != null)
{
Expand Down Expand Up @@ -464,7 +463,7 @@ private void ConfigureStreamForUnboundedQueries()
_totalResultsCalculated = true;
}
}

private void Initialize(out bool processedAllDocuments)
{
_initialized = true;
Expand All @@ -474,56 +473,8 @@ private void Initialize(out bool processedAllDocuments)

if (start == 0)
{
var count = 0;
foreach (var document in _documents.GetDocumentsFrom<QueriedDocument>(_context, _collection, 0, start, _query.PageSize))
{
count++;
RetrieverInput retrieverInput = new(null, QueryResultRetrieverCommon.ZeroScore, null);
if (_fieldsToFetch.IsProjection)
{
var result = _resultsRetriever.GetProjectionFromDocument(document, ref retrieverInput, _fieldsToFetch, _context, _token);
if (result.Document != null)
{
if (IsStartingPoint(result.Document))
break;
}
else if (result.List != null)
{
bool match = false;
foreach (Document item in result.List)
{
if (IsStartingPoint(item))
{
match = true;
break;
}
}

if (match)
break;
}
}
else
{
if (IsStartingPoint(_inner.Current))
{
break;
}
}

bool IsStartingPoint(Document d)
{
return d.Data.Count > 0 && _alreadySeenProjections.Add(d.DataHash) && _alreadySeenProjections.Count == _query.Start;
}
}

if (_alreadySeenProjections.Count == _query.Start)
break;

if (count < _query.PageSize)
break;

start += count;
_inner = GetDocuments(out _totalResultsCalculated, start).GetEnumerator();
return;
}

if (_query.SkipDuplicateChecking)
Expand Down Expand Up @@ -561,7 +512,6 @@ void ReleaseDocument()
if (document != null)
{
document.Dispose();
_context.Transaction.ForgetAbout(document);
}
}
}
Expand All @@ -580,7 +530,7 @@ bool IsStartingPoint(Document d)
if (count < _query.Start)
processedAllDocuments = true;
}

public void Reset()
{
throw new NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Raven.Server.ServerWide.Context;
using Raven.Server.Utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ namespace Raven.Server.Documents.Queries.Results
{
public abstract class QueryResultRetrieverCommon
{
public static readonly int LoadedDocumentsCacheSize = 16 * 1024;
public const int LoadedDocumentsCacheSize = 16 * 1024;
public const int NoLoadedDocumentsCacheSize = 32;

public static readonly Lucene.Net.Search.ScoreDoc ZeroScore = new Lucene.Net.Search.ScoreDoc(-1, 0f);

Expand Down Expand Up @@ -149,10 +150,13 @@ protected QueryResultRetrieverBase(
DocumentFields = query?.DocumentFields ?? DocumentFields.All;

_blittableTraverser = reduceResults ? BlittableJsonTraverser.FlatMapReduceResults : BlittableJsonTraverser.Default;


var cacheSize = query?.Metadata.HasIncludeOrLoad ?? false
? LoadedDocumentsCacheSize
: NoLoadedDocumentsCacheSize;
_loadedDocumentCache = typeof(TDocument) == typeof(QueriedDocument) && DocumentContext != null
? (LruDictionary<string, TDocument>)(object)(new QueriedDocumentCache(DocumentContext, LoadedDocumentsCacheSize))
: new LruDictionary<string, TDocument>(LoadedDocumentsCacheSize);
? (LruDictionary<string, TDocument>)(object)(new QueriedDocumentCache(DocumentContext, cacheSize))
: new LruDictionary<string, TDocument>(cacheSize);
}


Expand Down

0 comments on commit 55736cc

Please sign in to comment.