use of jetbrains.exodus.entitystore.Entity in project xodus by JetBrains.
the class InMemoryTimSortIterable method iterator.
@Override
public Iterator<Entity> iterator() {
return new Iterator<Entity>() {
private List<Entity> src;
private int runCount;
private int nodeCount;
/**
* for leaves (runs) this is index of first element left
* in particular, after init this is the leftmost element of the run
* for other nodes from[] contains index of the minimal element calculated
* or -1 if it's not calculated yet
*/
private int[] from;
private int[] len;
private int[] left;
private int[] right;
private int current;
private int head;
@Override
public boolean hasNext() {
if (src == null) {
init();
}
return current < src.size();
}
@Override
public Entity next() {
if (src == null) {
init();
}
if (current >= src.size()) {
throw new NoSuchElementException();
}
computeMinimum();
int r = from[head];
markAsUsed(r);
current++;
return src.get(r);
}
private void computeMinimum() {
ArrayList<Integer> stack = new ArrayList<>();
stack.add(head);
while (from[head] < 0) {
int node = stack.get(stack.size() - 1);
if (from[left[node]] < 0) {
stack.add(left[node]);
} else if (from[right[node]] < 0) {
stack.add(right[node]);
} else {
if (len[right[node]] <= 0 || (len[left[node]] > 0 && comparator.compare(src.get(from[left[node]]), src.get(from[right[node]])) <= 0)) {
from[node] = from[left[node]];
len[left[node]]--;
} else {
from[node] = from[right[node]];
len[right[node]]--;
}
stack.remove(stack.size() - 1);
}
}
}
private void markAsUsed(int r) {
int i = head;
while (left[i] >= 0) {
from[i] = -1;
if (from[left[i]] == r) {
i = left[i];
} else {
i = right[i];
}
}
from[i]++;
if (len[i] <= 0) {
from[i] = src.size();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
public void init() {
src = new ArrayList<>();
for (final Entity entity : source) {
src.add(entity);
}
int n = src.size();
if (n == 0) {
return;
}
int minRunLength = n;
int b = 0;
while (minRunLength >= InMemoryTimSortIterable.MIN_RUN_LENGTH) {
b |= minRunLength;
minRunLength >>= 1;
}
minRunLength += b & 1;
int maxRuns = 1 + (n + minRunLength - 1) / minRunLength;
from = new int[maxRuns * 2 - 1];
len = new int[maxRuns * 2 - 1];
left = new int[maxRuns * 2 - 1];
Arrays.fill(left, -1);
right = new int[maxRuns * 2 - 1];
ArrayList<Integer> stack = new ArrayList<>();
runCount = 0;
boolean growing = true;
for (int i = 1; i < n; i++) {
// on the run
if ((comparator.compare(src.get(i - 1), src.get(i)) <= 0) == growing) {
continue;
}
// start of run
if (i == from[runCount] + 1) {
growing = !(growing);
continue;
}
// reverse if needed
if (!(growing)) {
reverse(i);
}
// insert several next elements into run to achieve run length minRunLength
for (; i < n && i - from[runCount] < minRunLength; i++) {
Entity t = src.get(i);
int j = i - 1;
for (; j >= from[runCount] && comparator.compare(src.get(j), t) > 0; j--) {
src.set(j + 1, src.get(j));
}
src.set(j + 1, t);
}
len[runCount] = i - from[runCount];
from[++runCount] = i;
stack.add(runCount - 1);
// collapse
while (true) {
int s = stack.size();
if (s >= 3 && len[stack.get(s - 3)] <= len[stack.get(s - 2)] + len[stack.get(s - 1)]) {
if (len[stack.get(s - 3)] < len[stack.get(s - 1)]) {
unite(stack, s - 3, maxRuns);
} else {
unite(stack, s - 2, maxRuns);
}
} else if (s >= 2 && len[stack.get(s - 2)] <= len[stack.get(s - 1)]) {
unite(stack, s - 2, maxRuns);
} else {
break;
}
}
}
// last run
len[runCount] = n - from[runCount];
if (len[runCount] > 0) {
// reverse if needed
if (!(growing)) {
reverse(n);
}
stack.add(runCount);
runCount++;
}
// collapse remaining
for (int i = stack.size() - 2; i >= 0; i--) {
if (i > 0 && len[stack.get(i - 1)] < len[stack.get(i + 1)]) {
unite(stack, i - 1, maxRuns);
} else {
unite(stack, i, maxRuns);
}
}
// for inner nodes min value is unset
for (int i = 0; i < nodeCount; i++) {
from[i + maxRuns] = -1;
}
head = stack.get(0);
}
private void reverse(int to) {
for (int l = from[runCount], r = to - 1; l < r; l++, r--) {
Entity t = src.get(l);
src.set(l, src.get(r));
src.set(r, t);
}
}
/**
* replaces stack[p] and stack[p + 1] with a node pointing to them
* @param stack stack
* @param p p
* @param shift shift
*/
private void unite(ArrayList<Integer> stack, int p, int shift) {
int node = nodeCount + shift;
from[node] = from[stack.get(p)];
len[node] = len[stack.get(p)] + len[stack.get(p + 1)];
left[node] = stack.get(p);
right[node] = stack.get(p + 1);
stack.set(p, node);
nodeCount++;
stack.remove(p + 1);
}
};
}
use of jetbrains.exodus.entitystore.Entity in project xodus by JetBrains.
the class LinksEqualDecorator method instantiateDecorated.
protected Iterable<Entity> instantiateDecorated(String entityType, QueryEngine queryEngine, ModelMetaData metaData) {
Iterable<Entity> result = decorated.instantiate(entityType, queryEngine, metaData);
final EntityMetaData emd = metaData == null ? null : metaData.getEntityMetaData(entityType);
if (emd != null) {
for (String subType : emd.getSubTypes()) {
result = queryEngine.unionAdjusted(result, instantiateDecorated(subType, queryEngine, metaData));
}
}
return result;
}
use of jetbrains.exodus.entitystore.Entity in project xodus by JetBrains.
the class TreeKeepingEntityIterable method instantiateForWholeHierarchy.
private Iterable<Entity> instantiateForWholeHierarchy(final String entityType, final NodeBase ast) {
final ModelMetaData mmd = queryEngine.getModelMetaData();
final EntityMetaData emd = mmd == null ? null : mmd.getEntityMetaData(entityType);
Iterable<Entity> result = ast.getClone().instantiate(entityType, queryEngine, mmd);
if (!(emd == null || ast.polymorphic())) {
for (String subType : emd.getSubTypes()) {
if (Utils.getUnionSubtypes()) {
// union returns sorted by id results provided its operands are sorted by id
result = queryEngine.unionAdjusted(result, instantiateForWholeHierarchy(subType, ast));
} else {
result = queryEngine.concatAdjusted(result, instantiateForWholeHierarchy(subType, ast));
}
}
}
return queryEngine.adjustEntityIterable(result);
}
use of jetbrains.exodus.entitystore.Entity in project xodus by JetBrains.
the class TreeKeepingEntityIterable method instantiate.
@SuppressWarnings("UnusedDeclaration")
@Override
public Iterable<Entity> instantiate() {
optimize();
Iterable<Entity> result;
if (instance == null) {
result = instantiateForWholeHierarchy();
} else if (optimizedTree instanceof GetAll) {
result = instance;
} else {
// clone
TreeKeepingEntityIterable tkei = new TreeKeepingEntityIterable(null, entityType, optimizedTree.getClone(), queryEngine);
tkei.optimizedTree = optimizedTree;
result = queryEngine.toEntityIterable(queryEngine.intersect(instance, tkei));
}
if (sorts != null) {
result = sorts.apply(entityType, result, queryEngine);
}
if (result == null) {
result = instantiateForWholeHierarchy();
}
if (isExplainOn) {
Iterable<Entity> explained = result;
while (explained instanceof SortEngine.InMemorySortIterable) {
explained = ((SortEngine.InMemorySortIterable) explained).source;
}
if (explained instanceof EntityIterableBase) {
EntityIterable entityIterable = ((EntityIterableBase) explained).getSource();
if (entityIterable instanceof EntityIterableBase && entityIterable != EntityIterableBase.EMPTY) {
EntityIterableBase entityIterableBase = ((EntityIterableBase) entityIterable);
final PersistentEntityStoreImpl store = queryEngine.getPersistentStore();
Explainer explainer = store.getExplainer();
if (!Explainer.isExplainForcedForThread()) {
explainer.start(origin);
}
entityIterableBase.setOrigin(origin);
explainer.explain(origin, Explainer.INITIAL_TREE, annotatedTree);
explainer.explain(origin, Explainer.OPTIMIZED_TREE, optimizedTree);
if (!Explainer.isExplainForcedForThread()) {
for (Entity entity : result) {
explainer.explain(origin, Explainer.ITERABLE_ADVANCES);
}
explainer.log(origin);
}
}
}
}
return result;
}
Aggregations