<commit_before>/*
 * TreeTank - Embedded Native XML Database
 * 
 * Copyright 2007 <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id$
 */

package org.treetank.utils;

import org.treetank.api.IConstants;
import org.treetank.api.IPage;
import org.treetank.pagelayer.IndirectPage;
import org.treetank.pagelayer.PageCache;
import org.treetank.pagelayer.PageReference;

public final class StaticTree {

  private PageReference mStartReference;

  private PageCache mCache;

  private int[] mCurrentOffsets;

  private IPage[] mCurrentPages;

  public StaticTree(final PageReference startReference, final PageCache cache) {
    mStartReference = startReference;
    mCache = cache;
    mCurrentOffsets = new int[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];
    mCurrentPages = new IPage[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];
  }

  public final PageReference get(final long key) throws Exception {

    // Indirect reference.
    PageReference reference = mStartReference;
    IPage page = null;

    // Remaining levels.
    int levelSteps = 0;
    long levelKey = key;
    for (int i = 0; i < mCurrentOffsets.length; i++) {

      // Calculate offset of current level.
      levelSteps =
          (int) (levelKey >> IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i]);
      levelKey -= levelSteps << IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i];

      // Fetch page from current level.
      page = mCache.dereference(reference, IConstants.INDIRECT_PAGE);
      reference = ((IndirectPage) page).getPageReference(levelSteps);
    }

    return reference;

  }

  public static final int[] calcIndirectPageOffsets(final long key) {

    final int[] levels =
        new int[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];

    long tmpKey = key;
    for (int i = 0; i < levels.length; i++) {
      levels[i] = (int) (tmpKey >> IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i]);
      tmpKey -= levels[i] << IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i];
    }

    return levels;

  }

}
<commit_msg>StaticTree now remembers the pages and only dereferences them if it encounters a new offset on the given level.<commit_after>/*
 * TreeTank - Embedded Native XML Database
 * 
 * Copyright 2007 <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id$
 */

package org.treetank.utils;

import org.treetank.api.IConstants;
import org.treetank.pagelayer.IndirectPage;
import org.treetank.pagelayer.PageCache;
import org.treetank.pagelayer.PageReference;

public final class StaticTree {

  private PageReference mStartReference;

  private PageCache mCache;

  private int[] mCurrentOffsets;

  private IndirectPage[] mCurrentPages;

  public StaticTree(final PageReference startReference, final PageCache cache) {
    mStartReference = startReference;
    mCache = cache;
    mCurrentOffsets = new int[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];
    mCurrentPages =
        new IndirectPage[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];
    for (int i = 0; i < mCurrentOffsets.length; i++) {
      mCurrentOffsets[i] = -1;
    }
  }

  public final PageReference get(final long key) throws Exception {

    // Indirect reference.
    PageReference reference = mStartReference;

    // Remaining levels.
    int levelSteps = 0;
    long levelKey = key;
    for (int i = 0; i < mCurrentOffsets.length; i++) {

      // Calculate offset of current level.
      levelSteps =
          (int) (levelKey >> IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i]);
      levelKey -= levelSteps << IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i];

      // Fetch page from current level.
      if (levelSteps != mCurrentOffsets[i]) {
        mCurrentOffsets[i] = levelSteps;
        mCurrentPages[i] =
            (IndirectPage) mCache.dereference(
                reference,
                IConstants.INDIRECT_PAGE);
      }
      reference = mCurrentPages[i].getPageReference(levelSteps);
    }

    return reference;

  }

  public static final int[] calcIndirectPageOffsets(final long key) {

    final int[] levels =
        new int[IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT.length];

    long tmpKey = key;
    for (int i = 0; i < levels.length; i++) {
      levels[i] = (int) (tmpKey >> IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i]);
      tmpKey -= levels[i] << IConstants.INP_LEVEL_PAGE_COUNT_EXPONENT[i];
    }

    return levels;

  }

}

{'fraction_non_alphanumeric': 0.08683926645091694, 'fraction_numerical': 0.003236245954692557, 'mean_word_length': 3.4539631705364293, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3732849', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1640, 'n_words': 510}