Search in sources :

Example 11 with ArrayByteIterable

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

the class VirtualFileSystem method openFile.

/**
 * Returns existing {@linkplain File} with specified path or creates the new one if {@code create} is {@code true},
 * otherwise returns {@code null}. If {@code create} is {@code true} it never returns {@code null}.
 *
 * @param txn    {@linkplain Transaction} instance
 * @param path   file path
 * @param create {@code true} if new file creation is allowed
 * @return existing or newly created {@linkplain File} if if {@code create} is {@code true}, or {@code null}
 * @see File
 */
@Nullable
public File openFile(@NotNull final Transaction txn, @NotNull final String path, boolean create) {
    final ArrayByteIterable key = StringBinding.stringToEntry(path);
    final ByteIterable value = pathnames.get(txn, key);
    if (value != null) {
        return new File(path, value);
    }
    if (create) {
        return createFile(txn, path);
    }
    return null;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ArrayByteIterable

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

the class AbortTest method testAbort.

public void testAbort() {
    final StoreConfig dbConfig = StoreConfig.WITHOUT_DUPLICATES;
    Transaction txn = env.beginTransaction();
    final Store store = env.openStore("testDatabase", dbConfig, txn);
    final ArrayByteIterable dbEntry = new ArrayByteIterable(new byte[4]);
    store.put(txn, dbEntry, dbEntry);
    txn.revert();
    Assert.assertTrue(store.count(txn) == 0);
    // env.setThreadTransaction(txn);
    store.put(txn, dbEntry, dbEntry);
    txn.flush();
    Assert.assertTrue(store.count(txn) == 1);
    txn.abort();
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 13 with ArrayByteIterable

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

the class TreeCursorNoDuplicatesTest method testGetSearchKeyRange3.

@Test
public void testGetSearchKeyRange3() throws IOException {
    tm = createMutableTree(false, 1);
    getTreeMutable().put(new ArrayByteIterable(new byte[] { 1 }), value("v1"));
    final ArrayByteIterable key = new ArrayByteIterable(new byte[] { 1, 2, 1, 0 });
    getTreeMutable().put(key, value("v2"));
    Cursor c = getTreeMutable().openCursor();
    assertEquals(value("v2"), c.getSearchKeyRange(new ArrayByteIterable(new byte[] { 1, 2, 1 })));
    assertEquals(key, c.getKey());
    assertFalse(c.getNext());
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Cursor(jetbrains.exodus.env.Cursor) Test(org.junit.Test)

Example 14 with ArrayByteIterable

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

the class EnvironmentTestInMemory method putRandomKeyValue.

private void putRandomKeyValue(Store primary, Store secondary, Transaction txn, int keysCount, int valuesCount, Persistent23TreeMap.MutableMap<Integer, Integer> testMap) {
    final int key = rnd.nextInt(keysCount);
    final ArrayByteIterable keyEntry = IntegerBinding.intToCompressedEntry(key);
    final int value = rnd.nextInt(valuesCount);
    testMap.put(key, value);
    final ArrayByteIterable valueEntry = IntegerBinding.intToCompressedEntry(value);
    final ByteIterable oldValue = primary.get(txn, keyEntry);
    primary.put(txn, keyEntry, valueEntry);
    if (oldValue != null) {
        try (Cursor cursor = secondary.openCursor(txn)) {
            Assert.assertTrue(cursor.getSearchBoth(oldValue, keyEntry));
            Assert.assertTrue(cursor.deleteCurrent());
        }
    }
    secondary.put(txn, valueEntry, keyEntry);
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 15 with ArrayByteIterable

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

the class GarbageCollectorTestInMemory method testTextIndexLikeWithDeletionsAndConcurrentReading.

@Test
public void testTextIndexLikeWithDeletionsAndConcurrentReading() throws InterruptedException, BrokenBarrierException {
    final long started = System.currentTimeMillis();
    prepare();
    final Transaction txn = env.beginTransaction();
    final Store store = env.openStore("store", getStoreConfig(false), txn);
    final Store storeDups = env.openStore("storeDups", getStoreConfig(true), txn);
    txn.commit();
    final Throwable[] throwable = { null };
    final JobProcessor[] processors = new JobProcessor[10];
    for (int i = 0; i < processors.length; ++i) {
        processors[i] = ThreadJobProcessorPool.getOrCreateJobProcessor("test processor" + i);
        processors[i].start();
    }
    final CyclicBarrier barrier = new CyclicBarrier(processors.length + 1);
    processors[0].queue(new Job() {

        @Override
        protected void execute() throws Throwable {
            barrier.await();
            try {
                while (System.currentTimeMillis() - started < TEST_DURATION) {
                    env.executeInTransaction(new TransactionalExecutable() {

                        @Override
                        public void execute(@NotNull final Transaction txn) {
                            int randomInt = rnd.nextInt() & 0x3fffffff;
                            final int count = 4 + (randomInt) & 0x1f;
                            for (int j = 0; j < count; randomInt += ++j) {
                                final int intKey = randomInt & 0x3fff;
                                final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                                final int valueLength = 50 + (randomInt % 100);
                                store.put(txn, key, new ArrayByteIterable(new byte[valueLength]));
                                storeDups.put(txn, key, IntegerBinding.intToEntry(randomInt % 32));
                            }
                            randomInt = (randomInt * randomInt) & 0x3fffffff;
                            for (int j = 0; j < count; randomInt += ++j) {
                                final int intKey = randomInt & 0x3fff;
                                final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                                store.delete(txn, key);
                                try (Cursor cursor = storeDups.openCursor(txn)) {
                                    if (cursor.getSearchBoth(key, IntegerBinding.intToEntry(randomInt % 32))) {
                                        cursor.deleteCurrent();
                                    }
                                }
                            }
                        }
                    });
                    Thread.sleep(0);
                }
            } catch (Throwable t) {
                throwable[0] = t;
            }
        }
    });
    for (int i = 1; i < processors.length; ++i) {
        processors[i].queue(new Job() {

            @Override
            protected void execute() throws Throwable {
                try {
                    barrier.await();
                    while (System.currentTimeMillis() - started < TEST_DURATION) {
                        int randomInt = rnd.nextInt() & 0x3fffffff;
                        for (int j = 0; j < 100; randomInt += ++j) {
                            final int intKey = randomInt & 0x3fff;
                            final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                            getAutoCommit(store, key);
                            getAutoCommit(storeDups, key);
                            Thread.sleep(0);
                        }
                        Thread.sleep(50);
                    }
                } catch (Throwable t) {
                    throwable[0] = t;
                }
            }
        });
    }
    barrier.await();
    for (final JobProcessor processor : processors) {
        processor.finish();
    }
    final Throwable t = throwable[0];
    if (t != null) {
        memory.dump(new File(System.getProperty("user.home"), "dump"));
        logger.error("User code exception: ", t);
        Assert.assertTrue(false);
    }
}
Also used : JobProcessor(jetbrains.exodus.core.execution.JobProcessor) Store(jetbrains.exodus.env.Store) Cursor(jetbrains.exodus.env.Cursor) NotNull(org.jetbrains.annotations.NotNull) CyclicBarrier(java.util.concurrent.CyclicBarrier) TransactionalExecutable(jetbrains.exodus.env.TransactionalExecutable) Transaction(jetbrains.exodus.env.Transaction) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Job(jetbrains.exodus.core.execution.Job) File(java.io.File) Test(org.junit.Test)

Aggregations

ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)29 ByteIterable (jetbrains.exodus.ByteIterable)15 Transaction (jetbrains.exodus.env.Transaction)4 TransactionalExecutable (jetbrains.exodus.env.TransactionalExecutable)4 Nullable (org.jetbrains.annotations.Nullable)4 Test (org.junit.Test)4 File (java.io.File)3 ByteIterator (jetbrains.exodus.ByteIterator)3 Cursor (jetbrains.exodus.env.Cursor)3 Store (jetbrains.exodus.env.Store)3 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)2 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)2 NotNull (org.jetbrains.annotations.NotNull)2 ArrayList (java.util.ArrayList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)1 StringBinding.entryToString (jetbrains.exodus.bindings.StringBinding.entryToString)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1 PersistentLong23TreeSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet)1 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)1