Search in sources :

Example 31 with Entity

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

the class InMemoryMergeSortIterableWithArrayList method iterator.

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

        private List<Entity> src;

        private int height;

        /**
         * smallest power of 2 not less than src.size()
         */
        private int size2;

        private int[] next;

        /**
         * need it only for hasNext()
         */
        private int current;

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

        @Override
        public Entity next() {
            if (src == null) {
                init();
            }
            int segment = 1;
            // next[current] == src.size() means current segment is exhausted
            while (next[1] < 0) {
                segment <<= 1;
                if (segment >= size2 || (next[segment] >= 0 && next[segment + 1] >= 0)) {
                    if ((next[segment + 1] >= src.size()) || ((next[segment] < src.size()) && (comparator.compare(src.get(next[segment]), src.get(next[segment + 1])) <= 0))) {
                        next[segment >> 1] = next[segment];
                    } else {
                        next[segment >> 1] = next[segment + 1];
                    }
                    segment >>= 2;
                } else if (next[segment] >= 0) {
                    segment++;
                }
            }
            int r = next[1];
            if (r >= src.size()) {
                throw new NoSuchElementException();
            }
            next[r + size2] = src.size();
            for (int i = (r + size2) >> 1; i >= 1; i >>= 1) {
                next[i] = -1;
            }
            current++;
            return src.get(r);
        }

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

        public void init() {
            src = new ArrayList<>();
            for (final Entity it : source) {
                src.add(it);
            }
            height = 1;
            for (int i = src.size(); i > 1; i = (i + 1) >> 1) {
                height++;
            }
            next = new int[1 << height];
            size2 = 1 << (height - 1);
            for (int i = 0; i < src.size(); i++) {
                next[i + size2] = i;
            }
            for (int i = size2 + src.size(); i < next.length; i++) {
                next[i] = src.size();
            }
            for (int i = 1; i < size2; i++) {
                next[i] = -1;
            }
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity)

Example 32 with Entity

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

the class InMemoryQuickSortIterable method iterator.

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

        private List<Entity> src;

        private List<Integer> left;

        private List<Integer> right;

        private List<Integer> medianStart;

        private List<Integer> medianEnd;

        private List<Entity> medians;

        private List<Entity> toRight;

        private int top;

        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();
            }
            while (top >= 0 && right.get(top) < current) {
                left.remove(top);
                right.remove(top);
                medianStart.remove(top);
                medianEnd.remove(top);
                top--;
            }
            // or     current == medianEnd[top] + 1 and we should go deeper
            if (top < 0 || current > medianEnd.get(top)) {
                do {
                    int l;
                    int r;
                    if (top < 0) {
                        l = 0;
                        r = src.size() - 1;
                    } else if (current == left.get(top)) {
                        l = left.get(top);
                        r = medianStart.get(top) - 1;
                    } else {
                        l = medianEnd.get(top) + 1;
                        r = right.get(top);
                    }
                    Entity median = src.get((l + r) / 2);
                    int i = l;
                    medians.clear();
                    toRight.clear();
                    do {
                        while (i <= r && comparator.compare(src.get(i), median) < 0) {
                            if (toRight.size() + medians.size() > 0) {
                                src.set(i - toRight.size() - medians.size(), src.get(i));
                            }
                            i++;
                        }
                        // src[i] >= median
                        if (i <= r) {
                            Entity entity = src.get(i);
                            if (comparator.compare(entity, median) == 0) {
                                medians.add(entity);
                            } else {
                                toRight.add(entity);
                            }
                            i++;
                        }
                    } while (i <= r);
                    // i == r + 1
                    int current = i - toRight.size() - medians.size();
                    for (Entity e : medians) {
                        src.set(current++, e);
                    }
                    for (Entity e : toRight) {
                        src.set(current++, e);
                    }
                    left.add(l);
                    right.add(r);
                    medianStart.add(r + 1 - toRight.size() - medians.size());
                    medianEnd.add(r - toRight.size());
                    top++;
                } while (current < medianStart.get(top));
            }
            return src.get(current++);
        }

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

        public void init() {
            src = new ArrayList<>();
            for (final Entity it : source) {
                src.add(it);
            }
            left = new ArrayList<>();
            right = new ArrayList<>();
            medianStart = new ArrayList<>();
            medianEnd = new ArrayList<>();
            medians = new ArrayList<>();
            toRight = new ArrayList<>();
            top = -1;
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity)

Example 33 with Entity

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

the class AddNullStaticTypedEntityIterable method instantiate.

@Override
public Iterable<Entity> instantiate() {
    Iterable<Entity> instantiatedDecorated = decorated.instantiate();
    Iterable<Entity> instantiatedNullContainer = nullContainer.instantiate();
    if (queryEngine.isPersistentIterable(instantiatedDecorated) && queryEngine.isPersistentIterable(instantiatedNullContainer)) {
        EntityIterableBase entityIterableBaseDecorated = ((EntityIterableBase) instantiatedDecorated).getSource();
        EntityIterableBase entityIterableBaseNullContainer = ((EntityIterableBase) instantiatedNullContainer).getSource();
        return queryEngine.wrap(new AddNullDecoratorIterable(queryEngine.getPersistentStore().getAndCheckCurrentTransaction(), entityIterableBaseDecorated, entityIterableBaseNullContainer));
    }
    return new Iterable<Entity>() {

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

                private Iterator<Entity> iterator = null;

                private Boolean hasNull = null;

                @Override
                public boolean hasNext() {
                    if (iterator == null) {
                        iterator = decorated.iterator();
                    }
                    if (iterator.hasNext()) {
                        return true;
                    }
                    if (hasNull == null) {
                        hasNull = false;
                        for (Entity entity : nullContainer) {
                            if (entity == null) {
                                hasNull = true;
                                break;
                            }
                        }
                    }
                    return hasNull;
                }

                @Override
                public Entity next() {
                    if (hasNext()) {
                        if (hasNull == null) {
                            Entity result = iterator.next();
                            if (result == null) {
                                hasNull = false;
                            }
                            return result;
                        } else {
                            hasNull = false;
                            return null;
                        }
                    }
                    throw new NoSuchElementException();
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) AddNullDecoratorIterable(jetbrains.exodus.entitystore.iterate.binop.AddNullDecoratorIterable) AddNullDecoratorIterable(jetbrains.exodus.entitystore.iterate.binop.AddNullDecoratorIterable) EntityIterableBase(jetbrains.exodus.entitystore.iterate.EntityIterableBase) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException)

Example 34 with Entity

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

the class And method instantiateCustom.

private static Iterable<Entity> instantiateCustom(@NotNull final String entityType, @NotNull final QueryEngine queryEngine, @NotNull final ModelMetaData metaData, @NotNull final NodeBase self, @NotNull final LinksEqualDecorator decorator, @NotNull final Instantiatable directClosure) {
    final Iterable<Entity> selfInstance = self.instantiate(entityType, queryEngine, metaData);
    if (selfInstance instanceof EntityIterableBase) {
        final EntityIterable result = ((EntityIterableBase) selfInstance).findLinks(((EntityIterableBase) decorator.instantiateDecorated(decorator.getLinkEntityType(), queryEngine, metaData)).getSource(), decorator.getLinkName());
        if (traceFindLinks) {
            final Iterator<Entity> directIt = directClosure.instantiate().iterator();
            final EntityIterator it = result.iterator();
            while (true) {
                final boolean directHasNext = directIt.hasNext();
                final boolean hasNext = it.hasNext();
                if (directHasNext != hasNext) {
                    throw new EntityStoreException("Invalid custom findLinks() result: different sizes");
                }
                if (!hasNext) {
                    break;
                }
                if (!directIt.next().getId().equals(it.nextId())) {
                    throw new EntityStoreException("Invalid custom findLinks() result");
                }
            }
        }
        return result;
    }
    return directClosure.instantiate();
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable) EntityIterator(jetbrains.exodus.entitystore.EntityIterator) EntityIterableBase(jetbrains.exodus.entitystore.iterate.EntityIterableBase) EntityStoreException(jetbrains.exodus.entitystore.EntityStoreException)

Example 35 with Entity

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

the class LongProcessRunner method oneMoreStep.

@Override
protected void oneMoreStep() throws Exception {
    logger.info("One more step...");
    EntityIterable iterable = txn.find("Person", "name", "Vadim");
    Entity entity = iterable.iterator().next();
    logger.info("Starting separate thread...");
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(5 * 1000);
                logger.info("Requesting euthanasia");
                getStreamer().writeString(store.getLocation());
            } catch (Exception e) {
                logger.error("Something went terribly wrong: ", e);
            }
        }
    }.start();
    logger.info("...separate thread started");
    logger.info("Getting ready for a rush...");
    long l = 0;
    while (true) {
        entity.setProperty("password", "dummypassword" + l++);
        Thread.sleep(0);
    }
}
Also used : Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable)

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