Exemple #1
0
 /**
  * Creates an index of the specified type for the specified field. A unique ID is assigned to this
  * index, and its information is stored in the idxcat table.
  *
  * @param idxName the name of the index
  * @param tblName the name of the indexed table
  * @param fldName the name of the indexed field
  * @param idxType the index type of the indexed field
  * @param tx the calling transaction
  */
 public void createIndex(
     String idxName, String tblName, String fldName, int idxType, Transaction tx) {
   RecordFile rf = ti.open(tx);
   rf.insert();
   rf.setVal(ICAT_IDXNAME, new VarcharConstant(idxName));
   rf.setVal(ICAT_TBLNAME, new VarcharConstant(tblName));
   rf.setVal(ICAT_FLDNAME, new VarcharConstant(fldName));
   rf.setVal(ICAT_IDXTYPE, new IntegerConstant(idxType));
   rf.close();
 }
 /** Synchronizes the indicated blocks with the owner. */
 private void synchronizeBlocks(Iterable<BlockIo> blocks, boolean fromCore) throws IOException {
   // write block vector elements to the data file.
   for (BlockIo cur : blocks) {
     owner.synch(cur);
     if (fromCore) {
       cur.decrementTransactionCount();
       if (!cur.isInTransaction()) {
         owner.releaseFromTransaction(cur, true);
       }
     }
   }
 }
  /** Synchs in-core transactions to data file and opens a fresh log */
  private void synchronizeLogFromMemory() throws IOException {
    close();

    TreeSet<BlockIo> blockList = new TreeSet<BlockIo>(new BlockIoComparator());

    int numBlocks = 0;
    int writtenBlocks = 0;
    for (int i = 0; i < _maxTxns; i++) {
      if (txns[i] == null) continue;
      // Add each block to the blockList, replacing the old copy of this
      // block if necessary, thus avoiding writing the same block twice
      for (Iterator<BlockIo> k = txns[i].iterator(); k.hasNext(); ) {
        BlockIo block = k.next();
        if (blockList.contains(block)) {
          block.decrementTransactionCount();
        } else {
          writtenBlocks++;
          boolean result = blockList.add(block);
        }
        numBlocks++;
      }

      txns[i] = null;
    }
    // Write the blocks from the blockList to disk
    synchronizeBlocks(blockList, true);

    owner.sync();
    open();
  }
  /** Discards the indicated blocks and notify the owner. */
  private void discardBlocks(ArrayList<BlockIo> blocks) throws IOException {
    for (BlockIo cur : blocks) {

      cur.decrementTransactionCount();
      if (!cur.isInTransaction()) {
        owner.releaseFromTransaction(cur, false);
      }
    }
  }
Exemple #5
0
 /**
  * Returns a map containing the index info for all indexes on the specified table.
  *
  * @param tblName the name of the table
  * @param tx the calling transaction
  * @return a map of IndexInfo objects, keyed by their field names
  */
 public Map<String, IndexInfo> getIndexInfo(String tblName, Transaction tx) {
   Map<String, IndexInfo> result = new HashMap<String, IndexInfo>();
   RecordFile rf = ti.open(tx);
   rf.beforeFirst();
   while (rf.next())
     if (((String) rf.getVal(ICAT_TBLNAME).asJavaVal()).equals(tblName)) {
       String idxname = (String) rf.getVal(ICAT_IDXNAME).asJavaVal();
       String fldname = (String) rf.getVal(ICAT_FLDNAME).asJavaVal();
       int idxtype = (Integer) rf.getVal(ICAT_IDXTYPE).asJavaVal();
       IndexInfo ii = new IndexInfo(idxname, tblName, fldname, idxtype);
       result.put(fldname, ii);
     }
   rf.close();
   return result;
 }
  /** Startup recovery on all files */
  private void recover() throws IOException {
    String logName = makeLogName();
    File logFile = new File(logName);
    if (!logFile.exists()) return;
    if (logFile.length() == 0) {
      logFile.delete();
      return;
    }

    FileInputStream fis = new FileInputStream(logFile);
    DataInputStream ois = new DataInputStream(new BufferedInputStream(fis));

    try {
      if (ois.readShort() != Magic.LOGFILE_HEADER) throw new Error("Bad magic on log file");
    } catch (IOException e) {
      // corrupted/empty logfile
      logFile.delete();
      return;
    }

    while (true) {
      ArrayList<BlockIo> blocks = null;
      try {
        blocks = (ArrayList<BlockIo>) Serialization.readObject(ois);
      } catch (ClassNotFoundException e) {
        throw new Error("Unexcepted exception: " + e);
      } catch (IOException e) {
        // corrupted logfile, ignore rest of transactions
        break;
      }
      synchronizeBlocks(blocks, false);

      // ObjectInputStream must match exactly each
      // ObjectOutputStream created during writes
      //            try {
      ois = new DataInputStream(fis);
      //            } catch (IOException e) {
      //                // corrupted logfile, ignore rest of transactions
      //                break;
      //            }
    }
    owner.sync();
    fis.close();
    logFile.delete();
  }
 /** Builds logfile name */
 private String makeLogName() {
   return owner.getFileName() + extension;
 }