/// <exception cref="Java.Sql.SQLException"/> public static IList <Pair <string, long> > GetCounts(ICollection <string> strs) { Connect(); IList <Pair <string, long> > counts = new List <Pair <string, long> >(); string query = string.Empty; foreach (string str in strs) { str = str.Trim(); int ngram = str.Split("\\s+").Length; string table = tablenamePrefix + ngram; if (!ExistsTable(table)) { counts.Add(new Pair(str, (long)-1)); continue; } string phrase = EscapeString(str); query += "select count from " + table + " where phrase='" + phrase + "';"; } if (query.IsEmpty()) { return(counts); } IPreparedStatement stmt = connection.PrepareStatement(query); bool isresult = stmt.Execute(); IResultSet rs; IEnumerator <string> iter = strs.GetEnumerator(); do { rs = stmt.GetResultSet(); string ph = iter.Current; if (rs.Next()) { counts.Add(new Pair(ph, rs.GetLong("count"))); } else { counts.Add(new Pair(ph, (long)-1)); } isresult = stmt.GetMoreResults(); }while (isresult); System.Diagnostics.Debug.Assert((counts.Count == strs.Count)); return(counts); }
// // @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); } }