Author: elecharny
Date: Thu Jun 21 21:42:35 2012
New Revision: 1352707

URL: http://svn.apache.org/viewvc?rev=1352707&view=rev
Log:
Added the Cursor, Tuple and Transaction classes

Added:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Transaction.java
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Tuple.java

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java?rev=1352707&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java 
(added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java 
Thu Jun 21 21:42:35 2012
@@ -0,0 +1,194 @@
+/*
+ *  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.mavibot.btree;
+
+/**
+ * A Cursor is used to fetch elements in a BTree and is returned by the
+ * @see BTree#browse method. The cursor <strng>must</strong> be closed
+ * when the user is done with it.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ *
+ * @param <K> The type for the Key
+ * @param <V> The type for the stored value
+ */
+public class Cursor<K, V>
+{
+    /** The transaction used for this cursor */
+    private Transaction<K, V> transaction;
+    
+    /** The current leaf */
+    private Leaf<K, V> leaf;
+    
+    /** The current position in the leaf */
+    private int pos;
+    
+    /** The Tuple used to return the results */
+    private Tuple<K, V> tuple = new Tuple<K, V>();
+    
+    
+    /**
+     * Creates a new instance of Cursor, starting on a page at a given 
position.
+     * 
+     * @param transaction The transaction this operation is protected by
+     * @param leaf The page in which the first element is present
+     * @param pos The position of the first element
+     */
+    public Cursor( Transaction<K, V> transaction, Leaf<K, V> leaf, int pos )
+    {
+        this.transaction = transaction;
+        this.leaf = leaf;
+        this.pos = pos;
+    }
+    
+    
+    /**
+     * Find the next key/value
+     * 
+     * @return A Tuple containing the found key and value
+     */
+    public Tuple<K, V> next()
+    {
+        if ( leaf == null )
+        {
+            return new Tuple<K, V>();
+        }
+        
+        if ( pos == leaf.nbElems )
+        {
+            if ( leaf.nextPage == null )
+            {
+                // This is the end : no more value
+                return null;
+            }
+            else
+            {
+                leaf = leaf.nextPage;
+                pos = 0;
+            }
+        }
+
+        tuple.setKey( leaf.keys[pos] );
+        tuple.setValue( leaf.values[pos] );
+        
+        pos++;
+
+        return tuple;
+    }
+    
+    
+    /**
+     * Find the previous key/value
+     * 
+     * @return A Tuple containing the found key and value
+     */
+    public Tuple<K, V> prev()
+    {
+        if ( pos == 0 )
+        {
+            if ( leaf.prevPage == null )
+            {
+                // This is the end : no more value
+                return null;
+            }
+            else
+            {
+                leaf = leaf.prevPage;
+                pos = leaf.getNbElems();
+            }
+        }
+
+        pos--;
+
+        tuple.setKey( leaf.keys[pos] );
+        tuple.setValue( leaf.values[pos] );
+
+        return tuple;
+    }
+    
+    
+    /**
+     * Tells if the cursor can return a next element
+     * @return true if there are some more elements
+     */
+    public boolean hasNext()
+    {
+        if ( leaf == null )
+        {
+            return false;
+        }
+        
+        if ( pos < leaf.nbElems )
+        {
+            return true;
+        }
+        else
+        {
+            if ( leaf.nextPage == null )
+            {
+                // This is the end : no more value
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+    
+    
+    /**
+     * Tells if the cursor can return a previous element
+     * @return true if there are some more elements
+     */
+    public boolean hasPrev()
+    {
+        if ( leaf == null )
+        {
+            return false;
+        }
+        
+        if ( pos > 0 )
+        {
+            return true;
+        }
+        else
+        {
+            if ( leaf.prevPage == null )
+            {
+                // This is the end : no more value
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+    
+    
+    /**
+     * Closes the cursor, thus releases the associated transaction
+     */
+    public void close()
+    {
+        transaction.close();
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Transaction.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Transaction.java?rev=1352707&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Transaction.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Transaction.java
 Thu Jun 21 21:42:35 2012
@@ -0,0 +1,100 @@
+/*
+ *  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.mavibot.btree;
+
+import java.util.Date;
+
+/**
+ * The Transaction is used to protect the BTree against concurrent modifcation,
+ * and insure that a read is always done against one single revision. It's also
+ * used to gather many modifications under one single revision, if needed.
+ * <p/>
+ * A Transaction should be closed when the user is done with it, otherwise the
+ * pages associated with the given revision, and all the referenced pages, will
+ * remain on the storage.
+ * <p/>
+ * A Transaction can be hold for quite a long time, for instance while doing
+ * a browse against a big BTree. At some point, transactions which are pending
+ * for too long will be closed by the transaction manager.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ *
+ * @param <K> The type for the Key
+ * @param <V> The type for the stored value
+ */
+public class Transaction<K, V>
+{
+    /** The associated revision */
+    private long revision;
+    
+    /** The date of creation */
+    private long creationDate;
+    
+    /** The BTree on which we are having a transaction */
+    private BTree<K, V> btree;
+    
+    /**
+     * Creates a new transaction instance
+     * @param revision The revision this transaction is using
+     * @param creationDate The creation date for this transaction
+     */
+    public Transaction( BTree<K, V> btree, long revision, long creationDate )
+    {
+        this.revision = revision;
+        this.creationDate = creationDate;
+        this.btree = btree;
+    }
+
+
+    /**
+     * @return the revision
+     */
+    public long getRevision()
+    {
+        return revision;
+    }
+
+
+    /**
+     * @return the creationDate
+     */
+    public long getCreationDate()
+    {
+        return creationDate;
+    }
+    
+    
+    /**
+     * Close the transaction, releasing the revision it was using.
+     */
+    public void close()
+    {
+        btree.commitTransaction( this );
+    }
+    
+    
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        return "Transaction[" + revision + ":" + new Date( creationDate ) + 
"]";
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Tuple.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Tuple.java?rev=1352707&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Tuple.java 
(added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Tuple.java 
Thu Jun 21 21:42:35 2012
@@ -0,0 +1,92 @@
+/*
+ *  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.mavibot.btree;
+
+/**
+ * The Tuple class is used when we browse a btree, it will contain the results
+ * fetched from the btree.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ *
+ * @param <K> The type for the Key
+ * @param <V> The type for the stored value
+ */
+public class Tuple<K, V>
+{
+    /** The key */
+    private K key;
+    
+    /** The value */
+    private V value;
+    
+    /**
+     * Creates a Tuple containing a key and its associated value.
+     * @param key The key
+     * @param value The associated value
+     */
+    /* No qualifier */ Tuple()
+    {
+    }
+
+
+    /**
+     * @return the key
+     */
+    public K getKey()
+    {
+        return key;
+    }
+
+
+    /**
+     * @param key the key to set
+     */
+    /* No qualifier*/ void setKey( K key )
+    {
+        this.key = key;
+    }
+
+
+    /**
+     * @return the value
+     */
+    public V getValue()
+    {
+        return value;
+    }
+
+
+    /**
+     * @param value the value to set
+     */
+    /* No qualifier*/ void setValue( V value )
+    {
+        this.value = value;
+    }
+
+    
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        return "<" + key + "," + value + ">";
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to