Search in sources :

Example 21 with Transaction

use of jetbrains.exodus.env.Transaction in project xodus by JetBrains.

the class VfsStreamsTests method writeRandomAccessOverwriteRead2.

@Test
public void writeRandomAccessOverwriteRead2() throws IOException {
    final Transaction txn = env.beginTransaction();
    final File file0 = vfs.createFile(txn, "file0");
    OutputStream outputStream = vfs.appendFile(txn, file0);
    outputStream.write((HOEGAARDEN + HOEGAARDEN + HOEGAARDEN + HOEGAARDEN).getBytes(UTF_8));
    outputStream.close();
    txn.flush();
    outputStream = vfs.writeFile(txn, file0, 1000000);
    outputStream.write("x".getBytes(UTF_8));
    outputStream.close();
    txn.flush();
    final InputStream inputStream = vfs.readFile(txn, file0);
    Assert.assertEquals(HOEGAARDEN + HOEGAARDEN + HOEGAARDEN + HOEGAARDEN + 'x', streamAsString(inputStream));
    inputStream.close();
    txn.abort();
}
Also used : Transaction(jetbrains.exodus.env.Transaction) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 22 with Transaction

use of jetbrains.exodus.env.Transaction in project xodus by JetBrains.

the class VfsStreamsTests method testWriteAndSeek2.

@Test
@TestFor(issues = "XD-624")
public void testWriteAndSeek2() throws IOException {
    Transaction txn = env.beginTransaction();
    vfs.getConfig().setClusteringStrategy(new ClusteringStrategy.LinearClusteringStrategy(8));
    final File file = vfs.createFile(txn, "file0");
    OutputStream outputStream = vfs.writeFile(txn, file);
    final byte[] bytes = HOEGAARDEN.getBytes(UTF_8);
    outputStream.write(bytes);
    outputStream.close();
    outputStream = vfs.writeFile(txn, file, bytes.length);
    outputStream.write(bytes);
    outputStream.close();
    txn.flush();
    final InputStream inputStream = vfs.readFile(txn, file);
    Assert.assertEquals(HOEGAARDEN + HOEGAARDEN, streamAsString(inputStream));
    txn.abort();
}
Also used : Transaction(jetbrains.exodus.env.Transaction) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test) TestFor(jetbrains.exodus.TestFor)

Example 23 with Transaction

use of jetbrains.exodus.env.Transaction in project xodus by JetBrains.

the class VfsStressTests method testLuceneDirectoryLike.

@Test
public void testLuceneDirectoryLike() throws IOException {
    final VfsConfig config = new VfsConfig();
    config.setClusteringStrategy(new ClusteringStrategy.QuadraticClusteringStrategy(4));
    vfs = new VirtualFileSystem(getEnvironment(), config, StoreConfig.WITHOUT_DUPLICATES);
    vfs.setClusterConverter(getClusterConverter());
    Transaction txn = env.beginTransaction();
    File bigFile = vfs.createFile(txn, "big_file");
    txn.commit();
    for (int i = 0; i < 1000; ++i) {
        txn = env.beginTransaction();
        String temp = String.valueOf(i) + Math.random();
        OutputStream outputStream = vfs.writeFile(txn, vfs.createFile(txn, temp));
        outputStream.write(("testLuceneDirectoryLike" + i).getBytes());
        outputStream.close();
        txn.commit();
        txn = env.beginTransaction();
        final File tempFile = vfs.openFile(txn, temp, false);
        Assert.assertNotNull(tempFile);
        final long sourceLen = vfs.getFileLength(txn, bigFile);
        outputStream = vfs.appendFile(txn, bigFile);
        InputStream inputStream = vfs.readFile(txn, tempFile);
        int count = 0;
        while (true) {
            int c = inputStream.read();
            if (c < 0) {
                break;
            }
            ++count;
            outputStream.write(c);
        }
        outputStream.close();
        Assert.assertEquals(("testLuceneDirectoryLike" + i).getBytes().length, count);
        Assert.assertEquals(count, vfs.getFileLength(txn, tempFile));
        Assert.assertEquals(vfs.getFileLength(txn, bigFile), sourceLen + vfs.getFileLength(txn, tempFile));
        vfs.deleteFile(txn, temp);
        txn.commit();
    }
}
Also used : Transaction(jetbrains.exodus.env.Transaction) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 24 with Transaction

use of jetbrains.exodus.env.Transaction in project xodus by JetBrains.

the class EnvironmentConfigMBeanTest method readOnly_XD_444.

@Test
public void readOnly_XD_444() throws Exception {
    beanIsAccessible();
    final Transaction txn = env.beginTransaction();
    try {
        env.openStore("New Store", StoreConfig.WITHOUT_DUPLICATES, txn);
        Assert.assertFalse(txn.isIdempotent());
        platformMBeanServer.setAttribute(envConfigName, new Attribute(READ_ONLY_ATTR, true));
        TestUtil.runWithExpectedException(new Runnable() {

            @Override
            public void run() {
                txn.flush();
            }
        }, ReadonlyTransactionException.class);
    } finally {
        txn.abort();
    }
}
Also used : Transaction(jetbrains.exodus.env.Transaction) Test(org.junit.Test)

Example 25 with Transaction

use of jetbrains.exodus.env.Transaction 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

Transaction (jetbrains.exodus.env.Transaction)43 Test (org.junit.Test)33 OutputStream (java.io.OutputStream)22 InputStream (java.io.InputStream)18 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)4 Store (jetbrains.exodus.env.Store)4 TransactionalExecutable (jetbrains.exodus.env.TransactionalExecutable)4 File (java.io.File)3 ByteIterable (jetbrains.exodus.ByteIterable)3 Cursor (jetbrains.exodus.env.Cursor)3 TestFor (jetbrains.exodus.TestFor)2 NotNull (org.jetbrains.annotations.NotNull)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 HashSet (jetbrains.exodus.core.dataStructures.hash.HashSet)1 Job (jetbrains.exodus.core.execution.Job)1 JobProcessor (jetbrains.exodus.core.execution.JobProcessor)1 BlobsTable (jetbrains.exodus.entitystore.tables.BlobsTable)1 Environment (jetbrains.exodus.env.Environment)1