Example #1
0
  public String getLine(int lineNo) throws Exception {
    if (lineNo <= 0 || lineNo > mNumLines) {
      return null;
    }

    String line = null;
    Jedis jedis = pool_.getResource();
    try {
      String lineNoStr = Integer.toString(lineNo);
      line = jedis.hget(lineNoStr, LINE_KEY);
      if (line == null) {
        RecordId lineId = getRecordId(lineNo);

        byte[] buffer = new byte[BLOCK_SIZE];
        synchronized (mDataFile) {
          mDataFile.seek(lineId.pageNo() * BLOCK_SIZE);
          mDataFile.read(buffer);
        }
        ByteBuffer bb = ByteBuffer.wrap(buffer);

        int numRecords = bb.getInt(BLOCK_SIZE - INT_SIZE);
        int indexPos = BLOCK_SIZE - 3 * INT_SIZE;
        int curLineNo = lineNo - lineId.slotNo() + 1;
        String[] recordKeys = {PAGE_NO_KEY, SLOT_NO_KEY};
        for (int i = 1; i < numRecords; i++, curLineNo++) {
          int offset = bb.getInt(indexPos);
          int len = bb.getInt(indexPos + INT_SIZE);

          ByteBuffer linebb = ByteBuffer.wrap(buffer, offset, len);
          byte[] lineBuf = new byte[linebb.remaining()];
          linebb.get(lineBuf);
          String lineStr = new String(lineBuf);

          if (i == lineId.slotNo()) {
            line = lineStr;
          }

          // Store in cache
          lineNoStr = Integer.toString(curLineNo);
          jedis.hdel(lineNoStr, recordKeys);
          jedis.hset(lineNoStr, LINE_KEY, lineStr);
          indexPos -= 2 * INT_SIZE;
        }
      }

      if (line == null) {
        throw new Exception("LineServer: Could not find line on page");
      }
    } catch (IOException ioe) {
      System.err.println("LineServer: IOException on line retrieval");
      throw ioe;
    } finally {
      pool_.returnResource(jedis);
    }

    return line;
  }
Example #2
0
  /**
   * Two RecordId objects are considered equal if they represent the same tuple.
   *
   * @return True if this and o represent the same tuple
   */
  @Override
  public boolean equals(Object o) {
    // some code goes here
    if (o instanceof RecordId) {
      RecordId tmp = (RecordId) o;

      return pid.equals(tmp.getPageId()) && tmp.tupleno == tupleno;
    }
    return false;
    // throw new UnsupportedOperationException("implement this");
  }