activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r829273 - in /activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api: IndexVisitor.java Predicate.java
Date Fri, 23 Oct 2009 23:07:05 GMT
Author: chirino
Date: Fri Oct 23 23:07:05 2009
New Revision: 829273

URL: http://svn.apache.org/viewvc?rev=829273&view=rev
Log:
Better decoupling of the predicate expressions from the index vitistors

Added:
    activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/Predicate.java
Modified:
    activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/IndexVisitor.java

Modified: activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/IndexVisitor.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/IndexVisitor.java?rev=829273&r1=829272&r2=829273&view=diff
==============================================================================
--- activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/IndexVisitor.java
(original)
+++ activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/IndexVisitor.java
Fri Oct 23 23:07:05 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.hawtdb.api;
 
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -36,7 +37,7 @@
     boolean isInterestedInKeysBetween(Key first, Key second);
 
     /**
-     * The keys and values of a BTree leaf node.
+     * The keys and values of an index node.
      *
      * @param keys
      * @param values
@@ -48,259 +49,97 @@
      */
     boolean isSatiated();
 
-    public interface Predicate<Key> {
-        boolean isInterestedInKeysBetween(Key first, Key second);
-        boolean isInterestedInKey(Key key);
-    }
-
-    abstract class PredicateVisitor<Key, Value> implements IndexVisitor<Key, Value>,
Predicate<Key> {
+    /**
+     * Uses a predicates to select the keys that will be visited.
+     * 
+     * @param <Key>
+     * @param <Value>
+     */
+    class PredicateVisitor<Key, Value> implements IndexVisitor<Key, Value> {
+        
         public static final int UNLIMITED=-1;
-		private int limit;
-
-		public PredicateVisitor(int limit) {
-		    this.limit = limit;
-		}
-
-		final public void visit(List<Key> keys, List<Value> values) {
-			for( int i=0; i < keys.size() && !isSatiated(); i++) {
-				Key key = keys.get(i);
-				if( isInterestedInKey(key) ) {
-				    if(limit > 0 )
-				        limit--;
-					matched(key, values.get(i));
-				}
-			}
-		}
-
-		protected void matched(Key key, Value value) {
-        }
-
-        public boolean isSatiated() {
-            return limit==0;
-        }
-    }
-
-    class OrVisitor<Key, Value> extends PredicateVisitor<Key, Value> {
-        private final List<Predicate<Key>> conditions;
-
-        public OrVisitor(List<Predicate<Key>> conditions) {
-            this(conditions, UNLIMITED);
-        }
-
-        public OrVisitor(List<Predicate<Key>> conditions, int limit) {
-            super(limit);
-            this.conditions = conditions;
-        }
-
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-            for (Predicate<Key> condition : conditions) {
-                if( condition.isInterestedInKeysBetween(first, second) ) {
-                    return true;
-                }
-            }
-            return false;
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            for (Predicate<Key> condition : conditions) {
-                if( condition.isInterestedInKey(key) ) {
-                    return true;
-                }
-            }
-            return false;
-        }
 
-        @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder();
-            boolean first=true;
-            for (Predicate<Key> condition : conditions) {
-                if( !first ) {
-                    sb.append(" OR ");
-                }
-                first=false;
-                sb.append("(");
-                sb.append(condition);
-                sb.append(")");
-            }
-            return sb.toString();
-        }
-    }
-
-    class AndVisitor<Key, Value> extends PredicateVisitor<Key, Value> {
-        private final List<Predicate<Key>> conditions;
-
-        public AndVisitor(List<Predicate<Key>> conditions) {
-            this(conditions, UNLIMITED);
+        private final Predicate<Key> predicate;
+        private int limit;
+        
+        public PredicateVisitor(Predicate<Key> predicate) {
+            this(predicate, UNLIMITED);
         }
-        public AndVisitor(List<Predicate<Key>> conditions, int limit) {
-            super(limit);
-            this.conditions = conditions;
+        
+        public PredicateVisitor(Predicate<Key> predicate, int limit) {
+            this.predicate = predicate;
+            this.limit = limit;
         }
 
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-            for (Predicate<Key> condition : conditions) {
-                if( !condition.isInterestedInKeysBetween(first, second) ) {
-                    return false;
-                }
-            }
-            return true;
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            for (Predicate<Key> condition : conditions) {
-                if( !condition.isInterestedInKey(key) ) {
-                    return false;
+        final public void visit(List<Key> keys, List<Value> values) {
+            for( int i=0; i < keys.size() && !isSatiated(); i++) {
+                Key key = keys.get(i);
+                if( predicate.isInterestedInKey(key) ) {
+                    if(limit > 0 )
+                        limit--;
+                    matched(key, values.get(i));
                 }
             }
-            return true;
         }
 
         @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder();
-            boolean first=true;
-            for (Predicate<Key> condition : conditions) {
-                if( !first ) {
-                    sb.append(" AND ");
-                }
-                first=false;
-                sb.append("(");
-                sb.append(condition);
-                sb.append(")");
-            }
-            return sb.toString();
+        public boolean isInterestedInKeysBetween(Key first, Key second) {
+            return predicate.isInterestedInKeysBetween(first, second);
         }
-    }
-
-    class BetweenVisitor<Key extends Comparable<? super Key>, Value> extends
PredicateVisitor<Key, Value> {
-		private final Key first;
-        private final Key last;
 
-        public BetweenVisitor(Key first, Key last) {
-            this(first, last, UNLIMITED);
-        }
-
-        public BetweenVisitor(Key first, Key last, int limit) {
-            super(limit);
-			this.first = first;
-            this.last = last;
-        }
-
-		final public boolean isInterestedInKeysBetween(Key left, Key right) {
-        	return (right==null || right.compareTo(first)>=0)
-                    && (left==null || left.compareTo(last)<0);
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            return key.compareTo(first) >=0 && key.compareTo(last) <0;
-        }
-
-        @Override
-        public String toString() {
-            return first+" <= key < "+last;
-        }
-    }
-
-    class GTVisitor<Key extends Comparable<? super Key>, Value> extends PredicateVisitor<Key,
Value> {
-		final private Key value;
-
-		public GTVisitor(Key value) {
-			this(value, UNLIMITED);
-		}
-		public GTVisitor(Key value, int limit) {
-		    super(limit);
-			this.value = value;
-		}
-
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-        	return second==null || isInterestedInKey(second);
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            return key.compareTo(value)>0;
-        }
-
-        @Override
-        public String toString() {
-            return "key > "+ value;
+        public boolean isSatiated() {
+            return limit==0;
         }
-    }
-
-    class GTEVisitor<Key extends Comparable<? super Key>, Value> extends PredicateVisitor<Key,
Value> {
-		final private Key value;
-
-        public GTEVisitor(Key value) {
-            this(value, UNLIMITED);
+        
+        /**
+         * Subclasses should override.  This method will be called for each key,
+         * value pair that matches the predicate.
+         * 
+         * @param key
+         * @param value
+         */
+        protected void matched(Key key, Value value) {
         }
-
-		public GTEVisitor(Key value, int limit) {
-            super(limit);
-			this.value = value;
-		}
-
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-        	return second==null || isInterestedInKey(second);
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            return key.compareTo(value)>=0;
+        
+        // 
+        // Helper static methods to help create predicate expressions.
+        //
+        
+        public static <Key> Predicate<Key> or(Predicate<Key>... conditions)
{
+            return new Predicate.OrPredicate<Key>(Arrays.asList(conditions));
         }
-
-        @Override
-        public String toString() {
-            return "key >= "+ value;
+        
+        public static <Key> Predicate<Key> or(List<Predicate<Key>>
conditions) {
+            return new Predicate.OrPredicate<Key>(conditions);
         }
-    }
-
-    class LTVisitor<Key extends Comparable<? super Key>, Value> extends PredicateVisitor<Key,
Value> {
-		final private Key value;
-
-        public LTVisitor(Key value) {
-            this(value, UNLIMITED);
+        
+        public static <Key> Predicate<Key> and(Predicate<Key>... conditions)
{
+            return new Predicate.AndPredicate<Key>(Arrays.asList(conditions));
         }
         
-		public LTVisitor(Key value, int limit) {
-            super(limit);
-			this.value = value;
-		}
-
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-        	return first==null || isInterestedInKey(first);
-		}
-
-        final public boolean isInterestedInKey(Key key) {
-            return key.compareTo(value)<0;
+        public static <Key> Predicate<Key> and(List<Predicate<Key>>
conditions) {
+            return new Predicate.AndPredicate<Key>(conditions);
+        }        
+
+        public static <Key extends Comparable<? super Key>> Predicate<Key>
gt(Key key) {
+            return new Predicate.GTPredicate<Key>(key);
+        }        
+        public static <Key extends Comparable<? super Key>> Predicate<Key>
gte(Key key) {
+            return new Predicate.GTEPredicate<Key>(key);
+        }        
+
+        public static <Key extends Comparable<? super Key>> Predicate<Key>
lt(Key key) {
+            return new Predicate.LTPredicate<Key>(key);
+        }        
+        public static <Key extends Comparable<? super Key>> Predicate<Key>
lte(Key key) {
+            return new Predicate.LTEPredicate<Key>(key);
         }
-
-        @Override
-        public String toString() {
-            return "key < "+ value;
+        
+        public static <Key extends Comparable<? super Key>> Predicate<Key>
lte(Key first, Key last) {
+            return new Predicate.BetweenPredicate<Key>(first, last);
         }
+        
     }
 
-    class LTEVisitor<Key extends Comparable<? super Key>, Value> extends PredicateVisitor<Key,
Value> {
-		final private Key value;
-
-		public LTEVisitor(Key value) {
-            this(value, UNLIMITED);
-		}
-        public LTEVisitor(Key value, int limit) {
-            super(limit);
-            this.value = value;
-        }
-
-		final public boolean isInterestedInKeysBetween(Key first, Key second) {
-        	return first==null || isInterestedInKey(first);
-		}
 
-        final public boolean isInterestedInKey(Key key) {
-            return key.compareTo(value)<=0;
-        }
-
-        @Override
-        public String toString() {
-            return "key <= "+ value;
-        }
-    }
 }
\ No newline at end of file

Added: activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/Predicate.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/Predicate.java?rev=829273&view=auto
==============================================================================
--- activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/Predicate.java
(added)
+++ activemq/sandbox/activemq-apollo/hawtdb/src/main/java/org/apache/hawtdb/api/Predicate.java
Fri Oct 23 23:07:05 2009
@@ -0,0 +1,223 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.hawtdb.api;
+
+import java.util.List;
+
+/**
+ * Handy predicates for restricting the range of keys visited in a visitor.
+ * 
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
+ * @param <Key>
+ */
+public interface Predicate<Key> {
+    boolean isInterestedInKeysBetween(Key first, Key second);
+    boolean isInterestedInKey(Key key);
+    
+    
+    class OrPredicate<Key> implements Predicate<Key> {
+        private final List<Predicate<Key>> conditions;
+
+        public OrPredicate(List<Predicate<Key>> conditions) {
+            this.conditions = conditions;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            for (Predicate<Key> condition : conditions) {
+                if( condition.isInterestedInKeysBetween(first, second) ) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            for (Predicate<Key> condition : conditions) {
+                if( condition.isInterestedInKey(key) ) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder();
+            boolean first=true;
+            for (Predicate<Key> condition : conditions) {
+                if( !first ) {
+                    sb.append(" OR ");
+                }
+                first=false;
+                sb.append("(");
+                sb.append(condition);
+                sb.append(")");
+            }
+            return sb.toString();
+        }
+    }
+
+    class AndPredicate<Key> implements Predicate<Key> {
+        private final List<Predicate<Key>> conditions;
+
+        public AndPredicate(List<Predicate<Key>> conditions) {
+            this.conditions = conditions;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            for (Predicate<Key> condition : conditions) {
+                if( !condition.isInterestedInKeysBetween(first, second) ) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            for (Predicate<Key> condition : conditions) {
+                if( !condition.isInterestedInKey(key) ) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder();
+            boolean first=true;
+            for (Predicate<Key> condition : conditions) {
+                if( !first ) {
+                    sb.append(" AND ");
+                }
+                first=false;
+                sb.append("(");
+                sb.append(condition);
+                sb.append(")");
+            }
+            return sb.toString();
+        }
+    }
+
+    class BetweenPredicate<Key extends Comparable<? super Key>> implements Predicate<Key>
{
+        private final Key first;
+        private final Key last;
+
+        public BetweenPredicate(Key first, Key last) {
+            this.first = first;
+            this.last = last;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key left, Key right) {
+            return (right==null || right.compareTo(first)>=0)
+                    && (left==null || left.compareTo(last)<0);
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            return key.compareTo(first) >=0 && key.compareTo(last) <0;
+        }
+
+        @Override
+        public String toString() {
+            return first+" <= key < "+last;
+        }
+    }
+
+    class GTPredicate<Key extends Comparable<? super Key>> implements Predicate<Key>
{
+        final private Key value;
+
+        public GTPredicate(Key value) {
+            this.value = value;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            return second==null || isInterestedInKey(second);
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            return key.compareTo(value)>0;
+        }
+
+        @Override
+        public String toString() {
+            return "key > "+ value;
+        }
+    }
+
+    class GTEPredicate<Key extends Comparable<? super Key>> implements Predicate<Key>
{
+        final private Key value;
+
+        public GTEPredicate(Key value) {
+            this.value = value;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            return second==null || isInterestedInKey(second);
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            return key.compareTo(value)>=0;
+        }
+
+        @Override
+        public String toString() {
+            return "key >= "+ value;
+        }
+    }
+
+    class LTPredicate<Key extends Comparable<? super Key>> implements Predicate<Key>
{
+        final private Key value;
+
+        public LTPredicate(Key value) {
+            this.value = value;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            return first==null || isInterestedInKey(first);
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            return key.compareTo(value)<0;
+        }
+
+        @Override
+        public String toString() {
+            return "key < "+ value;
+        }
+    }
+
+    class LTEPredicate<Key extends Comparable<? super Key>> implements Predicate<Key>
{
+        final private Key value;
+
+        public LTEPredicate(Key value) {
+            this.value = value;
+        }
+
+        final public boolean isInterestedInKeysBetween(Key first, Key second) {
+            return first==null || isInterestedInKey(first);
+        }
+
+        final public boolean isInterestedInKey(Key key) {
+            return key.compareTo(value)<=0;
+        }
+
+        @Override
+        public String toString() {
+            return "key <= "+ value;
+        }
+    }    
+}
\ No newline at end of file



Mime
View raw message