Author: cbegin
Date: Sun Oct 5 21:00:25 2008
New Revision: 701930
URL: http://svn.apache.org/viewvc?rev=701930&view=rev
Log:
finalized cache configuration design
Modified:
ibatis/trunk/java/ibatis-3/TODO
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/CachingExecutor.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/MappedStatement.java
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/test/java/org/apache/ibatis/monarch/example/BlogMapper.xml
Modified: ibatis/trunk/java/ibatis-3/TODO
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/TODO?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/TODO (original)
+++ ibatis/trunk/java/ibatis-3/TODO Sun Oct 5 21:00:25 2008
@@ -1,15 +1,17 @@
Core
* Improved Error Messages
-Monarch
- * Cache Parsing
- * Namespaces
+Monarch XML
* DTD / Schema
+ * Named parameter parsing/merging
+ * Loosely bound cache id to instance
+ - (perhaps use late-binding lookup proxies for all config elements?)
+mvn
+Monarch API
* Transaction Manager
* Mapper Interface Binding
* Client / Mapper Factory
- * Named parameter parsing/merging
-
+
Testing
* Functional tests
* Perf/Threaded Integration Tests
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/CachingExecutor.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/CachingExecutor.java?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/CachingExecutor.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/executor/CachingExecutor.java
Sun Oct 5 21:00:25 2008
@@ -26,10 +26,7 @@
}
public int update(MappedStatement ms, Object parameterObject) throws
SQLException {
- Cache cache = ms.getCache();
- if (cache != null) {
- tcm.clear(cache);
- }
+ flushCacheIfRequired(ms);
return delegate.update(ms, parameterObject);
}
@@ -38,15 +35,20 @@
if (ms != null) {
Cache cache = ms.getCache();
if (cache != null) {
+ flushCacheIfRequired(ms);
cache.getReadWriteLock().readLock().lock();
try {
- CacheKey key = createCacheKey(ms, parameterObject, offset, limit);
- if (cache.hasKey(key)) {
- return (List) cache.getObject(key);
+ if (ms.isUseCache()) {
+ CacheKey key = createCacheKey(ms, parameterObject, offset, limit);
+ if (cache.hasKey(key)) {
+ return (List) cache.getObject(key);
+ } else {
+ List list = delegate.query(ms, parameterObject, offset, limit,
resultHandler);
+ tcm.putObject(cache, key, list);
+ return list;
+ }
} else {
- List list = delegate.query(ms, parameterObject, offset, limit,
resultHandler);
- tcm.putObject(cache, key, list);
- return list;
+ return delegate.query(ms, parameterObject, offset, limit,
resultHandler);
}
} finally {
cache.getReadWriteLock().readLock().unlock();
@@ -56,6 +58,13 @@
return delegate.query(ms, parameterObject, offset, limit, resultHandler);
}
+ private void flushCacheIfRequired(MappedStatement ms) {
+ Cache cache = ms.getCache();
+ if (ms.isFlushCacheRequired()) {
+ tcm.clear(cache);
+ }
+ }
+
public List flushStatements() throws SQLException {
return delegate.flushStatements();
}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java
Sun Oct 5 21:00:25 2008
@@ -28,7 +28,9 @@
}
public CacheBuilder addDecorator(Class<? extends Cache> decorator) {
- this.decorators.add(decorator);
+ if (decorator != null) {
+ this.decorators.add(decorator);
+ }
return this;
}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/MappedStatement.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/MappedStatement.java?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/MappedStatement.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/mapping/MappedStatement.java
Sun Oct 5 21:00:25 2008
@@ -16,6 +16,8 @@
private Cache cache;
private ParameterMap parameterMap;
private List<ResultMap> resultMaps;
+ private boolean flushCacheRequired;
+ private boolean useCache;
private MappedStatement() {
}
@@ -33,6 +35,10 @@
mappedStatement.timeout = configuration.getDefaultStatementTimeout();
}
+ public String id() {
+ return mappedStatement.id;
+ }
+
public Builder parameterMap(ParameterMap parameterMap) {
mappedStatement.parameterMap = parameterMap;
return this;
@@ -68,6 +74,16 @@
return this;
}
+ public Builder flushCacheRequired(boolean flushCacheRequired) {
+ mappedStatement.flushCacheRequired = flushCacheRequired;
+ return this;
+ }
+
+ public Builder useCache(boolean useCache) {
+ mappedStatement.useCache = useCache;
+ return this;
+ }
+
public MappedStatement build() {
assert mappedStatement.configuration != null;
assert mappedStatement.id != null;
@@ -75,6 +91,7 @@
mappedStatement.resultMaps =
Collections.unmodifiableList(mappedStatement.resultMaps);
return mappedStatement;
}
+
}
public Configuration getConfiguration() {
@@ -117,6 +134,14 @@
return cache;
}
+ public boolean isFlushCacheRequired() {
+ return flushCacheRequired;
+ }
+
+ public boolean isUseCache() {
+ return useCache;
+ }
+
public String getSql(Object parameterObject) {
return sqlSource.getSql(parameterObject);
}
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java
Sun Oct 5 21:00:25 2008
@@ -4,7 +4,6 @@
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.type.*;
import org.apache.ibatis.xml.*;
-import org.apache.ibatis.cache.impl.PerpetualCache;
import org.apache.ibatis.cache.Cache;
import java.io.Reader;
@@ -50,41 +49,61 @@
}
// <configuration namespace="com.domain.MapperClass" />
- @Nodelet("/configuration")
+ @Nodelet("/mapper")
public void configurationElement(NodeletContext context) throws Exception {
namespace = context.getStringAttribute("namespace");
if (namespace == null) {
- throw new BuilderException("The configuration element requires a
namespace attribute to be specified.");
+ throw new BuilderException("The mapper element requires a namespace
attribute to be specified.");
}
}
// <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false" />
- @Nodelet("/configuration/cache")
- public void cacheTemplateElement(NodeletContext context) throws Exception {
- String type = context.getStringAttribute("type","LRU");
+ @Nodelet("/mapper/cache-ref")
+ public void cacheRefElement(NodeletContext context) throws Exception {
+ String ns = context.getStringAttribute("namespace");
+ if (ns == null) {
+ throw new BuilderException("cache-ref element requires a namespace
attribute.");
+ }
+ cache = configuration.getCache(namespaceCacheId(ns));
+ if (cache == null) {
+ throw new BuilderException("No cache for namespace '"+ns+"' could be
found.");
+ }
+ }
+
+ // <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false" />
+ @Nodelet("/mapper/cache")
+ public void cacheElement(NodeletContext context) throws Exception {
+ String type = context.getStringAttribute("perpetual","PERPETUAL");
type = typeAliasRegistry.resolveAlias(type);
Class typeClass = Class.forName(type);
- long flushInterval = context.getLongAttribute("flushInterval",3600000L);
- int size = context.getIntAttribute("size",1000);
+ String eviction = context.getStringAttribute("eviction","LRU");
+ eviction = typeAliasRegistry.resolveAlias(eviction);
+ Class evictionClass = Class.forName(eviction);
+
+ Long flushInterval = context.getLongAttribute("flushInterval",null);
+ Integer size = context.getIntAttribute("size",null);
boolean readOnly = context.getBooleanAttribute("readOnly",false);
Properties props = context.getChildrenAsProperties();
- cache = new CacheBuilder(namespace)
- .implementation(PerpetualCache.class)
- .addDecorator(typeClass)
+ cache = new CacheBuilder(namespaceCacheId(namespace))
+ .implementation(typeClass)
+ .addDecorator(evictionClass)
.clearInterval(flushInterval)
.size(size)
.readWrite(!readOnly)
.properties(props)
.build();
+
+ configuration.addCache(cache);
}
// <parameterMap id="" type="">
@Nodelet("/mapper/parameterMap")
public void parameterMapElement(NodeletContext context) throws Exception {
String id = context.getStringAttribute("id");
+ id = applyNamespace(id);
String type = context.getStringAttribute("type");
Class parameterClass = resolveClass(type);
parameterMappings = new ArrayList<ParameterMapping>();
@@ -110,8 +129,11 @@
@Nodelet("/mapper/resultMap")
public void resultMapElement(NodeletContext context) throws Exception {
String id = context.getStringAttribute("id");
+ id = applyNamespace(id);
+
String type = context.getStringAttribute("type");
String extend = context.getStringAttribute("extends");
+ extend = applyNamespace(extend);
Class typeClass = resolveClass(type);
@@ -194,6 +216,7 @@
public void resultMapDiscriminatorCaseElement(NodeletContext context) throws
Exception {
String value = context.getStringAttribute("value");
String resultMap = context.getStringAttribute("resultMap");
+ resultMap = applyNamespace(resultMap);
discriminatorMap.put(value, resultMap);
}
@@ -245,8 +268,20 @@
buildStatement(context,StatementType.STATEMENT);
}
+ private String applyNamespace(String base) {
+ if (base == null) return null;
+ if (base.contains(".")) return base;
+ return namespace + "." + base;
+ }
+
+ private String namespaceCacheId(String ns) {
+ return ns + ".Cache";
+ }
+
private void buildStatement(NodeletContext context, StatementType
statementType) {
String id = context.getStringAttribute("id");
+ id = applyNamespace(id);
+
String sql = context.getStringBody();
SqlSource sqlSource = new BasicSqlSource(sql);
@@ -266,13 +301,20 @@
private void setStatementCache(NodeletContext context,
MappedStatement.Builder statementBuilder) {
boolean isSelect = "select".equals(context.getNode().getNodeName());
- boolean useCache = context.getBooleanAttribute("useCache",isSelect);
+
boolean flushCache = context.getBooleanAttribute("flushCache",!isSelect);
+ statementBuilder.flushCacheRequired(flushCache);
+
+ boolean useCache = context.getBooleanAttribute("useCache",isSelect);
+ statementBuilder.useCache(useCache);
+
statementBuilder.cache(cache);
}
private void setStatementParameterMap(NodeletContext context,
MappedStatement.Builder statementBuilder) {
String parameterMap = context.getStringAttribute("parameterMap");
+ parameterMap = applyNamespace(parameterMap);
+
String parameterType = context.getStringAttribute("parameterType");
if (parameterMap != null) {
statementBuilder.parameterMap(configuration.getParameterMap(parameterMap));
@@ -281,7 +323,7 @@
Class parameterTypeClass = resolveClass(parameterType);
ParameterMap.Builder inlineParameterMapBuilder = new
ParameterMap.Builder(
configuration,
- context.getStringAttribute("id") + "-inline-parameter-map",
+ statementBuilder.id() + "-inline-parameter-map",
parameterTypeClass,
parameterMappings);
statementBuilder.parameterMap(inlineParameterMapBuilder.build());
@@ -290,6 +332,8 @@
private void setStatementResultMap(NodeletContext context,
MappedStatement.Builder statementBuilder) {
String resultMap = context.getStringAttribute("resultMap");
+ resultMap = applyNamespace(resultMap);
+
String resultType = context.getStringAttribute("resultType");
List<ResultMap> resultMaps = new ArrayList<ResultMap>();
if (resultMap != null) {
@@ -301,7 +345,7 @@
Class resultTypeClass = resolveClass(resultType);
ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
configuration,
- context.getStringAttribute("id")+ "-inline-result-map",
+ statementBuilder.id() + "-inline-result-map",
resultTypeClass,
new ArrayList<ResultMapping>());
resultMaps.add(inlineResultMapBuilder.build());
@@ -328,6 +372,8 @@
String jdbcType = context.getStringAttribute("jdbcType");
String nestedSelect = context.getStringAttribute("select");
String nestedResultMap = context.getStringAttribute("resultMap");
+ nestedResultMap = applyNamespace(nestedResultMap);
+
String typeHandler = context.getStringAttribute("typeHandler");
Class resultType = resultMapBuilder.type();
@@ -361,6 +407,7 @@
String javaType = context.getStringAttribute("javaType");
String jdbcType = context.getStringAttribute("jdbcType");
String resultMap = context.getStringAttribute("resultMap");
+ resultMap = applyNamespace(resultMap);
String mode = context.getStringAttribute("mode");
String typeHandler = context.getStringAttribute("typeHandler");
Integer numericScale = context.getIntAttribute("numericScale",null);
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/test/java/org/apache/ibatis/monarch/example/BlogMapper.xml
URL:
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/test/java/org/apache/ibatis/monarch/example/BlogMapper.xml?rev=701930&r1=701929&r2=701930&view=diff
==============================================================================
---
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/test/java/org/apache/ibatis/monarch/example/BlogMapper.xml
(original)
+++
ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/test/java/org/apache/ibatis/monarch/example/BlogMapper.xml
Sun Oct 5 21:00:25 2008
@@ -2,7 +2,7 @@
<cache-ref namespace=""/>
- <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false">
+ <cache type="PERPETUAL" eviction="LRU" flushInterval="3600000" size="1000"
readOnly="false">
<property name="" value=""/>
</cache>