package org.basex.server;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.basex.data.Data;

/**
 * Class for all referenced Data.
 * 
 * @author Workgroup DBIS, University of Konstanz 2005-09, ISC License
 * @author Andreas Weiler
 */
public class DBPool {
  
  /** Map for Datareferences. */
  public static final HashMap<Data, Integer> DBPOOL = new
  HashMap<Data, Integer>();
  
  
  /**
   * Returns the Data of the Database.
   * @param db Name of the Database.
   * @return Data
   */
  public Data pin(final String db) {
    Iterator<Map.Entry<Data, Integer>> i = DBPOOL.entrySet().iterator();
    while(i.hasNext()) {
      Entry<Data, Integer> next = i.next();
      if(next.getKey().meta.dbname.equals(db)) {
        int j = next.getValue();
        j++;
        DBPOOL.put(next.getKey(), j);
        return next.getKey();
      }
    }
    return null;
  }
  
  /**
   * Removes a Datareference.
   * @param d Data
   */
  public void unpin(final Data d) {
    if(DBPOOL.get(d) == 0) {
      DBPOOL.remove(d);
    } else {
      int i = DBPOOL.get(d);
      i--;
      DBPOOL.put(d, i);
    }
  }
}

@@ -1223,11 +1223,154 @@
 %7D%0D%0A  %7D%0D%0A
+  %0D%0A  /**%0D%0A   * Adds a Datareference to the DBPOOL.%0D%0A   * @param d Data%0D%0A   */%0D%0A  public void add(final Data d) %7B%0D%0A    DBPOOL.put(d, 1);%0D%0A  %7D%0D%0A
 %7D%0D%0A

add Data added