[teiid-commits] teiid SVN: r2408 - in trunk: build/kits/jboss-container/deployers/teiid.deployer and 6 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Tue Aug 3 19:36:59 EDT 2010


Author: rareddy
Date: 2010-08-03 19:36:58 -0400 (Tue, 03 Aug 2010)
New Revision: 2408

Added:
   trunk/engine/src/main/java/org/teiid/cache/Cachable.java
   trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestCachedResults.java
   trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestSessionAwareCache.java
Modified:
   trunk/build/kits/jboss-container/deploy/teiid/teiid-jboss-beans.xml
   trunk/build/kits/jboss-container/deployers/teiid.deployer/teiid-deployer-jboss-beans.xml
   trunk/cache-jbosscache/src/main/java/org/teiid/cache/jboss/ClusterableCacheFactory.java
   trunk/engine/src/main/java/org/teiid/common/buffer/TupleBatch.java
   trunk/engine/src/main/java/org/teiid/common/buffer/TupleBuffer.java
   trunk/engine/src/main/java/org/teiid/dqp/internal/process/CachedResults.java
   trunk/engine/src/main/java/org/teiid/dqp/internal/process/DQPCore.java
   trunk/engine/src/main/java/org/teiid/dqp/internal/process/SessionAwareCache.java
   trunk/engine/src/main/resources/org/teiid/dqp/i18n.properties
   trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDQPCore.java
   trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDataTierManager.java
Log:
TEIID-1104: adding caching hook into JBoss Cache. If the Teiid server is started in the clustered mode and "cache manager" is available, then results set cache will use JBoss cache. 

Modified: trunk/build/kits/jboss-container/deploy/teiid/teiid-jboss-beans.xml
===================================================================
--- trunk/build/kits/jboss-container/deploy/teiid/teiid-jboss-beans.xml	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/build/kits/jboss-container/deploy/teiid/teiid-jboss-beans.xml	2010-08-03 23:36:58 UTC (rev 2408)
@@ -45,6 +45,7 @@
     </bean>
     
     <bean name="CacheFactory" class="org.teiid.cache.jboss.ClusterableCacheFactory">
+        <property name="enabled">true</property>
         <property name="clusteredCacheName">mvcc-shared</property>
     </bean>
     

Modified: trunk/build/kits/jboss-container/deployers/teiid.deployer/teiid-deployer-jboss-beans.xml
===================================================================
--- trunk/build/kits/jboss-container/deployers/teiid.deployer/teiid-deployer-jboss-beans.xml	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/build/kits/jboss-container/deployers/teiid.deployer/teiid-deployer-jboss-beans.xml	2010-08-03 23:36:58 UTC (rev 2408)
@@ -49,6 +49,7 @@
         <property name="connectorManagerRepository"><inject bean="ConnectorManagerRepository"/></property>  
         <property name="translatorRepository"><inject bean="translatorRepository"/></property>      
         <property name="containerLifeCycleListener"><inject bean="JBossLifeCycleListener"/></property>
+        <property name="threadPool"><inject bean="jboss.system:service=ThreadPool"/></property>
         <depends>SystemVDBDeployer</depends>
     </bean>       
     

Modified: trunk/cache-jbosscache/src/main/java/org/teiid/cache/jboss/ClusterableCacheFactory.java
===================================================================
--- trunk/cache-jbosscache/src/main/java/org/teiid/cache/jboss/ClusterableCacheFactory.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/cache-jbosscache/src/main/java/org/teiid/cache/jboss/ClusterableCacheFactory.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -38,6 +38,7 @@
 	private static final long serialVersionUID = -1992994494154581234L;
 	private CacheFactory delegate;
 	private String cacheName;
+	private boolean enabled = false;
 	
 	@Override
 	public <K, V> Cache<K, V> get(Type type, CacheConfiguration config) {
@@ -67,11 +68,18 @@
 	}
 	
 	private Object getClusteredCache() {
-		try {
-			Context ctx = new InitialContext();
-			return ctx.lookup("java:CacheManager"); //$NON-NLS-1$
-		} catch (NamingException e) {
-			return null;
-		}	
+		if (this.enabled) {
+			try {
+				Context ctx = new InitialContext();
+				return ctx.lookup("java:CacheManager"); //$NON-NLS-1$
+			} catch (NamingException e) {
+				return null;
+			}
+		}
+		return null;
 	}
+	
+	public void setEnabled(boolean value) {
+		this.enabled = value;
+	}
 }

Added: trunk/engine/src/main/java/org/teiid/cache/Cachable.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/cache/Cachable.java	                        (rev 0)
+++ trunk/engine/src/main/java/org/teiid/cache/Cachable.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+package org.teiid.cache;
+
+import org.teiid.common.buffer.BufferManager;
+
+public interface Cachable {
+
+	boolean prepare(Cache cache, BufferManager bufferManager);
+	
+	boolean restore(Cache cache, BufferManager bufferManager);
+}


Property changes on: trunk/engine/src/main/java/org/teiid/cache/Cachable.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/engine/src/main/java/org/teiid/common/buffer/TupleBatch.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/common/buffer/TupleBatch.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/java/org/teiid/common/buffer/TupleBatch.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -52,6 +52,9 @@
     // Optional state
     private boolean terminationFlag = false;
     
+    // for distributed cache purposes
+    private String[] preservedTypes;
+    
     /**
      * Contains ordered data types of each of the columns in the batch. Although it is not serialized,
      * this array is a serialization aid and must be set before serialization and deserialization using
@@ -174,6 +177,10 @@
 
     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
         terminationFlag = in.readBoolean();
+        preservedTypes = (String[])in.readObject();
+        if (types == null) {
+        	types = preservedTypes;
+        }
         tuples = new ArrayList<List>();
         for (List tuple : BatchSerializer.readBatch(in, types)) {
         	tuples.add(tuple);
@@ -181,11 +188,16 @@
     }
     public void writeExternal(ObjectOutput out) throws IOException {
         out.writeBoolean(terminationFlag);
+        out.writeObject(this.preservedTypes);
         BatchSerializer.writeBatch(out, types, getAllTuples());
     }
     
     public void setRowOffset(int rowOffset) {
 		this.rowOffset = rowOffset;
 	}
+    
+    public void preserveTypes() {
+    	this.preservedTypes = types;
+    }
 }
 

Modified: trunk/engine/src/main/java/org/teiid/common/buffer/TupleBuffer.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/common/buffer/TupleBuffer.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/java/org/teiid/common/buffer/TupleBuffer.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -201,11 +201,11 @@
 			Assertion.isNotNull(entry);
 			BatchManager.ManagedBatch batch = entry.getValue();
 	    	result = batch.getBatch(!forwardOnly, types);
-	    	result.setDataTypes(types);
 	    	if (forwardOnly) {
 				batches.remove(entry.getKey());
 			}
 		}
+		result.setDataTypes(types);
 		if (isFinal && result.getEndRow() == rowCount) {
 			result.setTerminationFlag(true);
 		}

Modified: trunk/engine/src/main/java/org/teiid/dqp/internal/process/CachedResults.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/dqp/internal/process/CachedResults.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/java/org/teiid/dqp/internal/process/CachedResults.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -23,18 +23,37 @@
 package org.teiid.dqp.internal.process;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
 
+import org.teiid.cache.Cachable;
+import org.teiid.cache.Cache;
+import org.teiid.common.buffer.BufferManager;
+import org.teiid.common.buffer.TupleBatch;
 import org.teiid.common.buffer.TupleBuffer;
+import org.teiid.common.buffer.BufferManager.TupleSourceType;
+import org.teiid.core.TeiidComponentException;
+import org.teiid.core.util.Assertion;
+import org.teiid.dqp.DQPPlugin;
+import org.teiid.logging.LogConstants;
+import org.teiid.logging.LogManager;
 import org.teiid.query.analysis.AnalysisRecord;
 import org.teiid.query.sql.lang.Command;
 
 
-public class CachedResults implements Serializable {
+public class CachedResults implements Serializable, Cachable {
 	private static final long serialVersionUID = -5603182134635082207L;
+	
 	private Command command;
 	private AnalysisRecord analysisRecord;
-	private TupleBuffer results;
+	private transient TupleBuffer results;
 	
+	private List<?> schema;
+	private int batchSize;
+	
+	protected ArrayList<UUID> cachedBatches = new ArrayList<UUID>();
+	
 	public AnalysisRecord getAnalysisRecord() {
 		return analysisRecord;
 	}
@@ -49,6 +68,8 @@
 	
 	public void setResults(TupleBuffer results) {
 		this.results = results;
+		this.schema = results.getSchema();
+		this.batchSize = results.getBatchSize();
 	}
 	
 	public void setCommand(Command command) {
@@ -58,5 +79,44 @@
 	public Command getCommand() {
 		return command;
 	}
-		
+
+	@Override
+	public boolean prepare(Cache cache, BufferManager bufferManager) {
+		Assertion.assertTrue(!this.results.isForwardOnly());
+		try {
+			for (int row = 1; row <= this.results.getRowCount(); row+=this.results.getBatchSize()) {
+				TupleBatch batch = results.getBatch(row);
+				UUID uuid = java.util.UUID.randomUUID();
+				batch.preserveTypes();
+				cache.put(uuid, batch);
+				this.cachedBatches.add(uuid);
+			}
+			return true;
+		} catch (TeiidComponentException e) {
+			LogManager.logDetail(LogConstants.CTX_DQP, DQPPlugin.Util.getString("failed_to_put_in_cache")); //$NON-NLS-1$
+		}
+		return false;
+	}
+
+	@Override
+	public synchronized boolean restore(Cache cache, BufferManager bufferManager) {
+		try {
+			if (this.results == null) {
+				TupleBuffer buffer = bufferManager.createTupleBuffer(this.schema, "cached", TupleSourceType.FINAL); //$NON-NLS-1$
+				buffer.setBatchSize(this.batchSize);
+	
+				for (UUID uuid : this.cachedBatches) {
+					TupleBatch batch =  (TupleBatch)cache.get(uuid);
+					if (batch != null) {					
+						buffer.addTupleBatch(batch, true);
+					}
+				}
+				this.results = buffer;				
+			}
+			return true;
+		} catch (TeiidComponentException e) {
+			LogManager.logDetail(LogConstants.CTX_DQP, DQPPlugin.Util.getString("not_found_cache")); //$NON-NLS-1$
+		}
+		return false;
+	}	
 }

Modified: trunk/engine/src/main/java/org/teiid/dqp/internal/process/DQPCore.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/dqp/internal/process/DQPCore.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/java/org/teiid/dqp/internal/process/DQPCore.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -649,10 +649,12 @@
         //result set cache
         if (config.isResultSetCacheEnabled()) {
 			this.rsCache = new SessionAwareCache<CachedResults>(config.getResultSetCacheMaxEntries(), this.cacheFactory, Cache.Type.RESULTSET);
+			this.rsCache.setBufferManager(this.bufferManager);
         }
 
         //prepared plan cache
         prepPlanCache = new SessionAwareCache<PreparedPlan>(config.getPreparedPlanCacheMaxCount(), this.cacheFactory, Cache.Type.PREPAREDPLAN);
+        prepPlanCache.setBufferManager(this.bufferManager);
 		
         //get buffer manager
         this.bufferManager = bufferService.getBufferManager();

Modified: trunk/engine/src/main/java/org/teiid/dqp/internal/process/SessionAwareCache.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/dqp/internal/process/SessionAwareCache.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/java/org/teiid/dqp/internal/process/SessionAwareCache.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -27,10 +27,13 @@
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.teiid.cache.Cachable;
 import org.teiid.cache.Cache;
+import org.teiid.cache.CacheConfiguration;
 import org.teiid.cache.CacheFactory;
 import org.teiid.cache.DefaultCache;
 import org.teiid.cache.DefaultCacheFactory;
+import org.teiid.common.buffer.BufferManager;
 import org.teiid.core.util.EquivalenceUtil;
 import org.teiid.core.util.HashCodeUtil;
 import org.teiid.query.parser.ParseInfo;
@@ -50,6 +53,8 @@
 	
 	private AtomicInteger cacheHit = new AtomicInteger();
 	
+	private BufferManager bufferManager;
+	
 	SessionAwareCache(){
 		this(DEFAULT_MAX_SIZE_TOTAL, new DefaultCacheFactory(), Cache.Type.RESULTSET);
 	}
@@ -65,27 +70,38 @@
 		this.maxSize = maxSize;
 		this.localCache = new DefaultCache<CacheID, T>("local", maxSize); //$NON-NLS-1$
 		
-		this.distributedCache = localCache;
-		
-//		if (type == Cache.Type.PREPAREDPLAN) {
-//			this.distributedCache = localCache;
-//		}
-//		else {
-//			this.distributedCache = cacheFactory.get(type, new CacheConfiguration(CacheConfiguration.Policy.LRU, -1, SessionAwareCache.this.maxSize));
-//		}
+		if (type == Cache.Type.PREPAREDPLAN) {
+			this.distributedCache = localCache;
+		}
+		else {
+			this.distributedCache = cacheFactory.get(type, new CacheConfiguration(CacheConfiguration.Policy.LRU, -1, SessionAwareCache.this.maxSize));
+		}
 	}	
 	
 	public T get(CacheID id){
+		
 		id.setSessionId(id.originalSessionId);
 		T result = localCache.get(id);
+		
 		if (result == null) {
 			id.setSessionId(null);
+			
+			id.setUserName(id.originalUserName);
 			result = distributedCache.get(id);
+			
 			if (result == null) {
 				id.setUserName(null);
 				result = distributedCache.get(id);
 			}
+			
+			if (result != null && result instanceof Cachable) {
+				Cachable c = (Cachable)result;
+				if (!c.restore(this.distributedCache, this.bufferManager)) {
+					result = null;
+				}
+			}
 		}
+		
 		if (result != null) {
 			cacheHit.getAndIncrement();
 		}
@@ -103,15 +119,32 @@
 		if (!id.cachable) {
 			return;
 		}
+		
 		if (sessionSpecific) {
 			id.setSessionId(id.originalSessionId);
 			this.localCache.put(id, t);
-		} else {
+		} 
+		else {
+			
+			boolean insert = true;
+			
 			id.setSessionId(null);
-			if (!userSpecific) {
+			
+			if (userSpecific) {
+				id.setUserName(id.originalUserName);
+			}
+			else {
 				id.setUserName(null);
 			}
-			this.distributedCache.put(id, t);
+			
+			if (t instanceof Cachable) {
+				Cachable c = (Cachable)t;
+				insert = c.prepare(this.distributedCache, this.bufferManager);
+			}
+			
+			if (insert) {
+				this.distributedCache.put(id, t);
+			}
 		}
 	}
 	
@@ -132,15 +165,16 @@
 		private String sessionId;
 		private String originalSessionId;
 		private List<Serializable> parameters;
-		//private String userName;
-		boolean cachable = true;
+		private String userName;
+		private String originalUserName;
+		private boolean cachable = true;
 				
 		CacheID(DQPWorkContext context, ParseInfo pi, String sql){
 			this.sql = sql;
 			this.vdbInfo = new VDBKey(context.getVdbName(), context.getVdbVersion());
 			this.pi = pi;
 			this.originalSessionId = context.getSessionId();
-			//this.userName = context.getUserName();
+			this.originalUserName = context.getUserName();
 		}
 		
 		private void setSessionId(String sessionId) {
@@ -162,7 +196,7 @@
 		}
 		
 		public void setUserName(String name) {
-			//this.userName = name;
+			this.userName = name;
 		}
 						
 		public boolean equals(Object obj){
@@ -174,19 +208,18 @@
 	        } 
         	CacheID that = (CacheID)obj;
             return this.pi.equals(that.pi) && this.vdbInfo.equals(that.vdbInfo) && this.sql.equals(that.sql) 
-            	//&& EquivalenceUtil.areEqual(this.userName, that.userName)            	
+            	&& EquivalenceUtil.areEqual(this.userName, that.userName)            	
             	&& EquivalenceUtil.areEqual(this.sessionId, that.sessionId)
             	&& EquivalenceUtil.areEqual(this.parameters, that.parameters);
 		}
 		
 	    public int hashCode() {
-	        return HashCodeUtil.hashCode(0, vdbInfo, sql, pi,  sessionId, parameters);
+	        return HashCodeUtil.hashCode(0, vdbInfo, sql, pi, this.userName, sessionId, parameters);
 	    }
 	    
 	    @Override
 	    public String toString() {
-	    	return "Cache Entry<" + originalSessionId + "> params:" + parameters + " sql:" + sql; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-	    			
+	    	return "Cache Entry<" + originalSessionId + "="+ originalUserName + "> params:" + parameters + " sql:" + sql; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 	    }
 	}
 	
@@ -198,4 +231,7 @@
         return maxSize;
     }
     
+    public void setBufferManager(BufferManager bufferManager) {
+    	this.bufferManager = bufferManager;
+    }
 }

Modified: trunk/engine/src/main/resources/org/teiid/dqp/i18n.properties
===================================================================
--- trunk/engine/src/main/resources/org/teiid/dqp/i18n.properties	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/main/resources/org/teiid/dqp/i18n.properties	2010-08-03 23:36:58 UTC (rev 2408)
@@ -521,4 +521,6 @@
 translator_not_found=Translator {0} not accessible.
 datasource_not_found=Data Source {0} not accessible.
 failed_to_bind_translator=Failed to bind the translator {0} on the jndi tree
-failed_to_unbind_translator=Failed to un-bind the translator {0} from the jndi tree.
\ No newline at end of file
+failed_to_unbind_translator=Failed to un-bind the translator {0} from the jndi tree.
+not_found_cache=Results not found in cache
+failed_to_put_in_cache=Failed to put results in the cache
\ No newline at end of file

Added: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestCachedResults.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestCachedResults.java	                        (rev 0)
+++ trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestCachedResults.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+package org.teiid.dqp.internal.process;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+import org.teiid.cache.Cache;
+import org.teiid.cache.DefaultCache;
+import org.teiid.common.buffer.BatchManager;
+import org.teiid.common.buffer.TupleBatch;
+import org.teiid.common.buffer.TupleBuffer;
+import org.teiid.core.TeiidComponentException;
+import org.teiid.core.types.DataTypeManager;
+import org.teiid.dqp.service.FakeBufferService;
+import org.teiid.query.sql.symbol.ElementSymbol;
+
+
+public class TestCachedResults {
+
+	private final class FakeBatchManager implements BatchManager {
+		@Override
+		public void remove() {
+			
+		}
+
+		@Override
+		public ManagedBatch createManagedBatch(final TupleBatch batch)
+				throws TeiidComponentException {
+			return new ManagedBatch() {
+				
+				@Override
+				public void remove() {
+					
+				}
+				
+				@Override
+				public TupleBatch getBatch(boolean cache, String[] types)
+						throws TeiidComponentException {
+					return batch;
+				}
+			};
+		}
+	}
+	
+	@Test
+	public void testCaching() throws Exception {
+		FakeBufferService fbs = new FakeBufferService();
+		
+		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
+		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
+		List<ElementSymbol> schema = Arrays.asList(x);
+		TupleBuffer tb = new TupleBuffer(new FakeBatchManager(), "x", schema, null, 4); //$NON-NLS-1$ 
+		tb.setForwardOnly(false);
+		
+		tb.addTuple(Arrays.asList(1));	
+		tb.addTuple(Arrays.asList(2));
+		tb.addTuple(Arrays.asList(3));
+		tb.addTuple(Arrays.asList(4));
+		tb.addTuple(Arrays.asList(5));
+		tb.addTuple(Arrays.asList(6));
+		tb.addTuple(Arrays.asList(7));
+		tb.addTuple(Arrays.asList(8));
+		tb.addTuple(Arrays.asList(9));
+		tb.addTuple(Arrays.asList(10));
+		
+		tb.close();
+		
+		CachedResults results = new CachedResults();
+		results.setResults(tb);
+		
+		Cache cache = new DefaultCache("dummy", 250); //$NON-NLS-1$
+		results.prepare(cache, fbs.getBufferManager());
+		
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		ObjectOutputStream oos = new ObjectOutputStream(baos);
+		oos.writeObject(results);
+		oos.close();
+		
+		ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+		CachedResults cachedResults = (CachedResults)ois.readObject();
+		ois.close();
+		
+		cachedResults.restore(cache, fbs.getBufferManager());
+		
+		// since restored, simulate a async cache flush
+		cache.clear();
+		
+		TupleBuffer cachedTb = cachedResults.getResults();
+		
+		assertEquals(tb.getRowCount(), cachedTb.getRowCount());
+		assertEquals(tb.getBatchSize(), cachedTb.getBatchSize());
+		
+		assertArrayEquals(tb.getBatch(1).getAllTuples(), cachedTb.getBatch(1).getAllTuples());
+		assertArrayEquals(tb.getBatch(9).getAllTuples(), cachedTb.getBatch(9).getAllTuples());
+	}	
+}


Property changes on: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestCachedResults.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDQPCore.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDQPCore.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDQPCore.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -34,6 +34,7 @@
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.teiid.api.exception.query.QueryResolverException;
+import org.teiid.cache.DefaultCacheFactory;
 import org.teiid.client.RequestMessage;
 import org.teiid.client.ResultsMessage;
 import org.teiid.dqp.internal.datamgr.ConnectorManagerRepository;
@@ -56,6 +57,7 @@
         
         core = new DQPCore();
         core.setBufferService(new FakeBufferService());
+        core.setCacheFactory(new DefaultCacheFactory());
         core.setConnectorManagerRepository(repo);
         core.setTransactionService(new FakeTransactionService());
         

Modified: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDataTierManager.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDataTierManager.java	2010-08-03 19:14:04 UTC (rev 2407)
+++ trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestDataTierManager.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -26,6 +26,7 @@
 
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.teiid.cache.DefaultCacheFactory;
 import org.teiid.client.RequestMessage;
 import org.teiid.common.buffer.BlockedException;
 import org.teiid.core.TeiidException;
@@ -73,6 +74,7 @@
         rm = new DQPCore();
         rm.setTransactionService(new FakeTransactionService());
         rm.setBufferService(new FakeBufferService());
+        rm.setCacheFactory(new DefaultCacheFactory());
         rm.start(new DQPConfiguration());
         FakeBufferService bs = new FakeBufferService();
 

Added: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestSessionAwareCache.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestSessionAwareCache.java	                        (rev 0)
+++ trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestSessionAwareCache.java	2010-08-03 23:36:58 UTC (rev 2408)
@@ -0,0 +1,125 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+package org.teiid.dqp.internal.process;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.teiid.adminapi.impl.SessionMetadata;
+import org.teiid.cache.Cachable;
+import org.teiid.cache.Cache;
+import org.teiid.common.buffer.BufferManager;
+import org.teiid.dqp.internal.process.SessionAwareCache.CacheID;
+import org.teiid.query.parser.ParseInfo;
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+
+ at SuppressWarnings("nls")
+public class TestSessionAwareCache {
+	
+	@Test
+	public void testSessionSpecfic() {
+		
+		SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>();
+		
+		CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		
+		Cachable result = Mockito.mock(Cachable.class);
+		
+		id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		cache.put(id, true, false, result);
+		
+		// make sure that in the case of session specific; we do not call prepare
+		// as session is local only call for distributed
+		Mockito.verify(result, times(0)).prepare((Cache)anyObject(), (BufferManager)anyObject());
+		
+		Object c = cache.get(id);
+		
+		Mockito.verify(result, times(0)).restore((Cache)anyObject(), (BufferManager)anyObject());
+		
+		assertTrue(result==c);
+	}
+	
+	@Test
+	public void testUserSpecfic() {
+		
+		SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>();
+		
+		CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		
+		Cachable result = Mockito.mock(Cachable.class);
+		Mockito.stub(result.prepare((Cache)anyObject(), (BufferManager)anyObject())).toReturn(true);
+		Mockito.stub(result.restore((Cache)anyObject(), (BufferManager)anyObject())).toReturn(true);
+				
+		cache.put(id, false, true, result);
+		
+		// make sure that in the case of session specific; we do not call prepare
+		// as session is local only call for distributed
+		Mockito.verify(result, times(1)).prepare((Cache)anyObject(), (BufferManager)anyObject());
+		
+		id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		
+		Object c = cache.get(id);
+		
+		Mockito.verify(result, times(1)).restore((Cache)anyObject(), (BufferManager)anyObject());		
+		
+		assertTrue(result==c);
+	}
+	
+	@Test
+	public void testNoScope() {
+		
+		SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>();
+		
+		CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		
+		Cachable result = Mockito.mock(Cachable.class);
+		Mockito.stub(result.prepare((Cache)anyObject(), (BufferManager)anyObject())).toReturn(true);
+		Mockito.stub(result.restore((Cache)anyObject(), (BufferManager)anyObject())).toReturn(true);		
+		
+		cache.put(id, false, false, result);
+		
+		// make sure that in the case of session specific; we do not call prepare
+		// as session is local only call for distributed
+		Mockito.verify(result, times(1)).prepare((Cache)anyObject(), (BufferManager)anyObject());
+		
+		id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
+		
+		Object c = cache.get(id);
+		
+		Mockito.verify(result, times(1)).restore((Cache)anyObject(), (BufferManager)anyObject());		
+		
+		assertTrue(result==c);
+	}
+
+	
+	public static DQPWorkContext buildWorkContext() {
+		DQPWorkContext workContext = new DQPWorkContext();
+		SessionMetadata session = new SessionMetadata();
+		workContext.setSession(session);
+		session.setVDBName("vdb-name"); //$NON-NLS-1$
+		session.setVDBVersion(1); 
+		session.setSessionId(String.valueOf(1));
+		session.setUserName("foo"); //$NON-NLS-1$
+		return workContext;
+	}
+}


Property changes on: trunk/engine/src/test/java/org/teiid/dqp/internal/process/TestSessionAwareCache.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the teiid-commits mailing list