Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 663F010742 for ; Tue, 9 Dec 2014 16:33:38 +0000 (UTC) Received: (qmail 86982 invoked by uid 500); 9 Dec 2014 16:33:38 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 86952 invoked by uid 500); 9 Dec 2014 16:33:38 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 86943 invoked by uid 99); 9 Dec 2014 16:33:38 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Dec 2014 16:33:38 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 09 Dec 2014 16:33:32 +0000 Received: (qmail 83907 invoked by uid 99); 9 Dec 2014 16:33:12 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Dec 2014 16:33:12 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id BA040A20E15; Tue, 9 Dec 2014 16:33:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergi@apache.org To: commits@ignite.incubator.apache.org Date: Tue, 09 Dec 2014 16:33:14 -0000 Message-Id: In-Reply-To: <48da8482d91744fe9addd7f412e2ee49@git.apache.org> References: <48da8482d91744fe9addd7f412e2ee49@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/13] incubator-ignite git commit: ignite-qry - merged X-Virus-Checked: Checked by ClamAV on apache.org http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/GridH2ResultSetIterator.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/GridH2ResultSetIterator.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/GridH2ResultSetIterator.java deleted file mode 100644 index c37752f..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/GridH2ResultSetIterator.java +++ /dev/null @@ -1,122 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2; - -import org.apache.ignite.spi.*; -import org.gridgain.grid.*; -import org.gridgain.grid.util.typedef.internal.*; - -import java.sql.*; -import java.util.*; - - -/** - * Iterator over result set. - */ -abstract class GridH2ResultSetIterator implements IgniteSpiCloseableIterator { - /** */ - private static final long serialVersionUID = 0L; - - /** */ - private final ResultSet data; - - /** */ - protected final Object[] row; - - /** */ - private boolean hasRow; - - /** - * @param data Data array. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - protected GridH2ResultSetIterator(ResultSet data) throws IgniteSpiException { - this.data = data; - - if (data != null) { - try { - row = new Object[data.getMetaData().getColumnCount()]; - } - catch (SQLException e) { - throw new IgniteSpiException(e); - } - } - else - row = null; - } - - /** - * @return {@code true} If next row was fetched successfully. - */ - private boolean fetchNext() { - if (data == null) - return false; - - try { - if (!data.next()) - return false; - - for (int c = 0; c < row.length; c++) - row[c] = data.getObject(c + 1); - - return true; - } - catch (SQLException e) { - throw new GridRuntimeException(e); - } - } - - /** {@inheritDoc} */ - @Override public boolean hasNext() { - return hasRow || (hasRow = fetchNext()); - } - - /** {@inheritDoc} */ - @SuppressWarnings("IteratorNextCanNotThrowNoSuchElementException") - @Override public T next() { - if (!hasNext()) - throw new NoSuchElementException(); - - hasRow = false; - - return createRow(); - } - - /** - * @return Row. - */ - protected abstract T createRow(); - - /** {@inheritDoc} */ - @Override public void remove() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public void close() throws GridException { - if (data == null) - // Nothing to close. - return; - - try { - U.closeQuiet(data.getStatement()); - } - catch (SQLException e) { - throw new GridException(e); - } - - U.closeQuiet(data); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString((Class)getClass(), this); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2AbstractKeyValueRow.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2AbstractKeyValueRow.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2AbstractKeyValueRow.java deleted file mode 100644 index ce56b57..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2AbstractKeyValueRow.java +++ /dev/null @@ -1,446 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.apache.ignite.spi.*; -import org.gridgain.grid.*; -import org.gridgain.grid.util.typedef.internal.*; -import org.h2.message.*; -import org.h2.result.*; -import org.h2.value.*; -import org.jetbrains.annotations.*; - -import java.lang.ref.*; -import java.math.*; -import java.sql.Date; -import java.sql.*; -import java.util.*; - -/** - * Table row implementation based on {@link org.apache.ignite.spi.indexing.IndexingTypeDescriptor}. - */ -public abstract class GridH2AbstractKeyValueRow extends GridH2Row { - /** */ - private static final int DEFAULT_COLUMNS_COUNT = 2; - - /** Key column. */ - public static final int KEY_COL = 0; - - /** Value column. */ - public static final int VAL_COL = 1; - - /** */ - protected final GridH2RowDescriptor desc; - - /** */ - @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") - protected long expirationTime; - - /** - * Constructor. - * - * @param desc Row descriptor. - * @param key Key. - * @param keyType Key type. - * @param val Value. - * @param valType Value type. - * @param expirationTime Expiration time. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - protected GridH2AbstractKeyValueRow(GridH2RowDescriptor desc, Object key, int keyType, @Nullable Object val, - int valType, long expirationTime) throws IgniteSpiException { - super(wrap(key, keyType), - val == null ? null : wrap(val, valType)); // We remove by key only, so value can be null here. - - this.desc = desc; - this.expirationTime = expirationTime; - } - - /** - * Protected constructor for {@link GridH2KeyValueRowOffheap} - * - * @param desc Row descriptor. - */ - protected GridH2AbstractKeyValueRow(GridH2RowDescriptor desc) { - super(new Value[DEFAULT_COLUMNS_COUNT]); - - this.desc = desc; - } - - /** - * Wraps object to respective {@link Value}. - * - * @param obj Object. - * @param type Value type. - * @return Value. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - private static Value wrap(Object obj, int type) throws IgniteSpiException { - switch (type) { - case Value.BOOLEAN: - return ValueBoolean.get((Boolean)obj); - case Value.BYTE: - return ValueByte.get((Byte)obj); - case Value.SHORT: - return ValueShort.get((Short)obj); - case Value.INT: - return ValueInt.get((Integer)obj); - case Value.FLOAT: - return ValueFloat.get((Float)obj); - case Value.LONG: - return ValueLong.get((Long)obj); - case Value.DOUBLE: - return ValueDouble.get((Double)obj); - case Value.UUID: - UUID uuid = (UUID)obj; - return ValueUuid.get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); - case Value.DATE: - return ValueDate.get((Date)obj); - case Value.TIME: - return ValueTime.get((Time)obj); - case Value.TIMESTAMP: - if (obj instanceof java.util.Date && !(obj instanceof Timestamp)) - obj = new Timestamp(((java.util.Date) obj).getTime()); - - return GridH2Utils.toValueTimestamp((Timestamp)obj); - case Value.DECIMAL: - return ValueDecimal.get((BigDecimal)obj); - case Value.STRING: - return ValueString.get(obj.toString()); - case Value.BYTES: - return ValueBytes.get((byte[])obj); - case Value.JAVA_OBJECT: - return ValueJavaObject.getNoCopy(obj, null, null); - case Value.ARRAY: - Object[] arr = (Object[])obj; - - Value[] valArr = new Value[arr.length]; - - for (int i = 0; i < arr.length; i++) { - Object o = arr[i]; - - valArr[i] = o == null ? ValueNull.INSTANCE : wrap(o, DataType.getTypeFromClass(o.getClass())); - } - - return ValueArray.get(valArr); - - case Value.GEOMETRY: - return ValueGeometry.getFromGeometry(obj); - } - - throw new IgniteSpiException("Failed to wrap value[type=" + type + ", value=" + obj + "]"); - } - - /** - * @return Expiration time of respective cache entry. - */ - public long expirationTime() { - return expirationTime; - } - - /** {@inheritDoc} */ - @Override public int getColumnCount() { - return DEFAULT_COLUMNS_COUNT + desc.fieldsCount(); - } - - /** - * Should be called to remove reference on value. - * - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public synchronized void onSwap() throws GridException { - setValue(VAL_COL, null); - } - - /** - * Should be called when entry getting unswapped. - * - * @param val Value. - * @throws GridException If failed. - */ - public synchronized void onUnswap(Object val) throws GridException { - setValue(VAL_COL, wrap(val, desc.valueType())); - } - - /** - * Atomically updates weak value. - * - * @param exp Expected value. - * @param upd New value. - * @return Expected value if update succeeded, unexpected value otherwise. - */ - protected synchronized Value updateWeakValue(Value exp, Value upd) { - Value res = super.getValue(VAL_COL); - - if (res != exp && !(res instanceof WeakValue)) - return res; - - setValue(VAL_COL, new WeakValue(upd)); - - return exp; - } - - /** - * @return Synchronized value. - */ - protected synchronized Value syncValue() { - return super.getValue(VAL_COL); - } - - /** {@inheritDoc} */ - @Override public Value getValue(int col) { - if (col < DEFAULT_COLUMNS_COUNT) { - Value v = super.getValue(col); - - if (col == VAL_COL) { - while ((v = WeakValue.unwrap(v)) == null) { - v = getOffheapValue(VAL_COL); - - if (v != null) { - setValue(VAL_COL, v); - - if (super.getValue(KEY_COL) == null) - cache(); - - return v; - } - - try { - Object valObj = desc.readFromSwap(getValue(KEY_COL).getObject()); - - if (valObj != null) { - Value upd = wrap(valObj, desc.valueType()); - - Value res = updateWeakValue(v, upd); - - if (res == v) { - if (super.getValue(KEY_COL) == null) - cache(); - - return upd; - } - - v = res; - } - else { - // If nothing found in swap then we should be already unswapped. - v = syncValue(); - } - } - catch (GridException e) { - throw new GridRuntimeException(e); - } - } - } - - if (v == null) { - assert col == KEY_COL : col; - - v = getOffheapValue(KEY_COL); - - assert v != null : v; - - setValue(KEY_COL, v); - - if (super.getValue(VAL_COL) == null) - cache(); - } - - assert !(v instanceof WeakValue) : v; - - return v; - } - - col -= DEFAULT_COLUMNS_COUNT; - - assert col >= 0; - - Value v = getValue(desc.isKeyColumn(col) ? KEY_COL : VAL_COL); - - if (v == null) - return null; - - Object obj = v.getObject(); - - Object res = desc.columnValue(obj, col); - - if (res == null) - return ValueNull.INSTANCE; - - try { - return wrap(res, desc.fieldType(col)); - } - catch (IgniteSpiException e) { - throw DbException.convert(e); - } - } - - /** - * Caches this row for reuse. - */ - protected abstract void cache(); - - /** - * @param col Column. - * @return Value read from offheap memory or null if it is impossible. - */ - protected abstract Value getOffheapValue(int col); - - /** {@inheritDoc} */ - @Override public String toString() { - SB sb = new SB("Row@"); - - sb.a(Integer.toHexString(System.identityHashCode(this))); - - Value v = super.getValue(KEY_COL); - sb.a("[ key: ").a(v == null ? "nil" : v.getString()); - - v = WeakValue.unwrap(super.getValue(VAL_COL)); - sb.a(", val: ").a(v == null ? "nil" : v.getString()); - - sb.a(" ][ "); - - if (v != null) { - for (int i = 2, cnt = getColumnCount(); i < cnt; i++) { - v = getValue(i); - - if (i != 2) - sb.a(", "); - - sb.a(v == null ? "nil" : v.getString()); - } - } - - sb.a(" ]"); - - return sb.toString(); - } - - /** {@inheritDoc} */ - @Override public void setKeyAndVersion(SearchRow old) { - assert false; - } - - /** {@inheritDoc} */ - @Override public void setKey(long key) { - assert false; - } - - /** {@inheritDoc} */ - @Override public Row getCopy() { - assert false; - - return null; - } - - /** {@inheritDoc} */ - @Override public void setDeleted(boolean deleted) { - assert false; - } - - /** {@inheritDoc} */ - @Override public long getKey() { - assert false; - - return 0; - } - - /** {@inheritDoc} */ - @Override public void setSessionId(int sesId) { - assert false; - } - - /** {@inheritDoc} */ - @Override public void setVersion(int ver) { - assert false; - } - - /** - * Weak reference to value that was swapped but accessed in indexing SPI. - */ - private static class WeakValue extends Value { - /** - * Unwraps value. - * - * @param v Value. - * @return Unwrapped value. - */ - static Value unwrap(Value v) { - return (v instanceof WeakValue) ? ((WeakValue)v).get() : v; - } - - /** */ - private final WeakReference ref; - - /** - * @param v Value. - */ - private WeakValue(Value v) { - ref = new WeakReference<>(v); - } - - /** - * @return Referenced value. - */ - public Value get() { - return ref.get(); - } - - /** {@inheritDoc} */ - @Override public String getSQL() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public int getType() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public long getPrecision() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public int getDisplaySize() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public String getString() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public Object getObject() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public void set(PreparedStatement preparedStatement, int i) throws SQLException { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override protected int compareSecure(Value val, CompareMode compareMode) { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - throw new IllegalStateException(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Cursor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Cursor.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Cursor.java deleted file mode 100644 index 46c3581..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Cursor.java +++ /dev/null @@ -1,62 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.h2.index.*; -import org.h2.message.*; -import org.h2.result.*; - -import java.util.*; - -/** - * H2 Cursor implementation. - */ -public class GridH2Cursor implements Cursor { - /** */ - private Iterator iter; - - /** */ - private Row row; - - /** - * Constructor. - * - * @param iter Rows iterator. - */ - public GridH2Cursor(Iterator iter) { - this.iter = iter; - } - - /** {@inheritDoc} */ - @Override public Row get() { - return row; - } - - /** {@inheritDoc} */ - @Override public SearchRow getSearchRow() { - return get(); - } - - /** {@inheritDoc} */ - @Override public boolean next() { - row = null; - - if (iter.hasNext()) - row = iter.next(); - - return row != null; - } - - /** {@inheritDoc} */ - @Override public boolean previous() { - // Should never be called. - throw DbException.getUnsupportedException("previous"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2IndexBase.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2IndexBase.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2IndexBase.java deleted file mode 100644 index af07538..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2IndexBase.java +++ /dev/null @@ -1,204 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.apache.ignite.lang.*; -import org.apache.ignite.spi.indexing.*; -import org.gridgain.grid.*; -import org.gridgain.grid.util.lang.*; -import org.gridgain.grid.util.typedef.internal.*; -import org.h2.engine.*; -import org.h2.index.*; -import org.h2.message.*; -import org.h2.result.*; -import org.jetbrains.annotations.*; - -import java.util.*; - -/** - * Index base. - */ -public abstract class GridH2IndexBase extends BaseIndex { - /** */ - protected static final ThreadLocal filters = new ThreadLocal<>(); - - /** */ - protected final int keyCol; - - /** */ - protected final int valCol; - - /** - * @param keyCol Key column. - * @param valCol Value column. - */ - protected GridH2IndexBase(int keyCol, int valCol) { - this.keyCol = keyCol; - this.valCol = valCol; - } - - /** - * Sets key filters for current thread. - * - * @param fs Filters. - */ - public static void setFiltersForThread(IndexingQueryFilter fs) { - filters.set(fs); - } - - /** - * If the index supports rebuilding it has to creates its own copy. - * - * @return Rebuilt copy. - * @throws InterruptedException If interrupted. - */ - public GridH2IndexBase rebuild() throws InterruptedException { - return this; - } - - /** - * Put row if absent. - * - * @param row Row. - * @return Existing row or {@code null}. - */ - public abstract GridH2Row put(GridH2Row row); - - /** - * Remove row from index. - * - * @param row Row. - * @return Removed row. - */ - public abstract GridH2Row remove(SearchRow row); - - /** - * Takes or sets existing snapshot to be used in current thread. - * - * @param s Optional existing snapshot to use. - * @return Snapshot. - */ - public Object takeSnapshot(@Nullable Object s) { - return s; - } - - /** - * Releases snapshot for current thread. - */ - public void releaseSnapshot() { - // No-op. - } - - /** - * Filters rows from expired ones and using predicate. - * - * @param iter Iterator over rows. - * @return Filtered iterator. - */ - protected Iterator filter(Iterator iter) { - IgniteBiPredicate p = null; - - IndexingQueryFilter f = filters.get(); - - if (f != null) { - String spaceName = ((GridH2Table)getTable()).spaceName(); - - try { - p = f.forSpace(spaceName); - } - catch (GridException e) { - throw new GridRuntimeException(e); - } - } - - return new FilteringIterator(iter, U.currentTimeMillis(), p); - } - - /** {@inheritDoc} */ - @Override public long getDiskSpaceUsed() { - return 0; - } - - /** {@inheritDoc} */ - @Override public void checkRename() { - throw DbException.getUnsupportedException("rename"); - } - - /** {@inheritDoc} */ - @Override public void add(Session ses, Row row) { - throw DbException.getUnsupportedException("add"); - } - - /** {@inheritDoc} */ - @Override public void remove(Session ses, Row row) { - throw DbException.getUnsupportedException("remove row"); - } - - /** {@inheritDoc} */ - @Override public void remove(Session ses) { - throw DbException.getUnsupportedException("remove index"); - } - - /** {@inheritDoc} */ - @Override public void truncate(Session ses) { - throw DbException.getUnsupportedException("truncate"); - } - - /** {@inheritDoc} */ - @Override public boolean needRebuild() { - return false; - } - - /** - * Iterator which filters by expiration time and predicate. - */ - protected class FilteringIterator extends GridFilteredIterator { - /** */ - private final IgniteBiPredicate fltr; - - /** */ - private final long time; - - /** - * @param iter Iterator. - * @param time Time for expired rows filtering. - */ - protected FilteringIterator(Iterator iter, long time, - IgniteBiPredicate fltr) { - super(iter); - - this.time = time; - this.fltr = fltr; - } - - /** - * @param row Row. - * @return If this row was accepted. - */ - @SuppressWarnings("unchecked") - @Override protected boolean accept(GridH2Row row) { - if (row instanceof GridH2AbstractKeyValueRow) { - if (((GridH2AbstractKeyValueRow) row).expirationTime() <= time) - return false; - } - - if (fltr == null) - return true; - - Object key = row.getValue(keyCol).getObject(); - Object val = row.getValue(valCol).getObject(); - - assert key != null; - assert val != null; - - return fltr.apply(key, val); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOffheap.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOffheap.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOffheap.java deleted file mode 100644 index ec6127b..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOffheap.java +++ /dev/null @@ -1,346 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.apache.ignite.spi.*; -import org.gridgain.grid.*; -import org.gridgain.grid.util.*; -import org.gridgain.grid.util.offheap.unsafe.*; -import org.h2.store.*; -import org.h2.value.*; -import org.jetbrains.annotations.*; - -import java.util.concurrent.locks.*; - -/** - * Offheap row. - */ -public class GridH2KeyValueRowOffheap extends GridH2AbstractKeyValueRow { - /** */ - private static final GridStripedLock lock; - - /** - * Init locks. - */ - static { - int cpus = Runtime.getRuntime().availableProcessors(); - - lock = new GridStripedLock(cpus * cpus * 8); - } - - /** */ - private static final int OFFSET_KEY_SIZE = 4; // 4 after ref cnt int - - /** */ - private static final int OFFSET_VALUE_REF = OFFSET_KEY_SIZE + 4; // 8 - - /** */ - private static final int OFFSET_EXPIRATION = OFFSET_VALUE_REF + 8; // 16 - - /** */ - private static final int OFFSET_KEY = OFFSET_EXPIRATION + 8; // 24 - - /** */ - private static final int OFFSET_VALUE = 4; // 4 on separate page after val size int - - /** */ - private static final Data SIZE_CALCULATOR = Data.create(null, null); - - /** */ - @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") - private long ptr; - - /** - * @param desc Row descriptor. - * @param ptr Pointer. - */ - public GridH2KeyValueRowOffheap(GridH2RowDescriptor desc, long ptr) { - super(desc); - - assert ptr > 0 : ptr; - - this.ptr = ptr; - } - - /** - * Constructor. - * - * @param desc Row descriptor. - * @param key Key. - * @param keyType Key type. - * @param val Value. - * @param valType Value type. - * @param expirationTime Expiration time. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public GridH2KeyValueRowOffheap(GridH2RowDescriptor desc, Object key, int keyType, @Nullable Object val, int valType, - long expirationTime) throws IgniteSpiException { - super(desc, key, keyType, val, valType, expirationTime); - } - - /** {@inheritDoc} */ - @Override public long expirationTime() { - if (expirationTime == 0) { - long p = ptr; - - assert p > 0 : p; - - // We don't need any synchronization or volatility here because we publish via - // volatile write to tree node. - expirationTime = desc.memory().readLong(p + OFFSET_EXPIRATION); - } - - return expirationTime; - } - - /** {@inheritDoc} */ - @Override protected void cache() { - desc.cache(this); - } - - /** - * @param ptr Pointer to get lock for. - * @return Locked lock, must be released in {@code finally} block. - */ - @SuppressWarnings("LockAcquiredButNotSafelyReleased") - private static Lock lock(long ptr) { - assert (ptr & 7) == 0 : ptr; // Unsafe allocated pointers aligned. - - Lock l = lock.getLock(ptr >>> 3); - - l.lock(); - - return l; - } - - /** {@inheritDoc} */ - @SuppressWarnings("LockAcquiredButNotSafelyReleased") - @Override protected Value getOffheapValue(int col) { - GridUnsafeMemory mem = desc.memory(); - - long p = ptr; - - assert p > 0 : p; - - byte[] bytes = null; - - if (col == KEY_COL) { - int size = mem.readInt(p + OFFSET_KEY_SIZE); - - assert size > 0 : size; - - bytes = mem.readBytes(p + OFFSET_KEY, size); - } - else if (col == VAL_COL) { - Lock l = lock(p); - - desc.guard().begin(); - - try { - long valPtr = mem.readLongVolatile(p + OFFSET_VALUE_REF); - - if (valPtr == 0) // Value was evicted. - return null; - - int size = mem.readInt(valPtr); - - assert size > 0 : size; - - bytes = mem.readBytes(valPtr + OFFSET_VALUE, size); - } - finally { - desc.guard().end(); - - l.unlock(); - } - } - else - assert false : col; - - Data data = Data.create(null, bytes); - - return data.readValue(); - } - - /** {@inheritDoc} */ - @Override public long pointer() { - long p = ptr; - - assert p > 0: p; - - return p; - } - - /** {@inheritDoc} */ - @Override public synchronized void onSwap() throws GridException { - Lock l = lock(ptr); - - try { - final long p = ptr + OFFSET_VALUE_REF; - - final GridUnsafeMemory mem = desc.memory(); - - final long valPtr = mem.readLongVolatile(p); - - assert valPtr > 0: valPtr; - - desc.guard().finalizeLater(new Runnable() { - @Override public void run() { - mem.casLong(p, valPtr, 0); // If it was unswapped concurrently we will not update. - - mem.release(valPtr, mem.readInt(valPtr) + OFFSET_VALUE); - } - }); - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod") - @Override protected Value updateWeakValue(Value exp, Value upd) { - return exp; - } - - /** {@inheritDoc} */ - @Override public synchronized void onUnswap(Object val) throws GridException { - super.onUnswap(val); - - Value v = getValue(VAL_COL); - - byte[] bytes = new byte[SIZE_CALCULATOR.getValueLen(v)]; - - Data data = Data.create(null, bytes); - - data.writeValue(v); - - long p = ptr; - - assert p > 0 : p; - - Lock l = lock(p); - - try { - GridUnsafeMemory mem = desc.memory(); - - long valPtr = mem.allocate(bytes.length + OFFSET_VALUE); - - mem.writeInt(valPtr, bytes.length); - mem.writeBytes(valPtr + OFFSET_VALUE, bytes); - - mem.writeLongVolatile(p + OFFSET_VALUE_REF, valPtr); - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @Override protected synchronized Value syncValue() { - Value v = super.syncValue(); - - if (v != null) - return v; - - return getOffheapValue(VAL_COL); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"NonPrivateFieldAccessedInSynchronizedContext"}) - @Override public void incrementRefCount() { - long p = ptr; - - GridUnsafeMemory mem = desc.memory(); - - if (p == 0) { // Serialize data to offheap memory. - Value key = getValue(KEY_COL); - Value val = getValue(VAL_COL); - - assert key != null; - assert val != null; - - Data data = Data.create(null, new byte[SIZE_CALCULATOR.getValueLen(key)]); - - data.writeValue(key); - - int keySize = data.length(); - - p = mem.allocate(keySize + OFFSET_KEY); - - // We don't need any synchronization or volatility here because we publish via - // volatile write to tree node. - mem.writeInt(p, 1); - mem.writeLong(p + OFFSET_EXPIRATION, expirationTime); - mem.writeInt(p + OFFSET_KEY_SIZE, keySize); - mem.writeBytes(p + OFFSET_KEY, data.getBytes(), 0, keySize); - - data = Data.create(null, new byte[SIZE_CALCULATOR.getValueLen(val)]); - - data.writeValue(val); - - int valSize = data.length(); - - long valPtr = mem.allocate(valSize + OFFSET_VALUE); - - mem.writeInt(valPtr, valSize); - mem.writeBytes(valPtr + OFFSET_VALUE, data.getBytes(), 0, valSize); - - mem.writeLongVolatile(p + OFFSET_VALUE_REF, valPtr); - - ptr = p; - - desc.cache(this); - } - else { - for (;;) { - int cnt = mem.readIntVolatile(p); - - assert cnt > 0 : cnt; - - if (mem.casInt(p, cnt, cnt + 1)) - break; - } - } - } - - /** {@inheritDoc} */ - @Override public void decrementRefCount() { - long p = ptr; - - assert p > 0 : p; - - GridUnsafeMemory mem = desc.memory(); - - for (;;) { - int cnt = mem.readIntVolatile(p); - - assert cnt > 0 : cnt; - - if (cnt == 1) - break; - - if (mem.casInt(p, cnt, cnt - 1)) - return; - } - - desc.uncache(p); - - // Deallocate off-heap memory. - long valPtr = mem.readLongVolatile(p + OFFSET_VALUE_REF); - - assert valPtr >= 0 : valPtr; - - if (valPtr != 0) - mem.release(valPtr, mem.readInt(valPtr) + OFFSET_VALUE); - - mem.release(p, mem.readInt(p + OFFSET_KEY_SIZE) + OFFSET_KEY); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOnheap.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOnheap.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOnheap.java deleted file mode 100644 index 670a97f..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2KeyValueRowOnheap.java +++ /dev/null @@ -1,46 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - - -import org.apache.ignite.spi.IgniteSpiException; -import org.h2.value.Value; -import org.jetbrains.annotations.Nullable; - -/** - * Onheap row. - */ -public class GridH2KeyValueRowOnheap extends GridH2AbstractKeyValueRow { - /** - * Constructor. - * - * @param desc Row descriptor. - * @param key Key. - * @param keyType Key type. - * @param val Value. - * @param valType Value type. - * @param expirationTime Expiration time. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public GridH2KeyValueRowOnheap(GridH2RowDescriptor desc, Object key, int keyType, @Nullable Object val, int valType, - long expirationTime) throws IgniteSpiException { - super(desc, key, keyType, val, valType, expirationTime); - } - - /** {@inheritDoc} */ - @Override protected void cache() { - // No-op. - } - - /** {@inheritDoc} */ - @Override protected Value getOffheapValue(int col) { - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Row.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Row.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Row.java deleted file mode 100644 index a326b62..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Row.java +++ /dev/null @@ -1,40 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.h2.result.*; -import org.h2.value.*; - -/** - * Row with locking support needed for unique key conflicts resolution. - */ -public class GridH2Row extends Row implements GridSearchRowPointer { - /** - * @param data Column values. - */ - public GridH2Row(Value... data) { - super(data, MEMORY_CALCULATE); - } - - /** {@inheritDoc} */ - @Override public long pointer() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public void incrementRefCount() { - throw new IllegalStateException(); - } - - /** {@inheritDoc} */ - @Override public void decrementRefCount() { - throw new IllegalStateException(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2RowDescriptor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2RowDescriptor.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2RowDescriptor.java deleted file mode 100644 index d7c9aa0..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2RowDescriptor.java +++ /dev/null @@ -1,103 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import org.apache.ignite.spi.*; -import org.gridgain.grid.*; -import org.gridgain.grid.spi.indexing.h2.*; -import org.gridgain.grid.util.offheap.unsafe.*; -import org.jetbrains.annotations.*; - -/** - * Row descriptor. - */ -public interface GridH2RowDescriptor extends GridOffHeapSmartPointerFactory { - /** - * @return SPI. - */ - public GridH2IndexingSpi spi(); - - /** - * Creates new row. - * - * @param key Key. - * @param val Value. - * @param expirationTime Expiration time in millis. - * @return Row. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public GridH2AbstractKeyValueRow createRow(Object key, @Nullable Object val, long expirationTime) - throws IgniteSpiException; - - /** - * @param key Cache key. - * @return Value. - * @throws GridException If failed. - */ - public Object readFromSwap(Object key) throws GridException; - - /** - * @return Value type. - */ - public int valueType(); - - /** - * @return {@code true} If we need to store {@code toString()} of value. - */ - public boolean valueToString(); - - /** - * @return Total fields count. - */ - public int fieldsCount(); - - /** - * Gets value type for column index. - * - * @param col Column index. - * @return Value type. - */ - public int fieldType(int col); - - /** - * Gets column value by column index. - * - * @param obj Object to extract value from. - * @param col Column index. - * @return Column value. - */ - public Object columnValue(Object obj, int col); - - /** - * @param col Column index. - * @return {@code True} if column relates to key, false if it relates to value. - */ - public boolean isKeyColumn(int col); - - /** - * @return Unsafe memory. - */ - public GridUnsafeMemory memory(); - - /** - * @param row Deserialized offheap row to cache in heap. - */ - public void cache(GridH2KeyValueRowOffheap row); - - /** - * @param ptr Offheap pointer to remove from cache. - */ - public void uncache(long ptr); - - /** - * @return Guard. - */ - public GridUnsafeGuard guard(); -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2SpatialIndex.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2SpatialIndex.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2SpatialIndex.java deleted file mode 100644 index bcd70e7..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2SpatialIndex.java +++ /dev/null @@ -1,318 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - -import com.vividsolutions.jts.geom.*; -import org.h2.engine.*; -import org.h2.index.*; -import org.h2.index.Cursor; -import org.h2.message.*; -import org.h2.mvstore.*; -import org.h2.mvstore.rtree.*; -import org.h2.result.*; -import org.h2.table.*; -import org.h2.value.*; - -import java.util.*; -import java.util.concurrent.locks.*; - -/** - * Spatial index. - */ -public class GridH2SpatialIndex extends GridH2IndexBase implements SpatialIndex { - /** */ - private final ReadWriteLock lock = new ReentrantReadWriteLock(); - - /** */ - private volatile long rowCnt; - - /** */ - private long rowIds; - - /** */ - private boolean closed; - - /** */ - private final MVRTreeMap treeMap; - - /** */ - private final Map idToRow = new HashMap<>(); - - /** */ - private final Map keyToId = new HashMap<>(); - - /** */ - private final MVStore store; - - /** - * @param tbl Table. - * @param idxName Index name. - * @param cols Columns. - * @param keyCol Key column. - * @param valCol Value column. - */ - public GridH2SpatialIndex(Table tbl, String idxName, IndexColumn[] cols, int keyCol, int valCol) { - super(keyCol, valCol); - - if (cols.length > 1) - throw DbException.getUnsupportedException("can only do one column"); - - if ((cols[0].sortType & SortOrder.DESCENDING) != 0) - throw DbException.getUnsupportedException("cannot do descending"); - - if ((cols[0].sortType & SortOrder.NULLS_FIRST) != 0) - throw DbException.getUnsupportedException("cannot do nulls first"); - - if ((cols[0].sortType & SortOrder.NULLS_LAST) != 0) - throw DbException.getUnsupportedException("cannot do nulls last"); - - initBaseIndex(tbl, 0, idxName, cols, IndexType.createNonUnique(false, false, true)); - - table = tbl; - - if (cols[0].column.getType() != Value.GEOMETRY) { - throw DbException.getUnsupportedException("spatial index on non-geometry column, " + - cols[0].column.getCreateSQL()); - } - - // Index in memory - store = MVStore.open(null); - treeMap = store.openMap("spatialIndex", new MVRTreeMap.Builder()); - } - - /** - * Check closed. - */ - private void checkClosed() { - if (closed) - throw DbException.throwInternalError(); - } - - /** {@inheritDoc} */ - @Override public GridH2Row put(GridH2Row row) { - Lock l = lock.writeLock(); - - l.lock(); - - try { - checkClosed(); - - Value key = row.getValue(keyCol); - - assert key != null; - - Long rowId = keyToId.get(key); - - if (rowId != null) { - Long oldRowId = treeMap.remove(getEnvelope(idToRow.get(rowId), rowId)); - - assert rowId.equals(oldRowId); - } - else { - rowId = ++rowIds; - - keyToId.put(key, rowId); - } - - GridH2Row old = idToRow.put(rowId, row); - - treeMap.put(getEnvelope(row, rowId), rowId); - - if (old == null) - rowCnt++; // No replace. - - return old; - } - finally { - l.unlock(); - } - } - - /** - * @param row Row. - * @param rowId Row id. - * @return Envelope. - */ - private SpatialKey getEnvelope(SearchRow row, long rowId) { - Value v = row.getValue(columnIds[0]); - Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometry(); - Envelope env = g.getEnvelopeInternal(); - return new SpatialKey(rowId, - (float) env.getMinX(), (float) env.getMaxX(), - (float) env.getMinY(), (float) env.getMaxY()); - } - - /** {@inheritDoc} */ - @Override public GridH2Row remove(SearchRow row) { - Lock l = lock.writeLock(); - - l.lock(); - - try { - checkClosed(); - - Value key = row.getValue(keyCol); - - assert key != null; - - Long rowId = keyToId.remove(key); - - assert rowId != null; - - GridH2Row oldRow = idToRow.remove(rowId); - - assert oldRow != null; - - if (!treeMap.remove(getEnvelope(row, rowId), rowId)) - throw DbException.throwInternalError("row not found"); - - rowCnt--; - - return oldRow; - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @Override public void close(Session ses) { - Lock l = lock.writeLock(); - - l.lock(); - - try { - closed = true; - - store.close(); - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @Override protected long getCostRangeIndex(int[] masks, long rowCnt, TableFilter filter, SortOrder sortOrder) { - rowCnt += Constants.COST_ROW_OFFSET; - long cost = rowCnt; - long rows = rowCnt; - - if (masks == null) - return cost; - - for (Column column : columns) { - int idx = column.getColumnId(); - int mask = masks[idx]; - if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) { - cost = 3 + rows / 4; - - break; - } - } - - return cost; - } - - /** {@inheritDoc} */ - @Override public double getCost(Session ses, int[] masks, TableFilter filter, SortOrder sortOrder) { - return getCostRangeIndex(masks, rowCnt, filter, sortOrder); - } - - /** {@inheritDoc} */ - @Override public Cursor find(Session ses, SearchRow first, SearchRow last) { - Lock l = lock.readLock(); - - l.lock(); - - try { - checkClosed(); - - return new GridH2Cursor(rowIterator(treeMap.keySet().iterator())); - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @Override public boolean canGetFirstOrLast() { - return true; - } - - /** - * @param i Spatial key iterator. - * @return Iterator over rows. - */ - private Iterator rowIterator(Iterator i) { - if (!i.hasNext()) - return Collections.emptyIterator(); - - List rows = new ArrayList<>(); - - do { - GridH2Row row = idToRow.get(i.next().getId()); - - assert row != null; - - rows.add(row); - } - while (i.hasNext()); - - return filter(rows.iterator()); - } - - /** {@inheritDoc} */ - @Override public Cursor findFirstOrLast(Session ses, boolean first) { - Lock l = lock.readLock(); - - l.lock(); - - try { - checkClosed(); - - if (!first) - throw DbException.throwInternalError("Spatial Index can only be fetch by ascending order"); - - Iterator iter = rowIterator(treeMap.keySet().iterator()); - - return new SingleRowCursor(iter.hasNext() ? iter.next() : null); - } - finally { - l.unlock(); - } - } - - /** {@inheritDoc} */ - @Override public long getRowCount(Session ses) { - return rowCnt; - } - - /** {@inheritDoc} */ - @Override public long getRowCountApproximation() { - return rowCnt; - } - - /** {@inheritDoc} */ - @Override public Cursor findByGeometry(TableFilter filter, SearchRow intersection) { - Lock l = lock.readLock(); - - l.lock(); - - try { - if (intersection == null) - return find(filter.getSession(), null, null); - - return new GridH2Cursor(rowIterator(treeMap.findIntersectingKeys(getEnvelope(intersection, 0)))); - } - finally { - l.unlock(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/922a2bab/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Table.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Table.java b/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Table.java deleted file mode 100644 index 65a3571..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/spi/indexing/h2/opt/GridH2Table.java +++ /dev/null @@ -1,893 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.spi.indexing.h2.opt; - - -import org.apache.ignite.spi.*; -import org.gridgain.grid.*; -import org.gridgain.grid.util.offheap.unsafe.*; -import org.h2.api.*; -import org.h2.command.ddl.*; -import org.h2.engine.*; -import org.h2.index.*; -import org.h2.message.*; -import org.h2.result.*; -import org.h2.schema.*; -import org.h2.table.*; -import org.jdk8.backport.*; -import org.jetbrains.annotations.*; - -import java.sql.*; -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.locks.*; - -/** - * H2 Table implementation. - */ -public class GridH2Table extends TableBase { - /** */ - private final String spaceName; - - /** */ - private final GridH2RowDescriptor desc; - - /** */ - private final ArrayList idxs; - - /** */ - private final ReadWriteLock lock; - - /** */ - private final Set sessions = Collections.newSetFromMap(new ConcurrentHashMap8()); - - /** */ - private volatile Object[] actualSnapshot; - - /** */ - private final long writeLockWaitTime; - - /** - * Creates table. - * - * @param createTblData Table description. - * @param desc Row descriptor. - * @param idxsFactory Indexes factory. - * @param spaceName Space name. - */ - public GridH2Table(CreateTableData createTblData, @Nullable GridH2RowDescriptor desc, IndexesFactory idxsFactory, - @Nullable String spaceName) { - super(createTblData); - - assert idxsFactory != null; - - this.desc = desc; - this.spaceName = spaceName; - - writeLockWaitTime = desc == null ? 100 : desc.spi().getIndexWriteLockWaitTime(); - - assert writeLockWaitTime > 0 : writeLockWaitTime; - - idxs = idxsFactory.createIndexes(this); - - assert idxs != null; - assert idxs.size() >= 1; - - lock = new ReentrantReadWriteLock(); - - // Add scan index at 0 which is required by H2. - idxs.add(0, new ScanIndex(index(0))); - } - - /** {@inheritDoc} */ - @Override public long getDiskSpaceUsed() { - return 0; - } - - /** - * @return Row descriptor. - */ - public GridH2RowDescriptor rowDescriptor() { - return desc; - } - - /** - * Should be called when entry is swapped. - * - * @param key Entry key. - * @return {@code true} If row was found. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public boolean onSwap(Object key) throws GridException { - return onSwapUnswap(key, null); - } - - /** - * Should be called when entry is unswapped. - * - * @param key Key. - * @param val Value. - * @return {@code true} If row was found. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public boolean onUnswap(Object key, Object val) throws GridException { - assert val != null : "Key=" + key; - - return onSwapUnswap(key, val); - } - - /** - * Swaps or unswaps row. - * - * @param key Key. - * @param val Value for promote or {@code null} if we have to swap. - * @return {@code true} if row was found and swapped/unswapped. - * @throws GridException If failed. - */ - @SuppressWarnings("LockAcquiredButNotSafelyReleased") - private boolean onSwapUnswap(Object key, @Nullable Object val) throws GridException { - assert key != null; - - GridH2TreeIndex pk = pk(); - - GridH2AbstractKeyValueRow row = desc.createRow(key, null, 0); // Create search row. - - GridUnsafeMemory mem = desc.memory(); - - lock.readLock().lock(); - - if (mem != null) - desc.guard().begin(); - - try { - row = pk.findOne(row); - - if (row == null) - return false; - - if (val == null) - row.onSwap(); - else - row.onUnswap(val); - - return true; - } - finally { - lock.readLock().unlock(); - - if (mem != null) - desc.guard().end(); - } - } - - /** - * @return Space name. - */ - @Nullable String spaceName() { - return spaceName; - } - - /** {@inheritDoc} */ - @SuppressWarnings({"LockAcquiredButNotSafelyReleased", "SynchronizationOnLocalVariableOrMethodParameter", "unchecked"}) - @Override public void lock(@Nullable final Session ses, boolean exclusive, boolean force) { - if (ses != null) { - if (!sessions.add(ses)) - return; - - synchronized (ses) { - ses.addLock(this); - } - } - - Object[] snapshot; - - for (long waitTime = writeLockWaitTime;; waitTime *= 2) { // Increase wait time to avoid starvation. - snapshot = actualSnapshot; - - if (snapshot != null) { - // Reuse existing snapshot without locking. - for (int i = 1, len = idxs.size(); i < len; i++) - index(i).takeSnapshot(snapshot[i - 1]); - - return; - } - - try { - if (lock.writeLock().tryLock(waitTime, TimeUnit.MILLISECONDS)) - break; - } - catch (InterruptedException e) { - throw new GridRuntimeException("Thread got interrupted while trying to acquire index lock.", e); - } - } - - boolean snapshoted = false; - - try { - snapshot = actualSnapshot; // Try again inside of the lock. - - if (snapshot == null) { - snapshot = takeIndexesSnapshot(); - - if (desc == null || desc.memory() == null) // This optimization is disabled for off-heap index. - actualSnapshot = snapshot; - - snapshoted = true; - } - } - finally { - lock.writeLock().unlock(); - } - - if (!snapshoted) { - for (int i = 1, len = idxs.size(); i < len; i++) - index(i).takeSnapshot(snapshot[i - 1]); - } - } - - /** - * Must be called inside of write lock because when using multiple indexes we have to ensure that all of them have - * the same contents at snapshot taking time. - * - * @return New indexes data snapshot. - */ - @SuppressWarnings("unchecked") - private Object[] takeIndexesSnapshot() { - int len = idxs.size(); - - Object[] snapshot = new ConcurrentNavigableMap[len - 1]; - - for (int i = 1; i < len; i++) { // Take snapshots on all except first which is scan. - Object s = index(i).takeSnapshot(null); - - snapshot[i - 1] = s; - } - - return snapshot; - } - - /** {@inheritDoc} */ - @Override public void close(Session ses) { - assert !sessions.contains(ses); - } - - /** {@inheritDoc} */ - @Override public void unlock(@Nullable Session ses) { - if (ses != null) { - boolean res = sessions.remove(ses); - - assert res; - } - - for (int i = 1, len = idxs.size(); i < len; i++) // Release snapshots on all except first which is scan. - index(i).releaseSnapshot(); - } - - /** - * Closes table and releases resources. - */ - public void close() { - Lock l = lock.writeLock(); - - l.lock(); - - try { - for (int i = 1, len = idxs.size(); i < len; i++) - index(i).close(null); - } - finally { - l.unlock(); - } - } - - /** - * 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 key Key. - * @param val Value. - * @param expirationTime Expiration time. - * @return {@code True} if operation succeeded. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - public boolean update(Object key, @Nullable Object val, long expirationTime) throws IgniteSpiException { - GridH2Row row = desc.createRow(key, val, expirationTime); - - return doUpdate(row, val == null); - } - - /** - * Gets index by index. - * - * @param idx Index in list. - * @return Index. - */ - private GridH2IndexBase index(int idx) { - return (GridH2IndexBase)idxs.get(idx); - } - - /** - * Gets primary key. - * - * @return Primary key. - */ - private GridH2TreeIndex pk() { - return (GridH2TreeIndex)idxs.get(1); - } - - /** - * For testing only. - * - * @param row Row. - * @param del If given row should be deleted from table. - * @return {@code True} if operation succeeded. - * @throws org.apache.ignite.spi.IgniteSpiException If failed. - */ - @SuppressWarnings("LockAcquiredButNotSafelyReleased") - boolean doUpdate(GridH2Row row, boolean del) throws IgniteSpiException { - // Here we assume that each key can't be updated concurrently and case when different indexes - // getting updated from different threads with different rows with the same key is impossible. - GridUnsafeMemory mem = desc == null ? null : desc.memory(); - - lock.readLock().lock(); - - if (mem != null) - desc.guard().begin(); - - try { - GridH2TreeIndex pk = pk(); - - if (!del) { - GridH2Row old = pk.put(row); // Put to PK. - - int len = idxs.size(); - - int i = 1; - - // Put row if absent to all indexes sequentially. - // Start from 2 because 0 - Scan (don't need to update), 1 - PK (already updated). - while (++i < len) { - GridH2IndexBase idx = index(i); - - assert !idx.getIndexType().isUnique() : "Unique indexes are not supported."; - - GridH2Row old2 = idx.put(row); - - if (old2 != null) { // Row was replaced in index. - if (!eq(pk, old2, old)) - throw new IllegalStateException("Row conflict should never happen, unique indexes are " + - "not supported."); - } - else if (old != null) // Row was not replaced, need to remove manually. - idx.remove(old); - } - } - else { - // index(1) is PK, get full row from there (search row here contains only key but no other columns). - row = pk.remove(row); - - if (row != null) { - // Remove row from all indexes. - // Start from 2 because 0 - Scan (don't need to update), 1 - PK (already updated). - for (int i = 2, len = idxs.size(); i < len; i++) { - Row res = index(i).remove(row); - - assert eq(pk, res, row): "\n" + row + "\n" + res; - } - } - else - return false; - } - - // The snapshot is not actual after update. - actualSnapshot = null; - - return true; - } - finally { - lock.readLock().unlock(); - - if (mem != null) - desc.guard().end(); - } - } - - /** - * Check row equality. - * - * @param pk Primary key index. - * @param r1 First row. - * @param r2 Second row. - * @return {@code true} if rows are the same. - */ - private static boolean eq(Index pk, SearchRow r1, SearchRow r2) { - return r1 == r2 || (r1 != null && r2 != null && pk.compareRows(r1, r2) == 0); - } - - /** - * For testing only. - * - * @return Indexes. - */ - ArrayList indexes() { - ArrayList res = new ArrayList<>(idxs.size() - 1); - - for (int i = 1, len = idxs.size(); i < len ; i++) - res.add(index(i)); - - return res; - } - - /** - * Rebuilds all indexes of this table. - */ - public void rebuildIndexes() { - GridUnsafeMemory memory = desc == null ? null : desc.memory(); - - lock.writeLock().lock(); - - try { - if (memory == null && actualSnapshot == null) - actualSnapshot = takeIndexesSnapshot(); // Allow read access while we are rebuilding indexes. - - for (int i = 1, len = idxs.size(); i < len; i++) { - GridH2IndexBase newIdx = index(i).rebuild(); - - idxs.set(i, newIdx); - - if (i == 1) // ScanIndex at 0 and actualSnapshot can contain references to old indexes, reset them. - idxs.set(0, new ScanIndex(newIdx)); - } - } - catch (InterruptedException ignored) { - // No-op. - } - finally { - lock.writeLock().unlock(); - - actualSnapshot = null; - } - } - - /** {@inheritDoc} */ - @Override public Index addIndex(Session ses, String s, int i, IndexColumn[] idxCols, IndexType idxType, - boolean b, String s1) { - throw DbException.getUnsupportedException("addIndex"); - } - - /** {@inheritDoc} */ - @Override public void removeRow(Session ses, Row row) { - throw DbException.getUnsupportedException("removeRow"); - } - - /** {@inheritDoc} */ - @Override public void truncate(Session ses) { - throw DbException.getUnsupportedException("truncate"); - } - - /** {@inheritDoc} */ - @Override public void addRow(Session ses, Row row) { - throw DbException.getUnsupportedException("addRow"); - } - - /** {@inheritDoc} */ - @Override public void checkSupportAlter() { - throw DbException.getUnsupportedException("alter"); - } - - /** {@inheritDoc} */ - @Override public String getTableType() { - return EXTERNAL_TABLE_ENGINE; - } - - /** {@inheritDoc} */ - @Override public Index getScanIndex(Session ses) { - return getIndexes().get(0); // Scan must be always first index. - } - - /** {@inheritDoc} */ - @Override public Index getUniqueIndex() { - return getIndexes().get(1); // PK index is always second. - } - - /** {@inheritDoc} */ - @Override public ArrayList getIndexes() { - return idxs; - } - - /** {@inheritDoc} */ - @Override public boolean isLockedExclusively() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isLockedExclusivelyBy(Session ses) { - return false; - } - - /** {@inheritDoc} */ - @Override public long getMaxDataModificationId() { - return 0; - } - - /** {@inheritDoc} */ - @Override public boolean isDeterministic() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean canGetRowCount() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean canDrop() { - return true; - } - - /** {@inheritDoc} */ - @Override public long getRowCount(@Nullable Session ses) { - return getUniqueIndex().getRowCount(ses); - } - - /** {@inheritDoc} */ - @Override public long getRowCountApproximation() { - return getUniqueIndex().getRowCountApproximation(); - } - - /** {@inheritDoc} */ - @Override public void checkRename() { - throw DbException.getUnsupportedException("rename"); - } - - /** - * Creates index column for table. - * - * @param col Column index. - * @param sorting Sorting order {@link SortOrder} - * @return Created index column. - */ - public IndexColumn indexColumn(int col, int sorting) { - IndexColumn res = new IndexColumn(); - - res.column = getColumn(col); - res.columnName = res.column.getName(); - res.sortType = sorting; - - return res; - } - - /** - * H2 Table engine. - */ - @SuppressWarnings({"PublicInnerClass", "FieldAccessedSynchronizedAndUnsynchronized"}) - public static class Engine implements TableEngine { - /** */ - private static GridH2RowDescriptor rowDesc; - - /** */ - private static IndexesFactory idxsFactory; - - /** */ - private static GridH2Table resTbl; - - /** */ - private static String spaceName; - - /** {@inheritDoc} */ - @Override public TableBase createTable(CreateTableData createTblData) { - resTbl = new GridH2Table(createTblData, rowDesc, idxsFactory, spaceName); - - return resTbl; - } - - /** - * Creates table using given connection, DDL clause for given type descriptor and list of indexes. - * - * @param conn Connection. - * @param sql DDL clause. - * @param desc Row descriptor. - * @param factory Indexes factory. - * @param space Space name. - * @throws SQLException If failed. - * @return Created table. - */ - public static synchronized GridH2Table createTable(Connection conn, String sql, - @Nullable GridH2RowDescriptor desc, IndexesFactory factory, String space) - throws SQLException { - rowDesc = desc; - idxsFactory = factory; - spaceName = space; - - try { - try (Statement s = conn.createStatement()) { - s.execute(sql + " engine \"" + Engine.class.getName() + "\""); - } - - return resTbl; - } - finally { - resTbl = null; - idxsFactory = null; - rowDesc = null; - } - } - } - - /** - * Type which can create indexes list for given table. - */ - @SuppressWarnings({"PackageVisibleInnerClass", "PublicInnerClass"}) - public static interface IndexesFactory { - /** - * Create list of indexes. First must be primary key, after that all unique indexes and - * only then non-unique indexes. - * All indexes must be subtypes of {@link GridH2TreeIndex}. - * - * @param tbl Table to create indexes for. - * @return List of indexes. - */ - ArrayList createIndexes(GridH2Table tbl); - } - - /** - * Wrapper type for primary key. - */ - @SuppressWarnings("PackageVisibleInnerClass") - static class ScanIndex implements Index { - /** */ - static final String SCAN_INDEX_NAME_SUFFIX = "__SCAN_"; - - /** */ - private static final IndexType TYPE = IndexType.createScan(false); - - /** */ - private final GridH2IndexBase delegate; - - /** - * Constructor. - * - * @param delegate Index delegate to. - */ - private ScanIndex(GridH2IndexBase delegate) { - this.delegate = delegate; - } - - /** {@inheritDoc} */ - @Override public long getDiskSpaceUsed() { - return 0; - } - - /** {@inheritDoc} */ - @Override public void add(Session ses, Row row) { - delegate.add(ses, row); - } - - /** {@inheritDoc} */ - @Override public boolean canFindNext() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean canGetFirstOrLast() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean canScan() { - return delegate.canScan(); - } - - /** {@inheritDoc} */ - @Override public void close(Session ses) { - delegate.close(ses); - } - - /** {@inheritDoc} */ - @Override public void commit(int operation, Row row) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public int compareRows(SearchRow rowData, SearchRow compare) { - return delegate.compareRows(rowData, compare); - } - - /** {@inheritDoc} */ - @Override public Cursor find(TableFilter filter, SearchRow first, SearchRow last) { - return find(filter.getSession(), first, last); - } - - /** {@inheritDoc} */ - @Override public Cursor find(Session ses, SearchRow first, SearchRow last) { - return delegate.find(ses, null, null); - } - - /** {@inheritDoc} */ - @Override public Cursor findFirstOrLast(Session ses, boolean first) { - throw DbException.getUnsupportedException("SCAN"); - } - - /** {@inheritDoc} */ - @Override public Cursor findNext(Session ses, SearchRow higherThan, SearchRow last) { - throw DbException.throwInternalError(); - } - - /** {@inheritDoc} */ - @Override public int getColumnIndex(Column col) { - return -1; - } - - /** {@inheritDoc} */ - @Override public Column[] getColumns() { - return delegate.getColumns(); - } - - /** {@inheritDoc} */ - @Override public double getCost(Session ses, int[] masks, TableFilter tblFilter, SortOrder sortOrder) { - return getRowCountApproximation() + Constants.COST_ROW_OFFSET; - } - - /** {@inheritDoc} */ - @Override public IndexColumn[] getIndexColumns() { - return delegate.getIndexColumns(); - } - - /** {@inheritDoc} */ - @Override public IndexType getIndexType() { - return TYPE; - } - - /** {@inheritDoc} */ - @Override public String getPlanSQL() { - return delegate.getTable().getSQL() + "." + SCAN_INDEX_NAME_SUFFIX; - } - - /** {@inheritDoc} */ - @Override public Row getRow(Session ses, long key) { - return delegate.getRow(ses, key); - } - - /** {@inheritDoc} */ - @Override public long getRowCount(Session ses) { - return delegate.getRowCount(ses); - } - - /** {@inheritDoc} */ - @Override public long getRowCountApproximation() { - return delegate.getRowCountApproximation(); - } - - /** {@inheritDoc} */ - @Override public Table getTable() { - return delegate.getTable(); - } - - /** {@inheritDoc} */ - @Override public boolean isRowIdIndex() { - return delegate.isRowIdIndex(); - } - - /** {@inheritDoc} */ - @Override public boolean needRebuild() { - return false; - } - - /** {@inheritDoc} */ - @Override public void remove(Session ses) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void remove(Session ses, Row row) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void setSortedInsertMode(boolean sortedInsertMode) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void truncate(Session ses) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public Schema getSchema() { - return delegate.getSchema(); - } - - /** {@inheritDoc} */ - @Override public boolean isHidden() { - return delegate.isHidden(); - } - - /** {@inheritDoc} */ - @Override public void checkRename() { - throw DbException.getUnsupportedException("rename"); - } - - /** {@inheritDoc} */ - @Override public ArrayList getChildren() { - return delegate.getChildren(); - } - - /** {@inheritDoc} */ - @Override public String getComment() { - return delegate.getComment(); - } - - /** {@inheritDoc} */ - @Override public String getCreateSQL() { - return null; // Scan should return null. - } - - /** {@inheritDoc} */ - @Override public String getCreateSQLForCopy(Table tbl, String quotedName) { - return delegate.getCreateSQLForCopy(tbl, quotedName); - } - - /** {@inheritDoc} */ - @Override public Database getDatabase() { - return delegate.getDatabase(); - } - - /** {@inheritDoc} */ - @Override public String getDropSQL() { - return delegate.getDropSQL(); - } - - /** {@inheritDoc} */ - @Override public int getId() { - return delegate.getId(); - } - - /** {@inheritDoc} */ - @Override public String getName() { - return delegate.getName() + SCAN_INDEX_NAME_SUFFIX; - } - - /** {@inheritDoc} */ - @Override public String getSQL() { - return delegate.getSQL(); - } - - /** {@inheritDoc} */ - @Override public int getType() { - return delegate.getType(); - } - - /** {@inheritDoc} */ - @Override public boolean isTemporary() { - return delegate.isTemporary(); - } - - /** {@inheritDoc} */ - @Override public void removeChildrenAndResources(Session ses) { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void rename(String newName) { - throw DbException.getUnsupportedException("rename"); - } - - /** {@inheritDoc} */ - @Override public void setComment(String comment) { - throw DbException.getUnsupportedException("comment"); - } - - /** {@inheritDoc} */ - @Override public void setTemporary(boolean temporary) { - throw DbException.getUnsupportedException("temporary"); - } - } -}