Search in sources :

Example 21 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class H2DynamicTableSelfTest method testIndexNameConflictCheckDiscovery.

/**
 * Tests index name conflict check in discovery thread.
 * @throws Exception if failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexNameConflictCheckDiscovery() throws Exception {
    execute(grid(0), "CREATE TABLE \"Person\" (id int primary key, name varchar)");
    execute(grid(0), "CREATE INDEX \"idx\" ON \"Person\" (\"name\")");
    GridTestUtils.assertThrows(null, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            QueryEntity e = new QueryEntity();
            e.setTableName("City");
            e.setKeyFields(Collections.singleton("name"));
            e.setFields(new LinkedHashMap<>(Collections.singletonMap("name", String.class.getName())));
            e.setIndexes(Collections.singleton(new QueryIndex("name").setName("idx")));
            e.setKeyType("CityKey");
            e.setValueType("City");
            queryProcessor(client()).dynamicTableCreate("PUBLIC", e, CacheMode.PARTITIONED.name(), null, null, null, null, CacheAtomicityMode.ATOMIC, null, 10, false);
            return null;
        }
    }, SchemaOperationException.class, "Index already exists: idx");
}
Also used : QueryIndex(org.apache.ignite.cache.QueryIndex) BinaryObject(org.apache.ignite.binary.BinaryObject) QueryEntity(org.apache.ignite.cache.QueryEntity) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class GridQueryParsingTest method testParseTableFilter.

/**
 * Query AST transformation heavily depends on this behavior.
 *
 * @throws Exception If failed.
 */
public void testParseTableFilter() throws Exception {
    Prepared prepared = parse("select Person.old, p1.old, p1.addrId from Person, Person p1 " + "where exists(select 1 from sch2.Address a where a.id = p1.addrId)");
    GridSqlSelect select = (GridSqlSelect) new GridSqlQueryParser(false).parse(prepared);
    GridSqlJoin join = (GridSqlJoin) select.from();
    GridSqlTable tbl1 = (GridSqlTable) join.leftTable();
    GridSqlAlias tbl2Alias = (GridSqlAlias) join.rightTable();
    GridSqlTable tbl2 = tbl2Alias.child();
    // Must be distinct objects, even if it is the same table.
    assertNotSame(tbl1, tbl2);
    assertNotNull(tbl1.dataTable());
    assertNotNull(tbl2.dataTable());
    assertSame(tbl1.dataTable(), tbl2.dataTable());
    GridSqlColumn col1 = (GridSqlColumn) select.column(0);
    GridSqlColumn col2 = (GridSqlColumn) select.column(1);
    assertSame(tbl1, col1.expressionInFrom());
    // Alias in FROM must be included in column.
    assertSame(tbl2Alias, col2.expressionInFrom());
    // In EXISTS we must correctly reference the column from the outer query.
    GridSqlAst exists = select.where();
    GridSqlSubquery subqry = exists.child();
    GridSqlSelect subSelect = subqry.child();
    GridSqlColumn p1AddrIdCol = (GridSqlColumn) select.column(2);
    assertEquals("ADDRID", p1AddrIdCol.column().getName());
    assertSame(tbl2Alias, p1AddrIdCol.expressionInFrom());
    GridSqlColumn p1AddrIdColExists = subSelect.where().child(1);
    assertEquals("ADDRID", p1AddrIdCol.column().getName());
    assertSame(tbl2Alias, p1AddrIdColExists.expressionInFrom());
}
Also used : Prepared(org.h2.command.Prepared)

Example 23 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project elastic-core-maven by OrdinaryDude.

the class FullTextTrigger method search.

/**
 * Search the Lucene index
 *
 * The result set will have the following columns:
 *   SCHEMA  - Schema name (String)
 *   TABLE   - Table name (String)
 *   COLUMNS - Primary key column names (String[]) - this is always DB_ID
 *   KEYS    - Primary key values (Long[]) - this is always the DB_ID value for the table row
 *   SCORE   - Lucene score (Float)
 *
 * @param   conn                SQL connection
 * @param   schema              Schema name
 * @param   table               Table name
 * @param   queryText           Query expression
 * @param   limit               Number of rows to return
 * @param   offset              Offset with result set
 * @return                      Search results
 * @throws  SQLException        Unable to search the index
 */
public static ResultSet search(Connection conn, String schema, String table, String queryText, int limit, int offset) throws SQLException {
    // 
    // Get Lucene index access
    // 
    getIndexAccess(conn);
    // 
    // Create the result set columns
    // 
    SimpleResultSet result = new SimpleResultSet();
    result.addColumn("SCHEMA", Types.VARCHAR, 0, 0);
    result.addColumn("TABLE", Types.VARCHAR, 0, 0);
    result.addColumn("COLUMNS", Types.ARRAY, 0, 0);
    result.addColumn("KEYS", Types.ARRAY, 0, 0);
    result.addColumn("SCORE", Types.FLOAT, 0, 0);
    // 
    // Perform the search
    // 
    // The _QUERY field contains the table and row identification (schema.table;keyName;keyValue)
    // The _TABLE field is used to limit the search results to the current table
    // The _DATA field contains the indexed row data (this is the default search field)
    // The _MODIFIED field contains the row modification time (YYYYMMDDhhmmss) in GMT
    // 
    indexLock.readLock().lock();
    try {
        QueryParser parser = new QueryParser("_DATA", analyzer);
        parser.setDateResolution("_MODIFIED", DateTools.Resolution.SECOND);
        parser.setDefaultOperator(QueryParser.Operator.AND);
        Query query = parser.parse("_TABLE:" + schema.toUpperCase() + "." + table.toUpperCase() + " AND (" + queryText + ")");
        TopDocs documents = indexSearcher.search(query, limit);
        ScoreDoc[] hits = documents.scoreDocs;
        int resultCount = Math.min(hits.length, (limit == 0 ? hits.length : limit));
        int resultOffset = Math.min(offset, resultCount);
        for (int i = resultOffset; i < resultCount; i++) {
            Document document = indexSearcher.doc(hits[i].doc);
            String[] indexParts = document.get("_QUERY").split(";");
            String[] nameParts = indexParts[0].split("\\.");
            result.addRow(nameParts[0], nameParts[1], new String[] { indexParts[1] }, new Long[] { Long.parseLong(indexParts[2]) }, hits[i].score);
        }
    } catch (ParseException exc) {
        Logger.logDebugMessage("Lucene parse exception for query: " + queryText + "\n" + exc.getMessage());
        throw new SQLException("Lucene parse exception for query: " + queryText + "\n" + exc.getMessage());
    } catch (IOException exc) {
        Logger.logErrorMessage("Unable to search Lucene index", exc);
        throw new SQLException("Unable to search Lucene index", exc);
    } finally {
        indexLock.readLock().unlock();
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) Query(org.apache.lucene.search.Query) SQLException(java.sql.SQLException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 24 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project spf4j by zolyfarkas.

the class JdbcSemaphoreTest method testSingleMultipleInstance.

@Test(expected = SQLException.class)
public void testSingleMultipleInstance() throws SQLException, IOException, InterruptedException, TimeoutException {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test");
    ds.setUser("sa");
    ds.setPassword("sa");
    try (Connection conn = ds.getConnection()) {
        // only to keep the schema arround in this section
        createSchemaObjects(ds);
        JdbcLock lock = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
        JdbcLock lock2 = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
        lock.lock();
        Assert.assertFalse(lock.tryLock());
        lock.unlock();
        lock2.lock();
        lock.unlock();
    }
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 25 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project spf4j by zolyfarkas.

the class JdbcSemaphoreTest method testSingleProcess.

@Test
public void testSingleProcess() throws SQLException, IOException, InterruptedException, TimeoutException {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test");
    ds.setUser("sa");
    ds.setPassword("sa");
    try (Connection conn = ds.getConnection()) {
        // only to keep the schema arround in thsi section
        createSchemaObjects(ds);
        JdbcHeartBeat heartbeat = JdbcHeartBeat.getHeartBeatAndSubscribe(ds, HeartBeatTableDesc.DEFAULT, (JdbcHeartBeat.LifecycleHook) null);
        long lb = heartbeat.getLastRunDB();
        LOG.debug("last TS = {}", Instant.ofEpochMilli(lb));
        heartbeat.beat();
        testReleaseAck(ds, "testSem", 2);
        testReleaseAck(ds, "testSem2", 2);
        heartbeat.close();
    }
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15