Package org.fusesource.hawtdb.api

Examples of org.fusesource.hawtdb.api.Transaction


    @Test
    public void testAddRollback() throws IOException, ClassNotFoundException {

        // Create the root index at the root page.
        Transaction tx = pf.tx();
        SortedIndex<Long, String> root = ROOT_FACTORY.create(tx);
        assertEquals(0, root.getIndexLocation());
        tx.commit();
        pf.flush();

        int MAX_KEY=1000;
        long putCounter=0;
        long removeCounter=0;

        final Random random = new Random(7777);

        for(int recoveryLoop=0; recoveryLoop < 100; recoveryLoop ++) {
            if( recoveryLoop%10 == 0 ) {
                System.out.println("at recovery loop: "+recoveryLoop+", puts: "+putCounter+", removes: "+removeCounter);
            }

            // validate that the number of keys in the map is what is expected.
            {
                tx = pf.tx();
                root = ROOT_FACTORY.open(tx, 0);
                assertEquals(putCounter-removeCounter, root.size());
                tx.commit();
            }


            // Do a bunch of transactions before reloading the page file.
            for(int txLoop=0; txLoop < 1000; txLoop ++) {

                tx = pf.tx();
                root = ROOT_FACTORY.open(tx, 0);

                // do a couple of random inserts and deletes.
                for(int keyLoop=0; keyLoop < 10; keyLoop ++) {

                    Long key = (long)random.nextInt(MAX_KEY);
                    String value = root.get(key);
                    if( value == null ) {
                        root.put(key, "value");
                        putCounter++;
                    } else {
                        root.remove(key);
                        removeCounter++;
                    }

                }
                tx.commit();

                // flush every 100 commits..
                if( txLoop%100 == 0 ) {
                    pf.flush();
                }
View Full Code Here


    protected Index<String, Long> createIndex() {
        HashIndexFactory<String, Long> factory = new HashIndexFactory<String, Long>();
        factory.setKeyCodec(StringCodec.INSTANCE);
        factory.setValueCodec(LongCodec.INSTANCE);
        //
        Transaction tx = pageFile.tx();
        Index<String, Long> index = factory.create(tx);
        tx.commit();
        return index;
    }
View Full Code Here

        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 0; i < 1000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        mutationLatch.countDown();
                        if (preCommitLatch.await(60, TimeUnit.SECONDS)) {
                            writer.commit();
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction reader = pageFile.tx();
                    Index<String, Long> index = openIndex(reader);
                    try {
                        if (mutationLatch.await(60, TimeUnit.SECONDS)) {
                            for (int i = 0; i < 1000; i++) {
                                if (index.get("" + i) != null) {
                                    error.set(new RuntimeException("Bad transaction isolation!"));
                                    throw new RuntimeException();
                                }
                            }
                            reader.commit();
                            preCommitLatch.countDown();
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        assertTrue(commitLatch.await(60, TimeUnit.SECONDS));
        if (error.get() == null) {
            Transaction checker = pageFile.tx();
            Index<String, Long> index = openIndex(checker);
            for (int i = 0; i < 1000; i++) {
                assertEquals(Long.valueOf(i), index.get("" + i));
            }
            checker.commit();
        } else {
            throw (Exception) error.get();
        }
        //
        executor.shutdownNow();
View Full Code Here

        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 0; i < 1000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        preCommitLatch.countDown();
                        if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
                            try {
                                writer.commit();
                                System.out.println("Committed from 1.");
                            } catch (OptimisticUpdateException ex) {
                                System.out.println("Replaying from 1...");
                                run();
                            }
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 1000; i < 2000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        preCommitLatch.countDown();
                        if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
                            try {
                                writer.commit();
                                System.out.println("Committed from 2.");
                            } catch (OptimisticUpdateException ex) {
                                System.out.println("Replaying from 2...");
                                run();
                            }
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        assertTrue(commitLatch.await(60, TimeUnit.SECONDS));
        if (error.get() == null) {
            Transaction checker = pageFile.tx();
            Index<String, Long> index = openIndex(checker);
            for (int i = 0; i < 2000; i++) {
                assertEquals(Long.valueOf(i), index.get("" + i));
            }
            checker.commit();
        } else {
            throw (Exception) error.get();
        }
        //
        executor.shutdownNow();
View Full Code Here

    TxPageFile logDb           = (TxPageFile) arguments.get("logDb");
    Map<String, Object> result = new HashMap<>();

    if (logDb != null) {

      Transaction tx                            = logDb.tx();
      MultiIndexFactory multiIndexFactory       = new MultiIndexFactory(tx);
      IndexFactory<String, Object> indexFactory = new BTreeIndexFactory<>();

      try {
View Full Code Here

      if (key != null) {

        synchronized (logDb) {

          Transaction tx                            = logDb.tx();
          MultiIndexFactory multiIndexFactory       = new MultiIndexFactory(tx);
          IndexFactory<String, Object> indexFactory = new BTreeIndexFactory<>();
          SortedIndex<String, Object> index         = null;

          try {

            index = (SortedIndex<String, Object>) multiIndexFactory.openOrCreate(key, indexFactory);

          } catch (Throwable t) {

            t.printStackTrace();
           
            logger.log(Level.WARNING, "Could not open or create log db page for key {0}", key);

            index = (SortedIndex<String, Object>) multiIndexFactory.create(key, indexFactory);

          }

          String uuid  = UUID.randomUUID().toString().replaceAll("[\\-]+", "");

          index.put(uuid, obj);

          // disabled to avoid parameter evaluation
          // logger.log(Level.FINE, "Logged for key {0}: {1}", new Object[] { key, StringUtils.join((String[]) obj, ",") });
         
          tx.commit();
          logDb.flush();

        }

      }
View Full Code Here

    public <T> T execute(Work<T> work) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Executing work +++ start +++ " + work);
        }

        Transaction tx = pageFile.tx();
        T answer = doExecute(work, tx, pageFile);

        if (LOG.isTraceEnabled()) {
            LOG.trace("Executing work +++ done  +++ " + work);
        }
View Full Code Here

TOP

Related Classes of org.fusesource.hawtdb.api.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.