Example #1
0
  MasterPage findLatestMasterPage() throws IOException {
    MasterPage latest = null;
    for (int i = 0; i < MASTERPAGE_SLOTS; i++) {
      // Because of crypto IV, we cannot read in bulk...
      int position = MASTERPAGE_HEADER_SIZE + (i * MASTERPAGE_SLOT_SIZE);
      ByteBuffer buffer =
          Futures.get(readDirect(position, MASTERPAGE_SLOT_SIZE), IOException.class);

      MasterPage masterPage = new MasterPage(buffer, 0);
      if (!masterPage.isValid()) {
        log.warn("Found corrupted master page slot {}", i);
        continue;
      }

      if (latest == null) {
        latest = masterPage;
      } else if (latest.getTransactionId() < masterPage.getTransactionId()) {
        latest = masterPage;
      }
    }

    if (latest == null) {
      throw new IOException("All master pages corrupted");
    }

    return latest;
  }
Example #2
0
  ListenableFuture<Void> writeMasterPage(TransactionPage transactionPage, int transactionPageId) {
    long transactionId = transactionPage.getTransactionId();
    int newRootPage = transactionPage.getRootPageId();

    int slot = (int) (transactionId % MASTERPAGE_SLOTS);

    {
      int position = MASTERPAGE_HEADER_SIZE + (slot * MASTERPAGE_SLOT_SIZE);

      ByteBuffer mmap = ByteBuffer.allocate(MASTERPAGE_SLOT_SIZE);
      assert mmap.position() == 0;
      MasterPage.create(mmap, newRootPage, transactionPageId, transactionId);
      mmap.position(MASTERPAGE_SLOT_SIZE);
      mmap.flip();
      assert mmap.remaining() == MASTERPAGE_SLOT_SIZE;
      return writeDirect(position, mmap);
    }
  }
Example #3
0
  public ByteSource asByteSource(ReadOnlyTransaction txn) throws IOException {
    ByteBuffer snapshotHeader = ByteBuffer.allocate(HEADER_SIZE);

    {
      ByteBuffer header = buildHeader();
      assert header.remaining() == MASTERPAGE_HEADER_SIZE;
      snapshotHeader.put(header);
    }

    // We build a master header with just the current snapshot transaction
    for (int i = 0; i < MASTERPAGE_SLOTS; i++) {
      int position = MASTERPAGE_HEADER_SIZE + (i * MASTERPAGE_SLOT_SIZE);

      ByteBuffer mmap = ByteBuffer.allocate(MASTERPAGE_SLOT_SIZE);
      assert mmap.position() == 0;

      int rootPage = txn.rootPageId;
      int transactionPage = txn.transactionPageId;
      long transactionId = txn.snapshotTransactionId;

      MasterPage.create(mmap, rootPage, transactionPage, transactionId);
      mmap.position(MASTERPAGE_SLOT_SIZE);
      mmap.flip();
      assert mmap.remaining() == MASTERPAGE_SLOT_SIZE;

      snapshotHeader.position(position);
      snapshotHeader.put(mmap);
    }

    snapshotHeader.position(0);

    ByteSource dataByteSource = asByteSource0();
    dataByteSource = dataByteSource.slice(HEADER_SIZE, Long.MAX_VALUE);

    return ByteSource.concat(new ByteBufferByteSource(snapshotHeader), dataByteSource);
  }