Example #1
0
  /**
   * Remove a page.
   *
   * @param pos the position of the page
   * @param memory the memory usage
   */
  void removePage(long pos, int memory) {
    // we need to keep temporary pages,
    // to support reading old versions and rollback
    if (pos == 0) {
      // the page was not yet stored:
      // just using "unsavedMemory -= memory" could result in negative
      // values, because in some cases a page is allocated, but never
      // stored, so we need to use max
      unsavedMemory = Math.max(0, unsavedMemory - memory);
      return;
    }

    // This could result in a cache miss if the operation is rolled back,
    // but we don't optimize for rollback.
    // We could also keep the page in the cache, as somebody
    // could still read it (reading the old version).
    if (cache != null) {
      if (DataUtils.getPageType(pos) == DataUtils.PAGE_TYPE_LEAF) {
        // keep nodes in the cache, because they are still used for
        // garbage collection
        cache.remove(pos);
      }
    }

    BTreeChunk chunk = getChunk(pos);
    long maxLengthLive = DataUtils.getPageMaxLength(pos);

    // synchronize, because pages could be freed concurrently
    synchronized (chunk) {
      chunk.maxLenLive -= maxLengthLive;
      chunk.pageCountLive--;
      chunk.changed = true;
    }
  }