use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.
the class GridH2TableSelfTest method testIndexFindFirstOrLast.
/**
* @throws Exception If failed.
*/
public void testIndexFindFirstOrLast() throws Exception {
Index index = tbl.getIndexes().get(2);
assertTrue(index instanceof GridH2TreeIndex);
assertTrue(index.canGetFirstOrLast());
//find first on empty data
Cursor cursor = index.findFirstOrLast(null, true);
assertFalse(cursor.next());
assertNull(cursor.get());
//find last on empty data
cursor = index.findFirstOrLast(null, false);
assertFalse(cursor.next());
assertNull(cursor.get());
//fill with data
int rows = 100;
long t = System.currentTimeMillis();
Random rnd = new Random();
UUID min = null;
UUID max = null;
for (int i = 0; i < rows; i++) {
UUID id = UUID.randomUUID();
if (min == null || id.compareTo(min) < 0)
min = id;
if (max == null || id.compareTo(max) > 0)
max = id;
GridH2Row row = row(id, t++, id.toString(), rnd.nextInt(100));
((GridH2TreeIndex) index).put(row);
}
//find first
cursor = index.findFirstOrLast(null, true);
assertTrue(cursor.next());
assertEquals(min, cursor.get().getValue(0).getObject());
assertFalse(cursor.next());
//find last
cursor = index.findFirstOrLast(null, false);
assertTrue(cursor.next());
assertEquals(max, cursor.get().getValue(0).getObject());
assertFalse(cursor.next());
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.
the class H2TreeIndex method find.
/**
* {@inheritDoc}
*/
@Override
public Cursor find(Session ses, SearchRow lower, SearchRow upper) {
try {
IndexingQueryCacheFilter filter = partitionFilter(threadLocalFilter());
int seg = threadLocalSegment();
H2Tree tree = treeForRead(seg);
if (indexType.isPrimaryKey() && lower != null && upper != null && tree.compareRows(lower, upper) == 0) {
GridH2Row row = tree.findOne(lower, filter);
return (row == null) ? EMPTY_CURSOR : new SingleRowCursor(row);
} else {
GridCursor<GridH2Row> cursor = tree.find(lower, upper, filter);
return new H2Cursor(cursor);
}
} catch (IgniteCheckedException e) {
throw DbException.convert(e);
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.
the class H2TreeIndex method doFind0.
/**
* {@inheritDoc}
*/
@Override
protected H2Cursor doFind0(IgniteTree t, @Nullable SearchRow first, boolean includeFirst, @Nullable SearchRow last, IndexingQueryFilter filter) {
try {
IndexingQueryCacheFilter pf = partitionFilter(filter);
GridCursor<GridH2Row> range = t.find(first, last, pf);
if (range == null)
range = GridH2IndexBase.EMPTY_CURSOR;
return new H2Cursor(range);
} catch (IgniteCheckedException e) {
throw DbException.convert(e);
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.
the class GridH2Table method remove.
/**
* Remove row.
*
* @param row Row.
* @return {@code True} if was removed.
* @throws IgniteCheckedException If failed.
*/
public boolean remove(CacheDataRow row) throws IgniteCheckedException {
GridH2Row row0 = desc.createRow(row);
lock(false);
try {
ensureNotDestroyed();
boolean rmv = pk().removex(row0);
if (rmv) {
for (int i = pkIndexPos + 1, len = idxs.size(); i < len; i++) {
Index idx = idxs.get(i);
if (idx instanceof GridH2IndexBase)
((GridH2IndexBase) idx).removex(row0);
}
if (!tmpIdxs.isEmpty()) {
for (GridH2IndexBase idx : tmpIdxs.values()) idx.removex(row0);
}
size.decrement();
}
return rmv;
} finally {
unlock(false);
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.
the class IgniteH2Indexing method dynamicIndexCreate.
/**
* {@inheritDoc}
*/
@Override
public void dynamicIndexCreate(final String schemaName, final String tblName, final QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
// Locate table.
H2Schema schema = schemas.get(schemaName);
H2TableDescriptor desc = (schema != null ? schema.tableByName(tblName) : null);
if (desc == null)
throw new IgniteCheckedException("Table not found in internal H2 database [schemaName=" + schemaName + ", tblName=" + tblName + ']');
GridH2Table h2Tbl = desc.table();
// Create index.
final GridH2IndexBase h2Idx = desc.createUserIndex(idxDesc);
h2Tbl.proposeUserIndex(h2Idx);
try {
// Populate index with existing cache data.
final GridH2RowDescriptor rowDesc = h2Tbl.rowDescriptor();
SchemaIndexCacheVisitorClosure clo = new SchemaIndexCacheVisitorClosure() {
@Override
public void apply(CacheDataRow row) throws IgniteCheckedException {
GridH2Row h2Row = rowDesc.createRow(row);
h2Idx.putx(h2Row);
}
};
cacheVisitor.visit(clo);
// At this point index is in consistent state, promote it through H2 SQL statement, so that cached
// prepared statements are re-built.
String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, ifNotExists);
executeSql(schemaName, sql);
} catch (Exception e) {
// Rollback and re-throw.
h2Tbl.rollbackUserIndex(h2Idx.getName());
throw e;
}
}
Aggregations