cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject cayenne git commit: CAY-2013 In-memory evaluation of DB expressions - non-id attributes
Date Fri, 22 May 2015 12:22:58 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 02c2d0a9c -> debfa94e6


CAY-2013 In-memory evaluation of DB expressions - non-id attributes


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/debfa94e
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/debfa94e
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/debfa94e

Branch: refs/heads/master
Commit: debfa94e611f838e1c1103d15cedfc43fae758d4
Parents: 02c2d0a
Author: aadamchik <aadamchik@apache.org>
Authored: Fri May 22 15:11:58 2015 +0300
Committer: aadamchik <aadamchik@apache.org>
Committed: Fri May 22 15:20:02 2015 +0300

----------------------------------------------------------------------
 .../apache/cayenne/exp/parser/ASTDbPath.java    | 178 +++++++++++--------
 .../parser/ExpressionEvaluateInMemoryIT.java    |  15 ++
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |   1 +
 3 files changed, 116 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/debfa94e/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTDbPath.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTDbPath.java b/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTDbPath.java
index d5cc2d7..ade49ee 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTDbPath.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTDbPath.java
@@ -23,8 +23,10 @@ import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.ObjectId;
 import org.apache.cayenne.Persistent;
+import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.map.Entity;
 
@@ -35,82 +37,102 @@ import org.apache.cayenne.map.Entity;
  */
 public class ASTDbPath extends ASTPath {
 
-    public static final String DB_PREFIX = "db:";
-
-    ASTDbPath(int id) {
-        super(id);
-    }
-
-    public ASTDbPath() {
-        super(ExpressionParserTreeConstants.JJTDBPATH);
-    }
-
-    public ASTDbPath(Object value) {
-        super(ExpressionParserTreeConstants.JJTDBPATH);
-        setPath(value);
-    }
-
-    @Override
-    protected Object evaluateNode(Object o) throws Exception {
-        // TODO: implement resolving DB_PATH for DataObjects
-
-        if (o instanceof Entity) {
-            return evaluateEntityNode((Entity) o);
-        }
-
-        Map<?, ?> map = toMap(o);
-        return (map != null) ? map.get(path) : null;
-    }
-
-    protected Map<?, ?> toMap(Object o) {
-        if (o instanceof Map) {
-            return (Map<?, ?>) o;
-        } else if (o instanceof ObjectId) {
-            return ((ObjectId) o).getIdSnapshot();
-        } else if (o instanceof Persistent) {
-            Persistent persistent = (Persistent) o;
-
-            // TODO: returns ObjectId snapshot for now.. should probably
-            // retrieve full snapshot...
-            ObjectId oid = persistent.getObjectId();
-            return (oid != null) ? oid.getIdSnapshot() : null;
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Creates a copy of this expression node, without copying children.
-     */
-    @Override
-    public Expression shallowCopy() {
-        ASTDbPath copy = new ASTDbPath(id);
-        copy.path = path;
-        return copy;
-    }
-
-    /**
-     * @since 4.0
-     */
-    @Override
-    public void appendAsEJBQL(List<Object> parameterAccumulator, Appendable out, String
rootId) throws IOException {
-        // warning: non-standard EJBQL...
-        out.append(DB_PREFIX);
-        out.append(rootId);
-        out.append('.');
-        out.append(path);
-    }
-
-    /**
-     * @since 4.0
-     */
-    @Override
-    public void appendAsString(Appendable out) throws IOException {
-        out.append(DB_PREFIX).append(path);
-    }
-
-    @Override
-    public int getType() {
-        return Expression.DB_PATH;
-    }
+	private static final long serialVersionUID = 6623715674339310782L;
+
+	public static final String DB_PREFIX = "db:";
+
+	ASTDbPath(int id) {
+		super(id);
+	}
+
+	public ASTDbPath() {
+		super(ExpressionParserTreeConstants.JJTDBPATH);
+	}
+
+	public ASTDbPath(Object value) {
+		super(ExpressionParserTreeConstants.JJTDBPATH);
+		setPath(value);
+	}
+
+	@Override
+	protected Object evaluateNode(Object o) throws Exception {
+
+		if (o instanceof Entity) {
+			return evaluateEntityNode((Entity) o);
+		}
+
+		Map<?, ?> map = toMap(o);
+		return (map != null) ? map.get(path) : null;
+	}
+
+	protected Map<?, ?> toMap(Object o) {
+		if (o instanceof Map) {
+			return (Map<?, ?>) o;
+		} else if (o instanceof ObjectId) {
+			return ((ObjectId) o).getIdSnapshot();
+		} else if (o instanceof Persistent) {
+			Persistent persistent = (Persistent) o;
+
+			// before reading full snapshot, check if we can use smaller ID
+			// snapshot ... it is much cheaper...
+			return persistent.getObjectContext() != null ? toMap_AttachedObject(persistent.getObjectContext(),
+					persistent) : toMap_DetachedObject(persistent);
+		} else {
+			return null;
+		}
+	}
+
+	private Map<?, ?> toMap_AttachedObject(ObjectContext context, Persistent persistent)
{
+
+		// TODO: snapshotting API should not be limited to DataContext...
+		if (context instanceof DataContext) {
+			return ((DataContext) context).currentSnapshot(persistent);
+		}
+
+		return toMap_DetachedObject(persistent);
+	}
+
+	private Map<?, ?> toMap_DetachedObject(Persistent persistent) {
+
+		ObjectId oid = persistent.getObjectId();
+
+		// returning null here is for backwards compatibility. Should we throw
+		// instead?
+		return (oid != null) ? oid.getIdSnapshot() : null;
+	}
+
+	/**
+	 * Creates a copy of this expression node, without copying children.
+	 */
+	@Override
+	public Expression shallowCopy() {
+		ASTDbPath copy = new ASTDbPath(id);
+		copy.path = path;
+		return copy;
+	}
+
+	/**
+	 * @since 4.0
+	 */
+	@Override
+	public void appendAsEJBQL(List<Object> parameterAccumulator, Appendable out, String
rootId) throws IOException {
+		// warning: non-standard EJBQL...
+		out.append(DB_PREFIX);
+		out.append(rootId);
+		out.append('.');
+		out.append(path);
+	}
+
+	/**
+	 * @since 4.0
+	 */
+	@Override
+	public void appendAsString(Appendable out) throws IOException {
+		out.append(DB_PREFIX).append(path);
+	}
+
+	@Override
+	public int getType() {
+		return Expression.DB_PATH;
+	}
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/debfa94e/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryIT.java
index d1ae633..689182e 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryIT.java
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne.exp.parser;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -79,6 +80,20 @@ public class ExpressionEvaluateInMemoryIT extends ServerCase {
 	}
 
 	@Test
+	public void testEvaluateDB_PATH_DataObject() {
+
+		Artist a1 = (Artist) context.newObject("Artist");
+		a1.setArtistName("a1");
+		context.commitChanges();
+
+		Expression idExp = ExpressionFactory.exp("db:ARTIST_ID");
+		assertEquals(Cayenne.longPKForObject(a1), idExp.evaluate(a1));
+
+		Expression columnExp = ExpressionFactory.exp("db:ARTIST_NAME");
+		assertEquals("a1", columnExp.evaluate(a1));
+	}
+
+	@Test
 	public void testEvaluateOBJ_PATH_ObjEntity() {
 		ASTObjPath node = new ASTObjPath("paintingArray.paintingTitle");
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/debfa94e/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index b6446bb..3b827e8 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -26,6 +26,7 @@ CAY-2009 Non-blocking connection pool
 CAY-2010 DataSourceBuilder to help users create pooling and non-pooling DataSources
 CAY-2011 Support for Java 8 date and time types
 CAY-2012 ObjectSelect, SelectById: eliminating methods that reset query state
+CAY-2013 In-memory evaluation of DB expressions - non-id attributes
 
 Bug Fixes:
 


Mime
View raw message