<commit_before>package hk.ust.comp4321.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jdbm.helper.FastIterator;
import jdbm.htree.HTree;
import jdbm.RecordManager;
import jdbm.RecordManagerFactory;

/**
 * 
 * @author <NAME>
 *
 */
public class ForwardIndexTable
{

  // Constants and fields.
  // -------------------------------------------------------------------------

  private static ForwardIndexTable forwardIndexTable = new ForwardIndexTable();
  private static RecordManager recman;
  private static HTree hashtable;

  // Constructors.
  // -------------------------------------------------------------------------

  private ForwardIndexTable()
  {
    try
      {
        // Create a RecordManager name "SearchEngineDatabase"
        recman = RecordManagerFactory.createRecordManager ("SearchEngineDatabase"); 
        // get the record id of the object named "ForwardIndexTable"
        long recid = recman.getNamedObject ("ForwardIndexTable");

        if (recid != 0)
          {
            // load the hash table named"ForwardIndexTable"from the RecordManager
            hashtable = HTree.load (recman, recid); 
          }
        else
          {
            // create a hash table in the RecordManager
            hashtable = HTree.createInstance (recman); 
            // set the name of the hash table to "ForwardIndexTable"
            recman.setNamedObject ("ForwardIndexTable", hashtable.getRecid()); 
          }
      }
      catch(java.io.IOException ex)
        {
          System.err.println(ex.toString());
        }
  }

  // Class methods.
  // -------------------------------------------------------------------------

  /**
   * The class has only one instance. This method returns that unique one
   * instance of forward index table.
   * 
   * @return the unique forward index table
   */
  public static ForwardIndexTable getTable()
  {
    return ForwardIndexTable.forwardIndexTable;
  }

  // Instance methods.
  // -------------------------------------------------------------------------

  /**
   * This method returns the list of doc info with the given page id.
   * 
   * @param pageId the page id containing the doc info
   * @return list of doc info with the given page id
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public List<DocInfo> getDocInfoList (int pageId) throws IOException
  {
      return (List<DocInfo>)ForwardIndexTable.hashtable.get (pageId);
  }

  /**
   * This method returns the doc info with the given page id and word id. If
   * the doc info does not exist, it returns null.
   * 
   * @param  pageId       the page id containing the doc info
   * @param  wordId       the word id of the doc info
   * @return doc info with the given page id and word id
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public DocInfo getDocInfo (int pageId, int wordId) throws IOException
  {
    List<DocInfo> docInfoList = (List<DocInfo>)ForwardIndexTable.
                                    hashtable.get (pageId);

    // Check if the list does not exist
    if(docInfoList == null)
      return null;
    else
      {
        // Find the doc info
        for(int i = 0; i < docInfoList.size(); i++)
          if(docInfoList.get (i).getId() == wordId)
            return docInfoList.get (i);
        return null;
      }
  }

  /**
   * This method inserts a doc info, if the doc info exists, it will be
   * replaced.
   * 
   * @param  id            the doc id to be inserted
   * @param  pageInfo      the associated doc info to be inserted
   * @throws IOException
   */
  public void insertDocInfo (int id, DocInfo docInfo) throws IOException
  {
    @SuppressWarnings("unchecked")
    List<DocInfo> docInfoList = (List<DocInfo>)ForwardIndexTable.
                                                     hashtable.get (id);

    // Check if the list does not exist
    if(docInfoList == null)
      docInfoList = new ArrayList<DocInfo>();

    // Check if the doc info has already existed, if yes then remove it 
    for(int i = 0; i < docInfoList.size(); i++)
      {
        if(docInfoList.get (i).getId() == docInfo.getId())
          {
            docInfoList.remove (docInfoList.get (i));
            break;
          }
      }

    // Add doc info to the list
    docInfoList.add (docInfo);

    // Add the list to the database
    ForwardIndexTable.hashtable.remove (id);
    ForwardIndexTable.hashtable.put (id, docInfoList);
  }

  /**
   * This method returns an enumeration of the keys
   * @return an enumeration of the keys
   * @throws IOException
   */
  public FastIterator keys() throws IOException
  {
    return ForwardIndexTable.hashtable.keys();
  }

  /**
   * This method commits all changes since beginning of transaction and
   * terminates.
   * 
   * @throws IOException
   */
  public void terminate() throws IOException
  {
    recman.commit();
    recman.close();
  }
}
<commit_msg>Fix a bug, refactor<commit_after>package hk.ust.comp4321.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jdbm.helper.FastIterator;
import jdbm.htree.HTree;
import jdbm.RecordManager;
import jdbm.RecordManagerFactory;

/**
 * 
 * @author <NAME>
 *
 */
public class ForwardIndexTable
{

  // Constants and fields.
  // -------------------------------------------------------------------------

  private static ForwardIndexTable forwardIndexTable = new ForwardIndexTable();
  private static RecordManager recman;
  private static HTree hashtable;

  // Constructors.
  // -------------------------------------------------------------------------

  private ForwardIndexTable()
  {
    try
      {
        // Create a RecordManager name "SearchEngineDatabase"
        recman = RecordManagerFactory.createRecordManager ("SearchEngineDatabase"); 
        // get the record id of the object named "ForwardIndexTable"
        long recid = recman.getNamedObject ("ForwardIndexTable");

        if (recid != 0)
          {
            // load the hash table named"ForwardIndexTable"from the RecordManager
            hashtable = HTree.load (recman, recid); 
          }
        else
          {
            // create a hash table in the RecordManager
            hashtable = HTree.createInstance (recman); 
            // set the name of the hash table to "ForwardIndexTable"
            recman.setNamedObject ("ForwardIndexTable", hashtable.getRecid()); 
          }
      }
      catch(java.io.IOException ex)
        {
          System.err.println(ex.toString());
        }
  }

  // Class methods.
  // -------------------------------------------------------------------------

  /**
   * The class has only one instance. This method returns that unique one
   * instance of forward index table.
   * 
   * @return the unique forward index table
   */
  public static ForwardIndexTable getTable()
  {
    return ForwardIndexTable.forwardIndexTable;
  }

  // Instance methods.
  // -------------------------------------------------------------------------

  /**
   * This method returns the list of doc info with the given page id.
   * 
   * @param pageId the page id containing the doc info
   * @return list of doc info with the given page id
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public List<DocInfo> getDocInfoList (int pageId) throws IOException
  {
      return (List<DocInfo>)ForwardIndexTable.hashtable.get (pageId);
  }

  /**
   * This method returns the doc info with the given page id and word id. If
   * the doc info does not exist, it returns null.
   * 
   * @param  pageId       the page id containing the doc info
   * @param  wordId       the word id of the doc info
   * @return doc info with the given page id and word id
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public DocInfo getDocInfo (int pageId, int wordId) throws IOException
  {
    List<DocInfo> docInfoList = (List<DocInfo>)ForwardIndexTable.
                                    hashtable.get (pageId);

    // Check if the list does not exist
    if(docInfoList == null)
      return null;
    else
      {
        // Find the doc info
        for(int i = 0; i < docInfoList.size(); i++)
          if(docInfoList.get (i).getId() == wordId)
            return docInfoList.get (i);
        return null;
      }
  }

  /**
   * This method inserts a doc info, if the doc info exists, it will be
   * replaced.
   * 
   * @param  pageId            the doc id to be inserted
   * @param  pageInfo      the associated doc info to be inserted
   * @throws IOException
   */
  public void insertDocInfo (int pageId, DocInfo docInfo) throws IOException
  {
    @SuppressWarnings("unchecked")
    List<DocInfo> docInfoList = (List<DocInfo>)ForwardIndexTable.
                                                     hashtable.get (pageId);

    // Check if the list does not exist
    if(docInfoList == null)
      docInfoList = new ArrayList<DocInfo>();

    // Check if the doc info has already existed, if yes then remove it 
    for(int i = 0; i < docInfoList.size(); i++)
      {
        if(docInfoList.get (i).getId() == docInfo.getId())
          {
            docInfoList.remove (docInfoList.get (i));
            break;
          }
      }

    // Add doc info to the list
    docInfoList.add (docInfo);

    // Add the list to the database
    ForwardIndexTable.hashtable.remove (pageId);
    ForwardIndexTable.hashtable.put (pageId, docInfoList);
  }

  /**
   * This method returns an enumeration of the keys
   * @return an enumeration of the keys
   * @throws IOException
   */
  public FastIterator keys() throws IOException
  {
    return ForwardIndexTable.hashtable.keys();
  }

  /**
   * This method commits all changes since beginning of transaction and
   * terminates.
   * 
   * @throws IOException
   */
  public void terminate() throws IOException
  {
    recman.commit();
    recman.close();
  }
}

{'fraction_non_alphanumeric': 0.13965450310559005, 'fraction_numerical': 0.001358695652173913, 'mean_word_length': 1.7204329461457233, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7658809', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2675, 'n_words': 863}