예제 #1
0
파일: Store.java 프로젝트: nologic/nabs
  public static Set<String> getStoreNames(Environment env) throws DatabaseException {

    Set<String> set = new HashSet<String>();
    for (Object o : env.getDatabaseNames()) {
      String s = (String) o;
      if (s.startsWith(NAME_PREFIX)) {
        int start = NAME_PREFIX.length();
        int end = s.indexOf(NAME_SEPARATOR, start);
        set.add(s.substring(start, end));
      }
    }
    return set;
  }
예제 #2
0
  /**
   * Invoke an operation for the given environment.
   *
   * @param targetEnv The target JE environment. May be null if the environment is not open.
   * @param actionName operation name.
   * @param params operation parameters. May be null.
   * @param signature operation signature. May be null.
   * @return the operation result
   */
  public Object invoke(
      Environment targetEnv, String actionName, Object[] params, String[] signature)
      throws MBeanException {

    /* Sanity checking. */
    if (actionName == null) {
      throw new IllegalArgumentException("actionName cannot be null");
    }

    try {
      if (targetEnv != null) {
        if (actionName.equals(OP_CLEAN)) {
          int numFiles = targetEnv.cleanLog();
          return new Integer(numFiles);
        } else if (actionName.equals(OP_EVICT)) {
          targetEnv.evictMemory();
          return null;
        } else if (actionName.equals(OP_CHECKPOINT)) {
          CheckpointConfig config = new CheckpointConfig();
          if ((params != null) && (params.length > 0)) {
            Boolean force = (Boolean) params[0];
            config.setForce(force.booleanValue());
          }
          targetEnv.checkpoint(config);
          return null;
        } else if (actionName.equals(OP_SYNC)) {
          targetEnv.sync();
          return null;
        } else if (actionName.equals(OP_ENV_STAT)) {
          return targetEnv.getStats(getStatsConfig(params));
        } else if (actionName.equals(OP_LOCK_STAT)) {
          return targetEnv.getLockStats(getStatsConfig(params));
        } else if (actionName.equals(OP_TXN_STAT)) {
          return targetEnv.getTransactionStats(getStatsConfig(params));
        } else if (actionName.equals(OP_DB_NAMES)) {
          return targetEnv.getDatabaseNames();
        } else if (actionName.equals(OP_DB_STAT)) {
          return getDatabaseStats(targetEnv, params);
        }
      }

      return new IllegalArgumentException("actionName: " + actionName + " is not valid");
    } catch (DatabaseException e) {
      /*
       * Add both the message and the exception for easiest
       * deciphering of the problem. Sometimes the original exception
       * stacktrace gets hidden in server logs.
       */
      throw new MBeanException(e, e.getMessage());
    }
  }
예제 #3
0
  public static void main(String[] args) {
    Environment env = null;
    Database db = null;
    EnvironmentConfig envconfig = new EnvironmentConfig();
    envconfig.setAllowCreate(true);
    try {
      env = new Environment(new File("D://bdb"), envconfig);
      DatabaseConfig dbconfig = new DatabaseConfig();
      dbconfig.setAllowCreate(true);
      db = env.openDatabase(null, "dbac.db", dbconfig);
      String key = "mykey";
      DatabaseEntry thekey = new DatabaseEntry();
      thekey.setData(key.getBytes("utf-8"));

      Long value = new Long(123456);
      DatabaseEntry thevalue = new DatabaseEntry();
      EntryBinding myBinging = TupleBinding.getPrimitiveBinding(Long.class);
      myBinging.objectToEntry(value, thevalue);
      // LongBinding myLongBinging=(LongBinding)TupleBinding.getPrimitiveBinding(Long.class);
      // myLongBinging.objectToEntry(value, thevalue);
      db.put(null, thekey, thevalue);
      DatabaseEntry valueEntry = new DatabaseEntry();
      OperationStatus status = db.get(null, thekey, valueEntry, LockMode.DEFAULT);
      if (status == OperationStatus.SUCCESS) {
        // Long number=myLongBinging.entryToObject(valueEntry);
        Long number = (Long) myBinging.entryToObject(valueEntry);
        System.out.println(env.getDatabaseNames());
        System.out.println(number);
      }
    } catch (EnvironmentLockedException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null) {
        try {
          db.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
      if (env != null) {
        try {
          env.cleanLog();
          env.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
    }
  }
예제 #4
0
 private void examine(File file) throws DatabaseInitException {
   openEnvironment(file);
   for (String tableName : environment.getDatabaseNames()) {
     Database table = environment.openDatabase(null, tableName, null);
     Cursor cursor = table.openCursor(null, cursorConfig);
     try {
       int count = 0;
       while (cursor.getNext(new DatabaseEntry(), new DatabaseEntry(), LockMode.DEFAULT)
           == OperationStatus.SUCCESS) {
         count++;
       }
       if (count != 0) {
         System.out.println(tableName + ": " + count);
       }
     } finally {
       cursor.close();
     }
   }
 }
예제 #5
0
 public List<String> getDatabaseNames() {
   return myEnv.getDatabaseNames();
 }