use of jetbrains.exodus.entitystore.Entity in project xodus by JetBrains.
the class QueryTreeTest method t1.
private Entity t1(PersistentStoreTransaction txn, Entity enumeration) {
final Entity e = txn.newEntity("TstClass");
e.setLink("itself", e);
e.setLink("myEnum", enumeration);
e.setLink("self1", e);
e.setLink("self2", e);
return e;
}
use of jetbrains.exodus.entitystore.Entity 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.Entity 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.Entity 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.Entity in project xodus by JetBrains.
the class QueryEngine method adjustEntityIterable.
public Iterable<Entity> adjustEntityIterable(Iterable<Entity> it) {
if (it == EntityIterableBase.EMPTY) {
return it;
}
// try to convert collection to entity iterable.
if (it instanceof Collection) {
final Collection<Entity> collection = (Collection<Entity>) it;
final Iterator<Entity> itr = collection.iterator();
if (itr.hasNext()) {
final Entity e = itr.next();
if (!itr.hasNext()) {
final Iterable<Entity> wrapped = wrap(e);
if (wrapped != null) {
it = wrapped;
}
}
} else {
return EntityIterableBase.EMPTY;
}
}
// wrap with transient iterable
return isPersistentIterable(it) && !isWrapped(it) ? wrap(((EntityIterableBase) it).getSource()) : it;
}
Aggregations