use of org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase in project ignite by apache.
the class IgniteH2Indexing method createSortedIndex.
/**
* Create sorted index.
*
* @param schema Schema.
* @param name Index name,
* @param tbl Table.
* @param pk Primary key flag.
* @param cols Columns.
* @return Index.
*/
public GridH2IndexBase createSortedIndex(H2Schema schema, String name, GridH2Table tbl, boolean pk, List<IndexColumn> cols, int inlineSize) {
try {
GridCacheContext cctx = tbl.cache();
if (log.isDebugEnabled())
log.debug("Creating cache index [cacheId=" + cctx.cacheId() + ", idxName=" + name + ']');
final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
return new H2TreeIndex(cctx, tbl, name, pk, cols, inlineSize, segments);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase in project ignite by apache.
the class GridH2TableSelfTest method testTable.
/**
* Simple table test.
*
* @throws Exception If failed.
*/
public void testTable() throws Exception {
// Test insert.
long x = MAX_X;
Random rnd = new Random();
while (x-- > 0) {
UUID id = UUID.randomUUID();
GridH2Row row = row(id, System.currentTimeMillis(), rnd.nextBoolean() ? id.toString() : UUID.randomUUID().toString(), rnd.nextInt(100));
tbl.doUpdate(row, false);
}
assertEquals(MAX_X, tbl.getRowCountApproximation());
assertEquals(MAX_X, tbl.getRowCount(null));
for (GridH2IndexBase idx : tbl.indexes()) {
assertEquals(MAX_X, idx.getRowCountApproximation());
assertEquals(MAX_X, idx.getRowCount(null));
}
// Check correct rows order.
checkOrdered((GridH2TreeIndex) tbl.indexes().get(0), new Comparator<SearchRow>() {
@Override
public int compare(SearchRow o1, SearchRow o2) {
UUID id1 = (UUID) o1.getValue(0).getObject();
UUID id2 = (UUID) o2.getValue(0).getObject();
return id1.compareTo(id2);
}
});
checkOrdered((GridH2TreeIndex) tbl.indexes().get(1), new Comparator<SearchRow>() {
@Override
public int compare(SearchRow o1, SearchRow o2) {
Long x1 = (Long) o1.getValue(3).getObject();
Long x2 = (Long) o2.getValue(3).getObject();
int c = x2.compareTo(x1);
if (c != 0)
return c;
Timestamp t1 = (Timestamp) o1.getValue(1).getObject();
Timestamp t2 = (Timestamp) o2.getValue(1).getObject();
return t1.compareTo(t2);
}
});
checkOrdered((GridH2TreeIndex) tbl.indexes().get(2), new Comparator<SearchRow>() {
@Override
public int compare(SearchRow o1, SearchRow o2) {
String s1 = (String) o1.getValue(2).getObject();
String s2 = (String) o2.getValue(2).getObject();
return s2.compareTo(s1);
}
});
// Indexes data consistency.
ArrayList<? extends Index> idxs = tbl.indexes();
checkIndexesConsistent((ArrayList<Index>) idxs, null);
// Check unique index.
UUID id = UUID.randomUUID();
UUID id2 = UUID.randomUUID();
assertTrue(tbl.doUpdate(row(id, System.currentTimeMillis(), id.toString(), rnd.nextInt(100)), false));
assertTrue(tbl.doUpdate(row(id2, System.currentTimeMillis(), id2.toString(), rnd.nextInt(100)), false));
// Check index selection.
checkQueryPlan(conn, "SELECT * FROM T", SCAN_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE ID IS NULL", PK_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE ID = RANDOM_UUID()", PK_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE ID > RANDOM_UUID()", PK_NAME);
checkQueryPlan(conn, "SELECT * FROM T ORDER BY ID", PK_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE STR IS NULL", STR_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE STR = 'aaaa'", STR_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE STR > 'aaaa'", STR_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T ORDER BY STR DESC", STR_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE X IS NULL", NON_UNIQUE_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE X = 10000", NON_UNIQUE_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T WHERE X > 10000", NON_UNIQUE_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T ORDER BY X DESC", NON_UNIQUE_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T ORDER BY X DESC, T", NON_UNIQUE_IDX_NAME);
checkQueryPlan(conn, "SELECT * FROM T ORDER BY T, X DESC", SCAN_IDX_NAME);
// Simple queries.
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select id from t where x between 0 and 100");
int i = 0;
while (rs.next()) i++;
assertEquals(MAX_X + 2, i);
// -----
rs = s.executeQuery("select id from t where t is not null");
i = 0;
while (rs.next()) i++;
assertEquals(MAX_X + 2, i);
// ----
int cnt = 10 + rnd.nextInt(25);
long t = System.currentTimeMillis();
for (i = 0; i < cnt; i++) {
id = UUID.randomUUID();
assertTrue(tbl.doUpdate(row(id, t, id.toString(), 51), false));
}
rs = s.executeQuery("select x, id from t where x = 51 limit " + cnt);
i = 0;
while (rs.next()) {
assertEquals(51, rs.getInt(1));
i++;
}
assertEquals(cnt, i);
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase in project ignite by apache.
the class H2Utils method createSpatialIndex.
/**
* Create spatial index.
*
* @param tbl Table.
* @param idxName Index name.
* @param cols Columns.
*/
public static GridH2IndexBase createSpatialIndex(GridH2Table tbl, String idxName, IndexColumn[] cols) {
try {
Class<?> cls = Class.forName(SPATIAL_IDX_CLS);
Constructor<?> ctor = cls.getConstructor(GridH2Table.class, String.class, Integer.TYPE, IndexColumn[].class);
if (!ctor.isAccessible())
ctor.setAccessible(true);
final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
return (GridH2IndexBase) ctor.newInstance(tbl, idxName, segments, cols);
} catch (Exception e) {
throw new IgniteException("Failed to instantiate: " + SPATIAL_IDX_CLS, e);
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase in project ignite by apache.
the class GridH2Table method update.
/**
* Updates table for given key. If value is null then row with given key will be removed from table,
* otherwise value and expiration time will be updated or new row will be added.
*
* @param row Row to be updated.
* @param prevRow Previous row.
* @param prevRowAvailable Whether previous row is available.
* @throws IgniteCheckedException If failed.
*/
public void update(CacheDataRow row, @Nullable CacheDataRow prevRow, boolean prevRowAvailable) throws IgniteCheckedException {
assert desc != null;
GridH2KeyValueRowOnheap row0 = (GridH2KeyValueRowOnheap) desc.createRow(row);
GridH2KeyValueRowOnheap prevRow0 = prevRow != null ? (GridH2KeyValueRowOnheap) desc.createRow(prevRow) : null;
row0.prepareValuesCache();
if (prevRow0 != null)
prevRow0.prepareValuesCache();
try {
lock(false);
try {
ensureNotDestroyed();
boolean replaced;
if (prevRowAvailable)
replaced = pk().putx(row0);
else {
prevRow0 = (GridH2KeyValueRowOnheap) pk().put(row0);
replaced = prevRow0 != null;
}
if (!replaced)
size.increment();
for (int i = pkIndexPos + 1, len = idxs.size(); i < len; i++) {
Index idx = idxs.get(i);
if (idx instanceof GridH2IndexBase)
addToIndex((GridH2IndexBase) idx, row0, prevRow0);
}
if (!tmpIdxs.isEmpty()) {
for (GridH2IndexBase idx : tmpIdxs.values()) addToIndex(idx, row0, prevRow0);
}
} finally {
unlock(false);
}
} finally {
row0.clearValuesCache();
if (prevRow0 != null)
prevRow0.clearValuesCache();
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase in project ignite by apache.
the class GridH2Table method proposeUserIndex.
/**
* Add index that is in an intermediate state and is still being built, thus is not used in queries until it is
* promoted.
*
* @param idx Index to add.
* @throws IgniteCheckedException If failed.
*/
public void proposeUserIndex(Index idx) throws IgniteCheckedException {
assert idx instanceof GridH2IndexBase;
lock(true);
try {
ensureNotDestroyed();
for (Index oldIdx : idxs) {
if (F.eq(oldIdx.getName(), idx.getName()))
throw new IgniteCheckedException("Index already exists: " + idx.getName());
}
Index oldTmpIdx = tmpIdxs.put(idx.getName(), (GridH2IndexBase) idx);
assert oldTmpIdx == null;
} finally {
unlock(true);
}
}
Aggregations