Search in sources :

Example 6 with Entity

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

the class InMemoryQuickSortTwoSidesIterable method iterator.

@Override
public Iterator<Entity> iterator() {
    return new Iterator<Entity>() {

        private List<Entity> src;

        private Entity[] tmp;

        private int current;

        @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();
            }
            return src.get(current++);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void init() {
            src = new ArrayList<>();
            for (final Entity entity : source) {
                src.add(entity);
            }
            tmp = new Entity[src.size()];
            qsort(0, tmp.length - 1);
        }

        /**
         * sort src[left..right]
         * @param left left
         * @param right right
         */
        public void qsort(int left, int right) {
            if (left >= right) {
                return;
            }
            Entity median = src.get((left + right) / 2);
            int i = left;
            int j = right;
            int toRight = 0;
            int toLeft = 0;
            List<Entity> leftMedians = new ArrayList<>();
            List<Entity> rightMedians = new ArrayList<>();
            do {
                while (i <= j && comparator.compare(src.get(i), median) < 0) {
                    if (toRight + leftMedians.size() > 0) {
                        src.set(i - toRight - leftMedians.size(), src.get(i));
                    }
                    i++;
                }
                while (i <= j && comparator.compare(median, src.get(j)) < 0) {
                    if (toLeft + rightMedians.size() > 0) {
                        src.set(j + toLeft + rightMedians.size(), src.get(j));
                    }
                    j--;
                }
                // src[i] >= median and src[j] <= median
                if (i <= j) {
                    Entity leftEntity = src.get(i);
                    if (comparator.compare(leftEntity, median) == 0) {
                        leftMedians.add(leftEntity);
                    } else {
                        tmp[toRight++] = leftEntity;
                    }
                    i++;
                }
                if (i <= j) {
                    Entity rightEntity = src.get(j);
                    if (comparator.compare(rightEntity, median) == 0) {
                        rightMedians.add(rightEntity);
                    } else {
                        tmp[right - toLeft++] = src.get(j);
                    }
                    j--;
                }
            } while (i <= j);
            // i == j + 1
            int current = i - toRight - leftMedians.size();
            for (int k = right - toLeft + 1; k <= right; k++) {
                src.set(current++, tmp[k]);
            }
            for (Entity e : leftMedians) {
                src.set(current++, e);
            }
            for (int k = rightMedians.size() - 1; k >= 0; k--) {
                src.set(current++, rightMedians.get(k));
            }
            for (int k = 0; k < toRight; k++) {
                src.set(current++, tmp[k]);
            }
            qsort(left, i - toRight - leftMedians.size() + toLeft);
            qsort(current - toRight, right);
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity)

Example 7 with Entity

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

the class InMemoryMergeSortOnInitIterable method iterator.

@Override
public Iterator<Entity> iterator() {
    return new Iterator<Entity>() {

        private List<Entity>[] src;

        private int current;

        @Override
        public boolean hasNext() {
            if (src == null) {
                init();
            }
            return current < src[0].size();
        }

        @Override
        public Entity next() {
            if (src == null) {
                init();
            }
            if (current >= src[0].size()) {
                throw new NoSuchElementException();
            }
            return src[0].get(current++);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void init() {
            src = new ArrayList[] { new ArrayList<>(), null };
            for (final Entity entity : source) {
                src[0].add(entity);
            }
            src[1] = new ArrayList<>(src[0]);
            msort(0, src[0].size() - 1, 0);
        }

        /**
         * sort interval left..right to src[c][left..right]
         * @param left left
         * @param right right
         * @param c c
         */
        public void msort(int left, int right, int c) {
            if (left >= right) {
                for (int i = left; i <= right; i++) {
                    src[c].set(i, src[1 - c].get(i));
                }
                return;
            }
            int rStart = (left + right + 1) / 2;
            msort(left, rStart - 1, 1 - c);
            msort(rStart, right, 1 - c);
            int i = left;
            int j = rStart;
            for (int k = left; k <= right; k++) {
                if ((j > right) || ((i < rStart) && (comparator.compare(src[1 - c].get(i), src[1 - c].get(j)) <= 0))) {
                    src[c].set(k, src[1 - c].get(i++));
                } else {
                    src[c].set(k, src[1 - c].get(j++));
                }
            }
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity)

Example 8 with Entity

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

the class InMemoryQuickSortOnInitIterable method iterator.

@Override
public Iterator<Entity> iterator() {
    return new Iterator<Entity>() {

        private List<Entity> src;

        private Entity[] tmp;

        private int current;

        @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();
            }
            return src.get(current++);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void init() {
            src = new ArrayList<>();
            for (final Entity entity : source) {
                src.add(entity);
            }
            tmp = new Entity[src.size()];
            qsort(0, tmp.length - 1);
        }

        /**
         * sort src[left..right]
         * @param left left
         * @param right right
         */
        public void qsort(int left, int right) {
            if (left >= right) {
                return;
            }
            Entity median = src.get((left + right) / 2);
            int i = left;
            int toRight = 0;
            List<Entity> medians = new ArrayList<>();
            do {
                while (i <= right && comparator.compare(src.get(i), median) < 0) {
                    if (toRight + medians.size() > 0) {
                        src.set(i - toRight - medians.size(), src.get(i));
                    }
                    i++;
                }
                // src[i] >= median
                if (i <= right) {
                    Entity entity = src.get(i);
                    if (comparator.compare(entity, median) == 0) {
                        medians.add(entity);
                    } else {
                        tmp[toRight++] = entity;
                    }
                    i++;
                }
            } while (i <= right);
            // i == right + 1
            int current = i - toRight - medians.size();
            for (Entity e : medians) {
                src.set(current++, e);
            }
            for (int k = 0; k < toRight; k++) {
                src.set(current++, tmp[k]);
            }
            qsort(left, right - toRight - medians.size());
            qsort(right - toRight + 1, right);
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity)

Example 9 with Entity

use of jetbrains.exodus.entitystore.Entity 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 10 with Entity

use of jetbrains.exodus.entitystore.Entity 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

Entity (jetbrains.exodus.entitystore.Entity)44 EntityIterable (jetbrains.exodus.entitystore.EntityIterable)23 IOException (java.io.IOException)13 EntityIterableBase (jetbrains.exodus.entitystore.iterate.EntityIterableBase)7 InputStream (java.io.InputStream)6 UncheckedIOException (java.io.UncheckedIOException)5 ArrayList (java.util.ArrayList)5 ComparableGetter (jetbrains.exodus.entitystore.ComparableGetter)4 EntityStoreException (jetbrains.exodus.entitystore.EntityStoreException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 EntityId (jetbrains.exodus.entitystore.EntityId)3 File (java.io.File)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nonnull (javax.annotation.Nonnull)2 ExodusException (jetbrains.exodus.ExodusException)2 EntityMetaData (jetbrains.exodus.query.metadata.EntityMetaData)2 Serializable (java.io.Serializable)1