예제 #1
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
  public synchronized void deleteCollection(String name) {
    try {
      long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT);
      if (nameDirectory_recid == 0) throw new IOException("Collection not found");
      HTree<String, Long> dir = fetch(nameDirectory_recid);

      Long recid = dir.get(name);
      if (recid == null) throw new IOException("Collection not found");

      Object o = fetch(recid);
      // we can not use O instance since it is not correctly initialized
      if (o instanceof LinkedList2) {
        LinkedList2 l = (LinkedList2) o;
        l.clear();
        delete(l.rootRecid);
      } else if (o instanceof BTree) {
        ((BTree) o).clear();
      } else if (o instanceof HTree) {
        HTree t = (HTree) o;
        t.clear();
        HTreeDirectory n = (HTreeDirectory) fetch(t.rootRecid, t.SERIALIZER);
        n.deleteAllChildren();
        delete(t.rootRecid);
      } else {
        throw new InternalError("unknown collection type: " + (o == null ? null : o.getClass()));
      }
      delete(recid);
      collections.remove(name);

      dir.remove(name);

    } catch (IOException e) {
      throw new IOError(e);
    }
  }
예제 #2
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
 /**
  * Obtain the record id of a named object. Returns 0 if named object doesn't exist. Named objects
  * are used to store Map views and other well known objects.
  */
 protected synchronized long getNamedObject(String name) throws IOException {
   long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT);
   if (nameDirectory_recid == 0) {
     return 0;
   }
   HTree<String, Long> m = fetch(nameDirectory_recid);
   Long res = m.get(name);
   if (res == null) return 0;
   return res;
 }
예제 #3
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
 /**
  * Set the record id of a named object. Named objects are used to store Map views and other well
  * known objects.
  */
 protected synchronized void setNamedObject(String name, long recid) throws IOException {
   long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT);
   HTree<String, Long> m = null;
   if (nameDirectory_recid == 0) {
     // does not exists, create it
     m = new HTree<String, Long>(this, null, null, true);
     nameDirectory_recid = insert(m);
     setRoot(NAME_DIRECTORY_ROOT, nameDirectory_recid);
   } else {
     // fetch it
     m = fetch(nameDirectory_recid);
   }
   m.put(name, recid);
 }
예제 #4
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
  public synchronized <K, V> ConcurrentMap<K, V> getHashMap(String name) {
    Object o = getCollectionInstance(name);
    if (o != null) return (ConcurrentMap<K, V>) o;

    try {
      long recid = getNamedObject(name);
      if (recid == 0) return null;

      HTree tree = fetch(recid);
      tree.setPersistenceContext(this);
      if (!tree.hasValues()) {
        throw new ClassCastException("HashSet is not HashMap");
      }
      collections.put(name, new WeakReference<Object>(tree));
      return tree;
    } catch (IOException e) {
      throw new IOError(e);
    }
  }
예제 #5
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
  public synchronized <K> Set<K> getHashSet(String name) {
    Object o = getCollectionInstance(name);
    if (o != null) return (Set<K>) o;

    try {
      long recid = getNamedObject(name);
      if (recid == 0) return null;

      HTree tree = fetch(recid);
      tree.setPersistenceContext(this);
      if (tree.hasValues()) {
        throw new ClassCastException("HashMap is not HashSet");
      }
      Set<K> ret = new HTreeSet(tree);
      collections.put(name, new WeakReference<Object>(ret));
      return ret;
    } catch (IOException e) {
      throw new IOError(e);
    }
  }
예제 #6
0
파일: DBAbstract.java 프로젝트: joval/JDBM3
  public synchronized Map<String, Object> getCollections() {
    try {
      Map<String, Object> ret = new LinkedHashMap<String, Object>();
      long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT);
      if (nameDirectory_recid == 0) return ret;
      HTree<String, Long> m = fetch(nameDirectory_recid);

      for (Map.Entry<String, Long> e : m.entrySet()) {
        Object o = fetch(e.getValue());
        if (o instanceof BTree) {
          if (((BTree) o).hasValues) o = getTreeMap(e.getKey());
          else o = getTreeSet(e.getKey());
        } else if (o instanceof HTree) {
          if (((HTree) o).hasValues) o = getHashMap(e.getKey());
          else o = getHashSet(e.getKey());
        }

        ret.put(e.getKey(), o);
      }
      return Collections.unmodifiableMap(ret);
    } catch (IOException e) {
      throw new IOError(e);
    }
  }