Search in sources :

Example 1 with EntityIterable

use of jetbrains.exodus.entitystore.EntityIterable 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);
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable) ComparableGetter(jetbrains.exodus.entitystore.ComparableGetter) EntityIterableBase(jetbrains.exodus.entitystore.iterate.EntityIterableBase)

Example 2 with EntityIterable

use of jetbrains.exodus.entitystore.EntityIterable 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());
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable) ComparableGetter(jetbrains.exodus.entitystore.ComparableGetter) EntityIterableBase(jetbrains.exodus.entitystore.iterate.EntityIterableBase) ArrayList(java.util.ArrayList)

Example 3 with EntityIterable

use of jetbrains.exodus.entitystore.EntityIterable in project xodus by JetBrains.

the class SortEngine method getAllEntities.

private Iterable<Entity> getAllEntities(final String entityType, final ModelMetaData mmd) {
    queryEngine.assertOperational();
    EntityIterable it = queryEngine.instantiateGetAll(entityType);
    final EntityMetaData emd = mmd.getEntityMetaData(entityType);
    if (emd != null) {
        for (String subType : emd.getSubTypes()) {
            if (Utils.getUnionSubtypes()) {
                it = ((EntityIterable) getAllEntities(subType, mmd)).union(it);
            } else {
                it = ((EntityIterable) getAllEntities(subType, mmd)).concat(it);
            }
        }
    }
    return queryEngine.wrap(it);
}
Also used : EntityIterable(jetbrains.exodus.entitystore.EntityIterable)

Example 4 with EntityIterable

use of jetbrains.exodus.entitystore.EntityIterable in project meghanada-server by mopemope.

the class ProjectDatabase method deleteObject.

public boolean deleteObject(String entityType, String id) throws Exception {
    return this.entityStore.computeInTransaction(txn -> {
        EntityIterable it = txn.find(entityType, ID, id);
        Entity entity = it.getFirst();
        if (isNull(entity)) {
            return false;
        }
        return entity.delete();
    });
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable)

Example 5 with EntityIterable

use of jetbrains.exodus.entitystore.EntityIterable in project meghanada-server by mopemope.

the class ProjectDatabase method findAllObject.

public <T> void findAllObject(String entityType, Class<T> clazz, Consumer<T> consumer) throws Exception {
    this.entityStore.executeInReadonlyTransaction(txn -> {
        EntityIterable all = txn.getAll(entityType);
        for (Entity entity : all) {
            try (InputStream in = entity.getBlob(SERIALIZE_KEY)) {
                T t = Serializer.readObject(in, clazz);
                consumer.accept(t);
            } catch (Exception e) {
                log.warn(e.getMessage());
            }
        }
    });
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExodusException(jetbrains.exodus.ExodusException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) EntityStoreException(jetbrains.exodus.entitystore.EntityStoreException)

Aggregations

EntityIterable (jetbrains.exodus.entitystore.EntityIterable)24 Entity (jetbrains.exodus.entitystore.Entity)23 IOException (java.io.IOException)11 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)5 EntityStoreException (jetbrains.exodus.entitystore.EntityStoreException)4 EntityIterableBase (jetbrains.exodus.entitystore.iterate.EntityIterableBase)4 UncheckedIOException (java.io.UncheckedIOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nonnull (javax.annotation.Nonnull)2 ExodusException (jetbrains.exodus.ExodusException)2 ComparableGetter (jetbrains.exodus.entitystore.ComparableGetter)2 EntityId (jetbrains.exodus.entitystore.EntityId)2 File (java.io.File)1 Serializable (java.io.Serializable)1 EntityIterator (jetbrains.exodus.entitystore.EntityIterator)1 Explainer (jetbrains.exodus.entitystore.Explainer)1 PersistentEntityStoreImpl (jetbrains.exodus.entitystore.PersistentEntityStoreImpl)1