Example #1
0
  private void openEnvironment(File file) throws DatabaseInitException {
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setCachePercent(30);
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    envConfig.setTxnTimeout(10, TimeUnit.SECONDS);
    envConfig.setLockTimeout(2000, TimeUnit.MILLISECONDS);
    try {
      environment = new Environment(file, envConfig);
      transactionConfig = new TransactionConfig();
      transactionConfig.setReadCommitted(true);

      cursorConfig = new CursorConfig();
      cursorConfig.setReadCommitted(true);
    } catch (EnvironmentLockedException e) {
      String message =
          "Environment locked exception. Another process is using the same database, or the current user has no write access (database location: \""
              + file.getAbsolutePath()
              + "\")";
      throw new DatabaseInitException(message);
    } catch (DatabaseException e) {
      String message = "A database initialisation error has occured (" + e.getMessage() + ")";
      throw new DatabaseInitException(message);
    }
  }
  @Override
  protected List<ObjectId> lookUpInternal(final byte[] partialId) {

    DatabaseEntry key;
    {
      byte[] keyData = partialId.clone();
      key = new DatabaseEntry(keyData);
    }

    DatabaseEntry data = new DatabaseEntry();
    data.setPartial(0, 0, true); // do not retrieve data

    List<ObjectId> matches;

    CursorConfig cursorConfig = new CursorConfig();
    cursorConfig.setReadCommitted(true);
    cursorConfig.setReadUncommitted(false);

    Cursor cursor = objectDb.openCursor(txn.getTransaction(), cursorConfig);
    try {
      // position cursor at the first closest key to the one looked up
      OperationStatus status = cursor.getSearchKeyRange(key, data, LockMode.DEFAULT);
      if (SUCCESS.equals(status)) {
        matches = new ArrayList<ObjectId>(2);
        final byte[] compKey = new byte[partialId.length];
        while (SUCCESS.equals(status)) {
          byte[] keyData = key.getData();
          System.arraycopy(keyData, 0, compKey, 0, compKey.length);
          if (Arrays.equals(partialId, compKey)) {
            matches.add(new ObjectId(keyData));
          } else {
            break;
          }
          status = cursor.getNext(key, data, LockMode.DEFAULT);
        }
      } else {
        matches = Collections.emptyList();
      }
      return matches;
    } finally {
      cursor.close();
    }
  }
Example #3
0
  /** Constructor helper. */
  private void init(DataView view, boolean writeAllowed, CursorConfig config, KeyRange range)
      throws DatabaseException {

    if (config == null) {
      config = view.cursorConfig;
    }
    this.view = view;
    this.writeAllowed = writeAllowed && view.writeAllowed;
    this.range = (range != null) ? range : view.range;
    readUncommitted = config.getReadUncommitted() || view.currentTxn.isReadUncommitted();
    initThangs();

    if (joinCursor == null) {
      cursor = new MyRangeCursor(this.range, config, view, this.writeAllowed);
    }
  }