/**
   * Converts the given DBIN slot, leaving bin/index set to the inserted BIN slot.
   *
   * <p>Enter/leave with bin field latched, although bin field may change.
   *
   * <p>If slot is inserted into current bin, leave bin field unchanged and set index field to
   * inserted slot.
   *
   * <p>If slot is inserted into a different bin, set bin/index fields to inserted slot.
   */
  private void convertDbinSlot(final DBIN dbin, final int dbinIndex, final byte[] binKey) {

    final byte[] newKey = DupKeyData.replaceData(binKey, dbin.getKey(dbinIndex));

    if (DEBUG) {
      System.out.println("DupConvert DBIN LN " + Key.dumpString(newKey, 0));
    }

    /*
     * If the current BIN can hold the new slot, don't bother to do a
     * search to find it.
     */
    if (bin.needsSplitting() || !bin.isKeyInBounds(newKey)) {

      /* Compact keys after finishing with a BIN. */
      bin.compactMemory();

      /* Evict without latches, before moving to a new BIN. */
      bin.releaseLatch();
      envImpl.daemonEviction(false /*backgroundIO*/);

      /* Find a BIN for insertion, split if necessary. */
      bin = dbin.getDatabase().getTree().searchSplitsAllowed(newKey, CacheMode.UNCHANGED);
    }

    final int newIndex =
        bin.insertEntry1(
            null /*ln*/,
            newKey,
            null /*data*/,
            dbin.getLsn(dbinIndex),
            dbin.getState(dbinIndex),
            false);

    if ((newIndex & IN.INSERT_SUCCESS) == 0) {
      throw EnvironmentFailureException.unexpectedState(
          "Key not inserted: " + Key.dumpString(newKey, 0) + " DB: " + dbin.getDatabase().getId());
    }

    index = newIndex & ~IN.INSERT_SUCCESS;

    /*
     * Evict LN from DBIN slot. Although we don't explicitly load DBIN LNs,
     * it may have been loaded by recovery.
     */
    dbin.detachNode(dbinIndex, false /*updateLsn*/, -1 /*lsn*/);

    nConverted += 1;
  }
  /**
   * Converts the bin/index slot, whether a singleton LN or a DIN root.
   *
   * <p>Enter/leave with bin field latched, although bin field may change.
   *
   * <p>When a singleton LN is converted, leaves with bin/index fields unchanged.
   *
   * <p>When a dup tree is converted, leaves with bin/index fields set to last inserted slot. This
   * is the slot of the highest key in the dup tree.
   */
  private void convertBinSlot() {
    if (DEBUG) {
      System.out.println(
          "DupConvert BIN LSN "
              + DbLsn.getNoFormatString(bin.getLsn(index))
              + " index "
              + index
              + " nEntries "
              + bin.getNEntries());
    }
    /* Delete slot if LN is deleted. */
    final boolean isDeleted;
    if (isLNDeleted(bin, index)) {
      deleteSlot();
      return;
    }

    final Node node = bin.fetchLNOrDIN(index, CacheMode.DEFAULT);

    if (!node.containsDuplicates()) {
      if (DEBUG) {
        System.out.println("DupConvert BIN LN " + Key.dumpString(bin.getKey(index), 0));
      }
      /* Fetching a non-deleted LN updates the slot key; we're done. */
      assert node instanceof LN;
      nConverted += 1;
      return;
    }

    /*
     * Delete the slot containing the DIN before re-inserting the dup tree,
     * so that the DIN slot key doesn't interfere with insertions.
     *
     * The DIN is evicted and memory usage is decremented. This is not
     * exactly correct because we keep a local reference to the DIN until
     * the dup tree is converted, but we tolerate this temporary
     * inaccuracy.
     */
    final byte[] binKey = bin.getKey(index);
    final DIN din = (DIN) node;
    deleteSlot();
    convertDin(din, binKey);
  }
Exemplo n.º 3
0
  /**
   * For unit test support:
   *
   * @return a string that dumps information about this IN, without
   */
  @Override
  public String dumpString(int nSpaces, boolean dumpTags) {
    StringBuilder sb = new StringBuilder();
    sb.append(TreeUtils.indent(nSpaces));
    sb.append(beginTag());
    sb.append('\n');

    sb.append(TreeUtils.indent(nSpaces + 2));
    sb.append("<dupkey>");
    sb.append(dupKey == null ? "" : Key.dumpString(dupKey, 0));
    sb.append("</dupkey>");
    sb.append('\n');

    sb.append(super.dumpString(nSpaces, false));

    sb.append(TreeUtils.indent(nSpaces));
    sb.append(endTag());
    return sb.toString();
  }
Exemplo n.º 4
0
  private void doDumpLoadTest(boolean printable, int nDumps) throws IOException, DatabaseException {

    Hashtable[] dataMaps = new Hashtable[nDumps];
    for (int i = 0; i < nDumps; i += 1) {
      dataMaps[i] = new Hashtable();
    }
    initDbs(nDumps, dataMaps);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baos);
    for (int i = 0; i < nDumps; i += 1) {
      DbDump dumper = new DbDump(env, dbName + i, out, null, printable);
      dumper.dump();
    }
    byte[] baosba = baos.toByteArray();
    BufferedReader rdr =
        new BufferedReader(new InputStreamReader(new ByteArrayInputStream(baosba)));
    for (int i = 0; i < nDumps; i += 1) {
      DbLoad loader = new DbLoad();
      loader.setEnv(env);
      loader.setInputReader(rdr);
      loader.setNoOverwrite(false);
      loader.setDbName(dbName + i);
      loader.load();
      verifyDb(dataMaps[i], i);
    }

    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    PrintStream out2 = new PrintStream(baos2);
    for (int i = 0; i < nDumps; i += 1) {
      DbDump dumper2 = new DbDump(env, dbName + i, out2, null, printable);
      dumper2.dump();
    }
    assertEquals(0, Key.compareKeys(baosba, baos2.toByteArray(), null));

    env.close();
  }
Exemplo n.º 5
0
  /**
   * Compress this BIN by removing any entries that are deleted. Deleted entries are those that have
   * LN's marked deleted or if the knownDeleted flag is set. Caller is responsible for latching and
   * unlatching this node.
   *
   * @param binRef is used to determine the set of keys to be checked for deletedness, or is null to
   *     check all keys.
   * @param canFetch if false, don't fetch any non-resident children. We don't want some callers of
   *     compress, such as the evictor, to fault in other nodes.
   * @return true if we had to requeue the entry because we were unable to get locks, false if all
   *     entries were processed and therefore any remaining deleted keys in the BINReference must
   *     now be in some other BIN because of a split.
   */
  @Override
  public boolean compress(
      BINReference binRef, boolean canFetch, LocalUtilizationTracker localTracker)
      throws DatabaseException {

    boolean ret = false;
    boolean setNewIdKey = false;
    boolean anyLocksDenied = false;
    DatabaseImpl db = getDatabase();
    EnvironmentImpl envImpl = db.getDbEnvironment();
    BasicLocker lockingTxn = BasicLocker.createBasicLocker(envImpl);

    try {
      for (int i = 0; i < getNEntries(); i++) {

        /*
         * We have to be able to lock the LN before we can compress the
         * entry.  If we can't, then, skip over it.
         *
         * We must lock the LN even if isKnownDeleted is true, because
         * locks protect the aborts. (Aborts may execute multiple
         * operations, where each operation latches and unlatches. It's
         * the LN lock that protects the integrity of the whole
         * multi-step process.)
         *
         * For example, during abort, there may be cases where we have
         * deleted and then added an LN during the same txn.  This
         * means that to undo/abort it, we first delete the LN (leaving
         * knownDeleted set), and then add it back into the tree.  We
         * want to make sure the entry is in the BIN when we do the
         * insert back in.
         */
        boolean deleteEntry = false;
        Node n = null;

        if (binRef == null
            || isEntryPendingDeleted(i)
            || isEntryKnownDeleted(i)
            || binRef.hasDeletedKey(new Key(getKey(i)))) {

          if (canFetch) {
            if (db.isDeferredWriteMode() && getLsn(i) == DbLsn.NULL_LSN) {
              /* Null LSNs are ok in DW. [#15588] */
              n = getTarget(i);
            } else {
              n = fetchTarget(i);
            }
          } else {
            n = getTarget(i);
            if (n == null) {
              /* Punt, we don't know the state of this child. */
              continue;
            }
          }

          if (n == null) {
            /* Cleaner deleted the log file.  Compress this LN. */
            deleteEntry = true;
          } else if (isEntryKnownDeleted(i)) {
            LockResult lockRet = lockingTxn.nonBlockingLock(n.getNodeId(), LockType.READ, db);
            if (lockRet.getLockGrant() == LockGrantType.DENIED) {
              anyLocksDenied = true;
              continue;
            }

            deleteEntry = true;
          } else {
            if (!n.containsDuplicates()) {
              LN ln = (LN) n;
              LockResult lockRet = lockingTxn.nonBlockingLock(ln.getNodeId(), LockType.READ, db);
              if (lockRet.getLockGrant() == LockGrantType.DENIED) {
                anyLocksDenied = true;
                continue;
              }

              if (ln.isDeleted()) {
                deleteEntry = true;
              }
            }
          }

          /* Remove key from BINReference in case we requeue it. */
          if (binRef != null) {
            binRef.removeDeletedKey(new Key(getKey(i)));
          }
        }

        /* At this point, we know we can delete. */
        if (deleteEntry) {
          boolean entryIsIdentifierKey =
              Key.compareKeys(getKey(i), getIdentifierKey(), getKeyComparator()) == 0;
          if (entryIsIdentifierKey) {

            /*
             * We're about to remove the entry with the idKey so
             * the node will need a new idkey.
             */
            setNewIdKey = true;
          }

          /*
           * When deleting a deferred-write LN entry, we count the
           * last logged LSN as obsolete.
           */
          if (localTracker != null && db.isDeferredWriteMode() && n instanceof LN) {
            LN ln = (LN) n;
            long lsn = getLsn(i);
            if (ln.isDirty() && lsn != DbLsn.NULL_LSN) {
              localTracker.countObsoleteNode(lsn, ln.getLogType(), ln.getLastLoggedSize(), db);
            }
          }

          boolean deleteSuccess = deleteEntry(i, true);
          assert deleteSuccess;

          /*
           * Since we're deleting the current entry, bump the current
           * index back down one.
           */
          i--;
        }
      }
    } finally {
      if (lockingTxn != null) {
        lockingTxn.operationEnd();
      }
    }

    if (anyLocksDenied && binRef != null) {
      db.getDbEnvironment().addToCompressorQueue(binRef, false);
      ret = true;
    }

    if (getNEntries() != 0 && setNewIdKey) {
      setIdentifierKey(getKey(0));
    }

    /* This BIN is empty and expendable. */
    if (getNEntries() == 0) {
      setGeneration(0);
    }

    return ret;
  }
Exemplo n.º 6
0
 /** DBINS need to dump their dup key */
 @Override
 protected void dumpLogAdditional(StringBuilder sb) {
   super.dumpLogAdditional(sb);
   sb.append(Key.dumpString(dupKey, 0));
 }