use of jetbrains.exodus.entitystore.iterate.EntityIterableBase 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.iterate.EntityIterableBase 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.iterate.EntityIterableBase 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;
}
use of jetbrains.exodus.entitystore.iterate.EntityIterableBase in project xodus by JetBrains.
the class EntityIterableCache method putIfNotCached.
/**
* @param it iterable.
* @return iterable which is cached or "it" itself if it's not cached.
*/
public EntityIterableBase putIfNotCached(@NotNull final EntityIterableBase it) {
if (isCachingDisabled || !it.canBeCached()) {
return it;
}
final EntityIterableHandle handle = it.getHandle();
final PersistentStoreTransaction txn = it.getTransaction();
final EntityIterableCacheAdapter localCache = txn.getLocalCache();
txn.localCacheAttempt();
final EntityIterableBase cached = localCache.tryKey(handle);
if (cached != null) {
if (!cached.getHandle().isExpired()) {
txn.localCacheHit();
return cached;
}
localCache.remove(handle);
}
if (txn.isMutable() || !txn.isCurrent() || !txn.isCachingRelevant()) {
return it;
}
// if cache is enough full, then cache iterables after they live some time in deferred cache
if (!localCache.isSparse()) {
final long currentMillis = System.currentTimeMillis();
final Object handleIdentity = handle.getIdentity();
final Long whenCached = deferredIterablesCache.tryKey(handleIdentity);
if (whenCached == null) {
deferredIterablesCache.cacheObject(handleIdentity, currentMillis);
return it;
}
if (whenCached + config.getEntityIterableCacheDeferredDelay() > currentMillis) {
return it;
}
}
// then instantiate iterable without queueing a job.
if (isDispatcherThread()) {
return it.getOrCreateCachedInstance(txn);
}
if (!isCachingQueueFull()) {
new EntityIterableAsyncInstantiation(handle, it, true).queue(Priority.below_normal);
}
return it;
}
use of jetbrains.exodus.entitystore.iterate.EntityIterableBase in project xodus by JetBrains.
the class BinaryOperatorsTests method testConcat2.
@TestFor(issue = "XD-566")
public void testConcat2() {
final StoreTransaction txn = getStoreTransaction();
Objects.requireNonNull(txn).newEntity("Issue");
Assert.assertEquals(1, toList(txn.getAll("User").concat(txn.getAll("Issue"))).size());
Assert.assertTrue(txn.flush());
txn.newEntity("User");
Assert.assertTrue(txn.flush());
Assert.assertEquals(2, toList(txn.getAll("User").concat(txn.getAll("Issue"))).size());
Objects.requireNonNull(txn.getAll("User").getFirst()).delete();
Assert.assertTrue(txn.flush());
while (true) {
txn.revert();
final EntityIterableBase concat = (EntityIterableBase) txn.getAll("User").concat(txn.getAll("Issue"));
Assert.assertEquals(1, toList(concat).size());
if (concat.isCached())
break;
Thread.yield();
}
txn.revert();
txn.newEntity("User");
Assert.assertTrue(txn.flush());
final EntityIterableBase concat = (EntityIterableBase) txn.getAll("User").concat(txn.getAll("Issue"));
Assert.assertFalse(concat.isCached());
Assert.assertEquals(2, toList(concat).size());
}
Aggregations