CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/ExponentialPageLimits.java

46 lines
1.2 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.lucene;
2021-09-20 18:20:59 +02:00
2021-09-21 02:01:02 +02:00
/**
* <pre>y = 2 ^ (x + pageIndexOffset) + firstPageLimit</pre>
*/
2021-09-20 18:20:59 +02:00
public class ExponentialPageLimits implements PageLimits {
2021-09-21 02:01:02 +02:00
private static final int DEFAULT_PAGE_INDEX_OFFSET = 0;
private final int pageIndexOffset;
2021-09-20 18:20:59 +02:00
private final int firstPageLimit;
private final int maxItemsPerPage;
public ExponentialPageLimits() {
2021-09-21 02:01:02 +02:00
this(DEFAULT_PAGE_INDEX_OFFSET);
}
public ExponentialPageLimits(int pageIndexOffset) {
this(pageIndexOffset, DEFAULT_MIN_ITEMS_PER_PAGE);
2021-09-20 18:20:59 +02:00
}
2021-09-21 02:01:02 +02:00
public ExponentialPageLimits(int pageIndexOffset, int firstPageLimit) {
this(pageIndexOffset, firstPageLimit, DEFAULT_MAX_ITEMS_PER_PAGE);
2021-09-20 18:20:59 +02:00
}
2021-09-21 02:01:02 +02:00
public ExponentialPageLimits(int pageIndexOffset, int firstPageLimit, int maxItemsPerPage) {
this.pageIndexOffset = pageIndexOffset;
2021-09-20 18:20:59 +02:00
this.firstPageLimit = firstPageLimit;
this.maxItemsPerPage = maxItemsPerPage;
}
@Override
public int getPageLimit(int pageIndex) {
2021-09-21 02:01:02 +02:00
var offsetedIndex = pageIndex + pageIndexOffset;
var power = 0b1L << offsetedIndex;
if (offsetedIndex >= 30) { // safety
2021-09-20 18:20:59 +02:00
return maxItemsPerPage;
}
2021-09-21 02:01:02 +02:00
var min = Math.max(firstPageLimit, Math.min(maxItemsPerPage, firstPageLimit + power));
2021-09-20 18:20:59 +02:00
assert min > 0;
2021-09-21 02:01:02 +02:00
return LuceneUtils.safeLongToInt(min);
2021-09-20 18:20:59 +02:00
}
}