/** @see org.geogit.storage.ObjectDatabase#put(org.geogit.storage.ObjectWriter) */
  @Override
  protected boolean putInternal(final ObjectId id, final byte[] rawData, final boolean override)
      throws IOException {
    final byte[] rawKey = id.getRawValue();
    DatabaseEntry key = new DatabaseEntry(rawKey);
    DatabaseEntry data = new DatabaseEntry(rawData);

    OperationStatus status;
    if (override) {
      status = objectDb.put(txn.getTransaction(), key, data);
    } else {
      status = objectDb.putNoOverwrite(txn.getTransaction(), key, data);
    }
    final boolean didntExist = SUCCESS.equals(status);

    if (LOGGER.isLoggable(Level.FINER)) {
      if (didntExist) {
        LOGGER.finer("Key already exists in blob store, blob reused for id: " + id);
      }
    }
    return didntExist;
  }
  /**
   * Put the given CrawlURI in at the appropriate place.
   *
   * @param curi
   * @throws DatabaseException
   */
  public void put(CrawlURI curi, boolean overwriteIfPresent) throws DatabaseException {
    DatabaseEntry insertKey = (DatabaseEntry) curi.getHolderKey();
    if (insertKey == null) {
      insertKey = calculateInsertKey(curi);
      curi.setHolderKey(insertKey);
    }
    DatabaseEntry value = new DatabaseEntry();
    crawlUriBinding.objectToEntry(curi, value);
    // Output tally on avg. size if level is FINE or greater.
    if (LOGGER.isLoggable(Level.FINE)) {
      tallyAverageEntrySize(curi, value);
    }
    OperationStatus status;
    if (overwriteIfPresent) {
      status = pendingUrisDB.put(null, insertKey, value);
    } else {
      status = pendingUrisDB.putNoOverwrite(null, insertKey, value);
    }

    if (status != OperationStatus.SUCCESS) {
      LOGGER.log(
          Level.SEVERE, "URI enqueueing failed; " + status + " " + curi, new RuntimeException());
    }
  }
Example #3
0
  public void testPutAndDelete() throws DatabaseException {

    SecondaryDatabase secDb = initDb();
    Database priDb = secDb.getPrimaryDatabase();

    DatabaseEntry data = new DatabaseEntry();
    DatabaseEntry key = new DatabaseEntry();
    OperationStatus status;
    Transaction txn = txnBegin();

    /* Database.put() */
    status = priDb.put(txn, entry(1), entry(2));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(2), data);

    /* Database.putNoOverwrite() */
    status = priDb.putNoOverwrite(txn, entry(1), entry(1));
    assertSame(OperationStatus.KEYEXIST, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(2), data);

    /* Database.put() overwrite */
    status = priDb.put(txn, entry(1), entry(3));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(3), data);

    /* Database.delete() */
    status = priDb.delete(txn, entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = priDb.delete(txn, entry(1));
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

    /* SecondaryDatabase.delete() */
    status = priDb.put(txn, entry(1), entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = priDb.put(txn, entry(2), entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(101), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(1), data);
    status = secDb.delete(txn, entry(101));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.delete(txn, entry(101));
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(101), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = priDb.get(txn, entry(1), data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = priDb.get(txn, entry(2), data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

    /*
     * Database.putNoDupData() cannot be called since the primary cannot be
     * configured for duplicates.
     */

    /* Primary and secondary are empty now. */

    /* Get a txn for a cursor. */
    txnCommit(txn);
    txn = txnBeginCursor();

    Cursor priCursor = null;
    SecondaryCursor secCursor = null;
    try {
      priCursor = priDb.openCursor(txn, null);
      secCursor = secDb.openSecondaryCursor(txn, null);

      /* Cursor.putNoOverwrite() */
      status = priCursor.putNoOverwrite(entry(1), entry(2));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(102), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(2), data);

      /* Cursor.putCurrent() */
      status = priCursor.putCurrent(entry(3));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(102), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = secCursor.getSearchKey(entry(103), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(3), data);

      /* Cursor.delete() */
      status = priCursor.delete();
      assertSame(OperationStatus.SUCCESS, status);
      status = priCursor.delete();
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getSearchKey(entry(103), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = priCursor.getSearchKey(entry(1), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* Cursor.put() */
      status = priCursor.put(entry(1), entry(4));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(104), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(4), data);

      /* SecondaryCursor.delete() */
      status = secCursor.delete();
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.delete();
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getCurrent(new DatabaseEntry(), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getSearchKey(entry(104), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = priCursor.getSearchKey(entry(1), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /*
       * Cursor.putNoDupData() cannot be called since the primary cannot
       * be configured for duplicates.
       */

      /* Primary and secondary are empty now. */
    } finally {
      if (secCursor != null) {
        secCursor.close();
      }
      if (priCursor != null) {
        priCursor.close();
      }
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();
  }