in entity-store/src/main/java/jetbrains/exodus/entitystore/iterate/SortIterable.java [124:157]
public EntityIterator getIteratorImpl(@NotNull final PersistentStoreTransaction txn) {
if (propIndex == EntityIterableBase.EMPTY) {
return new EntityTypeFilteredIterator(source, sourceTypeId);
}
final PersistentEntityStoreImpl store = getStore();
final EntityIterableCache entityIterableCache = store.getEntityIterableCache();
final EntityIterableBase cachedPropertyIndex = entityIterableCache.putIfNotCached(propIndex);
if (propIndex.nonCachedHasFastCountAndIsEmpty() && store.getConfig().isDebugAllowInMemorySort()) {
// if property index is much greater than source then it makes sense to sort source in-memory (XD-609)
final long sourceSize = entityIterableCache.isDispatcherThread() ? -1 : source.getRoughCount();
if (sourceSize >= 0) {
final long indexSize = cachedPropertyIndex.size();
final long log2IndexSize = MathUtil.longLogarithm(indexSize);
final long sizeMulLog = sourceSize * log2IndexSize;
final boolean isCachedInstance = cachedPropertyIndex.isCachedInstance();
if ((isCachedInstance && sizeMulLog * sourceSize < indexSize) ||
(!isCachedInstance && sizeMulLog * log2IndexSize < indexSize)) {
return new OptionallyStableInMemorySortIterator((int) sourceSize, stableSort);
}
}
}
final EntityIterator propIterator = ascending ?
cachedPropertyIndex.getIteratorImpl(txn) : cachedPropertyIndex.getReverseIteratorImpl(txn);
if (propIterator == EntityIteratorBase.EMPTY) {
return new EntityTypeFilteredIterator(source, sourceTypeId);
}
if (stableSort) {
final StableSortIterator itr = new StableSortIterator((PropertyValueIterator) propIterator);
return new PropertyValueIteratorFixingDecorator(this, itr, itr);
}
return new EntityIteratorFixingDecorator(this, new NonStableSortIterator(txn, propIterator));
}