Modified: 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/Store.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/Store.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/Store.java 
(original)
+++ labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/Store.java 
Mon Jun 29 03:30:43 2009
@@ -18,35 +18,31 @@
  * limitations under the License.
  */
 
-import org.apache.labs.bananadb.store.lock.Lock;
+import org.apache.commons.pool.BasePoolableObjectFactory;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.labs.bananadb.store.data.FileHandler;
+import org.apache.labs.bananadb.store.data.FileHandler.Posting;
+import org.apache.labs.bananadb.store.data.Metadata;
 import org.apache.labs.bananadb.store.data.bananatrie.HashCodesPartition;
 import org.apache.labs.bananadb.store.data.bananatrie.Hashtable;
 import org.apache.labs.bananadb.store.data.bananatrie.KeysPartition;
 import org.apache.labs.bananadb.store.data.bananatrie.ValuesPartition;
-import org.apache.labs.bananadb.store.data.Metadata;
-import org.apache.labs.bananadb.store.data.FileHandler;
-import org.apache.labs.bananadb.store.sequence.SequenceManager;
-import org.apache.labs.bananadb.store.sequence.FilebasedSequenceManager;
+import org.apache.labs.bananadb.store.lock.Lock;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
 import java.util.NoSuchElementException;
 
-import org.apache.labs.bananadb.store.data.FileHandler.Posting;
-
 /**
  * This is the core Index<byte[], byte[]> that is stored on filesystem.
  *
- * @see org.apache.labs.bananadb.entity.EntityStore 
- *
- * todo make sure deleted postings are the last postings in all hash code 
chains!
- * <p/>
- * todo comsider implementing lock per file rather than store wide lock! but 
make sure it makes sense!
- *
  * @author kalle
+ * @see org.apache.labs.bananadb.entity.EntityStore
+ *      <p/>
+ *      todo make sure deleted postings are the last postings in all hash code 
chains!
+ *      <p/>
+ *      todo consider implementing lock per file rather than store wide lock! 
but make sure it makes sense!
  * @since 2009-mar-16 15:34:56
  */
 public class Store {
@@ -54,8 +50,8 @@
   private static final Log log = new Log(Store.class);
 
   private Configuration configuration;
-  private SequenceManager sequenceManager;
-  private List<Accessor> accessors = new ArrayList<Accessor>();
+
+  private GenericObjectPool accessorPool;
 
   public Store(File dataPath) throws IOException {
     this(new Configuration(dataPath));
@@ -65,7 +61,6 @@
     this.configuration = configuration;
   }
 
-
   public void open() throws IOException {
     if (!getConfiguration().getDataPath().exists()) {
       if (log.isInfo()) {
@@ -75,36 +70,61 @@
         throw new IOException("Could not create directory " + 
getConfiguration().getDataPath().getAbsolutePath());
       }
     }
-    File sequencePath = new File(configuration.getDataPath(), "seq");
-    sequenceManager = new FilebasedSequenceManager(sequencePath, 
configuration.getLockFactory(), configuration.getLockWaitTimeoutMilliseconds());
-  }
-
-  public SequenceManager getSequenceManager() {
-    return sequenceManager;
+    GenericObjectPool.Config config = new GenericObjectPool.Config();
+    config.maxIdle = 2;
+    config.maxActive = 20;
+    config.maxWait = 1000;
+    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
+    config.minEvictableIdleTimeMillis = 30000;
+    accessorPool = new GenericObjectPool(new BasePoolableObjectFactory() {
+      public Object makeObject() throws Exception {
+        return new Accessor(Store.this, false);
+      }
+
+      @Override
+      public void destroyObject(Object o) throws Exception {
+        ((Accessor) o).close();
+      }
+    }, config);
   }
 
+  public void close() throws IOException {
+    log.info("Closing store..");
+//    if (accessors.size() > 0) {
+//      log.warn("There are " + accessors.size() + " open accessors. They will 
be closed.");
+//    }
+//    for (Accessor accessor : new ArrayList<Accessor>(accessors)) {
+//      accessor.close();
+//    }
+    try {
+      accessorPool.close();
+    } catch (IOException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+    log.info("Store has been closed.");
 
-  List<Accessor> getAccessors() {
-    return accessors;
   }
 
-  public Accessor createAccessor(boolean readOnly) throws IOException {
-    Accessor accessor = new Accessor(this, readOnly);
-    accessors.add(accessor);
-    return accessor;
+  public Accessor borrowAccessor() throws IOException {
+    try {
+      return (Accessor) accessorPool.borrowObject();
+    } catch (IOException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
   }
 
-  public void close() throws IOException {
-    log.info("Closing store..");
-    if (accessors.size() > 0) {
-      log.warn("There are " + accessors.size() + " open accessors. They will 
be closed.");
+  public void returnAccessor(Accessor accessor) throws IOException {
+    try {
+      accessorPool.returnObject(accessor);
+    } catch (IOException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
     }
-    for (Accessor accessor : new ArrayList<Accessor>(accessors)) {
-      accessor.close();
-    }
-    sequenceManager.close();
-    log.info("Store has been closed.");
-
   }
 
   private void validateKey(byte[] key) {
@@ -156,7 +176,7 @@
 
     KeysPartition.Posting keyPosting = new KeysPartition.Posting();
 
-    KeysPartition keysPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition());
+    KeysPartition keysPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition(), true);
     keysPartition.readPosting(keyPosting, 
hashCodePosting.getFirstKeyPostingPartitionOffset());
     while (true) {
 
@@ -169,7 +189,7 @@
         return null;
       }
       if (keyPosting.getNextKeyPostingPartition() != 
keysPartition.getPartitionId()) {
-        keysPartition = 
accessor.getKeysPartition(keyPosting.getNextKeyPostingPartition());
+        keysPartition = 
accessor.getKeysPartition(keyPosting.getNextKeyPostingPartition(), true);
       }
       keysPartition.readPosting(keyPosting, 
keyPosting.getNextKeyPostingPartitionOffset());
     }
@@ -331,7 +351,7 @@
 
 
       return null;
-    
+
     } else {
 
       // there is a hashtable posting at the position for this hash code
@@ -404,7 +424,7 @@
       // seek for the same key
       //
 
-      KeysPartition currentKeyPostingPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition());
+      KeysPartition currentKeyPostingPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition(), true);
       KeysPartition previousKeyPostingPartition = null;
 
       KeysPartition.Posting currentKeyPosting = new KeysPartition.Posting();
@@ -435,7 +455,7 @@
         }
 
         if (currentKeyPosting.getNextKeyPostingPartition() != 
currentKeyPostingPartition.getPartitionId()) {
-          currentKeyPostingPartition = 
accessor.getKeysPartition(currentKeyPosting.getNextKeyPostingPartition());
+          currentKeyPostingPartition = 
accessor.getKeysPartition(currentKeyPosting.getNextKeyPostingPartition(), true);
         }
 
         previousKeyPostingPartition = currentKeyPostingPartition;
@@ -551,7 +571,6 @@
           // no durable postings
 
 
-
           if (previousKeyPosting.getFlag() == Posting.FLAG_DELETED
               && hashCodePosting.getFirstKeyPostingPartition() == 
previousKeyPostingPartition.getPartitionId()
               && hashCodePosting.getFirstKeyPostingPartitionOffset() == 
previousKeyPostingPartitionOffset) {
@@ -584,7 +603,7 @@
             previousKeyPosting.setNextKeyPostingPartitionOffset(-1);
             previousKeyPosting.setNextKeyPostingPartitionOffset(-1);
             previousKeyPostingPartition.writePosting(previousKeyPosting, 
previousKeyPostingPartitionOffset);
-            
+
           } else {
 
             // we have [pk] [ck]
@@ -605,7 +624,7 @@
             // mark old disconnected key as deleted
             // [ck deleted]
             
currentKeyPostingPartition.markPostingAsDeleted(currentKeyPostingPartitionOffset,
 revision);
-            
+
 
           }
         }
@@ -722,7 +741,7 @@
     //
 
 
-    KeysPartition currentKeyLinkPostingPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition());
+    KeysPartition currentKeyLinkPostingPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition(), true);
     KeysPartition.Posting currentKeyLinkPosting = new KeysPartition.Posting();
     int currentKeyPostingPartitionOffset = 
hashCodePosting.getFirstKeyPostingPartitionOffset();
 
@@ -746,7 +765,7 @@
       }
 
       if (currentKeyLinkPosting.getNextKeyPostingPartition() != 
currentKeyLinkPostingPartition.getPartitionId()) {
-        currentKeyLinkPostingPartition = 
accessor.getKeysPartition(currentKeyLinkPosting.getNextKeyPostingPartition());
+        currentKeyLinkPostingPartition = 
accessor.getKeysPartition(currentKeyLinkPosting.getNextKeyPostingPartition(), 
true);
       }
       previousKeyPostingPartition = currentKeyLinkPostingPartition;
       previousKeyPostingPartitionOffset = currentKeyPostingPartitionOffset;
@@ -810,7 +829,7 @@
         KeysPartition lastKeyLinkPartition;
         while (true) {
           lastKeyLinkPartitionPostingOffset = 
lastKeyLinkPosting.getNextKeyPostingPartitionOffset();
-          lastKeyLinkPartition = 
accessor.getKeysPartition(lastKeyLinkPosting.getNextKeyPostingPartition());
+          lastKeyLinkPartition = 
accessor.getKeysPartition(lastKeyLinkPosting.getNextKeyPostingPartition(), 
true);
           lastKeyLinkPartition.readPosting(lastKeyLinkPosting, 
lastKeyLinkPosting.getNextKeyPostingPartitionOffset());
           if (lastKeyLinkPosting.getNextKeyPostingPartition() < 0) {
             break;
@@ -914,7 +933,7 @@
 
     KeysPartition.Posting keyPosting = new KeysPartition.Posting();
 
-    KeysPartition keysPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition());
+    KeysPartition keysPartition = 
accessor.getKeysPartition(hashCodePosting.getFirstKeyPostingPartition(), true);
     keysPartition.readPosting(keyPosting, 
hashCodePosting.getFirstKeyPostingPartitionOffset());
     while (true) {
       if (keyPosting.getFlagForRevision(revision) == Posting.FLAG_IN_USE
@@ -926,7 +945,7 @@
         return false;
       }
       if (keyPosting.getNextKeyPostingPartition() != 
keysPartition.getPartitionId()) {
-        keysPartition = 
accessor.getKeysPartition(keyPosting.getNextKeyPostingPartition());
+        keysPartition = 
accessor.getKeysPartition(keyPosting.getNextKeyPostingPartition(), true);
       }
       keysPartition.readPosting(keyPosting, 
keyPosting.getNextKeyPostingPartitionOffset());
     }
@@ -935,15 +954,29 @@
 
   }
 
-  public Cursor values() {
-    // todo iterate all value partitions
-    throw new UnsupportedOperationException();
-  }
+  public Cursor<KeysPartition.Posting> keys() {
+    return new Cursor<KeysPartition.Posting>() {
 
-  public Cursor keys() {
-    // todo iterate all key partitions
-    throw new UnsupportedOperationException();
+      private int nextOffset = KeysPartition.HEADER_BYTE_SIZE;
+      private int nextPartition = 0;
 
+      public KeysPartition.Posting next(Accessor accessor, 
KeysPartition.Posting posting, long revision) throws IOException {
+        while (true) {
+          KeysPartition keysPartition = 
accessor.getKeysPartition(nextPartition, false);
+          if (keysPartition == null) {
+            return null;
+          }
+          keysPartition.readPosting(posting, nextOffset);
+          nextOffset += posting.getPostingByteSize();
+          if (posting.getFlag() == 0) {
+            nextPartition++;
+            nextOffset = KeysPartition.HEADER_BYTE_SIZE;
+          } else if (posting.getCreatedRevision() <= revision && 
(posting.getDeletedRevision() == -1 || posting.getDeletedRevision() > 
revision)) {
+            return posting;
+          }
+        }
+      }
+    };
   }
 
   /**
@@ -1151,7 +1184,4 @@
 
   }
 
-  public void setSequenceManager(SequenceManager sequenceManager) {
-    this.sequenceManager = sequenceManager;
-  }
 }

Modified: 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/FileHandler.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/FileHandler.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/FileHandler.java
 (original)
+++ 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/FileHandler.java
 Mon Jun 29 03:30:43 2009
@@ -101,10 +101,10 @@
   }
 
   public void close() throws IOException {
-    if (RAF == null) {
-      throw new IOException("Already closed");
+    if (RAF != null) {
+      RAF.close();
     }
-    RAF.close();
+
   }
 
   public abstract int getHeaderByteSize();
@@ -204,11 +204,11 @@
 //    readPosting(posting, getRAF());
 //  }
 
-  public void readPosting(P posting, int startOffset) throws IOException {
+  public void readPosting(P posting, long startOffset) throws IOException {
     readPosting(posting, startOffset, getRAF());
   }
 
-  public void readPosting(P posting, int startOffset, RandomAccessFile RAF) 
throws IOException {
+  public void readPosting(P posting, long startOffset, RandomAccessFile RAF) 
throws IOException {
     RAF.seek(startOffset);
     readPosting(posting, RAF);
   }

Modified: 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/bananatrie/KeysPartition.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/bananatrie/KeysPartition.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/bananatrie/KeysPartition.java
 (original)
+++ 
labs/bananadb/trunk/src/main/java/org/apache/labs/bananadb/store/data/bananatrie/KeysPartition.java
 Mon Jun 29 03:30:43 2009
@@ -18,12 +18,13 @@
  */
 
 
-import org.apache.labs.bananadb.store.lock.LockFactory;
 import org.apache.labs.bananadb.store.data.FileHandler;
+import org.apache.labs.bananadb.store.lock.LockFactory;
 
-import java.io.IOException;
 import java.io.File;
+import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.Arrays;
 
 /**
  * Key postings partition file.
@@ -135,6 +136,11 @@
       return 1 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + bytesLength + 8;
     }
 
+    /**
+     * 0 = never used
+     * 1 = in use
+     * 2 = deleted
+     */
     public byte getFlag() {
       return flag;
     }
@@ -214,6 +220,22 @@
     public void setDeletedRevision(long deletedRevision) {
       this.deletedRevision = deletedRevision;
     }
+
+    @Override
+    public String toString() {
+      return "Posting{" +
+          "flag=" + flag +
+          ", createdRevision=" + createdRevision +
+          ", nextKeyPostingPartition=" + nextKeyPostingPartition +
+          ", nextKeyPostingPartitionOffset=" + nextKeyPostingPartitionOffset +
+          ", keyHashCode=" + keyHashCode +
+          ", valuePostingPartition=" + valuePostingPartition +
+          ", valuePostingPartitionOffset=" + valuePostingPartitionOffset +
+          ", bytesLength=" + bytesLength +
+          ", bytes=" + (bytes == null ? null : Arrays.asList(bytes)) +
+          ", deletedRevision=" + deletedRevision +
+          '}';
+    }
   }
 
   public void readHeader(Header header, RandomAccessFile RAF) throws 
IOException {

Added: labs/bananadb/trunk/src/main/java/package.html
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/main/java/package.html?rev=789204&view=auto
==============================================================================
--- labs/bananadb/trunk/src/main/java/package.html (added)
+++ labs/bananadb/trunk/src/main/java/package.html Mon Jun 29 03:30:43 2009
@@ -0,0 +1,58 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+        "http://www.w3.org/TR/html4/loose.dtd";>
+<html>
+<head>
+    <title>Banana DB</title>
+</head>
+
+Example usage:
+
+Domain object annotation:
+<pre>
+    @Entity
+    public static class User implements Serializable {
+      private static final long serialVersionUID = 1l;
+        @PrimaryKey
+        @Sequence
+        private Long identity;
+
+        private String emailAdress;
+        private String password;
+
+        public Long getIdentity() {
+          return PK;
+        ...
+</pre>
+
+Putting and getting objects:
+    <pre>
+    EntityStore store = new EntityStore(new File("bananadb"));
+    store.open;
+
+    PrimaryIndex&ltLong, User> users = store.getPrimaryIndex(Long.class, 
User.class);
+
+    // optionally transactional
+    // store.getTxn().begin();
+
+    User user = new User();
+    user.setEmailAdress("[email protected]");
+    user.setPassword("1");
+    assertNull(user.getId());
+    assertNull(store.put(user));
+    assertNotNull(user.getId());
+
+    user.setPassword("2");
+    User oldUser = store.put(user);
+    assertEquals("1", oldUser.getPassword());
+    assertEquals("2", user.getPassword());
+
+    // optionally transactional
+    // store.getTxn().commit();
+
+     </pre>
+
+
+<body>
+
+</body>
+</html>
\ No newline at end of file

Modified: labs/bananadb/trunk/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/site/apt/index.apt?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- labs/bananadb/trunk/src/site/apt/index.apt (original)
+++ labs/bananadb/trunk/src/site/apt/index.apt Mon Jun 29 03:30:43 2009
@@ -1,37 +1,34 @@
     -----
-    self-contained key/value pair database for java
+    key/value pair database for java
 
 Banana DB
 
-    Banana DB is a self-contained key/value pair database implemented in Java.
+    Banana DB is a key/value pair database implemented in Java.
 
 
 * Features
 
-    * Small, ~100KB .jar file with no dependencies.
+    * Less than 200KB .jar-files with all dependencies included. 100% Apache.
 
-    * Top level API similar to working with any Map\<K, V\> on to of core 
\<byte[], byte[]\> index.
+    * A core byte array key/value file system storage with cursors.
 
-    * Thread safe and write locking over multiple JVMs using extended Lucene 
locks.
+    * Thread safe and write locking over multiple JVMs (using Lucene locks 
made reentrant).
 
-    * Optionally transactional.
+    * Simple annotational entity object management with optional support for 
transactions.
 
-    * More or less {{{http://en.wikipedia.org/wiki/ACID}ACID}}-compliant.
+    * More or less {{{http://en.wikipedia.org/wiki/ACID}ACID}}-compliant (see 
further down).
 
-    * Simple annotational API.
+    * Pluggable serialization strategies (java.io.Serializable as default).
 
-    * Pluggable serialization strategies. (java.io.Serializable as default)
 
-    
+* Possible upcoming features
 
-* Upcoming features
+    * Secondary indices
 
     * Optimized generic serialization.
 
-    * Secondary indices.
 
-
-* About the ACIDity
+* About the ACID support
 
         * Atomicity, write all postings or no postings at all during commit.
         In cases of fatal shutdowns such as loss of power, this is not 
supported, but is ment to solve it by using the durabillity revision log.
@@ -101,21 +98,41 @@
  I am not a lawyer. Please double-check any information regarding licensing 
models mentioned below.
 
 
-* Berkeley DB
+* Berkeley DB JE
 
     
{{{http://www.oracle.com/database/berkeley-db.html}http://www.oracle.com/database/berkeley-db.html}}
 
-    Berkeley DB is a key/value postings database with ancestry. 
+    Berkeley DB JE is a key/value postings database with ancestry.
+
+    It use the @Entity and @Persistent annotation to seperate your classes
+    in those that have primary keys and they who are just embedded in your 
entities,
+    similar to the JPA @Entity and @Embeddable.
+
+    This strategy makes it impossible for one entity class to contain 
normalized aggreated entity instances.
+
++-------------------------------+
+
+  @Entity
+  public class Track {
+    @PrimaryKey
+    private Long id;
+    private Album album;
+  }
+
+  @Entity
+  public class Album {
+    @PrimaryKey
+    private Long id;
+    private List<Track> albums;
+  }
+
++-------------------------------+
 
-    The Banana DB entity store consumer API
-    has a consumer API not too different from the one in Berkeley DB Java 
Edition.
-    Both are minimalistic and simple to use,
-    Berkeley DB does however have a few restrictions
-    on how to one is allowed to subclass and aggregate entity objects.
+    Above code snippet is invalid for Berkeley DB JE but valid for Banana DB.
 
-    Berkeley DB handles huge data dataset on HDD well, Banana DB does not.
+    Berkeley DB JE handles huge data dataset on HDD well, Banana DB probably 
does not.
 
-    Berkeley DB has a dual license model.
+    Berkeley DB JE has a dual license model.
 
     The license model for proprietary projects
     allows you to use Berkeley DB at no cost
@@ -125,13 +142,18 @@
     The license model for open source projects
     allows you to redistribute Berkeley DB.
 
+* Project Voldemort
+
+   {{{http://project-voldemort.com/}http://project-voldemort.com/}}
+
+   Worth mentioning.
 
 * JDBM
 
     {{{http://jdbm.sourceforge.net/}http://jdbm.sourceforge.net/}}
 
     JDBM has similar features as Banana DB
-    but a more complex consumer API compared to Banana DB.
+    but I don't like their consumer API.
 
     JDBM has a pluggable storage and include support complex data formats such 
as B+Tree and HTree
     and thus handles huge data dataset on HDD well, Banana DB does not.
@@ -140,81 +162,47 @@
 
  
 
-API examples
+Simple consumer API example
 
 +-------------------------------+
 
-{
-  EntityStore store = new EntityStore(new File("./bananadb"));
-  store.getConfiguration().setUsingDurablePostings(true);
-
-  PrimaryIndex<Long, User> users = store.getPrimaryIndex(Long.class, 
User.class);
-  PrimaryIndex<Long, Message> messages = store.getPrimaryIndex(Long.class, 
Message.class);
-
-  store.getTxn().begin();
-
-  User user = new User();
-  user.setPK(0l);
-  user.setEmailAdress("[email protected]");
-  user.setPassword("secret");
-
-  users.put(user);
-  assert users.containsKey(0l);
-
-  Message message = new Message();
-  message.setPK(0l);
-  message.setFromUserFK(0l);
-  message.setToUserFK(1l);
-  message.setSubject("Hello, world.");
-  messages.put(message);
-  long worldRevision = store.getTxn().commit();
-
-  store.getTxn().begin();
-  message.setSubject("Hello, tellus.");
-  messages.put(message);
-  store.getTxn().commit();
-
-  store.getTxn().begin();
-  assert "Hello, tellus.".equals(store.getMessages(0l).getMessage());
-
-  store.setDefaultReadRevision(worldRevision);
-  assert "Hello, world.".equals(store.getMessages(0l).getMessage());
-  store.getTxn().abort();
-
-}
-
-...@entity
-public static class User implements Serializable {
-
-private static final long serialVersionUID = 1l;
-
-  @PrimaryKey
-  private Long PK;
-
-  private String emailAdress;
-  private String password;
-
-  public Long getPK() {
-    return PK;
-
-  ...
-}
-
-...@entity
-public static class Message implements Serializable {
-
-  private static final long serialVersionUID = 1l;
-
-  @PrimaryKey
-  private Long PK;
-
-  private Long fromUserFK;
-  private Long toUserFK;
-  private String subject;
-  private String text;
+    @Entity
+    public static class User implements Serializable {
+      private static final long serialVersionUID = 1l;
+        @PrimaryKey
+        @Sequence
+        private Long identity;
+
+        private String emailAdress;
+        private String password;
+
+        public Long getIdentity() {
+          return PK;
+        ...
+
+
+    EntityStore store = new EntityStore(new File("bananadb"));
+    store.open;
+
+    PrimaryIndex&ltLong, User> users = store.getPrimaryIndex(Long.class, 
User.class);
+
+    // optionally transactional
+    // store.getTxn().begin();
+
+    User user = new User();
+    user.setEmailAdress("[email protected]");
+    user.setPassword("1");
+    assertNull(user.getId());
+    assertNull(store.put(user));
+    assertNotNull(user.getId());
+
+    user.setPassword("2");
+    User oldUser = store.put(user);
+    assertEquals("1", oldUser.getPassword());
+    assertEquals("2", user.getPassword());
 
-  ...
-}
+    // optionally transactional
+    // store.getTxn().commit();
 
 +-------------------------------+
 

Modified: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestEntityStore.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestEntityStore.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestEntityStore.java
 (original)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestEntityStore.java
 Mon Jun 29 03:30:43 2009
@@ -28,6 +28,44 @@
  */
 public class TestEntityStore extends EntityStoreTest {
 
+  @Test
+  public void testEntityCursor() throws Exception {
+
+    EntityStore store = entityStoreFactory("entityStore/testEntityCursor");
+
+    PrimaryIndex<Long, Message> messages = store.getPrimaryIndex(Long.class, 
Message.class);
+
+    messages.put(new Message(1l, "one"));
+    messages.put(new Message(2l, "two"));
+    messages.put(new Message(3l, "three"));
+    messages.put(new Message(4l, "four"));
+
+    EntityCursor<Long, Message> cursor = messages.cursor();
+
+    assertTrue(cursor.next());
+    assertEquals(1l, (long)cursor.key());
+    assertEquals("one", cursor.value().getSubject());
+
+    assertTrue(cursor.next());
+    assertEquals(2l, (long)cursor.key());
+    assertEquals("two", cursor.value().getSubject());
+
+    assertTrue(cursor.next());
+    assertEquals(3l, (long)cursor.key());
+    assertEquals("three", cursor.value().getSubject());
+
+    assertTrue(cursor.next());
+    assertEquals(4l, (long)cursor.key());
+    assertEquals("four", cursor.value().getSubject());
+
+    assertFalse(cursor.next());
+
+    cursor.close();
+
+    store.close();
+
+  }
+
 
   @Test
   public void testPrimaryIndex() throws Exception {
@@ -235,6 +273,13 @@
     private String subject;
     private String text;
 
+    public Message() {
+    }
+
+    public Message(Long PK, String subject) {
+      this.PK = PK;
+      this.subject = subject;
+    }
 
     public Long getPK() {
       return PK;

Modified: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestTransactionRevisions.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestTransactionRevisions.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestTransactionRevisions.java
 (original)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/entity/TestTransactionRevisions.java
 Mon Jun 29 03:30:43 2009
@@ -20,18 +20,14 @@
     store.getConfiguration().setUsingDurablePostingLinks(false);
     PrimaryIndex<Long, EntityClass> index = store.getPrimaryIndex(Long.class, 
EntityClass.class);
 
-    Accessor accessor = store.createAccessor(false);
-    Metadata.Header mdh = new Metadata.Header();
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(0l, mdh.getStoreRevision());
+    assertEquals(0l, store.getStoreRevision());
 
 
     store.getTxn().begin();
     index.put(new EntityClass(0l, "A"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(1l, mdh.getStoreRevision());
+    assertEquals(1l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -39,8 +35,7 @@
     index.put(new EntityClass(2l, "C"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(2l, mdh.getStoreRevision());
+    assertEquals(2l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -49,16 +44,14 @@
     index.remove(1l);
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(3l, mdh.getStoreRevision());
+    assertEquals(3l, store.getStoreRevision());
 
 
     store.getTxn().begin();
     index.remove(2l);
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(4l, mdh.getStoreRevision());
+    assertEquals(4l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -66,8 +59,7 @@
     index.put(new EntityClass(4l, "F"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(5l, mdh.getStoreRevision());
+    assertEquals(5l, store.getStoreRevision());
 
     store.getTxn().begin();
 
@@ -171,18 +163,14 @@
 
     PrimaryIndex<Long, EntityClass> index = store.getPrimaryIndex(Long.class, 
EntityClass.class);
 
-    Accessor accessor = store.createAccessor(false);
-    Metadata.Header mdh = new Metadata.Header();
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(0l, mdh.getStoreRevision());
+    assertEquals(0l, store.getStoreRevision());
 
 
     store.getTxn().begin();
     index.put(new EntityClass(0l, "A"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(1l, mdh.getStoreRevision());
+    assertEquals(1l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -190,8 +178,7 @@
     index.put(new EntityClass(2l, "C"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(2l, mdh.getStoreRevision());
+    assertEquals(2l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -200,16 +187,14 @@
     index.remove(1l);
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(3l, mdh.getStoreRevision());
+    assertEquals(3l, store.getStoreRevision());
 
 
     store.getTxn().begin();
     index.remove(2l);
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(4l, mdh.getStoreRevision());
+    assertEquals(4l, store.getStoreRevision());
 
 
     store.getTxn().begin();
@@ -217,8 +202,7 @@
     index.put(new EntityClass(4l, "F"));
     store.getTxn().commit();
 
-    accessor.getMetadata().readHeader(mdh);
-    assertEquals(5l, mdh.getStoreRevision());
+    assertEquals(5l, store.getStoreRevision());
 
     store.getTxn().begin();
 

Added: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestCursor.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestCursor.java?rev=789204&view=auto
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestCursor.java
 (added)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestCursor.java
 Mon Jun 29 03:30:43 2009
@@ -0,0 +1,67 @@
+package org.apache.labs.bananadb.store;
+
+import org.junit.Test;
+import org.apache.labs.bananadb.store.data.bananatrie.KeysPartition;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+
+/**
+ * @author kalle
+ * @since 2009-jun-29 01:04:22
+ */
+public class TestCursor extends StoreTest {
+
+  @Test
+  public void testCursor() throws IOException {
+
+    Charset utf8 = Charset.forName("UTF8");
+
+    Configuration configuration = new 
Configuration(getDirectory("testCursor"));
+    configuration.setInitialCapacity(100);
+    configuration.setHashCodesPartitionByteSize(10000);
+    configuration.setKeysPartitionByteSize(10000);
+    configuration.setValuesPartitionByteSize(10000);
+    Store store = new Store(configuration);
+    store.open();
+
+    long[] keyHashCodes = new long[]{
+        1, 2, 3, 4
+    };
+
+    byte[][] keys = new byte[][]{
+        new byte[]{0x01},
+        new byte[]{0x02},
+        new byte[]{0x03},
+        new byte[]{0x04},
+    };
+
+    byte[][] values = new byte[][]{
+        "one".getBytes(utf8),
+        "two".getBytes(utf8),
+        "three".getBytes(utf8),
+        "four".getBytes(utf8),
+    };
+
+    Accessor accessor = store.borrowAccessor();
+    for (int i = 0; i < 4; i++) {
+      store.put(accessor, keys[i], keyHashCodes[i], values[i], 0);
+    }
+
+    Cursor<KeysPartition.Posting> cursor = store.keys();
+
+    KeysPartition.Posting posting = new KeysPartition.Posting();
+    for (int i = 0; i < 4; i++) {
+      posting = cursor.next(accessor, posting, 0);
+      assertNotNull(posting);
+      assertTrue("At position " + i, Arrays.equals(keys[i], 
posting.getBytes()));
+    }
+    assertNull(cursor.next(accessor, posting, 0));
+
+    store.returnAccessor(accessor);
+
+    store.close();
+  }
+
+}

Modified: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestPostings.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestPostings.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestPostings.java
 (original)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestPostings.java
 Mon Jun 29 03:30:43 2009
@@ -27,7 +27,6 @@
 
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.Random;
 
 /**
  * @author kalle
@@ -48,7 +47,7 @@
     Accessor accessor;
 
 
-    accessor = store.createAccessor(false);
+    accessor = store.borrowAccessor();
     long revision = 1;
 
     store.put(accessor, keys[0], hashes[0], values[0], revision);
@@ -71,7 +70,7 @@
     assertEquals(hashes[0], hcp.getKeyHashCode());
 
     KeysPartition.Posting kp = new KeysPartition.Posting();
-    
accessor.getKeysPartition(hcp.getFirstKeyPostingPartition()).readPosting(kp, 
hcp.getFirstKeyPostingPartitionOffset());
+    accessor.getKeysPartition(hcp.getFirstKeyPostingPartition(), 
true).readPosting(kp, hcp.getFirstKeyPostingPartitionOffset());
 
 
     assertEquals(FileHandler.Posting.FLAG_IN_USE, kp.getFlag());
@@ -111,7 +110,7 @@
     assertEquals(hashes[0], hcp.getKeyHashCode());
 
     kp = new KeysPartition.Posting();
-    
accessor.getKeysPartition(hcp.getFirstKeyPostingPartition()).readPosting(kp, 
hcp.getFirstKeyPostingPartitionOffset());
+    accessor.getKeysPartition(hcp.getFirstKeyPostingPartition(), 
true).readPosting(kp, hcp.getFirstKeyPostingPartitionOffset());
 
     // the new posting first
 
@@ -133,7 +132,7 @@
 
 
     // then the old posting
-    accessor.getKeysPartition(kp.getNextKeyPostingPartition()).readPosting(kp, 
kp.getNextKeyPostingPartitionOffset());
+    accessor.getKeysPartition(kp.getNextKeyPostingPartition(), 
true).readPosting(kp, kp.getNextKeyPostingPartitionOffset());
     assertEquals(FileHandler.Posting.FLAG_DELETED, kp.getFlag());
     assertEquals(revision - 1, kp.getCreatedRevision());
     assertEquals(revision, kp.getDeletedRevision());
@@ -175,7 +174,7 @@
     assertEquals(hashes[0], hcp.getKeyHashCode());
 
     kp = new KeysPartition.Posting();
-    
accessor.getKeysPartition(hcp.getFirstKeyPostingPartition()).readPosting(kp, 
hcp.getFirstKeyPostingPartitionOffset());
+    accessor.getKeysPartition(hcp.getFirstKeyPostingPartition(), 
true).readPosting(kp, hcp.getFirstKeyPostingPartitionOffset());
 
     // the new posting first
 
@@ -197,7 +196,7 @@
 
 
     // then the older posting
-    accessor.getKeysPartition(kp.getNextKeyPostingPartition()).readPosting(kp, 
kp.getNextKeyPostingPartitionOffset());
+    accessor.getKeysPartition(kp.getNextKeyPostingPartition(), 
true).readPosting(kp, kp.getNextKeyPostingPartitionOffset());
     assertEquals(FileHandler.Posting.FLAG_DELETED, kp.getFlag());
     assertEquals(revision - 1, kp.getCreatedRevision());
     assertEquals(revision, kp.getDeletedRevision());
@@ -215,7 +214,7 @@
     assertTrue(Arrays.equals(values[1], vp.getBytes()));
 
     // then the oldest posting
-    accessor.getKeysPartition(kp.getNextKeyPostingPartition()).readPosting(kp, 
kp.getNextKeyPostingPartitionOffset());
+    accessor.getKeysPartition(kp.getNextKeyPostingPartition(), 
true).readPosting(kp, kp.getNextKeyPostingPartitionOffset());
     assertEquals(FileHandler.Posting.FLAG_DELETED, kp.getFlag());
     assertEquals(revision - 2, kp.getCreatedRevision());
     assertEquals(revision - 1, kp.getDeletedRevision());
@@ -233,7 +232,7 @@
     assertTrue(Arrays.equals(values[0], vp.getBytes()));
 
 
-    accessor.close();
+    store.returnAccessor(accessor);
 
     store.close();
   }
@@ -252,7 +251,7 @@
     Accessor accessor;
 
 
-    accessor = store.createAccessor(false);
+    accessor = store.borrowAccessor();
     long revision = 1;
 
     store.put(accessor, keys[0], hashes[0], values[0], revision);
@@ -275,7 +274,7 @@
     assertEquals(hashes[0], hcp.getKeyHashCode());
 
     KeysPartition.Posting kp = new KeysPartition.Posting();
-    
accessor.getKeysPartition(hcp.getFirstKeyPostingPartition()).readPosting(kp, 
hcp.getFirstKeyPostingPartitionOffset());
+    accessor.getKeysPartition(hcp.getFirstKeyPostingPartition(), 
true).readPosting(kp, hcp.getFirstKeyPostingPartitionOffset());
 
 
     assertEquals(FileHandler.Posting.FLAG_IN_USE, kp.getFlag());
@@ -315,7 +314,7 @@
     assertEquals(hashes[0], hcp.getKeyHashCode());
 
     kp = new KeysPartition.Posting();
-    
accessor.getKeysPartition(hcp.getFirstKeyPostingPartition()).readPosting(kp, 
hcp.getFirstKeyPostingPartitionOffset());
+    accessor.getKeysPartition(hcp.getFirstKeyPostingPartition(), 
true).readPosting(kp, hcp.getFirstKeyPostingPartitionOffset());
 
     assertEquals(FileHandler.Posting.FLAG_IN_USE, kp.getFlag());
     assertEquals(revision, kp.getCreatedRevision());
@@ -334,8 +333,7 @@
     assertTrue(Arrays.equals(values[1], vp.getBytes()));
 
 
-    accessor.close();
-
+    store.returnAccessor(accessor);
     // todo test delete postings
 
     store.close();

Modified: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestRehash.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestRehash.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestRehash.java
 (original)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestRehash.java
 Mon Jun 29 03:30:43 2009
@@ -21,7 +21,7 @@
     Store store = new Store(configuration);
     store.open();
 
-    Accessor accessor = store.createAccessor(false);
+    Accessor accessor = store.borrowAccessor();
 
     assertEquals(0, accessor.getHashtable().getVersionId());
 
@@ -72,6 +72,8 @@
       assertTrue(Arrays.equals(value, values[i]));
     }
 
+    store.returnAccessor(accessor);
+
     store.close();
 
   }

Modified: 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestStoreAccessors.java
URL: 
http://svn.apache.org/viewvc/labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestStoreAccessors.java?rev=789204&r1=789203&r2=789204&view=diff
==============================================================================
--- 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestStoreAccessors.java
 (original)
+++ 
labs/bananadb/trunk/src/test/java/org/apache/labs/bananadb/store/TestStoreAccessors.java
 Mon Jun 29 03:30:43 2009
@@ -46,17 +46,16 @@
 
     Accessor accessor;
 
-    // set read only
-    try {
-      store.createAccessor(true);
-      fail("Accessor is set read only! Store should be be created");
-    } catch (IOException ioe) {
-      // all good
-    }
+//    // set read only
+//    try {
+//      store.createAccessor(true);
+//      fail("Accessor is set read only! Store should be be created");
+//    } catch (IOException ioe) {
+//      // all good
+//    }
 
     // create the store
-    accessor = store.createAccessor(false);
-    accessor.close();
+    store.returnAccessor(store.borrowAccessor());
 
     byte[] key;
     long hash;
@@ -70,20 +69,20 @@
     Arrays.fill(value, (byte) 2);
 
 
-    // open read only again
-    accessor = store.createAccessor(true);
-
-    assertFalse(store.containsKey(accessor, key, hash));
-    try {
-      assertNull(store.put(accessor, key, hash, value, 1l));
-      fail("Accessor is set read only and should not be able to add 
entiteis!");
-    } catch (IOException ioe) {
-      // all good
-      accessor.close();
-    }
+//    // open read only again
+//    accessor = store.borrowAccessor();
+//
+//    assertFalse(store.containsKey(accessor, key, hash));
+//    try {
+//      assertNull(store.put(accessor, key, hash, value, 1l));
+//      fail("Accessor is set read only and should not be able to add 
entiteis!");
+//    } catch (IOException ioe) {
+//      // all good
+//      accessor.close();
+//    }
 
     // open read/write
-    accessor = store.createAccessor(false);
+    accessor = store.borrowAccessor();
 
     // simple general put/get/containsKey/remove tests
 
@@ -128,6 +127,7 @@
     assertTrue(Arrays.equals(value, store.remove(accessor, key, hash, 1l)));
     assertFalse(store.containsKey(accessor, key, hash));
 
+    store.returnAccessor(accessor);
 
     store.close();
 



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

Reply via email to