use of org.h2.result.Row 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;
}
use of org.h2.result.Row in project ignite by apache.
the class GridSqlInsert method getSQL.
/**
* {@inheritDoc}
*/
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
buff.append("INSERT").append("\nINTO ").append(into.getSQL()).append('(');
for (GridSqlColumn col : cols) {
buff.appendExceptFirst(", ");
buff.append('\n').append(col.getSQL());
}
buff.append("\n)\n");
if (direct)
buff.append("DIRECT ");
if (sorted)
buff.append("SORTED ");
if (!rows.isEmpty()) {
buff.append("VALUES\n");
StatementBuilder valuesBuff = new StatementBuilder();
for (GridSqlElement[] row : rows()) {
valuesBuff.appendExceptFirst(",\n");
StatementBuilder rowBuff = new StatementBuilder("(");
for (GridSqlElement e : row) {
rowBuff.appendExceptFirst(", ");
rowBuff.append(e != null ? e.getSQL() : "DEFAULT");
}
rowBuff.append(')');
valuesBuff.append(rowBuff.toString());
}
buff.append(valuesBuff.toString());
} else
buff.append('\n').append(qry.getSQL());
return buff.toString();
}
use of org.h2.result.Row in project ignite by apache.
the class GridSqlMerge method getSQL.
/**
* {@inheritDoc}
*/
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
buff.append("MERGE INTO ").append(into.getSQL()).append("(");
for (GridSqlColumn col : cols) {
buff.appendExceptFirst(", ");
buff.append('\n').append(col.getSQL());
}
buff.append("\n)\n");
if (keys != null) {
buff.append("KEY(\n");
buff.resetCount();
for (GridSqlColumn c : keys) {
buff.appendExceptFirst(",\n");
buff.append(c.getSQL());
}
buff.append(")\n");
}
if (!rows.isEmpty()) {
buff.append("VALUES\n");
StatementBuilder valuesBuff = new StatementBuilder();
for (GridSqlElement[] row : rows()) {
valuesBuff.appendExceptFirst(",\n");
StatementBuilder rowBuff = new StatementBuilder("(");
for (GridSqlElement e : row) {
rowBuff.appendExceptFirst(", ");
rowBuff.append(e != null ? e.getSQL() : "DEFAULT");
}
rowBuff.append(')');
valuesBuff.append(rowBuff.toString());
}
buff.append(valuesBuff.toString());
} else
buff.append('\n').append(qry.getSQL());
return buff.toString();
}
use of org.h2.result.Row in project ignite by apache.
the class GridSqlQueryParser method parseInsert.
/**
* @param insert Insert.
* @see <a href="http://h2database.com/html/grammar.html#insert">H2 insert spec</a>
*/
private GridSqlInsert parseInsert(Insert insert) {
GridSqlInsert res = (GridSqlInsert) h2ObjToGridObj.get(insert);
if (res != null)
return res;
res = new GridSqlInsert();
h2ObjToGridObj.put(insert, res);
Table srcTbl = INSERT_TABLE.get(insert);
GridSqlElement tbl = parseTable(srcTbl);
res.into(tbl).direct(INSERT_DIRECT.get(insert)).sorted(INSERT_SORTED.get(insert));
Column[] srcCols = INSERT_COLUMNS.get(insert);
GridSqlColumn[] cols = new GridSqlColumn[srcCols.length];
for (int i = 0; i < srcCols.length; i++) {
cols[i] = new GridSqlColumn(srcCols[i], tbl, null, null, srcCols[i].getName());
cols[i].resultType(fromColumn(srcCols[i]));
}
res.columns(cols);
List<Expression[]> srcRows = INSERT_ROWS.get(insert);
if (!srcRows.isEmpty()) {
List<GridSqlElement[]> rows = new ArrayList<>(srcRows.size());
for (Expression[] srcRow : srcRows) {
GridSqlElement[] row = new GridSqlElement[srcRow.length];
for (int i = 0; i < srcRow.length; i++) row[i] = parseExpression(srcRow[i], false);
rows.add(row);
}
res.rows(rows);
} else {
res.rows(Collections.<GridSqlElement[]>emptyList());
res.query(parseQuery(INSERT_QUERY.get(insert)));
}
return res;
}
use of org.h2.result.Row in project ignite by apache.
the class MapQueryResult method fetchNextPage.
/**
* @param rows Collection to fetch into.
* @param pageSize Page size.
* @return {@code true} If there are no more rows available.
*/
synchronized boolean fetchNextPage(List<Value[]> rows, int pageSize) {
assert lazyWorker == null || lazyWorker == MapQueryLazyWorker.currentWorker();
if (closed)
return true;
boolean readEvt = cctx != null && cctx.name() != null && cctx.events().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
page++;
for (int i = 0; i < pageSize; i++) {
if (!res.next())
return true;
Value[] row = res.currentRow();
if (cpNeeded) {
boolean copied = false;
for (int j = 0; j < row.length; j++) {
Value val = row[j];
if (val instanceof GridH2ValueCacheObject) {
GridH2ValueCacheObject valCacheObj = (GridH2ValueCacheObject) val;
row[j] = new GridH2ValueCacheObject(valCacheObj.getCacheObject(), h2.objectContext()) {
@Override
public Object getObject() {
return getObject(true);
}
};
copied = true;
}
}
if (i == 0 && !copied)
// No copy on read caches, skip next checks.
cpNeeded = false;
}
assert row != null;
if (readEvt) {
GridKernalContext ctx = h2.kernalContext();
ctx.event().record(new CacheQueryReadEvent<>(ctx.discovery().localNode(), "SQL fields query result set row read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SQL.name(), cctx.name(), null, qry.query(), null, null, params, qrySrcNodeId, null, null, null, null, row(row)));
}
rows.add(res.currentRow());
}
return false;
}
Aggregations