Exemple #1
0
 /*
  * public Set<Integer> getPatterns(String sentId, Integer tokenId) throws SQLException, IOException, ClassNotFoundException {
  * if(useDBForTokenPatterns){
  * Connection conn = SQLConnection.getConnection();
  *
  * String query = "Select patterns from " + tableName + " where sentid=\'" + sentId + "\' and tokenid = " + tokenId;
  * Statement stmt = conn.createStatement();
  * ResultSet rs = stmt.executeQuery(query);
  * Set<Integer> pats = null;
  * if(rs.next()){
  * byte[] st = (byte[]) rs.getObject(1);
  * ByteArrayInputStream baip = new ByteArrayInputStream(st);
  * ObjectInputStream ois = new ObjectInputStream(baip);
  * pats = (Set<Integer>) ois.readObject();
  *
  * }
  * conn.close();
  * return pats;
  * }
  * else
  * return patternsForEachToken.get(sentId).get(tokenId);
  * }*/
 public override IDictionary <int, ICollection <E> > GetPatternsForAllTokens(string sentId)
 {
     try
     {
         IConnection conn = SQLConnection.GetConnection();
         //Map<Integer, Set<Integer>> pats = new ConcurrentHashMap<Integer, Set<Integer>>();
         string     query = "Select patterns from " + tableName + " where sentid=\'" + sentId + "\'";
         IStatement stmt  = conn.CreateStatement();
         IResultSet rs    = stmt.ExecuteQuery(query);
         IDictionary <int, ICollection <E> > patsToken = new Dictionary <int, ICollection <E> >();
         if (rs.Next())
         {
             byte[] st = (byte[])rs.GetObject(1);
             ByteArrayInputStream baip = new ByteArrayInputStream(st);
             ObjectInputStream    ois  = new ObjectInputStream(baip);
             patsToken = (IDictionary <int, ICollection <E> >)ois.ReadObject();
         }
         //pats.put(rs.getInt("tokenid"), patsToken);
         conn.Close();
         return(patsToken);
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
Exemple #2
0
 //
 //  @Override
 //  public ConcurrentHashIndex<SurfacePattern> readPatternIndex(String dir){
 //    //dir parameter is not used!
 //    try{
 //      Connection conn = SQLConnection.getConnection();
 //      //Map<Integer, Set<Integer>> pats = new ConcurrentHashMap<Integer, Set<Integer>>();
 //      String query = "Select index from " + patternindicesTable + " where tablename=\'" + tableName + "\'";
 //      Statement stmt = conn.createStatement();
 //      ResultSet rs = stmt.executeQuery(query);
 //      ConcurrentHashIndex<SurfacePattern> index = null;
 //      if(rs.next()){
 //        byte[] st = (byte[]) rs.getObject(1);
 //        ByteArrayInputStream baip = new ByteArrayInputStream(st);
 //        ObjectInputStream ois = new ObjectInputStream(baip);
 //        index  = (ConcurrentHashIndex<SurfacePattern>) ois.readObject();
 //      }
 //      assert index != null;
 //      return index;
 //    }catch(SQLException e){
 //      throw new RuntimeException(e);
 //    } catch (ClassNotFoundException e) {
 //      throw new RuntimeException(e);
 //    } catch (IOException e) {
 //      throw new RuntimeException(e);
 //    }
 //  }
 //
 //  @Override
 //  public void savePatternIndex(ConcurrentHashIndex<SurfacePattern> index, String file) {
 //    try {
 //      createUpsertFunctionPatternIndex();
 //      Connection conn = SQLConnection.getConnection();
 //      PreparedStatement  st = conn.prepareStatement("select upsert_patternindex(?,?)");
 //      st.setString(1,tableName);
 //      ByteArrayOutputStream baos = new ByteArrayOutputStream();
 //      ObjectOutputStream oos = new ObjectOutputStream(baos);
 //      oos.writeObject(index);
 //      byte[] patsAsBytes = baos.toByteArray();
 //      ByteArrayInputStream bais = new ByteArrayInputStream(patsAsBytes);
 //      st.setBinaryStream(2, bais, patsAsBytes.length);
 //      st.execute();
 //      st.close();
 //      conn.close();
 //      System.out.println("Saved the pattern hash index for " + tableName + " in DB table " + patternindicesTable);
 //    }catch (SQLException e){
 //      throw new RuntimeException(e);
 //    } catch (IOException e) {
 //      throw new RuntimeException(e);
 //    }
 //  }
 //batch processing below is copied from Java Ranch
 //TODO: make this into an iterator!!
 public override IDictionary <string, IDictionary <int, ICollection <E> > > GetPatternsForAllTokens(ICollection <string> sampledSentIds)
 {
     try
     {
         IDictionary <string, IDictionary <int, ICollection <E> > > pats = new Dictionary <string, IDictionary <int, ICollection <E> > >();
         IConnection          conn          = SQLConnection.GetConnection();
         IEnumerator <string> iter          = sampledSentIds.GetEnumerator();
         int totalNumberOfValuesLeftToBatch = sampledSentIds.Count;
         while (totalNumberOfValuesLeftToBatch > 0)
         {
             int batchSize = SingleBatch;
             if (totalNumberOfValuesLeftToBatch >= LargeBatch)
             {
                 batchSize = LargeBatch;
             }
             else
             {
                 if (totalNumberOfValuesLeftToBatch >= MediumBatch)
                 {
                     batchSize = MediumBatch;
                 }
                 else
                 {
                     if (totalNumberOfValuesLeftToBatch >= SmallBatch)
                     {
                         batchSize = SmallBatch;
                     }
                 }
             }
             totalNumberOfValuesLeftToBatch -= batchSize;
             StringBuilder inClause = new StringBuilder();
             for (int i = 0; i < batchSize; i++)
             {
                 inClause.Append('?');
                 if (i != batchSize - 1)
                 {
                     inClause.Append(',');
                 }
             }
             IPreparedStatement stmt = conn.PrepareStatement("select sentid, patterns from " + tableName + " where sentid in (" + inClause.ToString() + ")");
             for (int i_1 = 0; i_1 < batchSize && iter.MoveNext(); i_1++)
             {
                 stmt.SetString(i_1 + 1, iter.Current);
             }
             // or whatever values you are trying to query by
             stmt.Execute();
             IResultSet rs = stmt.GetResultSet();
             while (rs.Next())
             {
                 string sentid             = rs.GetString(1);
                 byte[] st                 = (byte[])rs.GetObject(2);
                 ByteArrayInputStream baip = new ByteArrayInputStream(st);
                 ObjectInputStream    ois  = new ObjectInputStream(baip);
                 pats[sentid] = (IDictionary <int, ICollection <E> >)ois.ReadObject();
             }
         }
         conn.Close();
         return(pats);
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }