Search in sources :

Example 31 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class GarbageCollectorTest method reopenDbAfterGc.

@Test
public void reopenDbAfterGc() {
    set1KbFileWithoutGC();
    ByteIterable key = StringBinding.stringToEntry("key");
    Store store = openStoreAutoCommit("updateSameKey");
    for (int i = 0; i < 1000; ++i) {
        putAutoCommit(store, key, key);
    }
    Assert.assertEquals(1, countAutoCommit(store));
    env.getGC().cleanWholeLog();
    store = openStoreAutoCommit("updateSameKey", StoreConfig.USE_EXISTING);
    Assert.assertEquals(1, countAutoCommit(store));
    reopenEnvironment();
    store = openStoreAutoCommit("updateSameKey", StoreConfig.USE_EXISTING);
    Assert.assertEquals(1, countAutoCommit(store));
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Test(org.junit.Test)

Example 32 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class BTreeDeleteSpecificTest method testDeleteKeys.

@Test
public void testDeleteKeys() throws IOException {
    tm = new BTreeEmpty(log, policy, false, 1).getMutableCopy();
    for (int i = 0; i < 125; i++) {
        getTreeMutable().put(kv(i, "k" + i));
    }
    ByteIterable key = getTreeMutable().getRoot().getKey(1).getKey();
    refresh();
    tm.delete(key);
    assertTrue(getTreeMutable().getRoot().getKey(1).compareKeyTo(getTreeMutable().getRoot().getChild(1).getMinKey().getKey()) == 0);
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) Test(org.junit.Test)

Example 33 with ByteIterable

use of jetbrains.exodus.ByteIterable in project pwm by pwm-project.

the class XodusLocalDB method put.

@Override
public boolean put(final LocalDB.DB db, final String key, final String value) throws LocalDBException {
    checkStatus(true);
    return environment.computeInTransaction(transaction -> {
        final ByteIterable k = bindMachine.keyToEntry(key);
        final ByteIterable v = bindMachine.valueToEntry(value);
        final Store store = getStore(db);
        return store.put(transaction, k, v);
    });
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) Store(jetbrains.exodus.env.Store)

Example 34 with ByteIterable

use of jetbrains.exodus.ByteIterable in project pwm by pwm-project.

the class XodusLocalDB method putIfAbsent.

@LocalDB.WriteOperation
public boolean putIfAbsent(final LocalDB.DB db, final String key, final String value) throws LocalDBException {
    checkStatus(true);
    return environment.computeInTransaction(transaction -> {
        final ByteIterable k = bindMachine.keyToEntry(key);
        final ByteIterable v = bindMachine.valueToEntry(value);
        final Store store = getStore(db);
        final ByteIterable existingValue = store.get(transaction, k);
        if (existingValue != null) {
            return false;
        }
        return store.put(transaction, k, v);
    });
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) Store(jetbrains.exodus.env.Store)

Example 35 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class PersistentEntityStoreRefactorings method refactorMakeLinkTablesConsistent.

void refactorMakeLinkTablesConsistent(final Store internalSettings) {
    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 making links' tables consistent for [" + entityType + ']');
                }
                runReadonlyTransactionSafeForEntityType(entityType, new Runnable() {

                    @Override
                    public void run() {
                        final Collection<Pair<ByteIterable, ByteIterable>> redundantLinks = new ArrayList<>();
                        final Collection<Pair<ByteIterable, ByteIterable>> deleteLinks = new ArrayList<>();
                        final int entityTypeId = store.getEntityTypeId(txn, entityType, false);
                        final LinksTable linksTable = store.getLinksTable(txn, entityTypeId);
                        final Transaction envTxn = txn.getEnvironmentTransaction();
                        final LongSet all = new PackedLongHashSet();
                        final LongSet linkFilter = new PackedLongHashSet();
                        try (Cursor cursor = store.getEntitiesIndexCursor(txn, entityTypeId)) {
                            while (cursor.getNext()) {
                                all.add(LongBinding.compressedEntryToLong(cursor.getKey()));
                            }
                        }
                        final IntHashSet redundantLinkTypes = new IntHashSet();
                        final IntHashSet deletedLinkTypes = new IntHashSet();
                        final IntHashSet deletedLinkIds = new IntHashSet();
                        try (Cursor cursor = linksTable.getFirstIndexCursor(envTxn)) {
                            while (cursor.getNext()) {
                                final ByteIterable first = cursor.getKey();
                                final ByteIterable second = cursor.getValue();
                                LinkValue linkValue = null;
                                final long localId = LongBinding.compressedEntryToLong(first);
                                if (!all.contains(localId)) {
                                    try {
                                        linkValue = LinkValue.entryToLinkValue(second);
                                        deletedLinkTypes.add(linkValue.getEntityId().getTypeId());
                                        deletedLinkIds.add(linkValue.getLinkId());
                                    } catch (ArrayIndexOutOfBoundsException ignore) {
                                    }
                                    do {
                                        deleteLinks.add(new Pair<>(first, second));
                                    } while (cursor.getNextDup());
                                    continue;
                                } else {
                                    linkFilter.add((first.hashCode() << 31L) + second.hashCode());
                                }
                                if (linkValue == null) {
                                    try {
                                        linkValue = LinkValue.entryToLinkValue(second);
                                    } catch (ArrayIndexOutOfBoundsException ignore) {
                                        deleteLinks.add(new Pair<>(first, second));
                                    }
                                }
                                if (linkValue != null) {
                                    final EntityId targetEntityId = linkValue.getEntityId();
                                    // if target doesn't exist
                                    if (store.getLastVersion(txn, targetEntityId) < 0) {
                                        deletedLinkTypes.add(targetEntityId.getTypeId());
                                        deletedLinkIds.add(linkValue.getLinkId());
                                        deleteLinks.add(new Pair<>(first, second));
                                        continue;
                                    } else {
                                        linkFilter.add((first.hashCode() << 31L) + second.hashCode());
                                    }
                                    if (!linksTable.contains2(envTxn, first, second)) {
                                        redundantLinkTypes.add(targetEntityId.getTypeId());
                                        redundantLinks.add(new Pair<>(first, second));
                                    }
                                }
                            }
                        }
                        if (!redundantLinks.isEmpty()) {
                            store.getEnvironment().executeInTransaction(new TransactionalExecutable() {

                                @Override
                                public void execute(@NotNull final Transaction txn) {
                                    for (final Pair<ByteIterable, ByteIterable> badLink : redundantLinks) {
                                        linksTable.put(txn, badLink.getFirst(), badLink.getSecond());
                                    }
                                }
                            });
                            if (logger.isInfoEnabled()) {
                                logger.info(redundantLinks.size() + " missing links found for [" + entityType + ']');
                            }
                            redundantLinks.clear();
                        }
                        try (Cursor cursor = linksTable.getSecondIndexCursor(envTxn)) {
                            while ((cursor.getNext())) {
                                final ByteIterable second = cursor.getKey();
                                final ByteIterable first = cursor.getValue();
                                if (!linkFilter.contains((first.hashCode() << 31L) + second.hashCode())) {
                                    if (!linksTable.contains(envTxn, first, second)) {
                                        redundantLinks.add(new Pair<>(first, second));
                                    }
                                }
                            }
                        }
                        final int redundantLinksSize = redundantLinks.size();
                        final int deleteLinksSize = deleteLinks.size();
                        if (redundantLinksSize > 0 || deleteLinksSize > 0) {
                            store.getEnvironment().executeInTransaction(new TransactionalExecutable() {

                                @Override
                                public void execute(@NotNull final Transaction txn) {
                                    for (final Pair<ByteIterable, ByteIterable> redundantLink : redundantLinks) {
                                        deletePair(linksTable.getSecondIndexCursor(txn), redundantLink.getFirst(), redundantLink.getSecond());
                                    }
                                    for (final Pair<ByteIterable, ByteIterable> deleteLink : deleteLinks) {
                                        deletePair(linksTable.getFirstIndexCursor(txn), deleteLink.getFirst(), deleteLink.getSecond());
                                        deletePair(linksTable.getSecondIndexCursor(txn), deleteLink.getSecond(), deleteLink.getFirst());
                                    }
                                }
                            });
                            if (logger.isInfoEnabled()) {
                                if (redundantLinksSize > 0) {
                                    final ArrayList<String> redundantLinkTypeNames = new ArrayList<>(redundantLinkTypes.size());
                                    for (final int typeId : redundantLinkTypes) {
                                        redundantLinkTypeNames.add(store.getEntityType(txn, typeId));
                                    }
                                    logger.info(redundantLinksSize + " redundant links found and fixed for [" + entityType + "] and targets: " + redundantLinkTypeNames);
                                }
                                if (deleteLinksSize > 0) {
                                    final ArrayList<String> deletedLinkTypeNames = new ArrayList<>(deletedLinkTypes.size());
                                    for (final int typeId : deletedLinkTypes) {
                                        try {
                                            final String entityTypeName = store.getEntityType(txn, typeId);
                                            deletedLinkTypeNames.add(entityTypeName);
                                        } catch (Throwable t) {
                                        // ignore
                                        }
                                    }
                                    final ArrayList<String> deletedLinkIdsNames = new ArrayList<>(deletedLinkIds.size());
                                    for (final int typeId : deletedLinkIds) {
                                        try {
                                            final String linkName = store.getLinkName(txn, typeId);
                                            deletedLinkIdsNames.add(linkName);
                                        } catch (Throwable t) {
                                        // ignore
                                        }
                                    }
                                    logger.info(deleteLinksSize + " phantom links found and fixed for [" + entityType + "] and targets: " + deletedLinkTypeNames);
                                    logger.info("Link types: " + deletedLinkIdsNames);
                                }
                            }
                        }
                        // reset link null indices
                        Settings.delete(internalSettings, "Link null-indices present");
                    }
                });
            }
        }
    });
}
Also used : Pair(jetbrains.exodus.core.dataStructures.Pair) PersistentLongSet(jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Aggregations

ByteIterable (jetbrains.exodus.ByteIterable)86 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)45 Test (org.junit.Test)26 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)15 Nullable (org.jetbrains.annotations.Nullable)11 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)8 Cursor (jetbrains.exodus.env.Cursor)7 RandomAccessLoggable (jetbrains.exodus.log.RandomAccessLoggable)7 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)6 Store (jetbrains.exodus.env.Store)4 TreeSet (java.util.TreeSet)3 Transaction (jetbrains.exodus.env.Transaction)3 Exchange (com.persistit.Exchange)2 ExodusException (jetbrains.exodus.ExodusException)2 TokyoCabinetBenchmark (jetbrains.exodus.benchmark.TokyoCabinetBenchmark)2 Pair (jetbrains.exodus.core.dataStructures.Pair)2 HashSet (jetbrains.exodus.core.dataStructures.hash.HashSet)2 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)2 PersistentStoreTransaction (jetbrains.exodus.entitystore.PersistentStoreTransaction)2 Loggable (jetbrains.exodus.log.Loggable)2