Beispiel #1
0
 /**
  * Load a persistent BTree.
  *
  * @param recman RecordManager used to store the persistent btree
  * @param recid Record id of the BTree
  */
 public static BTree load(RecordManager recman, long recid) throws IOException {
   BTree btree = (BTree) recman.fetch(recid);
   btree._recid = recid;
   btree._recman = recman;
   btree._bpageSerializer = new BPage();
   btree._bpageSerializer._btree = btree;
   return btree;
 }
Beispiel #2
0
  /**
   * Create a new persistent BTree with the given number of entries per node.
   *
   * @param recman Record manager used for persistence.
   * @param comparator Comparator used to order index entries
   * @param keySerializer Serializer used to serialize index keys (optional)
   * @param valueSerializer Serializer used to serialize index values (optional)
   * @param pageSize Number of entries per page (must be even).
   */
  public static BTree createInstance(
      RecordManager recman,
      Comparator comparator,
      Serializer keySerializer,
      Serializer valueSerializer,
      int pageSize)
      throws IOException {
    BTree btree;

    if (recman == null) {
      throw new IllegalArgumentException("Argument 'recman' is null");
    }

    if (comparator == null) {
      throw new IllegalArgumentException("Argument 'comparator' is null");
    }

    if (!(comparator instanceof Serializable)) {
      throw new IllegalArgumentException("Argument 'comparator' must be serializable");
    }

    if (keySerializer != null && !(keySerializer instanceof Serializable)) {
      throw new IllegalArgumentException("Argument 'keySerializer' must be serializable");
    }

    if (valueSerializer != null && !(valueSerializer instanceof Serializable)) {
      throw new IllegalArgumentException("Argument 'valueSerializer' must be serializable");
    }

    // make sure there's an even number of entries per BPage
    if ((pageSize & 1) != 0) {
      throw new IllegalArgumentException("Argument 'pageSize' must be even");
    }

    btree = new BTree();
    btree._recman = recman;
    btree._comparator = comparator;
    btree._keySerializer = keySerializer;
    btree._valueSerializer = valueSerializer;
    btree._pageSize = pageSize;
    btree._bpageSerializer = new BPage();
    btree._bpageSerializer._btree = btree;
    btree._recid = recman.insert(btree);
    return btree;
  }