예제 #1
0
 /**
  * Precomputes a template query if it is not already precomputed, returning whether precomputing
  * was necessary.
  *
  * @param t the TemplateQuery to precompute
  * @return true if the template was not already precomputed
  * @throws ObjectStoreException if there is a problem precomputing
  */
 public boolean precomputeTemplate(TemplateQuery t) throws ObjectStoreException {
   List<QueryNode> indexes = new ArrayList<QueryNode>();
   Query q = TemplatePrecomputeHelper.getPrecomputeQuery(t, indexes);
   ObjectStoreInterMineImpl osimi = (ObjectStoreInterMineImpl) os;
   if (!osimi.isPrecomputed(q, "template")) {
     osimi.precompute(q, indexes, "template");
     return true;
   }
   return false;
 }
예제 #2
0
  private List getListTracksTrend(String event) {
    List listTracksTrend = new ArrayList<Object[]>();

    String sql =
        "SELECT date_part('doy', timestamp) AS day, COUNT(timestamp)"
            + " FROM listtrack WHERE event='"
            + event
            + "'"
            + " GROUP BY date_part('doy', timestamp)";
    Connection connection = null;
    Calendar calendar = Calendar.getInstance();
    try {
      connection = ((ObjectStoreInterMineImpl) uos).getConnection();
      Statement stm = connection.createStatement();
      ResultSet rs = stm.executeQuery(sql);
      int dayOfYear;
      Object[] track;
      while (rs.next()) {
        track = new Object[2];
        dayOfYear = rs.getInt(1);
        calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
        track[0] = calendar.getTime();
        track[1] = rs.getInt(2);
        listTracksTrend.add(track);
      }
      ((ObjectStoreInterMineImpl) uos).releaseConnection(connection);
    } catch (SQLException sqle) {
      sqle.printStackTrace();
    }
    return listTracksTrend;
  }
예제 #3
0
 /**
  * Calls ObjectStore.releaseGoFaster() if this object wraps a Results object from an
  * ObjectStoreInterMineImpl.
  *
  * @throws ObjectStoreException if ObjectStoreInterMineImpl.releaseGoFaster() throws the exception
  */
 public synchronized void releaseGoFaster() throws ObjectStoreException {
   goingFaster--;
   if (goingFaster == 0) {
     ObjectStore os = osResults.getObjectStore();
     if (os instanceof ObjectStoreInterMineImpl) {
       ((ObjectStoreInterMineImpl) os).releaseGoFaster(osResults.getQuery());
     }
   }
 }
예제 #4
0
 /**
  * Calls ObjectStore.goFaster() if this object wraps a Results object from an
  * ObjectStoreInterMineImpl.
  *
  * @throws ObjectStoreException if ObjectStoreInterMineImpl.goFaster() throws the exception
  */
 public synchronized void goFaster() throws ObjectStoreException {
   goingFaster++;
   if (goingFaster == 1) {
     osResults = changeResultBatchSize(osResults, BIG_BATCH_SIZE);
     ObjectStore os = osResults.getObjectStore();
     if (os instanceof ObjectStoreInterMineImpl) {
       ((ObjectStoreInterMineImpl) os).goFaster(osResults.getQuery());
     }
   }
 }
예제 #5
0
 public List getTracksDataTable(String sqlQuery) {
   List<Object[]> trackTable = new ArrayList<Object[]>();
   Connection connection = null;
   try {
     connection = ((ObjectStoreInterMineImpl) uos).getConnection();
     Statement stm = connection.createStatement();
     ResultSet rs = stm.executeQuery(sqlQuery);
     Object[] track;
     while (rs.next()) {
       track = new Object[3];
       track[0] = rs.getString(1);
       track[1] = rs.getInt(2);
       track[2] = rs.getInt(3) + rs.getInt(4);
       trackTable.add(track);
     }
     ((ObjectStoreInterMineImpl) uos).releaseConnection(connection);
   } catch (SQLException sqle) {
     sqle.printStackTrace();
   }
   return trackTable;
 }
예제 #6
0
 /** {@inheritDoc} */
 @Override
 public void startElement(String uri, String localName, String qName, Attributes attrs)
     throws SAXException {
   if ("userprofiles".equals(qName)) {
     String value = attrs.getValue(MetadataManager.PROFILE_FORMAT_VERSION);
     if (value == null) {
       version = 0;
     } else {
       version = Integer.parseInt(value);
     }
     ObjectStoreWriter userprofileOsw = profileManager.getProfileObjectStoreWriter();
     Connection con = null;
     try {
       con = ((ObjectStoreInterMineImpl) userprofileOsw).getConnection();
       if (!DatabaseUtil.tableExists(con, "bagvalues")) {
         DatabaseUtil.createBagValuesTables(con);
       }
     } catch (SQLException sqle) {
       LOG.error("Problem retrieving connection", sqle);
     } finally {
       ((ObjectStoreInterMineImpl) userprofileOsw).releaseConnection(con);
     }
     sharedBagsByUsers = new HashedMap();
   }
   if ("userprofile".equals(qName)) {
     startTime = System.currentTimeMillis();
     profileHandler = new ProfileHandler(profileManager, osw, version, sharedBagsByUsers);
   }
   if (profileHandler != null) {
     profileHandler.startElement(uri, localName, qName, attrs);
   }
   if ("tracks".equals(qName)) {
     trackHandler = new TrackManagerHandler(profileManager.getProfileObjectStoreWriter());
   }
   if (trackHandler != null) {
     trackHandler.startElement(uri, localName, qName, attrs);
   }
 }
예제 #7
0
  /**
   * Read the UTRs collection of MRNA then set the fivePrimeUTR and threePrimeUTR fields with the
   * corresponding UTRs.
   *
   * @throws Exception if anything goes wrong
   */
  public void createUtrRefs() throws Exception {
    long startTime = System.currentTimeMillis();
    Query q = new Query();
    q.setDistinct(false);

    QueryClass qcMRNA = new QueryClass(model.getClassDescriptorByName("MRNA").getType());
    q.addFrom(qcMRNA);
    q.addToSelect(qcMRNA);
    q.addToOrderBy(qcMRNA);

    QueryClass qcUTR = new QueryClass(model.getClassDescriptorByName("UTR").getType());
    q.addFrom(qcUTR);
    q.addToSelect(qcUTR);
    q.addToOrderBy(qcUTR);

    QueryCollectionReference mrnaUtrsRef = new QueryCollectionReference(qcMRNA, "UTRs");
    ContainsConstraint mrnaUtrsConstraint =
        new ContainsConstraint(mrnaUtrsRef, ConstraintOp.CONTAINS, qcUTR);

    QueryObjectReference fivePrimeRef = new QueryObjectReference(qcMRNA, "fivePrimeUTR");
    ContainsConstraint fivePrimeNullConstraint =
        new ContainsConstraint(fivePrimeRef, ConstraintOp.IS_NULL);
    QueryObjectReference threePrimeRef = new QueryObjectReference(qcMRNA, "threePrimeUTR");
    ContainsConstraint threePrimeNullConstraint =
        new ContainsConstraint(threePrimeRef, ConstraintOp.IS_NULL);

    ConstraintSet cs = new ConstraintSet(ConstraintOp.AND);
    cs.addConstraint(mrnaUtrsConstraint);
    cs.addConstraint(fivePrimeNullConstraint);
    cs.addConstraint(threePrimeNullConstraint);

    q.setConstraint(cs);

    ObjectStore os = osw.getObjectStore();

    ((ObjectStoreInterMineImpl) os).precompute(q, Constants.PRECOMPUTE_CATEGORY);
    Results res = os.execute(q, 500, true, true, true);

    int count = 0;
    InterMineObject lastMRNA = null;

    InterMineObject fivePrimeUTR = null;
    InterMineObject threePrimeUTR = null;

    osw.beginTransaction();

    Class<? extends FastPathObject> fivePrimeUTRCls =
        model.getClassDescriptorByName("FivePrimeUTR").getType();

    Iterator<?> resIter = res.iterator();
    while (resIter.hasNext()) {
      ResultsRow<?> rr = (ResultsRow<?>) resIter.next();
      InterMineObject mrna = (InterMineObject) rr.get(0);
      InterMineObject utr = (InterMineObject) rr.get(1);

      if (lastMRNA != null && !mrna.getId().equals(lastMRNA.getId())) {
        // clone so we don't change the ObjectStore cache
        InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA);
        if (fivePrimeUTR != null) {
          tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR);
          fivePrimeUTR = null;
        }
        if (threePrimeUTR != null) {
          tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR);
          threePrimeUTR = null;
        }
        osw.store(tempMRNA);
        count++;
      }

      if (DynamicUtil.isInstance(utr, fivePrimeUTRCls)) {
        fivePrimeUTR = utr;
      } else {
        threePrimeUTR = utr;
      }

      lastMRNA = mrna;
    }

    if (lastMRNA != null) {
      // clone so we don't change the ObjectStore cache
      InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA);
      tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR);
      tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR);
      osw.store(tempMRNA);
      count++;
    }
    LOG.info(
        "Stored MRNA "
            + count
            + " times ("
            + count * 2
            + " fields set)"
            + " - took "
            + (System.currentTimeMillis() - startTime)
            + " ms.");
    osw.commitTransaction();

    // now ANALYSE tables relating to class that has been altered - may be rows added
    // to indirection tables
    if (osw instanceof ObjectStoreWriterInterMineImpl) {
      ClassDescriptor cld = model.getClassDescriptorByName("MRNA");
      DatabaseUtil.analyse(((ObjectStoreWriterInterMineImpl) osw).getDatabase(), cld, false);
    }
  }
예제 #8
0
  /**
   * Return a Set of PrimaryKeys relevant to a given Source for a ClassDescriptor. The Set contains
   * all the primary keys that exist on a particular class that are used by the source, without
   * performing any recursion. The Model.getClassDescriptorsForClass() method is recommended if you
   * wish for all the primary keys of the class' parents as well.
   *
   * @param cld the ClassDescriptor
   * @param source the Source
   * @param os the ObjectStore that these PrimaryKeys are used in, for creating indexes
   * @return a Set of PrimaryKeys
   */
  public static Set<PrimaryKey> getPrimaryKeys(ClassDescriptor cld, Source source, ObjectStore os) {
    GetPrimaryKeyCacheKey key = new GetPrimaryKeyCacheKey(cld, source);
    synchronized (getPrimaryKeyCache) {
      Set<PrimaryKey> keySet = getPrimaryKeyCache.get(key);
      if (keySet == null) {
        keySet = new LinkedHashSet<PrimaryKey>();
        Properties keys = getKeyProperties(source);
        if (keys != null) {
          if (!verifiedSources.contains(source)) {
            String packageNameWithDot =
                cld.getName().substring(0, cld.getName().lastIndexOf('.') + 1);
            LOG.info(
                "Verifying primary key config for source "
                    + source
                    + ", packageName = "
                    + packageNameWithDot);
            for (Map.Entry<Object, Object> entry : keys.entrySet()) {
              String cldName = (String) entry.getKey();
              String keyList = (String) entry.getValue();
              if (!cldName.contains(".")) {
                ClassDescriptor iCld =
                    cld.getModel().getClassDescriptorByName(packageNameWithDot + cldName);
                if (iCld != null) {
                  Map<String, PrimaryKey> map = PrimaryKeyUtil.getPrimaryKeys(iCld);

                  String[] tokens = keyList.split(",");
                  for (int i = 0; i < tokens.length; i++) {
                    String token = tokens[i].trim();
                    if (map.get(token) == null) {
                      throw new IllegalArgumentException(
                          "Primary key "
                              + token
                              + " for class "
                              + cldName
                              + " required by datasource "
                              + source.getName()
                              + " in "
                              + source.getName()
                              + "_keys.properties is not defined in "
                              + cld.getModel().getName()
                              + "_keyDefs.properties");
                    }
                  }
                } else {
                  LOG.warn(
                      "Ignoring entry for "
                          + cldName
                          + " in file "
                          + cld.getModel().getName()
                          + "_keyDefs.properties - not in model!");
                }
              }
            }
            verifiedSources.add(source);
          }
          Map<String, PrimaryKey> map = PrimaryKeyUtil.getPrimaryKeys(cld);
          String cldName = TypeUtil.unqualifiedName(cld.getName());
          String keyList = (String) keys.get(cldName);
          if (keyList != null) {
            String[] tokens = keyList.split(",");
            for (int i = 0; i < tokens.length; i++) {
              String token = tokens[i].trim();
              if (map.get(token) == null) {
                throw new IllegalArgumentException(
                    "Primary key "
                        + token
                        + " for class "
                        + cld.getName()
                        + " required by data source "
                        + source.getName()
                        + " in "
                        + source.getName()
                        + "_keys.properties is not defined in "
                        + cld.getModel().getName()
                        + "_keyDefs.properties");
              } else {
                keySet.add(map.get(token));
              }
            }
          }
          for (Map.Entry<Object, Object> entry : keys.entrySet()) {
            String propKey = (String) entry.getKey();
            String fieldList = (String) entry.getValue();
            int posOfDot = propKey.indexOf('.');
            if (posOfDot > 0) {
              String propCldName = propKey.substring(0, posOfDot);
              if (cldName.equals(propCldName)) {
                String keyName = propKey.substring(posOfDot + 1);
                PrimaryKey pk = new PrimaryKey(keyName, fieldList, cld);
                if (!keySet.contains(pk)) {
                  keySet.add(pk);
                  if (os instanceof ObjectStoreInterMineImpl) {
                    ObjectStoreInterMineImpl osimi = (ObjectStoreInterMineImpl) os;
                    DatabaseSchema schema = osimi.getSchema();
                    ClassDescriptor tableMaster = schema.getTableMaster(cld);
                    String tableName = DatabaseUtil.getTableName(tableMaster);
                    List<String> fields = new ArrayList<String>();

                    for (String field : pk.getFieldNames()) {
                      String colName = DatabaseUtil.generateSqlCompatibleName(field);
                      if (tableMaster.getReferenceDescriptorByName(field, true) != null) {
                        colName += "id";
                      }
                      fields.add(colName);
                    }
                    String sql =
                        "CREATE INDEX "
                            + tableName
                            + "__"
                            + keyName
                            + " ON "
                            + tableName
                            + " ("
                            + StringUtil.join(fields, ", ")
                            + ")";
                    System.out.println("Creating index: " + sql);
                    LOG.info("Creating index: " + sql);
                    Connection conn = null;
                    try {
                      conn = osimi.getConnection();
                      conn.createStatement().execute(sql);
                    } catch (SQLException e) {
                      LOG.warn("Index creation failed", e);
                    } finally {
                      if (conn != null) {
                        osimi.releaseConnection(conn);
                      }
                    }
                  }
                }
              }
            }
          }
        } else {
          throw new IllegalArgumentException(
              "Unable to find keys for source "
                  + source.getName()
                  + " in file "
                  + source.getName()
                  + "_keys.properties");
        }
        getPrimaryKeyCache.put(key, keySet);
      }
      return keySet;
    }
  }
예제 #9
0
 /**
  * Creates an SQL query from a PathQuery.
  *
  * @param pathQuery the query to convert
  * @return an SQL String
  * @throws ObjectStoreException if problem creating query
  */
 public String makeSql(PathQuery pathQuery) throws ObjectStoreException {
   Query query = makeQuery(pathQuery);
   ObjectStoreInterMineImpl osimi = (ObjectStoreInterMineImpl) os;
   return osimi.generateSql(query);
 }