Beispiel #1
0
 @Override
 public void removeMapKeys(
     String collection, String name, ColumnField mapField, ArrayList<ColumnField> mapKeys)
     throws FailedDBOperationException {
   JSONObject record = null;
   try {
     record = lookupEntireRecord(collection, name);
   } catch (RecordNotFoundException e) {
   }
   DatabaseConfig.getLogger().log(Level.FINE, "Record before:{0}", record);
   if (record == null) {
     throw new FailedDBOperationException(collection, name, "Record not found.");
   }
   if (mapField != null && mapKeys != null) {
     try {
       JSONObject json = record.getJSONObject(mapField.getName());
       for (int i = 0; i < mapKeys.size(); i++) {
         String fieldName = mapKeys.get(i).getName();
         json.remove(fieldName);
       }
       DatabaseConfig.getLogger().log(Level.FINE, "Json after:{0}", json);
       record.put(mapField.getName(), json);
       DatabaseConfig.getLogger().log(Level.FINE, "Record after:{0}", record);
     } catch (JSONException e) {
       DatabaseConfig.getLogger().log(Level.SEVERE, "Problem updating json: {0}", e.getMessage());
     }
   }
   getMap(collection).put(name, record);
 }
  /** Test exclusive creation. */
  @Test
  public void testExclusive() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setExclusiveCreate(true);

      /* Should succeed and create the database. */
      Database dbA = env.openDatabase(null, "foo", dbConfig);
      dbA.close();

      /* Should not succeed, because the database exists. */
      try {
        env.openDatabase(null, "foo", dbConfig);
        fail("Database already exists");
      } catch (DatabaseException e) {
      }
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
Beispiel #3
0
 @edu.umd.cs.findbugs.annotations.SuppressWarnings(
     value = "SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE",
     justification = "Dynamic based upon tables in the database")
 public void dropTables(long start, long end) {
   String cluster = System.getProperty("CLUSTER");
   if (cluster == null) {
     cluster = "unknown";
   }
   DatabaseWriter dbw = new DatabaseWriter(cluster);
   try {
     HashMap<String, String> dbNames = dbc.startWith("report.db.name.");
     for (Entry<String, String> entry : dbNames.entrySet()) {
       String tableName = entry.getValue();
       if (!RegexUtil.isRegex(tableName)) {
         log.warn(
             "Skipping tableName: '"
                 + tableName
                 + "' because there was an error parsing it as a regex: "
                 + RegexUtil.regexError(tableName));
         return;
       }
       String[] tableList = dbc.findTableName(tableName, start, end);
       for (String tl : tableList) {
         log.debug("table name: " + tableList[0]);
         try {
           String[] parts = tl.split("_");
           int partition = Integer.parseInt(parts[parts.length - 2]);
           StringBuilder table = new StringBuilder();
           for (int i = 0; i < parts.length - 2; i++) {
             if (i != 0) {
               table.append("_");
             }
             table.append(parts[i]);
           }
           partition = partition - 3;
           if (partition >= 0) {
             StringBuilder dropPartition = new StringBuilder();
             dropPartition.append("drop table if exists ");
             dropPartition.append(table);
             dropPartition.append("_");
             dropPartition.append(partition);
             dropPartition.append("_");
             dropPartition.append(parts[parts.length - 1]);
             final String query = dropPartition.toString();
             dbw.execute(query);
           }
         } catch (NumberFormatException e) {
           log.error("Error in parsing table partition number, skipping table:" + tableList[0]);
         } catch (ArrayIndexOutOfBoundsException e) {
           log.debug(
               "Skipping table:" + tableList[0] + ", because it has no partition configuration.");
         }
       }
     }
     dbw.close();
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
Beispiel #4
0
 public DatabaseConfig getConfig__wrappee__base() throws DatabaseException {
   DatabaseConfig showConfig = configuration.cloneConfig();
   Comparator btComp = (databaseImpl == null ? null : databaseImpl.getBtreeComparator());
   Comparator dupComp = (databaseImpl == null ? null : databaseImpl.getDuplicateComparator());
   showConfig.setBtreeComparator(btComp == null ? null : btComp.getClass());
   showConfig.setDuplicateComparator(dupComp == null ? null : dupComp.getClass());
   return showConfig;
 }
Beispiel #5
0
 private void init__wrappee__base(Environment env, DatabaseConfig config)
     throws DatabaseException {
   handleLocker = null;
   envHandle = env;
   configuration = config.cloneConfig();
   isWritable = !configuration.getReadOnly();
   state = OPEN;
 }
Beispiel #6
0
  public static Database buildsecondary(Database std) {
    // Parse database loaded
    try {
      io.deleteFile("secondarydb");
      // SecondaryDatabases started
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setType(DatabaseType.HASH);
      dbConfig.setAllowCreate(true);
      dbConfig.setUnsortedDuplicates(true);
      Database secdb = new Database("secondarydb", null, dbConfig);
      // Cursors started
      Cursor stdcursor = std.openCursor(null, null);
      Cursor secdbcursor = secdb.openCursor(null, null);
      // Key and Data started
      DatabaseEntry stdkey = new DatabaseEntry();
      DatabaseEntry stddata = new DatabaseEntry();
      DatabaseEntry seckey = new DatabaseEntry();
      DatabaseEntry secdata = new DatabaseEntry();

      while (stdcursor.getNext(stdkey, stddata, LockMode.DEFAULT)
          == OperationStatus.SUCCESS) { // Writing into secondary db
        String[] key = new String(stdkey.getData()).split(",");
        String data = new String(stddata.getData());
        // DEBUG:
        // System.out.println("key 0:" + key[0] + " key 1:" + key[1] + " data:" + data);
        seckey.setData(key[1].getBytes());
        OperationStatus operation = secdbcursor.getSearchKey(seckey, secdata, LockMode.DEFAULT);

        String b = null;
        while (operation == OperationStatus.SUCCESS) {
          b = new String(secdata.getData());
          secdbcursor.delete();
          operation = secdbcursor.getNextDup(seckey, secdata, LockMode.DEFAULT);
        }
        if (b == null) {
          seckey.setData(key[1].getBytes());
          secdata.setData(("(" + key[0] + "," + data + ")").getBytes());
          secdb.put(null, seckey, secdata);
        }
        if (b != null) {
          secdata.setData(b.concat("(" + key[0] + "," + data + ")").getBytes());
          secdb.put(null, seckey, secdata);
        }
        seckey.setData(null);
        secdata.setData(null);

        stdkey.setData(null);
        stddata.setData(null);
      }
      // io.debugread(secdb);

      return secdb;
    } catch (Exception e) {
      System.out.println("Error creating <user>,<song,rating> secondary table!\n");
      System.out.println(e.getMessage());
    }
    return null; // SHOULD NEVER HAPPEN
  }
Beispiel #7
0
  public static Database setupDatabase(boolean readOnly, String dbName, Bdb bdb)
      throws IOException, DatabaseException {
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setReadOnly(readOnly);
    dbConfig.setAllowCreate(!readOnly);
    dbConfig.setDeferredWrite(!readOnly);

    return bdb.bdbEnv.openDatabase(null, dbName, dbConfig);
  }
Beispiel #8
0
 void initExisting__wrappee__base(
     Environment env, Locker locker, DatabaseImpl databaseImpl, DatabaseConfig dbConfig)
     throws DatabaseException {
   validateConfigAgainstExistingDb(dbConfig, databaseImpl);
   init(env, dbConfig);
   this.databaseImpl = databaseImpl;
   databaseImpl.addReferringHandle(this);
   configuration.setSortedDuplicates(databaseImpl.getSortedDuplicates());
   configuration.setTransactional(databaseImpl.isTransactional());
 }
Beispiel #9
0
 public WorkQueues(Environment env, String dbName, boolean resumable) throws DatabaseException {
   this.env = env;
   this.resumable = resumable;
   DatabaseConfig dbConfig = new DatabaseConfig();
   dbConfig.setAllowCreate(true);
   dbConfig.setTransactional(resumable);
   dbConfig.setDeferredWrite(!resumable);
   urlsDB = env.openDatabase(null, dbName, dbConfig);
   webURLBinding = new WebURLTupleBinding();
 }
Beispiel #10
0
  public DatabaseConfig getConfig() throws DatabaseException {

    DatabaseConfig showConfig = configuration.cloneConfig();

    /*
     * Set the comparators from the database impl, they might have
     * changed from another handle.
     */
    Comparator btComp = (databaseImpl == null ? null : databaseImpl.getBtreeComparator());
    Comparator dupComp = (databaseImpl == null ? null : databaseImpl.getDuplicateComparator());
    showConfig.setBtreeComparator(btComp == null ? null : btComp.getClass());
    showConfig.setDuplicateComparator(dupComp == null ? null : dupComp.getClass());
    return showConfig;
  }
  @Inject
  public DatabaseProjectStoreManager(DBI dbi, ConfigMapper cfm, DatabaseConfig config) {
    super(config.getType(), dao(config.getType()), dbi);

    dbi.registerMapper(new StoredProjectMapper(cfm));
    dbi.registerMapper(new StoredRevisionMapper(cfm));
    dbi.registerMapper(new StoredWorkflowDefinitionMapper(cfm));
    dbi.registerMapper(new StoredWorkflowDefinitionWithProjectMapper(cfm));
    dbi.registerMapper(new WorkflowConfigMapper());
    dbi.registerMapper(new IdNameMapper());
    dbi.registerMapper(new ScheduleStatusMapper());
    dbi.registerArgumentFactory(cfm.getArgumentFactory());

    this.cfm = cfm;
  }
 /**
  * Create database writer based on configuration
  *
  * @param config
  * @param dbConfigName
  * @throws DataAccessObjectInitializationException
  */
 DatabaseWriter(Config config, String dbConfigName)
     throws DataAccessObjectInitializationException {
   this.config = config;
   String dbConfigFilename =
       config.constructConfigFilePath(DatabaseContext.DEFAULT_CONFIG_FILENAME);
   if (!(new File(dbConfigFilename).exists())) {
     throw new DataAccessObjectInitializationException(
         Messages.getFormattedString(
             "DatabaseDAO.errorConfigFileExists", dbConfigFilename)); // $NON-NLS-1$
   }
   DatabaseConfig dbConfig = DatabaseConfig.getInstance(dbConfigFilename, dbConfigName);
   dataSource = dbConfig.getDataSource();
   sqlConfig = dbConfig.getSqlConfig();
   dbContext = new DatabaseContext(dbConfigName);
 }
Beispiel #13
0
  public BDBCreator(String tableName, ArrayList<Integer> keyPositions, int secondaryIndex) {
    /* Creates and loads a BDB table with a secondary index on the column defined by*/
    this.tableName = tableName.toLowerCase();
    this.keyPositions = keyPositions;
    this.secIndexPos = secondaryIndex;
    this.tMap = Main.indexTypeMaps.get(this.tableName);
    setEnvironment();

    dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    myDB = myDbEnvironment.openDatabase(null, this.tableName, dbConfig);
    SecondaryConfig secConfig = new SecondaryConfig();
    secConfig.setAllowCreate(true);
    secConfig.setSortedDuplicates(true);

    createSecDB(secConfig);
    setConfig(dbConfig, secConfig);

    loadData();

    if (secDB != null) {
      secDB.close();
    }
    if (myDB != null) {
      myDB.close();
    }

    closeEnvironment();
  }
    public void run() {
      double gtps, itps;
      int n, ret;
      long start_time, end_time;

      //
      // Open the database files.
      //
      int err;
      try {
        DatabaseConfig config = new DatabaseConfig();
        config.setTransactional(true);
        adb = dbenv.openDatabase(null, "account", null, config);
        bdb = dbenv.openDatabase(null, "branch", null, config);
        tdb = dbenv.openDatabase(null, "teller", null, config);
        hdb = dbenv.openDatabase(null, "history", null, config);
      } catch (DatabaseException dbe) {
        TpcbExample.errExit(dbe, "Open of db files failed");
      } catch (FileNotFoundException fnfe) {
        TpcbExample.errExit(fnfe, "Open of db files failed, missing file");
      }

      start_time = (new Date()).getTime();
      for (txns = n = ntxns, failed = 0; n-- > 0; ) if ((ret = txn()) != 0) failed++;
      end_time = (new Date()).getTime();
      if (end_time == start_time) end_time++;

      System.out.println(
          getName()
              + ": "
              + (long) txns
              + " txns: "
              + failed
              + " failed, "
              + TpcbExample.showRounded((txns - failed) / (double) (end_time - start_time), 2)
              + " TPS");

      try {
        adb.close();
        bdb.close();
        tdb.close();
        hdb.close();
      } catch (DatabaseException dbe2) {
        TpcbExample.errExit(dbe2, "Close of db files failed");
      }
    }
  public void runTest(DatabaseType type) throws DatabaseException, FileNotFoundException {
    int i;
    DatabaseConfig conf = new DatabaseConfig();
    conf.setErrorStream(TestUtils.getErrorStream());
    conf.setErrorPrefix("HashCompareTest");
    conf.setType(type);
    if (type == DatabaseType.HASH) {
      conf.setHashComparator(new HashComparator());
    } else conf.setBtreeComparator(new BtreeComparator());
    conf.setAllowCreate(true);

    Database db = new Database(HASHCOMPARETEST_DBNAME, null, conf);

    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry("world".getBytes());
    for (i = 0; i < 100; i++) {
      key.setData((new String("key" + i)).getBytes());
      db.put(null, key, data);
    }
    i = 0;
    Cursor dbc;
    dbc = db.openCursor(null, CursorConfig.DEFAULT);
    while (dbc.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
      ++i;
    }
    //		System.out.println("retrieved " + i + " entries");
    dbc.close();
    db.close();
  }
  public MongoDatabaseConnection(DatabaseConfig config) throws DatabaseConnectionException {

    Mongo m = null;
    String hostName = config.get("host");
    String dbName = config.get("database");
    String collectionName = config.get("collection");

    try {
      m = new Mongo(hostName);
    } catch (UnknownHostException e) {
      throw new DatabaseConnectionException(e.getMessage());
    } catch (MongoException e) {
      throw new DatabaseConnectionException(e.getMessage());
    }

    this.database = m.getDB(dbName);
    this.collection = database.getCollection(collectionName);
  }
Beispiel #17
0
  /** Open a database, called by Environment. */
  void initExisting(
      Environment env, Locker locker, DatabaseImpl databaseImpl, DatabaseConfig dbConfig)
      throws DatabaseException {

    /*
     * Make sure the configuration used for the open is compatible with the
     * existing databaseImpl.
     */
    validateConfigAgainstExistingDb(dbConfig, databaseImpl);

    init(env, dbConfig);
    this.databaseImpl = databaseImpl;
    databaseImpl.addReferringHandle(this);

    /*
     * Copy the duplicates and transactional properties of the underlying
     * database, in case the useExistingConfig property is set.
     */
    configuration.setSortedDuplicates(databaseImpl.getSortedDuplicates());
    configuration.setTransactional(databaseImpl.isTransactional());
  }
Beispiel #18
0
 void initNew__wrappee__base(
     Environment env, Locker locker, String databaseName, DatabaseConfig dbConfig)
     throws DatabaseException {
   if (dbConfig.getReadOnly()) {
     throw new DatabaseException(
         "DatabaseConfig.setReadOnly() must be set to false " + "when creating a Database");
   }
   init(env, dbConfig);
   EnvironmentImpl environmentImpl = DbInternal.envGetEnvironmentImpl(envHandle);
   databaseImpl = environmentImpl.createDb(locker, databaseName, dbConfig, this);
   databaseImpl.addReferringHandle(this);
 }
Beispiel #19
0
  @Override
  public void updateEntireRecord(String collection, String name, ValuesMap valuesMap)
      throws FailedDBOperationException {
    DatabaseConfig.getLogger()
        .log(Level.FINE, "Update record {0}/{1}", new Object[] {name, valuesMap});
    JSONObject json = new JSONObject();
    try {
      json.put(NameRecord.NAME.getName(), name);
      json.put(NameRecord.VALUES_MAP.getName(), valuesMap);
      getMap(collection).put(name, json);
    } catch (JSONException e) {

    }
  }
  /*
   * This method will:
   * 1. apply the modified DatabaseConfig to the database.
   * 2. close the database and do a sync to make sure the new configuration
   *    is written to the log.
   * 3. open the database with a useExisting config and return the current
   *    DatabaseConfig.
   */
  private DatabaseConfig setAndGetDbConfig(Environment env, DatabaseConfig dbConfig, String dbName)
      throws Exception {

    Database db = env.openDatabase(null, "foo", dbConfig);
    db.close();

    env.sync();

    /*
     * Open with the useExisting config to see what attributes have been
     * persisted.
     */
    DatabaseConfig newConfig = new DatabaseConfig();
    newConfig.setReadOnly(true);
    newConfig.setTransactional(true);
    newConfig.setUseExistingConfig(true);

    db = env.openDatabase(null, dbName, newConfig);
    newConfig = db.getConfig();
    db.close();

    return newConfig;
  }
  public static void main(String[] args) {

    Environment myEnv;
    Database myDatabase;

    try {
      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setAllowCreate(true);
      myEnv = new Environment(new File("/Users/broso/testDB"), envConfig);

      // Environment open omitted for clarity
      DatabaseConfig myDbConfig = new DatabaseConfig();
      SecondaryConfig mySecConfig = new SecondaryConfig();
      myDbConfig.setAllowCreate(true);
      mySecConfig.setAllowCreate(true);
      // Duplicates are frequently required for secondary databases.
      mySecConfig.setSortedDuplicates(true);

      // Open the primary

      String dbName = "myPrimaryDatabase";
      Database myDb = myEnv.openDatabase(null, dbName, myDbConfig);

      // Open the secondary.
      // Key creators are described in the next section.
      // AKeyCreator akc = new AKeyCreator();

      // Get a secondary object and set the key creator on it.
      // mySecConfig.setKeyCreator(keyCreator);

      // Perform the actual open
      String secDbName = "mySecondaryDatabase";
      SecondaryDatabase mySecDb = myEnv.openSecondaryDatabase(null, secDbName, myDb, mySecConfig);
    } catch (DatabaseException de) {
      // Exception handling goes here
    }
  }
  /**
   * Test that we can retrieve a database configuration and that it clones its configuration
   * appropriately.
   */
  @Test
  public void testConfig() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      DatabaseConfig dbConfigA = new DatabaseConfig();
      dbConfigA.setAllowCreate(true);
      Database dbA = env.openDatabase(null, "foo", dbConfigA);

      /* Change the original dbConfig */
      dbConfigA.setAllowCreate(false);
      DatabaseConfig getConfig1 = dbA.getConfig();
      assertEquals(true, getConfig1.getAllowCreate());
      assertEquals(false, getConfig1.getSortedDuplicates());

      /*
       * Change the retrieved config, ought to have no effect on what the
       * Database is storing.
       */
      getConfig1.setSortedDuplicates(true);
      DatabaseConfig getConfig2 = dbA.getConfig();
      assertEquals(false, getConfig2.getSortedDuplicates());

      dbA.close();
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
Beispiel #23
0
  public BDBCreator(String tableName, ArrayList<Integer> keyPositions) {
    /* Creates and loads a BDB table without a secondary index*/
    this.tableName = tableName.toLowerCase();
    this.keyPositions = keyPositions;
    this.tMap = Main.indexTypeMaps.get(this.tableName);
    setEnvironment();

    dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    myDB = myDbEnvironment.openDatabase(null, this.tableName, dbConfig);

    loadData();

    if (myDB != null) {
      myDB.close();
    }
    closeEnvironment();
  }
Beispiel #24
0
  /** Initializes DatabaseFactory. */
  public static synchronized void init() {
    if (dataSource != null) {
      return;
    }

    DatabaseConfig.load();

    try {
      DatabaseConfig.DATABASE_DRIVER.newInstance();
    } catch (Exception e) {
      log.fatal("Error obtaining DB driver", e);
      throw new Error("DB Driver doesnt exist!");
    }

    connectionPool = new GenericObjectPool();

    if (DatabaseConfig.DATABASE_CONNECTIONS_MIN > DatabaseConfig.DATABASE_CONNECTIONS_MAX) {
      log.error(
          "Please check your database configuration. Minimum amount of connections is > maximum");
      DatabaseConfig.DATABASE_CONNECTIONS_MAX = DatabaseConfig.DATABASE_CONNECTIONS_MIN;
    }

    connectionPool.setMaxIdle(DatabaseConfig.DATABASE_CONNECTIONS_MIN);
    connectionPool.setMaxActive(DatabaseConfig.DATABASE_CONNECTIONS_MAX);

    /* test if connection is still valid before returning */
    connectionPool.setTestOnBorrow(true);

    try {
      dataSource = setupDataSource();
      Connection c = getConnection();
      DatabaseMetaData dmd = c.getMetaData();
      databaseName = dmd.getDatabaseProductName();
      databaseMajorVersion = dmd.getDatabaseMajorVersion();
      databaseMinorVersion = dmd.getDatabaseMinorVersion();
      c.close();
    } catch (Exception e) {
      log.fatal("Error with connection string: " + DatabaseConfig.DATABASE_URL, e);
      throw new Error("DatabaseFactory not initialized!");
    }

    log.info("Successfully connected to database");
  }
Beispiel #25
0
 @Override
 public void updateIndividualFields(
     String collection,
     String name,
     ColumnField valuesMapField,
     ArrayList<ColumnField> valuesMapKeys,
     ArrayList<Object> valuesMapValues)
     throws FailedDBOperationException {
   DatabaseConfig.getLogger()
       .log(Level.FINE, "Update fields {0}/{1}", new Object[] {name, valuesMapKeys});
   JSONObject record;
   try {
     record = lookupEntireRecord(collection, name);
   } catch (RecordNotFoundException e) {
     throw new FailedDBOperationException(collection, name, "Record not found.");
   }
   DatabaseConfig.getLogger().log(Level.FINE, "Record before:{0}", record);
   if (record == null) {
     throw new FailedDBOperationException(collection, name);
   }
   if (valuesMapField != null && valuesMapKeys != null) {
     try {
       JSONObject json = record.getJSONObject(valuesMapField.getName());
       for (int i = 0; i < valuesMapKeys.size(); i++) {
         String fieldName = valuesMapKeys.get(i).getName();
         switch (valuesMapKeys.get(i).type()) {
           case LIST_STRING:
             JSONDotNotation.putWithDotNotation(json, fieldName, valuesMapValues.get(i));
             // json.put(fieldName, valuesMapValues.get(i));
             break;
           case USER_JSON:
             JSONDotNotation.putWithDotNotation(
                 json, fieldName, JSONParse(valuesMapValues.get(i)));
             // json.put(fieldName, JSONParse(valuesMapValues.get(i)));
             break;
           default:
             DatabaseConfig.getLogger()
                 .log(Level.WARNING, "Ignoring unknown format: {0}", valuesMapKeys.get(i).type());
             break;
         }
       }
       DatabaseConfig.getLogger().log(Level.FINE, "Json after:{0}", json);
       record.put(valuesMapField.getName(), json);
       DatabaseConfig.getLogger().log(Level.FINE, "Record after:{0}", record);
     } catch (JSONException e) {
       DatabaseConfig.getLogger().log(Level.SEVERE, "Problem updating json: {0}", e.getMessage());
     }
   }
   getMap(collection).put(name, record);
 }
Beispiel #26
0
  private ConnectionSource setupConnection(DatabaseConfig dbConfig) throws SQLException {
    HikariDataSource ds = new HikariDataSource();

    if (!dbConfig.getUser().isEmpty()) {
      ds.setUsername(dbConfig.getUser());
    }
    if (!dbConfig.getPassword().isEmpty()) {
      ds.setPassword(dbConfig.getPassword());
    }

    ds.setJdbcUrl(dbConfig.getJDBCUrl());

    ds.setMaximumPoolSize(dbConfig.getMaxConnections());
    /* Keep the connection open for 5 minutes */
    //    ds.setMaxLifetime(300000);

    return new DataSourceConnectionSource(ds, new MySQLDatabase());
  }
Beispiel #27
0
  public static Database buildtertiary(Database std) {

    try {
      // configure new database instance
      io.deleteFile("tertiarydb");
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setType(DatabaseType.HASH);
      dbConfig.setAllowCreate(true);
      dbConfig.setUnsortedDuplicates(true);
      Database tdb = new Database("tertiarydb", null, dbConfig);

      // configure cursors and entries
      Cursor stdCurs = std.openCursor(null, null);
      Cursor tCurs = tdb.openCursor(null, null);
      DatabaseEntry stdKey = new DatabaseEntry();
      DatabaseEntry stdData = new DatabaseEntry();
      DatabaseEntry tKey = new DatabaseEntry();
      DatabaseEntry tData = new DatabaseEntry();

      // extract from linearly constructed database to populate <song>,<users> table, making
      // use of songs grouped by id
      String currUsers = "";
      String prevID = "default";
      boolean firstIter = true;
      while (stdCurs.getNext(stdKey, stdData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        // get rating data from current row
        String[] currIDUser = (new String(stdKey.getData())).split(",");
        String currID = currIDUser[0].trim();
        String currUser = currIDUser[1].trim();
        String currRating = (new String(stdData.getData()));
        stdKey.setData(null);
        stdData.setData(null);
        if (currID.equals(prevID) || firstIter) {
          // concatenate new username with current string
          currUsers += "(" + currUser + "," + currRating + ")";
        } else if (!firstIter) {
          // insert completed <usernames> into table under key <song id>
          tKey.setData(prevID.getBytes());
          tData.setData(currUsers.substring(0, currUsers.length()).getBytes());
          tCurs.put(tKey, tData);
          tKey.setData(null);
          tData.setData(null);
          // DEBUG:
          // System.out.println(prevID+","+currUsers.substring(0, currUsers.length()-1));
          // start the new <usernames> for the next song (in currID)
          currUsers = "(" + currUser + "," + currRating + ")";
        }
        prevID = currID;
        firstIter = false;
      }
      // repeat iteration for last song
      tKey.setData(prevID.getBytes());
      tData.setData(currUsers.substring(0, currUsers.length()).getBytes());
      tCurs.put(tKey, tData);
      tKey.setData(null);
      tData.setData(null);

      // DEBUG:
      // io.debugread(tdb);
      tCurs.close();

      return tdb;

    } catch (Exception e) {
      System.out.println(" error creating <song>,<users> tertiary table\n");
      System.out.println(e.getMessage());
    }
    return null; // should never happen
  }
Beispiel #28
0
  public static String top3(String line, Database secdb, Database tdb)
      throws DatabaseException, FileNotFoundException {
    // configure sqSum db for keeping track of cumulative squaresums
    io.deleteFile("sqSumdb");
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setType(DatabaseType.HASH);
    dbConfig.setAllowCreate(true);
    dbConfig.setUnsortedDuplicates(true);
    Database sqSumdb = new Database("sqSumdb", null, dbConfig);

    Cursor sqSumCurs = sqSumdb.openCursor(null, null);
    DatabaseEntry sqSumKey = new DatabaseEntry();
    DatabaseEntry sqSumData = new DatabaseEntry();

    Cursor secCurs = secdb.openCursor(null, null);
    Cursor tCurs = tdb.openCursor(null, null);
    DatabaseEntry secKey = new DatabaseEntry();
    DatabaseEntry secData = new DatabaseEntry();
    DatabaseEntry tKey = new DatabaseEntry();
    DatabaseEntry tData = new DatabaseEntry();

    // get input song users and ratings
    tKey.setData(line.getBytes());
    if (tCurs.getSearchKey(tKey, tData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
      // for each user in input song, get songs and ratings
      HashMap<String, String> iRatings = parseBrace(new String(tData.getData()));
      for (String user : iRatings.keySet()) {
        // get current user's rating for input song
        int inputRating = Integer.parseInt(iRatings.get(user));
        // get songs and ratings for user
        secKey.setData(null);
        secData.setData(null);
        secKey.setData(user.getBytes());
        if (secCurs.getSearchKey(secKey, secData, null) == OperationStatus.SUCCESS) {
          // for each song rated by user
          HashMap<String, String> currSongs = parseBrace(new String(secData.getData()));
          for (String song : currSongs.keySet()) {
            // input song should not be compared to itself, otherwise get the users and ratings for
            // it
            if (song.equals(line)) continue;
            // current user's rating for song under inspection
            int songRating = Integer.parseInt(currSongs.get(song));
            sqSumKey.setData(null);
            sqSumData.setData(null);
            sqSumKey.setData(song.getBytes());
            if (sqSumCurs.getSearchKey(sqSumKey, sqSumData, null) == OperationStatus.SUCCESS) {
              // if song is already in sqSum,N db, add to it
              String[] sumN = (new String(sqSumData.getData())).split(",");

              // add the new square sum to the previous and increment N
              int sum = Integer.parseInt(sumN[0]);
              int N = Integer.parseInt(sumN[1]);
              sum += (inputRating - songRating) * (inputRating - songRating);
              N += 1;
              String encoded = Integer.toString(sum) + "," + Integer.toString(N);

              // remove old entry for current song and replace it with the new one
              sqSumCurs.delete();
              sqSumKey.setData(null);
              sqSumData.setData(null);
              sqSumKey.setData(song.getBytes());
              sqSumData.setData(encoded.getBytes());
              sqSumdb.put(null, sqSumKey, sqSumData);

            } else {
              // if song in not already in sqSum,N db, create it
              int sum = (inputRating - songRating) * (inputRating - songRating);
              int N = 1;
              String encoded = Integer.toString(sum) + "," + Integer.toString(N);
              sqSumKey.setData(null);
              sqSumData.setData(null);
              sqSumKey.setData(song.getBytes());
              sqSumData.setData(encoded.getBytes());
              sqSumdb.put(null, sqSumKey, sqSumData);
            }
          }
        } else {
          System.out.println("ERROR:  user " + user + " not in secondary database.");
        }
      }
    } else {
      return line + " has not been rated by any user.";
    }
    // DEBUG
    // io.debugread(sqSumdb);

    // calculate distance for each song in, inserting into the top 3 spots as we iterate through
    String ranked = rank(sqSumdb);
    return line + ", " + ranked;
  }
Beispiel #29
0
  /** See if this new handle's configuration is compatible with the pre-existing database. */
  private void validateConfigAgainstExistingDb(DatabaseConfig config, DatabaseImpl databaseImpl)
      throws DatabaseException {

    /*
     * The allowDuplicates property is persistent and immutable.  It does
     * not need to be specified if the useExistingConfig property is set.
     */
    if (!config.getUseExistingConfig()) {
      if (databaseImpl.getSortedDuplicates() != config.getSortedDuplicates()) {
        throw new DatabaseException(
            "You can't open a Database with a duplicatesAllowed "
                + "configuration of "
                + config.getSortedDuplicates()
                + " if the underlying database was created with a "
                + "duplicatesAllowedSetting of "
                + databaseImpl.getSortedDuplicates()
                + ".");
      }
    }

    /*
     * The transactional property is kept constant while any handles are
     * open, and set when the first handle is opened.  It does not need to
     * be specified if the useExistingConfig property is set.
     */
    if (databaseImpl.hasOpenHandles()) {
      if (!config.getUseExistingConfig()) {
        if (config.getTransactional() != databaseImpl.isTransactional()) {
          throw new DatabaseException(
              "You can't open a Database with a transactional "
                  + "configuration of "
                  + config.getTransactional()
                  + " if the underlying database was created with a "
                  + "transactional configuration of "
                  + databaseImpl.isTransactional()
                  + ".");
        }
      }
    } else {
      databaseImpl.setTransactional(config.getTransactional());
    }

    /*
     * Only re-set the comparators if the override is allowed.
     */
    if (config.getOverrideBtreeComparator()) {
      databaseImpl.setBtreeComparator(config.getBtreeComparator());
    }

    if (config.getOverrideDuplicateComparator()) {
      databaseImpl.setDuplicateComparator(config.getDuplicateComparator());
    }
  }
  @Override
  public void onLoad() {
    instance = this; // Set the instance
    log = getLogger(); // Set the logger

    version =
        new TRVersion(
            getDescription().getVersion().equals("1.2") ? "1.20" : getDescription().getVersion());
    Log.init();

    // #################### load Config ####################
    saveDefaultConfig(false); // Copy config files

    config = this.getConfigx(); // Load the configuration files
    double configVer = config.getDouble(ConfigFile.General, "ConfigVersion", 0.9);
    if (configVer < 1.1) UpdateConfigFiles.v09(); // 0 --> newest
    else if (configVer < 1.5) { // Upgrade to 1.8
      AdvancedConfig.upgradeFile();
      DatabaseConfig.upgradeFile();
      GeneralConfig.upgradeFile();
      HackDupeConfig.upgradeOldHackFile();
      ModModificationsConfig.upgradeFile();
      SafeZonesConfig.upgradeFile();
      TPerformanceConfig.upgradeFile();
      LoggingConfig.upgradeFile();
      if (linkEEPatch()) EEPatchConfig.upgradeFile();
      reloadConfig();
    } else if (configVer < 1.6) { // Upgrade to 1.8
      GeneralConfig.upgradeFile();
      DatabaseConfig.upgradeFile();
      LoggingConfig.upgradeFile();
      if (linkEEPatch()) EEPatchConfig.upgradeFile();
      reloadConfig();
    } else if (configVer < 1.7) { // upgrade to 1.8
      GeneralConfig.upgradeFile();
      DatabaseConfig.upgradeFile();
      LoggingConfig.upgradeFile();
      if (linkEEPatch()) EEPatchConfig.upgradeFile();
      reloadConfig();
    } else if (configVer < 1.8) { // upgrade to 1.8
      GeneralConfig.upgradeFile();
      DatabaseConfig.upgradeFile();
      reloadConfig();
    }

    try { // Load all settings
      load(); // TODO loading eepatch
    } catch (Exception ex) {
      Warning.load("An error occurred: Unable to load settings!", true);
      Log.Exception(ex, true);
    }
    // #####################################################

    // ##################### load SQL ######################

    log.info("[DB] Loading Database...");
    if (!TRDB.loadDB()) {
      Warning.dbAndLoad("[DB] Failed to load Database!", true);
    } else {
      if (dbtype == DBType.SQLite) {
        if (TRDB.initSQLite()) log.info("[SQLite] SQLite Database loaded!");
        else {
          Warning.dbAndLoad("[SQLite] Failed to load SQLite Database!", true);
        }
      } else if (dbtype == DBType.MySQL) {
        if (TRDB.initMySQL()) {
          log.info("[MySQL] Database connection established!");
        } else {
          Warning.dbAndLoad("[MySQL] Failed to connect to MySQL Database!", true);
        }
      } else {
        Warning.dbAndLoad("[DB] Unknown Database type set!", true);
      }
    }
    // #####################################################

    // ###################### RPTimer ######################
    if (config.getBoolean2(ConfigFile.General, "UseAutoRPTimer", false)) {
      try {
        double value = config.getDouble(ConfigFile.ModModifications, "RPTimerMin", 0.2d);
        int ticks = (int) Math.round((value - 0.1d) * 20d);
        RedPowerLogic.minInterval = ticks; // set minimum interval for logic timers...
        log.info("Set the RedPower Timer Min interval to " + value + " seconds.");
      } catch (Exception e) {
        Warning.load("Setting the RedPower Timer failed!", false);
      }
    }
    // #####################################################

    // ###################### Patch CC #####################
    if (config.getBoolean2(ConfigFile.General, "PatchComputerCraft", true)) {
      PatchCC.start();
    }
    // #####################################################

    // BlockBreaker anti-dupe
    try {
      ArrayList<Block> miningLaser = new ArrayList<Block>();

      for (Block block : EntityMiningLaser.unmineableBlocks) {
        miningLaser.add(block);
      }
      miningLaser.add(Block.byId[194]);
      EntityMiningLaser.unmineableBlocks = miningLaser.toArray(new Block[miningLaser.size()]);
      log.fine("Patched Mining Laser + Auto Crafting Table MK II dupe.");
    } catch (Exception ex) {
      Warning.load("Unable to patch Mining Laser + Auto Crafting Table MK II dupe!", false);
    }

    try {
      RedPowerMachine.breakerBlacklist.add(Integer.valueOf(-1 << 15 | 194));

      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 6362)); // REP
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 6359)); // Wireless sniffer
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 6363)); // Private sniffer
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 27562)); // Alcbag
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 27585)); // Divining ROd
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 30122)); // Cropnalyser
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 30104)); // Debug item

      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 27592)); // transtablet
      RedPowerMachine.deployerBlacklist.add(Integer.valueOf(0 << 15 | 7493)); // Ender pouch
      log.fine("Patched BlockBreaker + Auto Crafting Table MK II dupe.");
      log.fine("Patched most Deployer Crash Bugs.");
    } catch (Exception ex) {
      Warning.load("Unable to patch BlockBreaker + Auto Crafting Table MK II dupe!", false);
      Warning.load("Unable to patch Deployer Crash Bugs!", false);
    }

    try {
      Ic2Recipes.addMaceratorRecipe(new ItemStack(135, 1, 2), new ItemStack(30254, 4, 0));
      Ic2Recipes.addMaceratorRecipe(new ItemStack(135, 1, 3), new ItemStack(30255, 4, 0));
      log.fine("Added Missing Nether Ores recipes.");
    } catch (Exception ex) {
      Warning.load("Unable to add missing Nether Ore recipes.", false);
    }
  }