Search in sources :

Example 11 with EntityId

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

the class EntityIdArrayCachedInstanceIterableFactory method createInstance.

public static CachedInstanceIterable createInstance(@NotNull final PersistentStoreTransaction txn, @NotNull final EntityIterableBase source, @NotNull final EntityIteratorBase it) {
    try {
        if (!it.hasNext()) {
            return new EmptyCachedInstanceIterable(txn, source);
        } else {
            final IntArrayList typeIds = IntArrayListSpinAllocator.alloc();
            final LongArrayList localIds = LongArrayListSpinAllocator.alloc();
            long min;
            long max;
            try {
                boolean onlyOneTypeId = true;
                boolean localSorted = true;
                if (source.isSortedById()) {
                    int lastTypeId = -1;
                    EntityId id = it.nextId();
                    while (true) {
                        final int nextTypeId;
                        if (id == null) {
                            nextTypeId = NULL_TYPE_ID;
                            localIds.add(0);
                        } else {
                            nextTypeId = id.getTypeId();
                            localIds.add(id.getLocalId());
                        }
                        if (nextTypeId != lastTypeId) {
                            if (lastTypeId != -1) {
                                onlyOneTypeId = false;
                                // add upper boundary for previous
                                typeIds.add(localIds.size() - 1);
                            }
                            typeIds.add(nextTypeId);
                            lastTypeId = nextTypeId;
                        }
                        if (!it.hasNext()) {
                            if (!onlyOneTypeId) {
                                // add boundary for last
                                typeIds.add(localIds.size());
                            }
                            break;
                        }
                        id = it.nextId();
                    }
                    min = localIds.get(0);
                    max = localIds.get(localIds.size() - 1);
                } else {
                    int lastTypeId = -1;
                    long lastLocalId = -1;
                    min = Long.MAX_VALUE;
                    max = Long.MIN_VALUE;
                    boolean compact = true;
                    EntityId id = it.nextId();
                    while (true) {
                        final int nextTypeId;
                        final long nextLocalId;
                        if (id == null) {
                            nextTypeId = NULL_TYPE_ID;
                            nextLocalId = 0;
                        } else {
                            nextTypeId = id.getTypeId();
                            nextLocalId = id.getLocalId();
                        }
                        if (localSorted) {
                            if (lastTypeId > nextTypeId || lastTypeId == nextTypeId && lastLocalId > nextLocalId) {
                                final int length;
                                if (nextTypeId == NULL_TYPE_ID && (length = localIds.size()) <= 1) {
                                    if (length == 1) {
                                        // direct conversion
                                        onlyOneTypeId = false;
                                        localSorted = false;
                                        compact = false;
                                    } else {
                                        typeIds.add(NULL_TYPE_ID);
                                    }
                                    lastLocalId = nextLocalId;
                                } else {
                                    localSorted = false;
                                }
                            } else {
                                lastLocalId = nextLocalId;
                            }
                        }
                        localIds.add(nextLocalId);
                        if (nextLocalId > max) {
                            max = nextLocalId;
                        }
                        if (nextLocalId < min) {
                            min = nextLocalId;
                        }
                        if (compact) {
                            if (localSorted) {
                                if (nextTypeId > lastTypeId) {
                                    if (lastTypeId != -1) {
                                        onlyOneTypeId = false;
                                        // add upper boundary for previous
                                        typeIds.add(localIds.size() - 1);
                                    }
                                    typeIds.add(nextTypeId);
                                }
                                lastTypeId = nextTypeId;
                            } else {
                                if (typeIds.size() > 1 || nextTypeId != lastTypeId) {
                                    onlyOneTypeId = false;
                                    compact = false;
                                    addNextTypeId(nextTypeId, typeIds, localIds);
                                }
                            }
                        } else {
                            typeIds.add(nextTypeId);
                        }
                        if (!it.hasNext()) {
                            if (compact && !onlyOneTypeId) {
                                // add boundary for last
                                typeIds.add(localIds.size());
                            }
                            break;
                        }
                        id = it.nextId();
                    }
                }
                if (localSorted) {
                    if (onlyOneTypeId) {
                        return makeSingleTypeSortedIterable(txn, source, it, typeIds, localIds, min, max);
                    } else {
                        return new MultiTypeSortedEntityIdArrayCachedInstanceIterable(txn, source, typeIds.toArray(), localIds.toArray(), it.toSet());
                    }
                } else {
                    if (onlyOneTypeId) {
                        return makeSingleTypeUnsortedIterable(txn, source, it, typeIds, localIds, min, max);
                    } else {
                        return new MultiTypeUnsortedEntityIdArrayCachedInstanceIterable(txn, source, typeIds.toArray(), localIds.toArray(), it.toSet());
                    }
                }
            } finally {
                LongArrayListSpinAllocator.dispose(localIds);
                IntArrayListSpinAllocator.dispose(typeIds);
            }
        }
    } finally {
        it.disposeIfShouldBe();
    }
}
Also used : EntityId(jetbrains.exodus.entitystore.EntityId) LongArrayList(jetbrains.exodus.core.dataStructures.LongArrayList) IntArrayList(jetbrains.exodus.core.dataStructures.IntArrayList)

Example 12 with EntityId

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

the class EntityIteratorFixingDecorator method nextIdImpl.

@Override
@Nullable
public EntityId nextIdImpl() {
    final EntityId result = hasNextImpl() ? iterator.nextIdImpl() : null;
    hasNextValid = false;
    return result;
}
Also used : EntityId(jetbrains.exodus.entitystore.EntityId) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

EntityId (jetbrains.exodus.entitystore.EntityId)12 Nullable (org.jetbrains.annotations.Nullable)6 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 Entity (jetbrains.exodus.entitystore.Entity)3 EntityIterable (jetbrains.exodus.entitystore.EntityIterable)2 ClassIndex (meghanada.reflect.ClassIndex)2 Serializable (java.io.Serializable)1 IntArrayList (jetbrains.exodus.core.dataStructures.IntArrayList)1 LongArrayList (jetbrains.exodus.core.dataStructures.LongArrayList)1 EntityIterableHandle (jetbrains.exodus.entitystore.EntityIterableHandle)1 EntityStoreException (jetbrains.exodus.entitystore.EntityStoreException)1 EntityIterableHandleBase (jetbrains.exodus.entitystore.iterate.EntityIterableHandleBase)1 NotNull (org.jetbrains.annotations.NotNull)1