Author: shawkins
Date: 2010-08-06 14:42:16 -0400 (Fri, 06 Aug 2010)
New Revision: 2425
Modified:
trunk/engine/src/main/java/org/teiid/common/buffer/STree.java
trunk/engine/src/main/java/org/teiid/common/buffer/TupleBrowser.java
trunk/engine/src/main/java/org/teiid/query/tempdata/TempTable.java
trunk/engine/src/main/java/org/teiid/query/tempdata/TempTableDataManager.java
trunk/engine/src/test/java/org/teiid/common/buffer/TestSTree.java
Log:
TEIID-168 updating mat view logic to allow for an ordered insert, a bulk insert is still
pending
Modified: trunk/engine/src/main/java/org/teiid/common/buffer/STree.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/common/buffer/STree.java 2010-08-06 18:15:20 UTC
(rev 2424)
+++ trunk/engine/src/main/java/org/teiid/common/buffer/STree.java 2010-08-06 18:42:16 UTC
(rev 2425)
@@ -41,6 +41,8 @@
*/
@SuppressWarnings("unchecked")
public class STree {
+
+ public enum InsertMode {ORDERED, NEW, UPDATE}
private static final Random seedGenerator = new Random();
@@ -73,7 +75,7 @@
this.leafManager = leafManager;
this.comparator = comparator;
this.pageSize = Math.max(pageSize, SPage.MIN_PERSISTENT_SIZE);
- pageSize >>>= 4;
+ pageSize >>>= 3;
while (pageSize > 0) {
pageSize >>>= 1;
shift++;
@@ -84,6 +86,23 @@
this.types = types;
this.keytypes = Arrays.copyOf(types, keyLength);
}
+
+ protected SPage findChildTail(SPage page) {
+ if (page == null) {
+ page = header[header.length - 1];
+ while (page.next != null) {
+ page = page.next;
+ }
+ return page;
+ }
+ if (page.children != null) {
+ page = page.children.get(page.children.size() - 1);
+ while (page.next != null) {
+ page = page.next;
+ }
+ }
+ return page;
+ }
/**
* Determine a new random level using an XOR rng.
@@ -149,18 +168,29 @@
return null;
}
- public List insert(List tuple, boolean replace) throws TeiidComponentException {
+ public List insert(List tuple, InsertMode mode) throws TeiidComponentException {
LinkedList<SearchResult> places = new LinkedList<SearchResult>();
- List match = find(tuple, places);
- if (match != null) {
- if (!replace) {
+ List match = null;
+ if (mode == InsertMode.ORDERED) {
+ SPage last = null;
+ while (last == null || last.children != null) {
+ last = findChildTail(last);
+ //TODO: do this lazily
+ TupleBatch batch = last.getValues();
+ places.add(new SearchResult(-batch.getTuples().size() -1, last, batch));
+ }
+ } else {
+ match = find(tuple, places);
+ if (match != null) {
+ if (mode != InsertMode.UPDATE) {
+ return match;
+ }
+ SearchResult last = places.getLast();
+ SPage page = last.page;
+ last.values.getTuples().set(last.index, tuple);
+ page.setValues(last.values);
return match;
}
- SearchResult last = places.getLast();
- SPage page = last.page;
- last.values.getTuples().set(last.index, tuple);
- page.setValues(last.values);
- return match;
}
List key = extractKey(tuple);
int level = randomLevel();
Modified: trunk/engine/src/main/java/org/teiid/common/buffer/TupleBrowser.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/common/buffer/TupleBrowser.java 2010-08-06
18:15:20 UTC (rev 2424)
+++ trunk/engine/src/main/java/org/teiid/common/buffer/TupleBrowser.java 2010-08-06
18:42:16 UTC (rev 2425)
@@ -97,9 +97,8 @@
values = upper.values;
}
} else {
- bound = tree.header[0];
- while (bound.next != null) {
- bound = bound.next;
+ while (bound == null || bound.children != null) {
+ bound = tree.findChildTail(bound);
}
}
Modified: trunk/engine/src/main/java/org/teiid/query/tempdata/TempTable.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/query/tempdata/TempTable.java 2010-08-06 18:15:20
UTC (rev 2424)
+++ trunk/engine/src/main/java/org/teiid/query/tempdata/TempTable.java 2010-08-06 18:42:16
UTC (rev 2425)
@@ -40,6 +40,7 @@
import org.teiid.common.buffer.TupleSource;
import org.teiid.common.buffer.BufferManager.BufferReserveMode;
import org.teiid.common.buffer.BufferManager.TupleSourceType;
+import org.teiid.common.buffer.STree.InsertMode;
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidException;
import org.teiid.core.TeiidProcessingException;
@@ -102,7 +103,7 @@
tuple.add(0, rowId.getAndIncrement());
}
currentTuple = tuple;
- insertTuple(tuple);
+ insertTuple(tuple, addRowId);
}
@Override
@@ -256,6 +257,7 @@
private String sessionID;
private TempMetadataID tid;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+ private boolean updatable = true;
private int keyBatchSize;
private int leafBatchSize;
@@ -329,23 +331,30 @@
TupleBrowser browser = createTupleBrower(condition, direction);
TupleSource ts = new QueryTupleSource(browser, map, projectedCols, condition);
+
+ boolean usingQueryTupleSource = false;
try {
TupleBuffer tb = null;
if (!orderByUsingIndex && orderBy != null) {
SortUtility sort = new SortUtility(ts, orderBy.getOrderByItems(), Mode.SORT, bm,
sessionID, projectedCols);
tb = sort.sort();
- } else {
+ } else if (!updatable) {
tb = bm.createTupleBuffer(projectedCols, sessionID, TupleSourceType.PROCESSOR);
- List next = null;
+ List<?> next = null;
while ((next = ts.nextTuple()) != null) {
tb.addTuple(next);
}
+ } else {
+ usingQueryTupleSource = true;
+ return ts;
}
tb.close();
return tb.createIndexedTupleSource(true);
} finally {
- //ensure the buffers get released
- ts.closeSource();
+ if (!usingQueryTupleSource) {
+ //ensure the buffers get released
+ ts.closeSource();
+ }
}
}
@@ -448,7 +457,7 @@
@Override
protected void undo(List tuple) throws TeiidComponentException,
TeiidProcessingException {
if (primaryKeyChangePossible) {
- insertTuple(tuple);
+ insertTuple(tuple, false);
} else {
updateTuple(tuple);
}
@@ -506,7 +515,7 @@
@Override
protected void undo(List tuple) throws TeiidComponentException,
TeiidProcessingException {
- insertTuple(tuple);
+ insertTuple(tuple, false);
}
};
int updateCount = up.process();
@@ -514,8 +523,8 @@
return CollectionTupleSource.createUpdateCountTupleSource(updateCount);
}
- private void insertTuple(List<Object> list) throws TeiidComponentException,
TeiidProcessingException {
- if (tree.insert(list, false) != null) {
+ private void insertTuple(List<Object> list, boolean ordered) throws
TeiidComponentException, TeiidProcessingException {
+ if (tree.insert(list, ordered?InsertMode.ORDERED:InsertMode.NEW) != null) {
throw new
TeiidProcessingException(QueryPlugin.Util.getString("TempTable.duplicate_key"));
//$NON-NLS-1$
}
}
@@ -527,7 +536,7 @@
}
private void updateTuple(List<?> tuple) throws TeiidComponentException {
- if (tree.insert(tuple, true) == null) {
+ if (tree.insert(tuple, InsertMode.UPDATE) == null) {
throw new AssertionError("Update failed"); //$NON-NLS-1$
}
}
@@ -535,5 +544,9 @@
void setPreferMemory(boolean preferMemory) {
this.tree.setPreferMemory(preferMemory);
}
+
+ void setUpdatable(boolean updatable) {
+ this.updatable = updatable;
+ }
}
\ No newline at end of file
Modified: trunk/engine/src/main/java/org/teiid/query/tempdata/TempTableDataManager.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/tempdata/TempTableDataManager.java 2010-08-06
18:15:20 UTC (rev 2424)
+++
trunk/engine/src/main/java/org/teiid/query/tempdata/TempTableDataManager.java 2010-08-06
18:42:16 UTC (rev 2425)
@@ -189,6 +189,7 @@
}
}
table = tts.addTempTable(tableName, create, bufferManager);
+ table.setUpdatable(false);
table.setPreferMemory(tableName.startsWith(CODE_PREFIX));
boolean success = false;
try {
Modified: trunk/engine/src/test/java/org/teiid/common/buffer/TestSTree.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/common/buffer/TestSTree.java 2010-08-06 18:15:20
UTC (rev 2424)
+++ trunk/engine/src/test/java/org/teiid/common/buffer/TestSTree.java 2010-08-06 18:42:16
UTC (rev 2425)
@@ -28,6 +28,7 @@
import java.util.List;
import org.junit.Test;
+import org.teiid.common.buffer.STree.InsertMode;
import org.teiid.core.TeiidComponentException;
import org.teiid.query.sql.symbol.ElementSymbol;
@@ -44,7 +45,7 @@
STree map = bm.createSTree(elements, "1", 1);
for (int i = 20000; i > 0; i--) {
- assertNull(map.insert(Arrays.asList(i, String.valueOf(i)), true));
+ assertNull(map.insert(Arrays.asList(i, String.valueOf(i)), InsertMode.NEW));
assertEquals(20000 - i + 1, map.getRowCount());
}
@@ -53,7 +54,7 @@
}
assertEquals(0, map.getRowCount());
- assertNull(map.insert(Arrays.asList(1, String.valueOf(1)), true));
+ assertNull(map.insert(Arrays.asList(1, String.valueOf(1)), InsertMode.NEW));
}
}