You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/02/21 19:21:24 UTC

[11/21] Rename index to query index, add working query parser code.

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/Results.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/Results.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/Results.java
new file mode 100644
index 0000000..aab3f79
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/Results.java
@@ -0,0 +1,1284 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query;
+
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
+import org.apache.usergrid.persistence.query.ir.SearchVisitor;
+import static org.apache.usergrid.persistence.query.SimpleEntityRef.ref;
+import org.apache.usergrid.persistence.model.entity.Entity;
+import org.apache.usergrid.persistence.model.entity.Id;
+
+
+@XmlRootElement
+public class Results implements Iterable<Entity> {
+
+
+    public enum Level {
+        IDS, REFS, CORE_PROPERTIES, ALL_PROPERTIES, LINKED_PROPERTIES
+    }
+
+
+    Level level = Level.IDS;
+    Id id;
+    List<Id> ids;
+    Set<Id> idSet;
+
+    EntityRef ref;
+    List<EntityRef> refs;
+    Map<Id, EntityRef> refsMap;
+    Map<String, List<EntityRef>> refsByType;
+
+    Entity entity;
+    List<Entity> entities;
+    Map<Id, Entity> entitiesMap;
+    Map<String, List<Entity>> entitiesByType;
+
+    List<ConnectionRef> connections;
+    boolean forwardConnections = true;
+
+    List<AggregateCounterSet> counters;
+
+    Set<String> types;
+
+    Map<Id, Map<String, Object>> metadata;
+    boolean metadataMerged = true;
+
+    Id nextResult;
+    String cursor;
+
+    Query query;
+    Object data;
+    String dataName;
+
+    private SearchVisitor searchVisitor;
+
+
+    public Results() {
+    }
+
+
+    public Results( Results r ) {
+        if ( r != null ) {
+            level = r.level;
+
+            id = r.id;
+            ids = r.ids;
+            idSet = r.idSet;
+
+            ref = r.ref;
+            refs = r.refs;
+            refsMap = r.refsMap;
+            refsByType = r.refsByType;
+
+            entity = r.entity;
+            entities = r.entities;
+            entitiesMap = r.entitiesMap;
+            entitiesByType = r.entitiesByType;
+
+            connections = r.connections;
+            forwardConnections = r.forwardConnections;
+
+            counters = r.counters;
+
+            types = r.types;
+
+            metadata = r.metadata;
+            metadataMerged = r.metadataMerged;
+
+            nextResult = r.nextResult;
+            cursor = r.cursor;
+
+            query = r.query;
+            data = r.data;
+            dataName = r.dataName;
+        }
+    }
+
+
+    public void init() {
+        level = Level.IDS;
+
+        id = null;
+        ids = null;
+        idSet = null;
+
+        ref = null;
+        refs = null;
+        refsMap = null;
+        refsByType = null;
+
+        entity = null;
+        entities = null;
+        entitiesMap = null;
+        entitiesByType = null;
+
+        connections = null;
+        forwardConnections = true;
+
+        counters = null;
+
+        types = null;
+
+        // metadata = null;
+        metadataMerged = false;
+
+        query = null;
+        data = null;
+        dataName = null;
+    }
+
+
+    public static Results fromIdList( List<Id> l ) {
+        Results r = new Results();
+        r.setIds( l );
+        return r;
+    }
+
+
+    public static Results fromIdList( List<Id> l, String type ) {
+        if ( type == null ) {
+            return fromIdList( l );
+        }
+        List<EntityRef> refs = new ArrayList<EntityRef>();
+        for ( Id u : l ) {
+            refs.add( ref( u ) );
+        }
+        Results r = new Results();
+        r.setRefs( refs );
+        return r;
+    }
+
+
+    public static Results fromId( Id id ) {
+        Results r = new Results();
+        if ( id != null ) {
+            List<Id> l = new ArrayList<Id>();
+            l.add( id );
+            r.setIds( l );
+        }
+        return r;
+    }
+
+
+    public static Results fromRefList( List<EntityRef> l ) {
+        Results r = new Results();
+        r.setRefs( l );
+        return r;
+    }
+
+
+//    public static Results fromEntities( List<? extends Entity> l ) {
+//        Results r = new Results();
+//        r.setEntities( l );
+//        return r;
+//    }
+
+
+    public static Results fromEntity( Entity e ) {
+        Results r = new Results();
+        r.setEntity( e );
+        return r;
+    }
+
+
+    public static Results fromRef( EntityRef ref ) {
+        if ( ref instanceof Entity ) {
+            return fromEntity( ( Entity ) ref );
+        }
+        Results r = new Results();
+        r.setRef( ref );
+        return r;
+    }
+
+
+//    public static Results fromData( Object obj ) {
+//        Results r = new Results();
+//        r.setData( obj );
+//        return r;
+//    }
+//
+//
+//    public static Results fromCounters( AggregateCounterSet counters ) {
+//        Results r = new Results();
+//        List<AggregateCounterSet> l = new ArrayList<AggregateCounterSet>();
+//        l.add( counters );
+//        r.setCounters( l );
+//        return r;
+//    }
+//
+//
+//    public static Results fromCounters( List<AggregateCounterSet> counters ) {
+//        Results r = new Results();
+//        r.setCounters( counters );
+//        return r;
+//    }
+//
+//
+//    @SuppressWarnings("unchecked")
+//    public static Results fromConnections( List<? extends ConnectionRef> connections ) {
+//        Results r = new Results();
+//        r.setConnections( ( List<ConnectionRef> ) connections, true );
+//        return r;
+//    }
+//
+//
+//    @SuppressWarnings("unchecked")
+//    public static Results fromConnections( List<? extends ConnectionRef> connections, boolean forward ) {
+//        Results r = new Results();
+//        r.setConnections( ( List<ConnectionRef> ) connections, forward );
+//        return r;
+//    }
+
+
+    public Level getLevel() {
+        return level;
+    }
+
+
+    @JsonSerialize(include = Inclusion.NON_NULL)
+    public Query getQuery() {
+        return query;
+    }
+
+
+    public void setQuery( Query query ) {
+        this.query = query;
+    }
+
+
+    public Results withQuery( Query query ) {
+        this.query = query;
+        return this;
+    }
+
+
+    @JsonSerialize(include = Inclusion.NON_NULL)
+    public Id getId() {
+        if ( id != null ) {
+            return id;
+        }
+        if ( entity != null ) {
+            id = entity.getId();
+            return id;
+        }
+        if ( ( ids != null ) && ( ids.size() > 0 ) ) {
+            id = ids.get( 0 );
+            return id;
+        }
+        if ( ( entities != null ) && ( entities.size() > 0 ) ) {
+            entity = entities.get( 0 );
+            id = entity.getId();
+            return id;
+        }
+        if ( ( refs != null ) && ( refs.size() > 0 ) ) {
+            EntityRef ref = refs.get( 0 );
+            id = ref.getId();
+        }
+        return id;
+    }
+
+
+    @JsonSerialize(include = Inclusion.NON_NULL)
+    public List<Id> getIds() {
+        if ( ids != null ) {
+            return ids;
+        }
+        /*
+         * if (connectionTypeAndEntityTypeToEntityIdMap != null) { ids = new
+         * ArrayList<Id>(); Set<Id> entitySet = new LinkedHashSet<Id>();
+         * for (String ctype : connectionTypeAndEntityTypeToEntityIdMap
+         * .keySet()) { Map<String, List<Id>> m =
+         * connectionTypeAndEntityTypeToEntityIdMap .get(ctype); for (String
+         * etype : m.keySet()) { List<Id> l = m.get(etype); for (Id id : l)
+         * { if (!entitySet.contains(id)) { ids.add(id); } } } } return ids; }
+         */
+        if ( connections != null ) {
+            ids = new ArrayList<Id>();
+            for ( ConnectionRef connection : connections ) {
+                if ( forwardConnections ) {
+                    ConnectedEntityRef c = connection.getConnectedEntity();
+                    if ( c != null ) {
+                        ids.add( c.getId() );
+                    }
+                }
+                else {
+                    EntityRef c = connection.getConnectingEntity();
+                    if ( c != null ) {
+                        ids.add( c.getId() );
+                    }
+                }
+            }
+            return ids;
+        }
+        if ( ( entities != null )
+        /* || (connectionTypeAndEntityTypeToEntityMap != null) */ ) {
+            // getEntities();
+            ids = new ArrayList<Id>();
+            for ( Entity entity : entities ) {
+                ids.add( entity.getId() );
+            }
+            return ids;
+        }
+        if ( refs != null ) {
+            ids = new ArrayList<Id>();
+            for ( EntityRef ref : refs ) {
+                ids.add( ref.getId() );
+            }
+            return ids;
+        }
+        if ( id != null ) {
+            ids = new ArrayList<Id>();
+            ids.add( id );
+            return ids;
+        }
+        if ( entity != null ) {
+            ids = new ArrayList<Id>();
+            ids.add( entity.getId() );
+            return ids;
+        }
+        return new ArrayList<Id>();
+    }
+
+
+    public void setIds( List<Id> resultsIds ) {
+        init();
+        ids = resultsIds;
+        level = Level.IDS;
+    }
+
+
+    public Results withIds( List<Id> resultsIds ) {
+        setIds( resultsIds );
+        return this;
+    }
+
+
+    @JsonSerialize(include = Inclusion.NON_NULL)
+    public Set<Id> getIdSet() {
+        if ( idSet != null ) {
+            return idSet;
+        }
+        getIds();
+        if ( ids != null ) {
+            idSet = new LinkedHashSet<Id>();
+            idSet.addAll( ids );
+            return idSet;
+        }
+        return new LinkedHashSet<Id>();
+    }
+
+
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    @SuppressWarnings("unchecked")
+//    public List<EntityRef> getRefs() {
+//        if ( refs != null ) {
+//            return refs;
+//        }
+//        List<?> l = getEntities();
+//        if ( ( l != null ) && ( l.size() > 0 ) ) {
+//            return ( List<EntityRef> ) l;
+//        }
+//        if ( connections != null ) {
+//            refs = new ArrayList<EntityRef>();
+//            for ( ConnectionRef connection : connections ) {
+//                if ( forwardConnections ) {
+//                    ConnectedEntityRef c = connection.getConnectedEntity();
+//                    if ( c != null ) {
+//                        refs.add( c );
+//                    }
+//                }
+//                else {
+//                    EntityRef c = connection.getConnectingEntity();
+//                    if ( c != null ) {
+//                        refs.add( c );
+//                    }
+//                }
+//            }
+//            return refs;
+//        }
+//        if ( ref != null ) {
+//            refs = new ArrayList<EntityRef>();
+//            refs.add( ref );
+//            return refs;
+//        }
+//        return new ArrayList<EntityRef>();
+//    }
+
+
+    public void setRefs( List<EntityRef> resultsRefs ) {
+        init();
+        refs = resultsRefs;
+        level = Level.REFS;
+    }
+
+
+    public Results withRefs( List<EntityRef> resultsRefs ) {
+        setRefs( resultsRefs );
+        return this;
+    }
+
+
+    public void setRef( EntityRef ref ) {
+        init();
+        this.ref = ref;
+        level = Level.REFS;
+    }
+
+
+    public Results withRef( EntityRef ref ) {
+        setRef( ref );
+        return this;
+    }
+
+
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public EntityRef getRef() {
+//        if ( ref != null ) {
+//            return ref;
+//        }
+//        Entity e = getEntity();
+//        if ( e != null ) {
+//            return ref( e.getId(), e.getVersion() );
+//        }
+//        Id u = getId();
+//        if ( u != null ) {
+//            return ref( u );
+//        }
+//        return null;
+//    }
+
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Map<Id, EntityRef> getRefsMap() {
+//        if ( refsMap != null ) {
+//            return refsMap;
+//        }
+//        getEntitiesMap();
+//        if ( entitiesMap != null ) {
+//            refsMap = cast( entitiesMap );
+//            return refsMap;
+//        }
+//        getRefs();
+//        if ( refs != null ) {
+//            refsMap = new LinkedHashMap<Id, EntityRef>();
+//            for ( EntityRef ref : refs ) {
+//                refsMap.put( ref.getId(), ref );
+//            }
+//        }
+//        return refsMap;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Entity getEntity() {
+//        mergeEntitiesWithMetadata();
+//        if ( entity != null ) {
+//            return entity;
+//        }
+//        if ( ( entities != null ) && ( entities.size() > 0 ) ) {
+//            entity = entities.get( 0 );
+//            return entity;
+//        }
+//        return null;
+//    }
+
+
+    public void setEntity( Entity resultEntity ) {
+        init();
+        entity = resultEntity;
+        level = Level.CORE_PROPERTIES;
+    }
+
+
+    public Results withEntity( Entity resultEntity ) {
+        setEntity( resultEntity );
+        return this;
+    }
+
+
+    public Iterator<Id> idIterator() {
+        List<Id> l = getIds();
+        if ( l != null ) {
+            return l.iterator();
+        }
+        return ( new ArrayList<Id>( 0 ) ).iterator();
+    }
+
+
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public List<Entity> getEntities() {
+//        mergeEntitiesWithMetadata();
+//        if ( entities != null ) {
+//            return entities;
+//        }
+//        /*
+//         * if (connectionTypeAndEntityTypeToEntityMap != null) { entities = new
+//         * ArrayList<Entity>(); Map<Id, Entity> eMap = new LinkedHashMap<Id,
+//         * Entity>(); for (String ctype :
+//         * connectionTypeAndEntityTypeToEntityMap.keySet()) { Map<String,
+//         * List<Entity>> m = connectionTypeAndEntityTypeToEntityMap .get(ctype);
+//         * for (String etype : m.keySet()) { List<Entity> l = m.get(etype); for
+//         * (Entity e : l) { if (!eMap.containsKey(e.getUuid())) { entities.add(e);
+//         * eMap.put(e.getUuid(), e); } } } } return entities; }
+//         */
+//        if ( entity != null ) {
+//            entities = new ArrayList<Entity>();
+//            entities.add( entity );
+//            return entities;
+//        }
+//        return new ArrayList<Entity>();
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Map<Id, Entity> getEntitiesMap() {
+//        if ( entitiesMap != null ) {
+//            return entitiesMap;
+//        }
+//        if ( entities != null ) {
+//            entitiesMap = new LinkedHashMap<Id, Entity>();
+//            for ( Entity entity : entities ) {
+//                entitiesMap.put( entity.getId(), entity );
+//            }
+//        }
+//        return entitiesMap;
+//    }
+//
+//
+//    public List<EntityRef> getEntityRefsByType( String type ) {
+//        if ( entitiesByType != null ) {
+//            return refsByType.get( type );
+//        }
+//        List<EntityRef> l = cast( getEntitiesByType( type ) );
+//        if ( l != null ) {
+//            return l;
+//        }
+//        getRefs();
+//        if ( refs == null ) {
+//            return null;
+//        }
+//        refsByType = new LinkedHashMap<String, List<EntityRef>>();
+//        for ( Entity entity : entities ) {
+//            l = refsByType.get( entity.getId().getType() );
+//            if ( l == null ) {
+//                l = new ArrayList<EntityRef>();
+//                refsByType.put( entity.getId().getType(), l );
+//            }
+//            l.add( entity );
+//        }
+//        return l;
+//    }
+//
+//
+//    public List<Entity> getEntitiesByType( String type ) {
+//        if ( entitiesByType != null ) {
+//            return entitiesByType.get( type );
+//        }
+//        getEntities();
+//        if ( entities == null ) {
+//            return null;
+//        }
+//        List<Entity> l = null;
+//        entitiesByType = new LinkedHashMap<String, List<Entity>>();
+//        for ( Entity entity : entities ) {
+//            l = entitiesByType.get( entity.getType() );
+//            if ( l == null ) {
+//                l = new ArrayList<Entity>();
+//                entitiesByType.put( entity.getType(), l );
+//            }
+//            l.add( entity );
+//        }
+//        return l;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Set<String> getTypes() {
+//        if ( types != null ) {
+//            return types;
+//        }
+//        getEntityRefsByType( "entity" );
+//        if ( entitiesByType != null ) {
+//            types = entitiesByType.keySet();
+//        }
+//        else if ( refsByType != null ) {
+//            types = refsByType.keySet();
+//        }
+//        return types;
+//    }
+//
+//
+//    public void merge( Results results ) {
+//        getEntitiesMap();
+//        results.getEntitiesMap();
+//        if ( entitiesMap != null || results.entitiesMap != null ) {
+//
+//            level = Level.ALL_PROPERTIES;
+//
+//            // do nothing, nothing to union
+//            if ( entitiesMap != null && results.entitiesMap == null ) {
+//                return;
+//                // other side has the results, assign and return
+//            }
+//            else if ( entitiesMap == null && results.entitiesMap != null ) {
+//                entities = results.entities;
+//                return;
+//            }
+//
+//            entitiesMap.putAll( results.entitiesMap );
+//            entities = new ArrayList<Entity>( entitiesMap.values() );
+//
+//            return;
+//        }
+//
+//        getRefsMap();
+//        results.getRefsMap();
+//        if ( ( refsMap != null ) || ( results.refsMap != null ) ) {
+//
+//            level = Level.REFS;
+//
+//            // do nothing, nothing to union
+//            if ( refsMap != null && results.refsMap == null ) {
+//                return;
+//                // other side has the results, assign and return
+//            }
+//            else if ( refsMap == null && results.refsMap != null ) {
+//                refs = results.refs;
+//                return;
+//            }
+//
+//            refsMap.putAll( results.refsMap );
+//            refs = new ArrayList<EntityRef>( refsMap.values() );
+//
+//            return;
+//        }
+//
+//        getIdSet();
+//        results.getIdSet();
+//        if ( ( idSet != null ) && ( results.idSet != null ) ) {
+//
+//            level = Level.IDS;
+//
+//            // do nothing, nothing to union
+//            if ( idSet != null && results.idSet == null ) {
+//                return;
+//                // other side has the results, assign and return
+//            }
+//            else if ( idSet == null && results.idSet != null ) {
+//                ids = results.ids;
+//                return;
+//            }
+//
+//            idSet.addAll( results.idSet );
+//            ids = new ArrayList<Id>( idSet );
+//
+//            return;
+//        }
+//    }
+//
+//
+//    /** Remove the passed in results from the current results */
+//    public void subtract( Results results ) {
+//        getEntitiesMap();
+//        results.getEntitiesMap();
+//
+//        if ( ( entitiesMap != null ) && ( results.entitiesMap != null ) ) {
+//            Map<Id, Entity> newMap = new LinkedHashMap<Id, Entity>();
+//            for ( Map.Entry<Id, Entity> e : entitiesMap.entrySet() ) {
+//                if ( !results.entitiesMap.containsKey( e.getKey() ) ) {
+//                    newMap.put( e.getKey(), e.getValue() );
+//                }
+//            }
+//            entitiesMap = newMap;
+//            entities = new ArrayList<Entity>( entitiesMap.values() );
+//            level = Level.ALL_PROPERTIES;
+//            return;
+//        }
+//
+//        getRefsMap();
+//        results.getRefsMap();
+//        if ( ( refsMap != null ) && ( results.refsMap != null ) ) {
+//            Map<Id, EntityRef> newMap = new LinkedHashMap<Id, EntityRef>();
+//            for ( Map.Entry<Id, EntityRef> e : refsMap.entrySet() ) {
+//                if ( !results.refsMap.containsKey( e.getKey() ) ) {
+//                    newMap.put( e.getKey(), e.getValue() );
+//                }
+//            }
+//            refsMap = newMap;
+//            refs = new ArrayList<EntityRef>( refsMap.values() );
+//            level = Level.REFS;
+//            return;
+//        }
+//
+//        getIdSet();
+//        results.getIdSet();
+//        if ( ( idSet != null ) && ( results.idSet != null ) ) {
+//            Set<Id> newSet = new LinkedHashSet<Id>();
+//            for ( Id uuid : idSet ) {
+//                if ( !results.idSet.contains( uuid ) ) {
+//                    newSet.add( uuid );
+//                }
+//            }
+//            idSet = newSet;
+//            ids = new ArrayList<Id>( idSet );
+//            level = Level.IDS;
+//            return;
+//        }
+//    }
+//
+//
+//    /** Perform an intersection of the 2 results */
+//    public void and( Results results ) {
+//        getEntitiesMap();
+//        results.getEntitiesMap();
+//
+//        if ( ( entitiesMap != null ) && ( results.entitiesMap != null ) ) {
+//            Map<Id, Entity> newMap = new LinkedHashMap<Id, Entity>();
+//            for ( Map.Entry<Id, Entity> e : entitiesMap.entrySet() ) {
+//                if ( results.entitiesMap.containsKey( e.getKey() ) ) {
+//                    newMap.put( e.getKey(), e.getValue() );
+//                }
+//            }
+//            entitiesMap = newMap;
+//            entities = new ArrayList<Entity>( entitiesMap.values() );
+//            level = Level.ALL_PROPERTIES;
+//            return;
+//        }
+//
+//        getRefsMap();
+//        results.getRefsMap();
+//        if ( ( refsMap != null ) && ( results.refsMap != null ) ) {
+//            Map<Id, EntityRef> newMap = new LinkedHashMap<Id, EntityRef>();
+//            for ( Map.Entry<Id, EntityRef> e : refsMap.entrySet() ) {
+//                if ( results.refsMap.containsKey( e.getKey() ) ) {
+//                    newMap.put( e.getKey(), e.getValue() );
+//                }
+//            }
+//            refsMap = newMap;
+//            refs = new ArrayList<EntityRef>( refsMap.values() );
+//            level = Level.REFS;
+//            ids = null;
+//            return;
+//        }
+//
+//        getIdSet();
+//        results.getIdSet();
+//        if ( ( idSet != null ) && ( results.idSet != null ) ) {
+//            Set<Id> newSet = new LinkedHashSet<Id>();
+//            for ( Id uuid : idSet ) {
+//                if ( results.idSet.contains( uuid ) ) {
+//                    newSet.add( uuid );
+//                }
+//            }
+//            idSet = newSet;
+//            ids = new ArrayList<Id>( idSet );
+//            level = Level.IDS;
+//            return;
+//        }
+//
+//        // should be empty
+//        init();
+//    }
+//
+//
+//    public void replace( Entity entity ) {
+//        entitiesMap = null;
+//        if ( ( this.entity != null ) && ( this.entity.getUuid().equals( entity.getUuid() ) ) ) {
+//            this.entity = entity;
+//        }
+//        if ( entities != null ) {
+//            ListIterator<Entity> i = entities.listIterator();
+//            while ( i.hasNext() ) {
+//                Entity e = i.next();
+//                if ( e.getUuid().equals( entity.getUuid() ) ) {
+//                    i.set( entity );
+//                }
+//            }
+//        }
+//    }
+//
+//
+//    public Results startingFrom( Id entityId ) {
+//        if ( entities != null ) {
+//            for ( int i = 0; i < entities.size(); i++ ) {
+//                Entity entity = entities.get( i );
+//                if ( entityId.equals( entity.getUuid() ) ) {
+//                    if ( i == 0 ) {
+//                        return this;
+//                    }
+//                    return Results.fromEntities( entities.subList( i, entities.size() ) );
+//                }
+//            }
+//        }
+//        if ( refs != null ) {
+//            for ( int i = 0; i < refs.size(); i++ ) {
+//                EntityRef entityRef = refs.get( i );
+//                if ( entityId.equals( entityRef.getUuid() ) ) {
+//                    if ( i == 0 ) {
+//                        return this;
+//                    }
+//                    return Results.fromRefList( refs.subList( i, refs.size() ) );
+//                }
+//            }
+//        }
+//        if ( ids != null ) {
+//            for ( int i = 0; i < ids.size(); i++ ) {
+//                Id uuid = ids.get( i );
+//                if ( entityId.equals( uuid ) ) {
+//                    if ( i == 0 ) {
+//                        return this;
+//                    }
+//                    return Results.fromIdList( ids.subList( i, ids.size() ) );
+//                }
+//            }
+//        }
+//        return this;
+//    }
+//
+//
+//    @SuppressWarnings("unchecked")
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public <E extends Entity> List<E> getList() {
+//        List<Entity> l = getEntities();
+//        return ( List<E> ) l;
+//    }
+//
+//
+//    public <E extends Entity> Iterator<E> iterator( Class<E> cls ) {
+//        List<E> l = getList();
+//        if ( l != null ) {
+//            return l.iterator();
+//        }
+//        return ( new ArrayList<E>( 0 ) ).iterator();
+//    }
+//
+
+    @Override
+    public Iterator<Entity> iterator() {
+        List<Entity> l = null; // getEntities();
+        if ( l != null ) {
+            return l.iterator();
+        }
+        return ( new ArrayList<Entity>( 0 ) ).iterator();
+    }
+
+//
+//    public Results findForProperty( String propertyName, Object propertyValue ) {
+//        return findForProperty( propertyName, propertyValue, 1 );
+//    }
+//
+//
+//    public Results findForProperty( String propertyName, Object propertyValue, int count ) {
+//        if ( propertyValue == null ) {
+//            return new Results();
+//        }
+//        List<Entity> l = getEntities();
+//        if ( l == null ) {
+//            return new Results();
+//        }
+//        List<Entity> found = new ArrayList<Entity>();
+//        for ( Entity e : l ) {
+//            if ( propertyValue.equals( e.getProperty( propertyName ) ) ) {
+//                found.add( e );
+//                if ( ( count > 0 ) && ( found.size() == count ) ) {
+//                    break;
+//                }
+//            }
+//        }
+//        return Results.fromEntities( found );
+//    }
+//
+//
+//    @SuppressWarnings("unchecked")
+//    public void setEntities( List<? extends Entity> resultsEntities ) {
+//        init();
+//        entities = ( List<Entity> ) resultsEntities;
+//        level = Level.CORE_PROPERTIES;
+//    }
+//
+//
+//    public Results withEntities( List<? extends Entity> resultsEntities ) {
+//        setEntities( resultsEntities );
+//        return this;
+//    }
+//
+//
+//    public boolean hasConnections() {
+//        return connections != null;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public List<ConnectionRef> getConnections() {
+//        return connections;
+//    }
+//
+//
+//    private void setConnections( List<ConnectionRef> connections, boolean forwardConnections ) {
+//        init();
+//        this.connections = connections;
+//        this.forwardConnections = forwardConnections;
+//        level = Level.REFS;
+//        for ( ConnectionRef connection : connections ) {
+//            if ( forwardConnections ) {
+//                this.setMetadata( connection.getConnectedEntity().getUuid(), "connection",
+//                        connection.getConnectionType() );
+//            }
+//            else {
+//                this.setMetadata( connection.getConnectingEntity().getUuid(), "connection",
+//                        connection.getConnectionType() );
+//            }
+//        }
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Object getObject() {
+//        if ( data != null ) {
+//            return data;
+//        }
+//        if ( entities != null ) {
+//            return entities;
+//        }
+//        if ( ids != null ) {
+//            return ids;
+//        }
+//        if ( entity != null ) {
+//            return entity;
+//        }
+//        if ( id != null ) {
+//            return id;
+//        }
+//        if ( counters != null ) {
+//            return counters;
+//        }
+//        return null;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public String getObjectName() {
+//        if ( dataName != null ) {
+//            return dataName;
+//        }
+//        if ( entities != null ) {
+//            return "entities";
+//        }
+//        if ( ids != null ) {
+//            return "ids";
+//        }
+//        if ( entity != null ) {
+//            return "entity";
+//        }
+//        if ( id != null ) {
+//            return "id";
+//        }
+//        return null;
+//    }
+//
+//
+//    public void setDataName( String dataName ) {
+//        this.dataName = dataName;
+//    }
+//
+//
+//    public Results withDataName( String dataName ) {
+//        this.dataName = dataName;
+//        return this;
+//    }
+//
+//
+//    public boolean hasData() {
+//        return data != null;
+//    }
+//
+//
+//    public void setData( Object data ) {
+//        this.data = data;
+//    }
+//
+//
+//    public Results withData( Object data ) {
+//        this.data = data;
+//        return this;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Object getData() {
+//        return data;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public List<AggregateCounterSet> getCounters() {
+//        return counters;
+//    }
+//
+//
+//    public void setCounters( List<AggregateCounterSet> counters ) {
+//        this.counters = counters;
+//    }
+//
+//
+//    public Results withCounters( List<AggregateCounterSet> counters ) {
+//        this.counters = counters;
+//        return this;
+//    }
+//
+//
+//    public int size() {
+//        if ( entities != null ) {
+//            return entities.size();
+//        }
+//        if ( refs != null ) {
+//            return refs.size();
+//        }
+//        if ( ids != null ) {
+//            return ids.size();
+//        }
+//        if ( entity != null ) {
+//            return 1;
+//        }
+//        if ( ref != null ) {
+//            return 1;
+//        }
+//        if ( id != null ) {
+//            return 1;
+//        }
+//        return 0;
+//    }
+//
+//
+//    public boolean isEmpty() {
+//        return size() == 0;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public Id getNextResult() {
+//        return nextResult;
+//    }
+//
+//
+//    public Results excludeCursorMetadataAttribute() {
+//        if ( metadata != null ) {
+//            for ( Entry<Id, Map<String, Object>> entry : metadata.entrySet() ) {
+//                Map<String, Object> map = entry.getValue();
+//                if ( map != null ) {
+//                    map.remove( Schema.PROPERTY_CURSOR );
+//                }
+//            }
+//        }
+//        return new Results( this );
+//    }
+//
+//
+//    public Results trim( int count ) {
+//        if ( count == 0 ) {
+//            return this;
+//        }
+//
+//        int size = size();
+//        if ( size <= count ) {
+//            return this;
+//        }
+//
+//        List<Id> ids = getIds();
+//        Id nextResult = null;
+//        String cursor = null;
+//        if ( ids.size() > count ) {
+//            nextResult = ids.get( count );
+//            ids = ids.subList( 0, count );
+//            if ( metadata != null ) {
+//                cursor = StringUtils.toString( MapUtils.getMapMap( metadata, nextResult, "cursor" ) );
+//            }
+//            if ( cursor == null ) {
+//                cursor = encodeBase64URLSafeString( bytes( nextResult ) );
+//            }
+//        }
+//
+//        Results r = new Results( this );
+//        if ( r.entities != null ) {
+//            r.entities = r.entities.subList( 0, count );
+//        }
+//        if ( r.refs != null ) {
+//            r.refs = r.refs.subList( 0, count );
+//        }
+//        if ( r.ids != null ) {
+//            r.ids = r.ids.subList( 0, count );
+//        }
+//        r.setNextResult( nextResult );
+//        r.setCursor( cursor );
+//
+//        return r;
+//    }
+//
+//
+//    public boolean hasMoreResults() {
+//        return nextResult != null;
+//    }
+//
+//
+//    public void setNextResult( Id nextResult ) {
+//        this.nextResult = nextResult;
+//    }
+//
+//
+//    public Results withNextResult( Id nextResult ) {
+//        this.nextResult = nextResult;
+//        return this;
+//    }
+//
+//
+//    @JsonSerialize(include = Inclusion.NON_NULL)
+//    public String getCursor() {
+//        return cursor;
+//    }
+//
+//
+//    public boolean hasCursor() {
+//        return cursor != null && cursor.length() > 0;
+//    }
+//
+//
+//    public void setCursor( String cursor ) {
+//        this.cursor = cursor;
+//    }
+//
+//
+//    public Results withCursor( String cursor ) {
+//        this.cursor = cursor;
+//        return this;
+//    }
+//
+//
+//    public void setMetadata( Id id, String name, Object value ) {
+//        if ( metadata == null ) {
+//            metadata = new LinkedHashMap<Id, Map<String, Object>>();
+//        }
+//        Map<String, Object> entityMetadata = metadata.get( id );
+//        if ( entityMetadata == null ) {
+//            entityMetadata = new LinkedHashMap<String, Object>();
+//            metadata.put( id, entityMetadata );
+//        }
+//        entityMetadata.put( name, value );
+//        metadataMerged = false;
+//        // updateIndex(id, name, value);
+//    }
+//
+//
+//    public Results withMetadata( Id id, String name, Object value ) {
+//        setMetadata( id, name, value );
+//        return this;
+//    }
+//
+//
+//    public void setMetadata( Id id, Map<String, Object> data ) {
+//        if ( metadata == null ) {
+//            metadata = new LinkedHashMap<Id, Map<String, Object>>();
+//        }
+//        Map<String, Object> entityMetadata = metadata.get( id );
+//        if ( entityMetadata == null ) {
+//            entityMetadata = new LinkedHashMap<String, Object>();
+//            metadata.put( id, entityMetadata );
+//        }
+//        entityMetadata.putAll( data );
+//        metadataMerged = false;
+//        /*
+//         * for (Entry<String, Object> m : data.entrySet()) { updateIndex(id,
+//         * m.getKey(), m.getValue()); }
+//         */
+//    }
+//
+//
+//    public Results withMetadata( Id id, Map<String, Object> data ) {
+//        setMetadata( id, data );
+//        return this;
+//    }
+//
+//
+//    public void setMetadata( Map<Id, Map<String, Object>> metadata ) {
+//        this.metadata = metadata;
+//    }
+//
+//
+//    public Results withMetadata( Map<Id, Map<String, Object>> metadata ) {
+//        this.metadata = metadata;
+//        return this;
+//    }
+//
+//
+//    public void mergeEntitiesWithMetadata() {
+//        if ( metadataMerged ) {
+//            return;
+//        }
+//        if ( metadata == null ) {
+//            return;
+//        }
+//        metadataMerged = true;
+//        getEntities();
+//        if ( entities != null ) {
+//            for ( Entity entity : entities ) {
+//                entity.clearMetadata();
+//                Map<String, Object> entityMetadata = metadata.get( entity.getUuid() );
+//                if ( entityMetadata != null ) {
+//                    entity.mergeMetadata( entityMetadata );
+//                }
+//            }
+//        }
+//    }
+//
+//
+//    protected QueryProcessor getQueryProcessor() {
+//        return queryProcessor;
+//    }
+//
+//
+//    public void setQueryProcessor( QueryProcessor queryProcessor ) {
+//        this.queryProcessor = queryProcessor;
+//    }
+//
+//
+//    public void setSearchVisitor( SearchVisitor searchVisitor ) {
+//        this.searchVisitor = searchVisitor;
+//    }
+//
+//
+//    /** uses cursor to get next batch of Results (returns null if no cursor) */
+//    public Results getNextPageResults() throws Exception {
+//        if ( !hasCursor() ) {
+//            return null;
+//        }
+//
+//        Query q = new Query( query );
+//        q.setCursor( getCursor() );
+//        queryProcessor.setQuery( q );
+//
+//        return queryProcessor.getResults( searchVisitor );
+//    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/SimpleEntityRef.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/SimpleEntityRef.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/SimpleEntityRef.java
new file mode 100644
index 0000000..57d1d4b
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/SimpleEntityRef.java
@@ -0,0 +1,129 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query;
+
+
+import java.util.UUID;
+import org.apache.usergrid.persistence.model.entity.Id;
+
+
+public class SimpleEntityRef implements EntityRef {
+
+    private final Id id;
+
+    private final UUID version;
+
+
+    public SimpleEntityRef( Id id, UUID version ) {
+        this.id = id;
+        this.version = version;
+    }
+
+
+    public SimpleEntityRef( EntityRef entityRef ) {
+        this.id = entityRef.getId();
+        this.version = entityRef.getVersion(); 
+    }
+
+
+    public static EntityRef ref() {
+        return new SimpleEntityRef( null, null );
+    }
+
+    public static EntityRef ref( Id id ) {
+        return new SimpleEntityRef( id, null );
+    }
+
+    public static EntityRef ref( Id id, UUID version ) {
+        return new SimpleEntityRef(  id, version );
+    }
+
+
+    public static EntityRef ref( EntityRef ref ) {
+        return new SimpleEntityRef( ref );
+    }
+
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
+        result = prime * result + ( ( version == null ) ? 0 : version.hashCode() );
+        return result;
+    }
+
+
+    @Override
+    public boolean equals( Object obj ) {
+        if ( this == obj ) {
+            return true;
+        }
+        if ( obj == null ) {
+            return false;
+        }
+        if ( getClass() != obj.getClass() ) {
+            return false;
+        }
+        SimpleEntityRef other = ( SimpleEntityRef ) obj;
+        if ( id == null ) {
+            if ( other.id != null ) {
+                return false;
+            }
+        }
+        else if ( !id.equals( other.id ) ) {
+            return false;
+        }
+        if ( version == null ) {
+            if ( other.version != null ) {
+                return false;
+            }
+        }
+        else if ( !version.equals( other.version ) ) {
+            return false;
+        }
+        return true;
+    }
+
+
+    @Override
+    public String toString() {
+        return id.toString() + "|" + version.toString();
+    }
+
+    public static Id getId( EntityRef ref ) {
+        if ( ref == null ) {
+            return null;
+        }
+        return ref.getId();
+    }
+
+
+    public static String getType( EntityRef ref ) {
+        if ( ref == null ) {
+            return null;
+        }
+        return ref.getId().getType();
+    }
+
+    public Id getId() {
+        return id;
+    }
+
+    public UUID getVersion() {
+        return version;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AllNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AllNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AllNode.java
new file mode 100644
index 0000000..e408552
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AllNode.java
@@ -0,0 +1,45 @@
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * Used to represent a "select all".  This will iterate over the entities by UUID
+ *
+ * @author tnine
+ */
+public class AllNode extends QueryNode {
+
+
+    private final boolean forceKeepFirst;
+
+
+    /**
+     * Note that the slice isn't used on select, but is used when creating cursors
+     *
+     * @param id. The unique numeric id for this node
+     * @param forceKeepFirst True if we don't allow the iterator to skip the first result, regardless of cursor state.
+     * Used for startUUID paging
+     */
+    public AllNode( int id, boolean forceKeepFirst ) {
+        this.forceKeepFirst = forceKeepFirst;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence.query.ir.NodeVisitor)
+     */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    @Override
+    public String toString() {
+        return "AllNode";
+    }
+
+    /** @return the skipFirstMatch */
+    public boolean isForceKeepFirst() {
+        return forceKeepFirst;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AndNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AndNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AndNode.java
new file mode 100644
index 0000000..3e889b2
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/AndNode.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * Node where the results need intersected.  Used instead of a SliceNode when one of the children is an operation other
+ * than slices.  I.E OR, NOT etc
+ *
+ * @author tnine
+ */
+public class AndNode extends BooleanNode {
+
+    /**
+     * @param left
+     * @param right
+     */
+    public AndNode( QueryNode left, QueryNode right ) {
+        super( left, right );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence.query.ir.NodeVisitor)
+     */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/BooleanNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/BooleanNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/BooleanNode.java
new file mode 100644
index 0000000..e4eb813
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/BooleanNode.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/** @author tnine */
+public abstract class BooleanNode extends QueryNode {
+
+    protected QueryNode left;
+    protected QueryNode right;
+
+
+    public BooleanNode( QueryNode left, QueryNode right ) {
+        this.left = left;
+        this.right = right;
+    }
+
+
+    /** @return the left */
+    public QueryNode getLeft() {
+        return left;
+    }
+
+
+    /** @return the right */
+    public QueryNode getRight() {
+        return right;
+    }
+
+
+    @Override
+    public String toString() {
+        return "BooleanNode [left=" + left + ", right=" + right + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/EmailIdentifierNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/EmailIdentifierNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/EmailIdentifierNode.java
new file mode 100644
index 0000000..ef95d1a
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/EmailIdentifierNode.java
@@ -0,0 +1,28 @@
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * Class to represent a UUID based Identifier query
+ *
+ * @author tnine
+ */
+public class EmailIdentifierNode extends QueryNode {
+
+    private final String identifier;
+
+
+    public EmailIdentifierNode( String identifier ) {
+        this.identifier = identifier;
+    }
+
+
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    public String getIdentifier() {
+        return identifier;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NameIdentifierNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NameIdentifierNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NameIdentifierNode.java
new file mode 100644
index 0000000..9b6f2e0
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NameIdentifierNode.java
@@ -0,0 +1,28 @@
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * Class to represent a UUID based Identifier query
+ *
+ * @author tnine
+ */
+public class NameIdentifierNode extends QueryNode {
+
+    private final String name;
+
+
+    public NameIdentifierNode( String name ) {
+        this.name = name;
+    }
+
+
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    public String getName() {
+        return name;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NodeVisitor.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NodeVisitor.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NodeVisitor.java
new file mode 100644
index 0000000..2cad64e
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NodeVisitor.java
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/** @author tnine */
+public interface NodeVisitor {
+
+    /**
+     *
+     * @param node
+     * @throws Exception
+     */
+    public void visit( AndNode node ) throws Exception;
+
+    /**
+     *
+     * @param node
+     * @throws Exception
+     */
+    public void visit( NotNode node ) throws Exception;
+
+    /**
+     *
+     * @param node
+     * @throws Exception
+     */
+    public void visit( OrNode node ) throws Exception;
+
+    /**
+     *
+     * @param node
+     * @throws Exception
+     */
+    public void visit( WithinNode node ) throws Exception;
+
+    /**
+     *
+     * @param node
+     * @throws Exception
+     */
+    public void visit( AllNode node ) throws Exception;
+
+    /** Visit the name identifier node */
+    public void visit( NameIdentifierNode nameIdentifierNode ) throws Exception;
+
+    /** Visit the uuid identifier node */
+    public void visit( UuidIdentifierNode uuidIdentifierNode );
+
+    /**
+     * @param orderByNode
+     * @throws Exception
+     */
+    public void visit( OrderByNode orderByNode ) throws Exception;
+
+    /** Visit the email id node */
+    public void visit( EmailIdentifierNode emailIdentifierNode ) throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NotNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NotNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NotNode.java
new file mode 100644
index 0000000..e8d037d
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/NotNode.java
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/** @author tnine */
+public class NotNode extends QueryNode {
+
+    protected QueryNode subtractNode, keepNode;
+
+
+    /** @param keepNode may be null if there are parents to this */
+    public NotNode( QueryNode subtractNode, QueryNode keepNode ) {
+        this.subtractNode = subtractNode;
+        this.keepNode = keepNode;
+//        throw new RuntimeException( "I'm a not node" );
+    }
+
+
+    /** @return the child */
+    public QueryNode getSubtractNode() {
+        return subtractNode;
+    }
+
+
+    /** @return the all */
+    public QueryNode getKeepNode() {
+        return keepNode;
+    }
+
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see
+     * org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence
+     * .query.ir.NodeVisitor)
+     */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    @Override
+    public String toString() {
+        return "NotNode [child=" + subtractNode + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrNode.java
new file mode 100644
index 0000000..2ce0b65
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrNode.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * @author tnine
+ */
+public class OrNode extends BooleanNode {
+
+    private final int id;
+
+
+    /**
+     * @param left
+     * @param right
+     */
+    public OrNode( QueryNode left, QueryNode right, int id ) {
+        super( left, right );
+        this.id = id;
+    }
+
+
+    /**
+     * Get the context id
+     */
+    public int getId() {
+        return this.id;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence.query.ir.NodeVisitor)
+     */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrderByNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrderByNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrderByNode.java
new file mode 100644
index 0000000..c3ae763
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/OrderByNode.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+import java.util.List;
+
+import org.apache.usergrid.persistence.query.Query.SortPredicate;
+
+
+/**
+ * Intermediate representation of ordering operations
+ *
+ * @author tnine
+ */
+public class OrderByNode extends QueryNode {
+
+
+    private final List<SortPredicate> secondarySorts;
+    private final QueryNode queryOperations;
+
+
+    /**
+     * @param firstPredicate The first predicate that is in the order by statement
+     * @param secondarySorts Any subsequent terms
+     * @param queryOperations The subtree for boolean evaluation
+     */
+    public OrderByNode( List<SortPredicate> secondarySorts, QueryNode queryOperations ) {
+        this.secondarySorts = secondarySorts;
+        this.queryOperations = queryOperations;
+    }
+
+
+    /** @return the sorts */
+    public List<SortPredicate> getSecondarySorts() {
+        return secondarySorts;
+    }
+
+
+    public QueryNode getQueryOperations() {
+        return queryOperations;
+    }
+
+
+    /*
+       * (non-Javadoc)
+       *
+       * @see
+       * org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence
+       * .query.ir.NodeVisitor)
+       */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    /** Return true if this order has secondary sorts */
+    public boolean hasSecondarySorts() {
+        return secondarySorts != null && secondarySorts.size() > 0;
+    }
+
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return "OrderByNode [sorts=" + secondarySorts + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/QueryNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/QueryNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/QueryNode.java
new file mode 100644
index 0000000..f6881f0
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/QueryNode.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * The visit the node
+ *
+ * @author tnine
+ */
+public abstract class QueryNode {
+
+    /** Visit this node */
+    public abstract void visit( NodeVisitor visitor ) throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/SearchVisitor.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/SearchVisitor.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/SearchVisitor.java
new file mode 100644
index 0000000..f040119
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/SearchVisitor.java
@@ -0,0 +1,68 @@
+package org.apache.usergrid.persistence.query.ir;
+
+import org.apache.usergrid.persistence.query.Query;
+
+/**
+ * Simple search visitor that builds query string. 
+ */
+public abstract class SearchVisitor implements NodeVisitor {
+    protected final Query query;
+
+
+    public SearchVisitor( Query query ) {
+        this.query = query;
+    }
+
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.usergrid.persistence.query.ir.NodeVisitor#visit(org.apache.usergrid.
+     * persistence.query.ir.AndNode)
+     */
+    @Override
+    public void visit( AndNode node ) throws Exception {
+        node.getLeft().visit( this );
+        node.getRight().visit( this );
+    }
+
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.usergrid.persistence.query.ir.NodeVisitor#visit(org.apache.usergrid.
+     * persistence.query.ir.NotNode)
+     */
+    @Override
+    public void visit( NotNode node ) throws Exception {
+        node.getSubtractNode().visit( this );
+        node.getKeepNode().visit( this );
+
+    }
+
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.usergrid.persistence.query.ir.NodeVisitor#visit(org.apache.usergrid.
+     * persistence.query.ir.OrNode)
+     */
+    @Override
+    public void visit( OrNode node ) throws Exception {
+        node.getLeft().visit( this );
+        node.getRight().visit( this );
+    }
+
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see
+     * org.apache.usergrid.persistence.query.ir.NodeVisitor#visit(org.apache.usergrid.persistence
+     * .query.ir.OrderByNode)
+     */
+    @Override
+    public void visit( OrderByNode orderByNode ) throws Exception {
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/UuidIdentifierNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/UuidIdentifierNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/UuidIdentifierNode.java
new file mode 100644
index 0000000..ce7ffa8
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/UuidIdentifierNode.java
@@ -0,0 +1,32 @@
+package org.apache.usergrid.persistence.query.ir;
+
+
+import java.util.UUID;
+
+
+/**
+ * Class to represent a UUID based Identifier query
+ *
+ * @author tnine
+ */
+public class UuidIdentifierNode extends QueryNode {
+
+
+    private final UUID uuid;
+
+
+    public UuidIdentifierNode( UUID uuid ) {
+        this.uuid = uuid;
+    }
+
+
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    public UUID getUuid() {
+        return uuid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/WithinNode.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/WithinNode.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/WithinNode.java
new file mode 100644
index 0000000..c44caf9
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/ir/WithinNode.java
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.ir;
+
+
+/**
+ * Intermediate represnetation of the within operator
+ *
+ * @author tnine
+ */
+public class WithinNode extends QueryNode {
+
+    private final String propertyName;
+    private final float distance;
+    private final float lattitude;
+    private final float longitude;
+
+
+    /**
+     * @param propertyName
+     * @param distance
+     * @param lattitude
+     * @param longitude
+     */
+    public WithinNode( String propertyName, float distance, float lattitude, float longitude, int nodeId ) {
+        this.propertyName = propertyName;
+        this.distance = distance;
+        this.lattitude = lattitude;
+        this.longitude = longitude;
+    }
+
+
+    /** @return the propertyName */
+    public String getPropertyName() {
+        return propertyName;
+    }
+
+
+    /** @return the distance */
+    public float getDistance() {
+        return distance;
+    }
+
+
+    /** @return the lattitude */
+    public float getLattitude() {
+        return lattitude;
+    }
+
+
+    /** @return the longitude */
+    public float getLongitude() {
+        return longitude;
+    }
+
+    /*
+       * (non-Javadoc)
+       *
+       * @see
+       * org.apache.usergrid.persistence.query.ir.QueryNode#visit(org.apache.usergrid.persistence
+       * .query.ir.NodeVisitor)
+       */
+    @Override
+    public void visit( NodeVisitor visitor ) throws Exception {
+        visitor.visit( this );
+    }
+
+
+    @Override
+    public String toString() {
+        return "WithinNode [propertyName=" + propertyName + ", distance=" + distance + ", lattitude=" + lattitude
+                + ", longitude=" + longitude + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/AndOperand.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/AndOperand.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/AndOperand.java
new file mode 100644
index 0000000..e4c82ed
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/AndOperand.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.CommonToken;
+import org.antlr.runtime.Token;
+import org.apache.usergrid.persistence.exceptions.PersistenceException;
+
+
+/** @author tnine */
+public class AndOperand extends BooleanOperand {
+
+    public AndOperand() {
+        super( new CommonToken( 0, "and" ) );
+    }
+
+
+    public AndOperand( Token t ) {
+        super( t );
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.apache.usergrid.persistence.query.tree.Operand#visit(org.apache.usergrid.persistence
+     * .query.tree.QueryVisitor)
+     */
+    @Override
+    public void visit( QueryVisitor visitor ) throws PersistenceException {
+        visitor.visit( this );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanLiteral.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanLiteral.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanLiteral.java
new file mode 100644
index 0000000..e624e25
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanLiteral.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.ClassicToken;
+import org.antlr.runtime.Token;
+
+
+/** @author tnine */
+public class BooleanLiteral extends Literal<Boolean> {
+
+    private boolean value;
+
+
+    /**
+     * @param t
+     */
+    protected BooleanLiteral( Token t ) {
+        super( t );
+        value = Boolean.valueOf( t.getText() );
+    }
+
+
+    /** The boolean literal */
+    public BooleanLiteral( boolean value ) {
+        super( new ClassicToken( 0, String.valueOf( value ) ) );
+        this.value = value;
+    }
+
+
+    public Boolean getValue() {
+        return value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanOperand.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanOperand.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanOperand.java
new file mode 100644
index 0000000..ecf5451
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/BooleanOperand.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.Token;
+
+
+/**
+ * A base class for any equality expression.  Expressions must have a property and a value. Examples are >=, >, =, <,
+ * <=,
+ *
+ * @author tnine
+ */
+public abstract class BooleanOperand extends Operand {
+
+
+    /**
+     * @param property
+     * @param literal
+     */
+    public BooleanOperand( Token t ) {
+        super( t );
+    }
+
+
+    public Operand getLeft() {
+        return ( Operand ) this.children.get( 0 );
+    }
+
+
+    public Operand getRight() {
+        return ( Operand ) this.children.get( 1 );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsOperand.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsOperand.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsOperand.java
new file mode 100644
index 0000000..aa75a1e
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsOperand.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.Token;
+import org.apache.usergrid.persistence.exceptions.PersistenceException;
+
+
+/** @author tnine */
+public class ContainsOperand extends EqualityOperand {
+
+    /**
+     * @param property
+     * @param literal
+     */
+    public ContainsOperand( Token t ) {
+        super( t );
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.apache.usergrid.persistence.query.tree.Operand#visit(org.apache.usergrid.persistence
+     * .query.tree.QueryVisitor)
+     */
+    @Override
+    public void visit( QueryVisitor visitor ) throws PersistenceException {
+        visitor.visit( this );
+    }
+
+
+    public StringLiteral getString() {
+        return ( StringLiteral ) getLiteral();
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.tree.EqualityOperand#newProperty(java.lang.String)
+     */
+    @Override
+    protected Property newProperty( String name ) {
+        return new ContainsProperty( name );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.tree.EqualityOperand#getProperty()
+     */
+    @Override
+    public ContainsProperty getProperty() {
+        return ( ContainsProperty ) this.children.get( 0 );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsProperty.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsProperty.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsProperty.java
new file mode 100644
index 0000000..1e35af6
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/ContainsProperty.java
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.ClassicToken;
+import org.antlr.runtime.Token;
+
+
+/**
+ * A property for full text searching that requires special renaming
+ *
+ * @author tnine
+ */
+public class ContainsProperty extends Property {
+
+    private String indexedName = null;
+
+
+    public ContainsProperty( Token t ) {
+        super( t );
+        this.indexedName = String.format( "%s.keywords", super.getValue() );
+    }
+
+
+    public ContainsProperty( String property ) {
+        this( new ClassicToken( 0, property ) );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.tree.Property#getIndexedValue()
+     */
+    @Override
+    public String getIndexedValue() {
+        return this.indexedName;
+    }
+
+
+    /** @return the property */
+    public ContainsProperty getProperty() {
+        return ( ContainsProperty ) this.children.get( 0 );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/Equal.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/Equal.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/Equal.java
new file mode 100644
index 0000000..d4d4406
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/Equal.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.ClassicToken;
+import org.antlr.runtime.Token;
+import org.apache.usergrid.persistence.exceptions.NoIndexException;
+
+
+/** @author tnine */
+public class Equal extends EqualityOperand {
+
+    /**
+     * @param property
+     * @param literal
+     */
+    public Equal( Token t ) {
+        super( t );
+    }
+
+
+    public Equal() {
+        super( new ClassicToken( 0, "=" ) );
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.apache.usergrid.persistence.query.tree.Operand#visit(org.apache.usergrid.persistence
+     * .query.tree.QueryVisitor)
+     */
+    @Override
+    public void visit( QueryVisitor visitor ) throws NoIndexException {
+        visitor.visit( this );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/EqualityOperand.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/EqualityOperand.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/EqualityOperand.java
new file mode 100644
index 0000000..1e3cbc9
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/EqualityOperand.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.Token;
+
+
+/**
+ * A base class for any equality expression. Expressions must have a property and a value. Examples are >=, >, =, <,
+ * <=,
+ *
+ * @author tnine
+ */
+public abstract class EqualityOperand extends Operand {
+
+    /**
+     * @param property
+     * @param literal
+     */
+    public EqualityOperand( Token t ) {
+        super( t );
+    }
+
+
+    public EqualityOperand( String propName, Literal<?> value ) {
+        super( null );
+    }
+
+
+    /** Set the property on this operand */
+    public void setProperty( String name ) {
+        setAtIndex( 0, newProperty( name ) );
+    }
+
+
+    /** Get the property to set into the equality. Allows subclasses to override the type */
+    protected Property newProperty( String name ) {
+        return new Property( name );
+    }
+
+
+    /** Set the literal on this operand from the given value */
+    public void setLiteral( Object value ) {
+        setAtIndex( 1, LiteralFactory.getLiteral( value ) );
+    }
+
+
+    /** Set the child at the specified index. If it doesn't exist, it's added until it does */
+    @SuppressWarnings("unchecked")
+    private void setAtIndex( int index, Literal<?> value ) {
+
+        if ( children == null ) {
+            children = createChildrenList();
+        }
+
+        while ( children.size() - 1 < index ) {
+            children.add( null );
+        }
+
+        setChild( index, value );
+    }
+
+
+    /** @return the property */
+    public Property getProperty() {
+        return ( Property ) this.children.get( 0 );
+    }
+
+
+    /** @return the literal */
+    public Literal<?> getLiteral() {
+        return ( Literal<?> ) this.children.get( 1 );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c0543f5f/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/FloatLiteral.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/FloatLiteral.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/FloatLiteral.java
new file mode 100644
index 0000000..6363234
--- /dev/null
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/FloatLiteral.java
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.persistence.query.tree;
+
+
+import org.antlr.runtime.ClassicToken;
+import org.antlr.runtime.Token;
+
+
+/** @author tnine */
+public class FloatLiteral extends Literal<Float> implements NumericLiteral {
+
+    private float value;
+
+
+    /**
+     * @param t
+     */
+    public FloatLiteral( Token t ) {
+        super( t );
+        value = Float.valueOf( t.getText() );
+    }
+
+
+    public FloatLiteral( float f ) {
+        super( new ClassicToken( 0, String.valueOf( f ) ) );
+        value = f;
+    }
+
+
+    /** @return the value */
+    public Float getValue() {
+        return value;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.usergrid.persistence.query.tree.NumericLiteral#getFloatValue()
+     */
+    @Override
+    public float getFloatValue() {
+        return value;
+    }
+}