Author: chirino
Date: Fri Sep 12 11:23:29 2008
New Revision: 694765

URL: http://svn.apache.org/viewvc?rev=694765&view=rev
Log:
Exposing a getLast() and getFirst() method on the index.


Modified:
    
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeIndex.java
    activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeNode.java

Modified: 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeIndex.java
URL: 
http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeIndex.java?rev=694765&r1=694764&r2=694765&view=diff
==============================================================================
--- 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeIndex.java 
(original)
+++ 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeIndex.java 
Fri Sep 12 11:23:29 2008
@@ -223,10 +223,14 @@
         getRoot(tx).visit(tx, visitor);
     }
 
-    synchronized Value getFirst(Transaction tx) throws IOException {
+    synchronized public Map.Entry<Key,Value> getFirst(Transaction tx) throws 
IOException {
         return getRoot(tx).getFirst(tx);
     }
 
+    synchronized public Map.Entry<Key,Value> getLast(Transaction tx) throws 
IOException {
+        return getRoot(tx).getLast(tx);
+    }
+
     ///////////////////////////////////////////////////////////////////
     // Internal implementation methods
     ///////////////////////////////////////////////////////////////////

Modified: 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeNode.java
URL: 
http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeNode.java?rev=694765&r1=694764&r2=694765&view=diff
==============================================================================
--- 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeNode.java 
(original)
+++ 
activemq/sandbox/kahadb/src/main/java/org/apache/kahadb/index/BTreeNode.java 
Fri Sep 12 11:23:29 2008
@@ -55,7 +55,31 @@
     // The next leaf node after this one.  Used for fast iteration of the 
entries.
     private long next = -1;
     
+    private final class KeyValueEntry implements Map.Entry<Key, Value> {
+        private final Key key;
+        private final Value value;
+
+        public KeyValueEntry(Key key, Value value) {
+            this.key = key;
+            this.value = value;
+        }
+
+        public Key getKey() {
+            return key;
+        }
+
+        public Value getValue() {
+            return value;
+        }
+
+        public Value setValue(Value value) {
+            throw new UnsupportedOperationException();
+        }
+
+    }
+
     private final class BTreeIterator implements Iterator<Map.Entry<Key, 
Value>> {
+        
         private final Transaction tx;
         BTreeNode<Key,Value> current;
         int nextIndex;
@@ -83,20 +107,7 @@
                             break;
                         }
                     }  else {
-                        nextEntry = new Map.Entry<Key, Value>() {
-                            private final Key key = current.keys[nextIndex];
-                            private final Value value = 
current.values[nextIndex];
-                            
-                            public Key getKey() {
-                                return key;
-                            }
-                            public Value getValue() {
-                                return value;
-                            }
-                            public Value setValue(Value value) {
-                                throw new UnsupportedOperationException();
-                            }
-                        };
+                        nextEntry = new KeyValueEntry(current.keys[nextIndex], 
current.values[nextIndex]);
                         nextIndex++;
                         break;
                     }
@@ -524,13 +535,26 @@
         }
     }
     
-    public Value getFirst(Transaction tx) throws IOException {
+    public Map.Entry<Key,Value> getFirst(Transaction tx) throws IOException {
         BTreeNode<Key, Value> node = this;
         while( node .isBranch() ) {
             node = node.getChild(tx, 0);
         }
         if( node.values.length>0 ) {
-            return node.values[0];
+            return new KeyValueEntry(keys[0], values[0]);
+        } else {
+            return null;
+        }
+    }
+
+    public Map.Entry<Key,Value> getLast(Transaction tx) throws IOException {
+        BTreeNode<Key, Value> node = this;
+        while( node.isBranch() ) {
+            node = node.getChild(tx, children.length-1);
+        }
+        if( node.values.length>0 ) {
+            int idx = values.length-1;
+            return new KeyValueEntry(keys[idx], values[idx]);
         } else {
             return null;
         }


Reply via email to