Package journal.io.api

Examples of journal.io.api.Journal$WriteCommand


    private static final File ISSUE55_DIR = new File(Issue55Test.class.getClassLoader().getResource("issue55").getFile());

    @Test
    public void test() throws IOException {
        Journal journal = new Journal();
        journal.setDirectory(ISSUE55_DIR);
        journal.open();
        journal.close();
    }
View Full Code Here


        if (dir.exists()) {
            deleteFilesInDirectory(dir);
        } else {
            dir.mkdirs();
        }
        journal = new Journal();
        journal.setDirectory(dir);
        if (configure(journal)) {
            journal.open();
        }
    }
View Full Code Here

        JournalBuilder builder = JournalBuilder.of(JOURNAL_DIR)
                .setChecksum(true)
                .setMaxFileLength(1024 * 1024)
                .setMaxWriteBatchSize(1024 * 10);
        // Open the journal:
        Journal journal = builder.open();

        // Write to the journal:
        int iterations = 1000;
        for (int i = 0; i < iterations; i++) {
            Journal.WriteType writeType = i % 2 == 0 ? Journal.WriteType.SYNC : Journal.WriteType.ASYNC;
            journal.write(new String("DATA" + i).getBytes("UTF-8"), writeType);
        }

        // Replay the journal forward by redoing:
        int i = 0;
        for (Location location : journal.redo()) {
            byte[] record = journal.read(location, Journal.ReadType.ASYNC);
            assertEquals("DATA" + i++, new String(record, "UTF-8"));
        }

        // Replay the journal backward by undoing:
        int j = 1000;
        for (Location location : journal.undo()) {
            byte[] record = journal.read(location, Journal.ReadType.ASYNC);
            assertEquals("DATA" + --i, new String(record, "UTF-8"));
        }

        // Delete locations:
        for (Location location : journal.redo()) {
            journal.delete(location);
        }

        // Compact logs:
        journal.compact();

        // Close the journal:
        journal.close();
    }
View Full Code Here

  @Singleton
  Journal getJournal() {

    try {

      final Journal journal = JournalBuilder.of(logDirectory).setPhysicalSync(true).open();

      Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
          //noinspection EmptyCatchBlock
          try {
            journal.close();
          } catch (IOException e) {
            //TODO log it
          }
        }
      }));
View Full Code Here

  private static enum Type {EMPTY, APPEND, COMMIT, MEMBERSHIP, SNAPSHOT, TERM, VOTE}

  public static void main(String... args) throws IOException {

    Journal journal = JournalBuilder.of(new File(args[0])).open();

    long term = 0;
    long index = 0;
    long committed = 0;
    List<String> membership = Collections.emptyList();
    Optional<String> lastVotedFor = Optional.absent();
    Type lastEntryType = Type.EMPTY;

    for (Location loc : journal.redo()) {

      byte[] rawEntry = journal.read(loc, Journal.ReadType.ASYNC);
      JournalEntry entry = JournalEntry.parseFrom(rawEntry);

      if (entry.hasAppend()) {

        Append a = entry.getAppend();
        index = a.getIndex();
        term = a.getEntry().getTerm();
        lastEntryType = Type.APPEND;

      } else if (entry.hasCommit()) {

        Commit c = entry.getCommit();
        committed = c.getIndex();
        lastEntryType = Type.COMMIT;

      } else if (entry.hasMembership()) {

        Membership m = entry.getMembership();
        membership = m.getMembersList();
        lastEntryType = Type.MEMBERSHIP;

      } else if (entry.hasSnapshot()) {

        Snapshot s = entry.getSnapshot();
        index = s.getLastIncludedIndex();
        term = s.getLastIncludedTerm();
        lastEntryType = Type.SNAPSHOT;

      } else if (entry.hasTerm()) {

        Term t = entry.getTerm();
        term = t.getTerm();
        lastEntryType = Type.TERM;

      } else if (entry.hasVote()) {

        Vote v = entry.getVote();
        lastVotedFor = Optional.fromNullable(v.getVotedFor());
        lastEntryType = Type.VOTE;

        System.out.println("Vote: " + lastVotedFor.orNull());

      }

      System.out.println(lastEntryType.toString() + " " + "term: " + term + ", index: " + index + ", committed: " + committed);
    }

    journal.close();

  }
View Full Code Here

TOP

Related Classes of journal.io.api.Journal$WriteCommand

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.