/* Do the real reading work. */
    private void doRead(Environment env, DatabaseConfig dbConfig, ArrayList<TestObject> list)
        throws Exception {

      Database db = env.openDatabase(null, dbName, dbConfig);
      Cursor cursor = db.openCursor(null, null);
      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      while (OperationStatus.SUCCESS == cursor.getNext(key, data, null)) {
        list.add(new TestObject(IntegerBinding.entryToInt(key), StringBinding.entryToString(data)));
      }
      cursor.close();
      db.close();
    }
  private void experienceLogFlushTask(String sleepTime, boolean flushBeforeCrash) throws Throwable {

    try {
      createRepEnvInfo(sleepTime);

      ReplicatedEnvironment master = RepTestUtils.joinGroup(repEnvInfo);
      long startTime = System.currentTimeMillis();

      StatsConfig stConfig = new StatsConfig();
      stConfig.setClear(true);

      /* Flush the existed dirty data before we do writes. */
      for (int i = 0; i < repEnvInfo.length; i++) {
        repEnvInfo[i].getEnv().sync();
        repEnvInfo[i].getEnv().getStats(stConfig);
      }

      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(true);

      Database db = master.openDatabase(null, dbName, dbConfig);

      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      for (int i = 1; i <= 100; i++) {
        IntegerBinding.intToEntry(i, key);
        StringBinding.stringToEntry(value, data);
        db.put(null, key, data);
      }

      assertTrue(System.currentTimeMillis() - startTime < 15000);

      Thread.sleep(15000);

      long endTime = System.currentTimeMillis();

      for (int i = 0; i < repEnvInfo.length; i++) {
        EnvironmentStats envStats = repEnvInfo[i].getEnv().getStats(stConfig);
        LogFlusher flusher = repEnvInfo[i].getRepNode().getLogFlusher();
        if (flushBeforeCrash) {
          /* Make sure the LogFlushTask has been invoked. */
          assertTrue(flusher.getFlushTask().scheduledExecutionTime() > startTime);
          assertTrue(flusher.getFlushTask().scheduledExecutionTime() < endTime);

          /*
           * Since the log file size is not so big, we can't assure
           * all the data will be written in the same log file, but
           * we can sure that a flush does happen.
           */
          assertTrue(envStats.getNSequentialWrites() >= 1);
          assertTrue(envStats.getNLogFSyncs() == 1);
        } else {

          /*
           * Make sure the LogFlushTask is not invoked after making
           * the changes.
           */
          assertTrue(flusher.getFlushTask().scheduledExecutionTime() < startTime);
          assertTrue(envStats.getNSequentialWrites() == 0);
          assertTrue(envStats.getNLogFSyncs() == 0);
        }
        assertTrue(envStats.getNFSyncs() == 0);
      }

      File[] envHomes = new File[3];
      /* Close the replicas without doing a checkpoint. */
      for (int i = 0; i < repEnvInfo.length; i++) {
        envHomes[i] = repEnvInfo[i].getEnvHome();
        repEnvInfo[i].getRepImpl().abnormalClose();
      }

      /*
       * Open a read only standalone Environment on the replicas to see
       * whether the data has been synced to the disk.
       */
      EnvironmentConfig newConfig = new EnvironmentConfig();
      newConfig.setAllowCreate(false);
      newConfig.setReadOnly(true);
      newConfig.setTransactional(true);

      for (int i = 0; i < repEnvInfo.length; i++) {
        Environment env = new Environment(envHomes[i], newConfig);

        dbConfig.setAllowCreate(false);
        dbConfig.setReadOnly(true);

        try {
          db = env.openDatabase(null, dbName, dbConfig);
        } catch (DatabaseNotFoundException e) {

          /*
           * If the system crashes before the flush, the database is
           * not synced to the disk, so this database can't be found
           * at all, it's expected.
           */
          assertFalse(flushBeforeCrash);
        }

        if (flushBeforeCrash) {
          assertTrue(db.count() == 100);
          for (int index = 1; index <= 100; index++) {
            IntegerBinding.intToEntry(index, key);
            OperationStatus status = db.get(null, key, data, null);
            if (flushBeforeCrash) {
              assertTrue(status == OperationStatus.SUCCESS);
              assertEquals(value, StringBinding.entryToString(data));
            }
          }
        }

        if (flushBeforeCrash) {
          db.close();
        }
        env.close();
      }
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
예제 #3
0
  @Override
  public int doWork(String[] args) {
    int maxDepth = 3;
    File dbHome = null;
    com.github.lindenb.jvarkit.util.cli.GetOpt opt =
        new com.github.lindenb.jvarkit.util.cli.GetOpt();
    int c;
    while ((c = opt.getopt(args, getGetOptDefault() + "D:M:")) != -1) {
      switch (c) {
        case 'D':
          {
            dbHome = new File(opt.getOptArg());
            break;
          }
        case 'M':
          {
            maxDepth = Integer.parseInt(opt.getOptArg());
            break;
          }
        default:
          {
            switch (handleOtherOptions(c, opt, args)) {
              case EXIT_FAILURE:
                return -1;
              case EXIT_SUCCESS:
                return 0;
              default:
                break;
            }
          }
      }
    }
    if (dbHome == null) {
      error("Undefined DB-Home");
    }
    environment = null;
    EnvironmentConfig envCfg = new EnvironmentConfig();
    this.database = null;
    Transaction txn = null;
    try {
      envCfg.setAllowCreate(true);
      this.environment = new Environment(dbHome, envCfg);
      DatabaseConfig cfg = new DatabaseConfig();
      cfg.setAllowCreate(true);
      cfg.setTemporary(true);
      this.database = environment.openDatabase(txn, "interactions", cfg);
      if (opt.getOptInd() == args.length) {
        info("reading stdin");
        LineIterator r = IOUtils.openStdinForLineIterator();
        load(txn, r);
        CloserUtil.close(r);
      } else {
        for (int optind = opt.getOptInd(); optind < args.length; ++optind) {
          String filename = args[optind];
          info("reading " + filename);
          LineIterator r = IOUtils.openURIForLineIterator(filename);
          load(txn, r);
          CloserUtil.close(r);
        }
      }
      DatabaseEntry key1 = new DatabaseEntry();
      DatabaseEntry data1 = new DatabaseEntry();
      DatabaseEntry key2 = new DatabaseEntry();
      DatabaseEntry data2 = new DatabaseEntry();

      Cursor c1 = this.database.openCursor(txn, null);
      Cursor c2 = this.database.openCursor(txn, null);
      while (c1.getNext(key1, data1, LockMode.DEFAULT) == OperationStatus.SUCCESS) {

        String prot1 = StringBinding.entryToString(key1);

        boolean first = true;

        while ((first
                ? c2.getFirst(key2, data2, LockMode.DEFAULT)
                : c2.getNext(key2, data2, LockMode.DEFAULT))
            == OperationStatus.SUCCESS) {
          first = false;
          String prot2 = StringBinding.entryToString(key2);
          if (prot2.compareTo(prot1) <= 0) continue;

          Stack<String> path = new Stack<String>();
          path.push(prot1);
          int depth = recursive(txn, prot2, path, -1, maxDepth);
          if (depth != -1) {
            System.out.println(prot1 + "\t" + prot2 + "\t" + depth);
          } else {
            // System.out.println(prot1+"\t"+prot2+"\t"+depth);
          }
          if (System.out.checkError()) break;
        }
      }
      CloserUtil.close(c2);
      CloserUtil.close(c1);
    } catch (Exception err) {
      error(err);
    } finally {
      CloserUtil.close(this.database);
      CloserUtil.close(this.environment);
    }
    return 0;
  }