Search in sources :

Example 1 with StreamableSqlQuery

use of com.infiniteautomation.mango.db.query.StreamableSqlQuery in project ma-core-public by infiniteautomation.

the class TestStreamableSqlQuery method testStreamableSqlQuery.

@Test
public void testStreamableSqlQuery() {
    OutputStream deadEnd = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        }
    };
    System.out.println("Max memory: " + Runtime.getRuntime().totalMemory());
    // Run this test with a low memory overhead, watch for oom
    String[] createTable = new String[] { "DROP TABLE IF EXISTS streamTest;", "CREATE TABLE streamTest ( id int not null auto_increment, testData longtext, primary key (id) ) engine=InnoDB;" };
    try {
        Common.databaseProxy.runScript(createTable, deadEnd);
        String[] insert = new String[] { "INSERT INTO streamTest (testData) values ('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'),('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'),('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'),('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'),('01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567');" };
        for (int k = 0; k < 50000; k += 1) {
            // Insert a million dummy records
            Common.databaseProxy.runScript(insert, deadEnd);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Failed to create test database.");
    }
    StreamTestDao std = new StreamTestDao(null, "streamTest");
    StreamableSqlQuery<StreamTestData> ssq = new StreamableSqlQuery<StreamTestData>(std, false, std.TEST_SELECT_ALL, new StreamableRowCallback<StreamTestData>() {

        boolean logged = false;

        @Override
        public void row(StreamTestData row, int index) throws Exception {
            // System.out.println("Got test data" + row.getTestData() + " on row " + index);
            if (!logged) {
                System.gc();
                System.out.println("Current free memory: " + Runtime.getRuntime().freeMemory());
                logged = true;
            }
        }
    }, null);
    System.gc();
    System.out.println("Pre-query free memory: " + Runtime.getRuntime().freeMemory());
    try {
        ssq.query();
    } catch (Exception e) {
        fail("Didn't execute query successfully.");
    }
    System.gc();
    System.out.println("Post-query free memory: " + Runtime.getRuntime().freeMemory());
    String[] dropTable = new String[] { "drop table streamTest;" };
    try {
        Common.databaseProxy.runScript(dropTable, deadEnd);
    } catch (Exception e) {
        fail("Failed to delete table when done");
    }
}
Also used : OutputStream(java.io.OutputStream) StreamableSqlQuery(com.infiniteautomation.mango.db.query.StreamableSqlQuery) IOException(java.io.IOException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 2 with StreamableSqlQuery

use of com.infiniteautomation.mango.db.query.StreamableSqlQuery in project ma-core-public by infiniteautomation.

the class AbstractBasicDao method createQuery.

/**
 * @param root
 * @param selectCallback
 * @param countCallback
 * @param modelMap
 * @param modifiers
 * @param applyLimitToSelectSql
 * @return
 */
public StreamableSqlQuery<T> createQuery(ASTNode root, StreamableRowCallback<T> selectCallback, StreamableRowCallback<Long> countCallback, Map<String, String> modelMap, Map<String, SQLColumnQueryAppender> modifiers, boolean applyLimitToSelectSql) {
    SQLStatement statement;
    if (useSubQuery) {
        statement = new SQLSubQuery(SELECT_ALL_BASE, COUNT_BASE, joins, getTableName(), TABLE_PREFIX, applyLimitToSelectSql, Common.envProps.getBoolean("db.forceUseIndex", false), null, this.indexes, this.databaseType);
    } else {
        statement = new SQLStatement(SELECT_ALL_BASE, COUNT_BASE, joins, getTableName(), TABLE_PREFIX, applyLimitToSelectSql, Common.envProps.getBoolean("db.forceUseIndex", false), this.indexes, this.databaseType);
    }
    if (root != null)
        root.accept(new RQLToSQLSelect<T>(this, modelMap, modifiers), statement);
    statement.build();
    return new StreamableSqlQuery<T>(this, Common.envProps.getBoolean("db.stream", false), statement, selectCallback, countCallback);
}
Also used : SQLSubQuery(com.infiniteautomation.mango.db.query.SQLSubQuery) StreamableSqlQuery(com.infiniteautomation.mango.db.query.StreamableSqlQuery) SQLStatement(com.infiniteautomation.mango.db.query.SQLStatement) RQLToSQLSelect(com.infiniteautomation.mango.db.query.RQLToSQLSelect)

Aggregations

StreamableSqlQuery (com.infiniteautomation.mango.db.query.StreamableSqlQuery)2 RQLToSQLSelect (com.infiniteautomation.mango.db.query.RQLToSQLSelect)1 SQLStatement (com.infiniteautomation.mango.db.query.SQLStatement)1 SQLSubQuery (com.infiniteautomation.mango.db.query.SQLSubQuery)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 SQLException (java.sql.SQLException)1 Test (org.junit.Test)1