예제 #1
0
  /** Parse the command from the byte buffer. */
  private boolean unroll(ByteBuffer b, Map<String, Integer> manifest) throws KevaDBException {
    long time = b.getLong();

    // Check if there are any sstables that are older than
    // this WAL entry. If so, we can safely skip this entry since
    // we know the results have already been persisted.
    if (manifest != null) {
      for (String uuid : manifest.keySet()) {
        SSTable table = db.getDiskService().getSSTable(db, uuid, manifest.get(uuid));
        if (table.getModificationTime() <= time) {
          return true;
        }
      }
    }

    // Check if this is a valid entry.
    short magic = b.getShort();
    if (magic != MAGIC_NUMBER) {
      return false;
    }

    // Read in the various lengths.
    int keyLength = b.getInt();
    int valueLength = b.getInt();
    int compressedLength = b.getInt();
    int optionLength = b.getInt();

    // Instantiate the various buffers.
    byte[] keyBuffer = new byte[keyLength];
    byte[] valueBuffer = new byte[valueLength];
    byte[] compressedBuffer = new byte[compressedLength];

    b.get(keyBuffer);
    b.get(compressedBuffer);

    // Now we need to decompress the value buffer.
    decompressor.decompress(compressedBuffer, 0, valueBuffer, 0, valueLength);

    // Now get the write options.
    WriteOptions options = null;
    if (optionLength > 0) {
      byte[] optionBuffer = new byte[optionLength];
      b.get(optionBuffer);
      options = OptionsSerializer.writeOptionsFromBytes(optionBuffer);
    }

    TableKey key = TableKey.fromBytes(ByteBuffer.wrap(keyBuffer));
    TableValue value = TableValueFactory.fromBytes(valueBuffer, valueLength);

    // Now put the value into the DB.
    db.put(key, value, options);
    return true;
  }
예제 #2
0
  /** Serialize the write. */
  private ByteBuffer serialize(
      final TableKey key, final TableValue value, final WriteOptions options) {
    int size;
    int optionSize = 0;
    byte[] optionBuf = null;
    byte[] keyData = key.serialize();
    byte[] valueData = value.getBytes();

    int maxCompressed = compressor.maxCompressedLength(valueData.length);
    byte[] compressed = new byte[maxCompressed];
    int actualCompressed =
        compressor.compress(valueData, 0, valueData.length, compressed, 0, maxCompressed);
    size =
        (Short.SIZE / 8)
            + 4 * (Integer.SIZE / 8)
            + (Long.SIZE / 8)
            + keyData.length
            + actualCompressed;

    if (options != null) {
      optionBuf = OptionsSerializer.getBytes(options);
      if (optionBuf != null) {
        optionSize = optionBuf.length;
      } else {
        optionSize = 0;
      }
      size += optionSize;
    }

    ByteBuffer buf = ByteBuffer.allocateDirect(size);
    buf.putLong(System.currentTimeMillis());
    buf.putShort(MAGIC_NUMBER); // Magic number.

    buf.putInt(keyData.length);
    buf.putInt(valueData.length);
    buf.putInt(actualCompressed);
    buf.putInt(optionSize);

    buf.put(keyData);
    buf.put(compressed, 0, actualCompressed);

    if (optionBuf != null) {
      buf.put(optionBuf);
    }

    return buf;
  }