use of jetbrains.exodus.entitystore.ComparableGetter in project xodus by JetBrains.
the class QueryTreeTest method testCloneAndAnalyze.
public void testCloneAndAnalyze() throws Exception {
NodeBase tree = new Root(new UnaryNot(new Concat(new Minus(getTree(queryEngine.query(null, "TstClass", new Or(new And(new PropertyEqual("i", 1), new PropertyEqual("i", 2)), new PropertyEqual("i", 3)))), getTree(queryEngine.query(null, "TstClass", new PropertyEqual("i", 4)))), new Concat(getTree(queryEngine.queryGetAll("TstClass")), new Concat(getTree(queryEngine.query(null, "TstClass", new LinkEqual("itself", QueryUtil.getFirst(queryEngine, queryEngine.queryGetAll("TstClass"))))), new Concat(new LinkNotNull("itself"), new Concat(getTree(queryEngine.query(null, "TstClass", new LinksEqualDecorator("itself", new LinkEqual("self1", QueryUtil.getFirst(queryEngine, queryEngine.queryGetAll("TstClass"))), "TstClass"))), new Concat(new PropertyNotNull("s"), new Concat(getTree(queryEngine.query(null, "TstClass", new PropertyRange("i", QueryUtil.nextGreater(0, Integer.class), QueryUtil.positiveInfinity(Integer.class)))), getTree(queryEngine.query(null, "TstClass", new PropertyStartsWith("s", "val"))))))))))));
NodeBase clone = tree.getClone();
Assert.assertEquals(tree, clone);
tree = ((UnaryNode) tree).getChild();
Assert.assertEquals(new Minus(NodeFactory.all(), ((UnaryNode) tree).getChild().getClone()), getOptimizedTree(tree));
ComparableGetter valueGetter = new ComparableGetter() {
@Override
public Comparable select(Entity entity) {
return entity.getProperty("i");
}
};
ComparableGetterSort genericSort = ComparableGetterSort.create(concat, valueGetter, true);
for (int i = 0; i < 4; i++) {
Assert.assertEquals(getAnalyzedSortCount(genericSort), i + 1);
genericSort = new ComparableGetterSort(genericSort, valueGetter, true);
}
Assert.assertEquals(getAnalyzedSortCount(genericSort), 4);
}
use of jetbrains.exodus.entitystore.ComparableGetter in project xodus by JetBrains.
the class SortEngine method sort.
public Iterable<Entity> sort(String entityType, final String propertyName, Iterable<Entity> source, final boolean ascending) {
ComparableGetter valueGetter = propertyGetter(propertyName);
final ModelMetaData mmd = queryEngine.getModelMetaData();
if (mmd != null) {
final EntityMetaData emd = mmd.getEntityMetaData(entityType);
if (emd != null) {
if (source == null) {
return mergeSorted(emd, new IterableGetter() {
@Override
public EntityIterableBase getIterable(String type) {
queryEngine.assertOperational();
return (EntityIterableBase) queryEngine.getPersistentStore().getAndCheckCurrentTransaction().sort(type, propertyName, ascending);
}
}, valueGetter, caseInsensitiveComparator(ascending));
}
final Iterable<Entity> i = queryEngine.toEntityIterable(source);
if (queryEngine.isPersistentIterable(i)) {
final EntityIterable it = ((EntityIterableBase) i).getSource();
if (it == EntityIterableBase.EMPTY) {
return queryEngine.wrap(EntityIterableBase.EMPTY);
}
if (it.getRoughCount() == 0 && it.count() == 0) {
return queryEngine.wrap(EntityIterableBase.EMPTY.asSortResult());
}
return mergeSorted(emd, new IterableGetter() {
@Override
public EntityIterableBase getIterable(String type) {
queryEngine.assertOperational();
return (EntityIterableBase) queryEngine.getPersistentStore().getAndCheckCurrentTransaction().sort(type, propertyName, it, ascending);
}
}, valueGetter, caseInsensitiveComparator(ascending));
}
}
}
if (source == null) {
source = getAllEntities(entityType, mmd);
}
return sortInMemory(source, valueGetter, ascending);
}
use of jetbrains.exodus.entitystore.ComparableGetter in project xodus by JetBrains.
the class SortEngine method mergeSorted.
private EntityIterableBase mergeSorted(EntityMetaData emd, IterableGetter sorted, final ComparableGetter valueGetter, final Comparator<Comparable<Object>> comparator) {
EntityIterableBase result;
if (!(emd.hasSubTypes())) {
result = sorted.getIterable(emd.getType());
} else {
final List<EntityIterable> iterables = new ArrayList<>(4);
EntityIterableBase source = sorted.getIterable(emd.getType()).getSource();
if (source != EntityIterableBase.EMPTY) {
iterables.add(source);
}
for (final String type : emd.getAllSubTypes()) {
source = sorted.getIterable(type).getSource();
if (source != EntityIterableBase.EMPTY) {
iterables.add(source);
}
}
int iterablesCount = iterables.size();
if (iterablesCount == 0) {
result = EntityIterableBase.EMPTY;
} else if (iterablesCount == 1) {
result = (EntityIterableBase) iterables.get(0);
} else {
queryEngine.assertOperational();
result = (EntityIterableBase) queryEngine.getPersistentStore().getAndCheckCurrentTransaction().mergeSorted(iterables, new ComparableGetter() {
@Override
public Comparable select(Entity entity) {
return valueGetter.select(attach(entity));
}
}, comparator);
}
}
return (EntityIterableBase) queryEngine.wrap(result.getSource().asSortResult());
}
use of jetbrains.exodus.entitystore.ComparableGetter in project xodus by JetBrains.
the class QueryTreeTest method testGenericSort.
public void testGenericSort() throws Exception {
ComparableGetterSort sortNode = new ComparableGetterSort(concat, new ComparableGetter() {
@Override
public Comparable select(Entity entity) {
return entity.getProperty("i");
}
}, true);
Assert.assertEquals(sortNode, sortNode.getClone());
Assert.assertEquals(4, QueryUtil.getSize(instantiate(sortNode)));
Assert.assertEquals(4, QueryUtil.getSize(instantiate(new And(sortNode, NodeFactory.all()))));
Assert.assertFalse(sortNode.equals(propertyEqual));
Assert.assertFalse(propertyEqual.equals(sortNode));
}
Aggregations