Permalink
Browse files

non index query1

  • Loading branch information...
1 parent d9a654c commit 6823d6a9e36c5a8cc3de54a8a7e5da548d67049a pulasthi committed Jun 11, 2011
View
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
- <classpathentry kind="src" output="target/classes" path="src/main/resources"/>
+ <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
@cjdelisle
cjdelisle Jun 11, 2011

What did you change here? I am unfamiliar with this so I am wondering what it is and why it needed to change.

@pulasthi
pulasthi Jun 11, 2011 Owner

i didn't change anything here.

@cjdelisle
cjdelisle Jun 11, 2011

maybe your IDE did it, anyway if you don't know what it's for and I don't know what it's for them you probably ought to revert it ;)

<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
- <classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
+ <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
@@ -19,7 +19,9 @@
import static com.spidertracks.datanucleus.utils.MetaDataUtils.getColumnName;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Stack;
@@ -29,6 +31,7 @@
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.query.QueryUtils;
import org.datanucleus.query.evaluator.AbstractExpressionEvaluator;
+import org.datanucleus.query.expression.DyadicExpression;
import org.datanucleus.query.expression.Expression;
import org.datanucleus.query.expression.Literal;
import org.datanucleus.query.expression.ParameterExpression;
@@ -62,6 +65,7 @@
private Stack<IndexParam> indexKeys = new Stack<IndexParam>();
private Stack<Operand> operationStack = new Stack<Operand>();
+ private ArrayList<String> primaryExpressions = new ArrayList<String>();
private AbstractClassMetaData metaData;
@@ -342,9 +346,23 @@ protected Object processLiteral(Literal expr) {
return param;
}
-
-
-
+ public ArrayList<String> getPrimaryExpressions(Expression expr){
+ setPrimaryExpressionList(expr);
+
+ return primaryExpressions;
+ }
+
+ private void setPrimaryExpressionList(Expression expr){
+ if(expr instanceof DyadicExpression){
+ setPrimaryExpressionList(expr.getLeft());
+ setPrimaryExpressionList(expr.getRight());
+ }else if(expr instanceof PrimaryExpression){
+ primaryExpressions.add(((PrimaryExpression) expr).getId());
+ }else{
+ return;
+ }
+
+ }
/**
* Get the index value off the stack. Will only pop if the stack sizes are
* equal
@@ -19,6 +19,8 @@
import static com.spidertracks.datanucleus.utils.MetaDataUtils.getDiscriminatorColumnName;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -59,7 +61,11 @@
*
*/
public class JDOQLQuery extends AbstractJDOQLQuery {
-
+ /**
+ * this boolean is to check whether the query has only has non indexed fields as filters
+ */
+ public boolean nonIndexedQuery = true;
+ private CassandraQueryExpressionEvaluator evaluator;
private static int DEFAULT_MAX = 1000;
/**
@@ -110,7 +116,7 @@ public JDOQLQuery(ExecutionContext ec, String query) {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Object performExecute(Map parameters) {
-
+
long startTime = System.currentTimeMillis();
if (NucleusLogger.QUERY.isDebugEnabled()) {
@@ -171,10 +177,12 @@ protected Object performExecute(Map parameters) {
// a query was specified, perform a filter with secondary cassandra
// indexes
- if (filter != null) {
-
- CassandraQueryExpressionEvaluator evaluator = new CassandraQueryExpressionEvaluator(acmd, range, byteContext, parameters);
-
+ evaluator = new CassandraQueryExpressionEvaluator(acmd, range, byteContext, parameters);
+ if(filter != null){
+ checkFilterValidity(filter);
+ }
+ if (filter != null && !nonIndexedQuery) {
+
Operand opTree = (Operand) filter.evaluate(evaluator);
// there's a discriminator so be sure to include it
@@ -197,7 +205,7 @@ protected Object performExecute(Map parameters) {
Collection<?> results = getObjectsOfCandidateType(candidateKeys, acmd,
clr, subclasses, idColumnBytes, descriminiatorCol, byteContext);
- if (this.getOrdering() != null || this.getGrouping() != null) {
+ if (this.getOrdering() != null || this.getGrouping() != null || nonIndexedQuery) {
// Apply any result restrictions to the results
JavaQueryEvaluator resultMapper = new JDOQLEvaluator(this, results,
@@ -215,7 +223,26 @@ protected Object performExecute(Map parameters) {
return results;
}
-
@cjdelisle
cjdelisle Jun 11, 2011

Our code style is to leave an empty line here and I see Todd Nine does the same thing so this should be changed.

+ public void checkFilterValidity(Expression filter){
+ AnnotaionEvaluator an = new AnnotaionEvaluator(candidateClass);
+ ArrayList<String> annotaionlist = an.getAnnotatedFields("javax.jdo.annotations.Index");
+
+ ArrayList<String> expressionlist = evaluator.getPrimaryExpressions(filter);
+ if(annotaionlist == null){
+ nonIndexedQuery =false;
+ return;
+ }
+ if(expressionlist == null){
+ return;
+ }
+ for(String ex:expressionlist){
+ if(annotaionlist.indexOf(ex)!=-1){
+ nonIndexedQuery = false;
+ return;
+ }
+ }
+
+ }
/**
* Used to load specific keys
*
@@ -19,6 +19,8 @@
import java.util.List;
import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
@@ -50,7 +52,13 @@
public static final String KEYSPACE = "TestingKeyspace";
protected static PersistenceManagerFactory pmf;
-
+ public static void main(String[] args) {
+ try {
+ setup();
+ } catch (Exception ex) {
+ Logger.getLogger(CassandraTest.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
/**
* Set embedded cassandra up and spawn it in a new thread.
*
@@ -66,15 +66,7 @@ public void testBasicPeristAndLoad() throws Exception {
PrimitiveObject object = new PrimitiveObject();
object.setTestByte((byte) 0xf1);
- object.setTestBool(true);
- object.setTestChar('t');
- object.setTestDouble(100.10);
- object.setTestFloat((float) 200.20);
- object.setTestInt(40);
- object.setTestLong(200);
- object.setTestShort((short) 5);
- object.setTestString("foobar");
@cjdelisle
cjdelisle Jun 11, 2011

Why are these removed?

@pulasthi
pulasthi Jun 11, 2011 Owner

ooh removed them a while back for a small test forgot to put them back :)

-
+
// now save our object
pm.makePersistent(object);
@@ -42,12 +42,26 @@
@Persistent
private byte testByte;
+ @Index
@Persistent
private char testChar;
-
- @Persistent
+
@Index
+ @Persistent
private String testString;
+
+
+ @Persistent
+ private String nonIndexedString;
+
+
+ public String getNonIndexedString() {
+ return nonIndexedString;
+ }
+
+ public void setNonIndexedString(String nonIndexedString) {
+ this.nonIndexedString = nonIndexedString;
+ }
public boolean isTestBool() {
return testBool;
@@ -86,6 +86,8 @@ public void setUp() throws Exception {
object1.setTestLong(100);
object1.setTestShort((short) 1);
object1.setTestString("one");
+ object1.setNonIndexedString("one1");
+
setupPm.makePersistent(object1);
@@ -99,6 +101,7 @@ public void setUp() throws Exception {
object2.setTestLong(200);
object2.setTestShort((short) 2);
object2.setTestString("two");
+ object2.setNonIndexedString("two1");
setupPm.makePersistent(object2);
@@ -112,6 +115,8 @@ public void setUp() throws Exception {
object3.setTestLong(300);
object3.setTestShort((short) 3);
object3.setTestString("three");
+ object3.setNonIndexedString("three1");
+
setupPm.makePersistent(object3);
@@ -262,7 +267,7 @@ public void testOrdering() {
}
/**
- * result test
+ *non index result test
*/
@SuppressWarnings("rawtypes")
@Test
@@ -271,8 +276,7 @@ public void testFilter() {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- Query q = pm.newQuery(PrimitiveObject.class);
- q.setFilter("testString == 'one'");
@cjdelisle
cjdelisle Jun 11, 2011

It looks like you modified an existing test instead of adding a new one, what became of the existing test, is what it proved still guaranteed?

@pulasthi
pulasthi Jun 11, 2011 Owner

yes, anyway i will put this as a new test

+ Query q = pm.newQuery("SELECT FROM com.spidertracks.datanucleus.basic.model.PrimitiveObject WHERE nonIndexedString == \"one1\"");
@cjdelisle
cjdelisle Jun 11, 2011

This is the longest line in the file and it doesn't display well in my editor. As a general rule when editing files, you don't want to add a line which is longer than the longest existing line. XWiki checkstyle rules will stop you from using any line longer than 120 characters, I keep my lines less than 100 by configuring my editor to have a 100 character window width.

@pulasthi
pulasthi Jun 11, 2011 Owner

i also set my editor to 100 lines. to avoid this from happening again

Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();

1 comment on commit 6823d6a

@cjdelisle

Your code style needs some work (this is a common issue, I was just like this 2 years back)
At XWiki we have very strict rules for code style which will cause the code to fail to compile if they are not followed. Unfortunately the code which you are working on has a different and less strict code style. I will send mail to Todd Nine and ask him what he thinks about integrating your patch and exactly what code style he expects so we can decide exactly what your patch needs to look like.

I notice a lot of "trailing whitespace" which should be fixed. If you open a terminal and navigate in to your /Datanucleus-Cassandra-Plugin/ then type
git diff HEAD~2 HEAD
You will this commit and the other with trailing whitespace highlighted in red.

Please sign in to comment.