Beispiel #1
0
  /** Basic tests */
  public void testBasics() throws IOException {

    Properties props = new Properties();
    RecordManager recman =
        RecordManagerFactory.createRecordManager(TestRecordFile.testFileName, props);
    HashBucket bucket = new HashBucket(0);

    // add
    bucket.addElement("key", "value");
    String s = (String) bucket.getValue("key");
    assertEquals("value", s);

    // replace
    bucket.addElement("key", "value2");
    s = (String) bucket.getValue("key");
    assertEquals("value2", s);

    // add
    bucket.addElement("key2", "value3");
    s = (String) bucket.getValue("key2");
    assertEquals("value3", s);

    // remove
    bucket.removeElement("key2");
    s = (String) bucket.getValue("key2");
    assertEquals(null, s);
    bucket.removeElement("key");
    s = (String) bucket.getValue("key");
    assertEquals(null, s);

    recman.close();
  }
  /**
   * Returns the value which is associated with the given key. Returns <code>null</code> if there is
   * not association for this key.
   *
   * @param key key whose associated value is to be returned
   */
  V get(K key) throws IOException {
    int hash = hashCode(key);
    long child_recid = _children[hash];
    if (child_recid == 0) {
      // not bucket/page --> not found
      return null;
    } else {
      HashNode<K, V> node = (HashNode<K, V>) _recman.fetch(child_recid, tree.SERIALIZER);
      // System.out.println("HashDirectory.get() child is : "+node);

      if (node instanceof HashDirectory) {
        // recurse into next directory level
        HashDirectory<K, V> dir = (HashDirectory<K, V>) node;
        dir.setPersistenceContext(_recman, child_recid);
        return dir.get(key);
      } else {
        // node is a bucket
        HashBucket<K, V> bucket = (HashBucket) node;
        return bucket.getValue(key);
      }
    }
  }