Search in sources :

Example 1 with MatchCollectorDB

use of de.ids_mannheim.korap.response.collector.MatchCollectorDB in project Krill by KorAP.

the class Resource method collect.

/**
 * Collect matches and aggregate the UIDs plus matchcount in the
 * database.
 *
 * @param text_id
 */
@PUT
@Path("/collect/{resultID}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String collect(String json, @PathParam("resultID") String resultID, @Context UriInfo uri) {
    Response kresp = _initResponse();
    if (kresp.hasErrors())
        return kresp.toJsonString();
    // Get the database
    try {
        final MatchCollectorDB mc = new MatchCollectorDB(1000, "Res_" + resultID);
        final ComboPooledDataSource pool = Node.getDBPool();
        mc.setDBPool("mysql", pool, pool.getConnection());
        // TODO: Only search in self documents (REPLICATION FTW!)
        final Krill ks = new Krill(json);
        // TODO: Reuse response!
        final MatchCollector result = Node.getIndex().collect(ks, mc);
        result.setNode(Node.getName());
        return result.toJsonString();
    } catch (SQLException e) {
        log.error(e.getLocalizedMessage());
    }
    ;
    kresp.addError(604, "Unable to connect to database");
    return kresp.toJsonString();
}
Also used : Response(de.ids_mannheim.korap.response.Response) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) MatchCollectorDB(de.ids_mannheim.korap.response.collector.MatchCollectorDB) Krill(de.ids_mannheim.korap.Krill) SQLException(java.sql.SQLException) MatchCollector(de.ids_mannheim.korap.response.MatchCollector) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 2 with MatchCollectorDB

use of de.ids_mannheim.korap.response.collector.MatchCollectorDB in project Krill by KorAP.

the class TestDatabase method TestDatabasePoolCollector.

@Test
public void TestDatabasePoolCollector() throws Exception {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    // Connect to a temporary file instead of a in-memory file
    cpds.setDriverClass("org.sqlite.JDBC");
    cpds.setJdbcUrl("jdbc:sqlite:");
    cpds.setMaxStatements(100);
    // This is part of the match collector
    conn = cpds.getConnection();
    conn.setAutoCommit(false);
    Statement stat = conn.createStatement();
    stat.executeUpdate("CREATE TABLE IF NOT EXISTS matchXYZ (text_id INTEGER, match_count INTEGER);");
    conn.commit();
    stat.close();
    MatchCollectorDB mc = new MatchCollectorDB(3, "matchXYZ");
    mc.setDBPool("sqlite", cpds, conn);
    mc.add(9, 5000);
    mc.add(12, 6785);
    mc.add(39, 56576);
    // First commit
    mc.add(45, 5000);
    mc.add(67, 6785);
    mc.add(81, 56576);
    // Second commit
    mc.add(94, 456);
    mc.close(false);
    // Final commit
    // conn = cpds.getConnection();
    stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("SELECT count('*') AS num FROM matchXYZ;");
    assertEquals(7, rs.getInt("num"));
    rs = stat.executeQuery("SELECT text_id, match_count FROM matchXYZ;");
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 9);
    assertEquals(rs.getInt("match_count"), 5000);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 12);
    assertEquals(rs.getInt("match_count"), 6785);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 39);
    assertEquals(rs.getInt("match_count"), 56576);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 45);
    assertEquals(rs.getInt("match_count"), 5000);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 67);
    assertEquals(rs.getInt("match_count"), 6785);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 81);
    assertEquals(rs.getInt("match_count"), 56576);
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 94);
    assertEquals(rs.getInt("match_count"), 456);
    assertFalse(rs.next());
    stat.close();
}
Also used : MatchCollectorDB(de.ids_mannheim.korap.response.collector.MatchCollectorDB) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 3 with MatchCollectorDB

use of de.ids_mannheim.korap.response.collector.MatchCollectorDB in project Krill by KorAP.

the class TestDatabase method TestDatabasePool.

/*
     * The following tests don't work well with in-memory dbs and
     * temporary dbs - should be improved
     */
@Test
public void TestDatabasePool() throws Exception {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    // Connect to a temporary file instead of a in-memory file
    cpds.setDriverClass("org.sqlite.JDBC");
    cpds.setJdbcUrl("jdbc:sqlite:");
    cpds.setMaxStatements(100);
    // This is part of the match collector
    this.conn = cpds.getConnection();
    conn.setAutoCommit(false);
    this.stat = conn.createStatement();
    stat.executeUpdate("CREATE TABLE IF NOT EXISTS result_a (text_id INTEGER, match_count INTEGER);");
    // conn.setAutoCommit(false);
    PreparedStatement prep = this.conn.prepareStatement("INSERT INTO result_a VALUES (?, ?);");
    prep.setInt(1, 5);
    prep.setInt(2, 8000);
    prep.addBatch();
    prep.executeBatch();
    ResultSet rs = stat.executeQuery("SELECT * FROM result_a;");
    rs.next();
    assertEquals(rs.getInt("text_id"), 5);
    assertEquals(rs.getInt("match_count"), 8000);
    rs.close();
    MatchCollectorDB mc = new MatchCollectorDB(2000, "result_a");
    mc.setDBPool("sqlite", cpds, this.conn);
    mc.add(9, 5000);
    mc.add(12, 6785);
    mc.add(39, 56576);
    mc.close(false);
    rs = stat.executeQuery("SELECT * FROM result_a;");
    assertTrue(rs.next());
    assertEquals(rs.getInt("text_id"), 5);
    assertEquals(rs.getInt("match_count"), 8000);
    rs.next();
    assertEquals(rs.getInt("text_id"), 9);
    assertEquals(rs.getInt("match_count"), 5000);
    rs.next();
    assertEquals(rs.getInt("text_id"), 12);
    assertEquals(rs.getInt("match_count"), 6785);
    rs.next();
    assertEquals(rs.getInt("text_id"), 39);
    assertEquals(rs.getInt("match_count"), 56576);
    rs.close();
}
Also used : MatchCollectorDB(de.ids_mannheim.korap.response.collector.MatchCollectorDB) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 4 with MatchCollectorDB

use of de.ids_mannheim.korap.response.collector.MatchCollectorDB in project Krill by KorAP.

the class TestDatabase method TestMatchCollectorDB.

@Test
public void TestMatchCollectorDB() throws Exception {
    MatchCollector mc = new MatchCollectorDB(2000, "matchXYZ");
    mc.add(5, 7);
    mc.add(8, 2);
    mc.add(9, 10);
    mc.add(16, 90);
    mc.commit();
    assertEquals(mc.getTotalResults(), 109);
    assertEquals(mc.getTotalResultDocs(), 4);
}
Also used : MatchCollectorDB(de.ids_mannheim.korap.response.collector.MatchCollectorDB) MatchCollector(de.ids_mannheim.korap.response.MatchCollector) Test(org.junit.Test)

Aggregations

MatchCollectorDB (de.ids_mannheim.korap.response.collector.MatchCollectorDB)4 Test (org.junit.Test)3 MatchCollector (de.ids_mannheim.korap.response.MatchCollector)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 Krill (de.ids_mannheim.korap.Krill)1 Response (de.ids_mannheim.korap.response.Response)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1