Search in sources :

Example 1 with PersistentLong23TreeSet

use of jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet in project xodus by JetBrains.

the class PersistentEntityStoreRefactorings method refactorCreateNullLinkIndices.

void refactorCreateNullLinkIndices() {
    store.executeInReadonlyTransaction(new StoreTransactionalExecutable() {

        @Override
        public void execute(@NotNull final StoreTransaction tx) {
            final PersistentStoreTransaction txn = (PersistentStoreTransaction) tx;
            for (final String entityType : store.getEntityTypes(txn)) {
                if (logger.isInfoEnabled()) {
                    logger.info("Refactoring creating null-value link indices for [" + entityType + ']');
                }
                safeExclusiveExecuteRefactoringForEntityType(entityType, new StoreTransactionalExecutable() {

                    @Override
                    public void execute(@NotNull final StoreTransaction tx) {
                        final PersistentStoreTransaction txn = (PersistentStoreTransaction) tx;
                        final int entityTypeId = store.getEntityTypeId(txn, entityType, false);
                        LinksTable links = store.getLinksTable(txn, entityTypeId);
                        final Store allLinksIndex = links.getAllLinksIndex();
                        final Transaction envTxn = txn.getEnvironmentTransaction();
                        if (allLinksIndex.count(envTxn) > 0) {
                            if (logger.isWarnEnabled()) {
                                logger.warn("Refactoring creating null-value link indices looped for [" + entityType + ']');
                            }
                            envTxn.getEnvironment().truncateStore(allLinksIndex.getName(), envTxn);
                            store.linksTables.remove(entityTypeId);
                            links = store.getLinksTable(txn, entityTypeId);
                        }
                        final Transaction readonlySnapshot = envTxn.getReadonlySnapshot();
                        try {
                            final Cursor cursor = links.getSecondIndexCursor(readonlySnapshot);
                            final long total = links.getSecondaryCount(readonlySnapshot);
                            long done = 0;
                            int prevLinkId = -1;
                            int linkId = -1;
                            PersistentLongSet.MutableSet idSet = new PersistentLong23TreeSet().beginWrite();
                            final String format = "done %4.1f%% for " + entityType;
                            while (cursor.getNext()) {
                                final PropertyKey linkKey = PropertyKey.entryToPropertyKey(cursor.getValue());
                                linkId = linkKey.getPropertyId();
                                final long entityLocalId = linkKey.getEntityLocalId();
                                if (prevLinkId != linkId) {
                                    if (prevLinkId == -1) {
                                        prevLinkId = linkId;
                                    } else {
                                        if (linkId < prevLinkId) {
                                            throw new IllegalStateException("Unsorted index");
                                        }
                                        done = dumpSetAndFlush(format, allLinksIndex, txn, total, done, prevLinkId, idSet);
                                        prevLinkId = linkId;
                                        linkId = -1;
                                    }
                                }
                                idSet.add(entityLocalId);
                            }
                            if (prevLinkId != -1) {
                                dumpSetAndFlush(format, allLinksIndex, txn, total, done, prevLinkId, idSet);
                            }
                            cursor.close();
                        } finally {
                            readonlySnapshot.abort();
                        }
                    }

                    private long dumpSetAndFlush(String format, Store allLinksIndex, PersistentStoreTransaction txn, double total, long done, int prevLinkId, PersistentLongSet.MutableSet idSet) {
                        final LongIterator itr = idSet.longIterator();
                        while (itr.hasNext()) {
                            allLinksIndex.putRight(txn.getEnvironmentTransaction(), IntegerBinding.intToCompressedEntry(prevLinkId), LongBinding.longToCompressedEntry(itr.next()));
                            done++;
                            if (done % 10000 == 0 && logger.isInfoEnabled()) {
                                logger.info(String.format(format, ((double) done * 100) / total));
                            }
                            if (done % 100000 == 0 && !txn.flush()) {
                                throw new IllegalStateException("cannot flush");
                            }
                        }
                        idSet.clear();
                        return done;
                    }
                });
            }
        }
    });
}
Also used : PersistentLong23TreeSet(jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet) NotNull(org.jetbrains.annotations.NotNull) PersistentLongSet(jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)

Aggregations

PersistentLong23TreeSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet)1 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)1 NotNull (org.jetbrains.annotations.NotNull)1