@Override
  public byte[] getXAttr(long fileId, String uid, String key) throws DatabaseException {

    ResultSet<byte[], byte[]> it = null;
    try {

      // peform a prefix lookup
      byte[] prefix = BabuDBStorageHelper.createXAttrPrefixKey(fileId, uid, key);
      it = database.prefixLookup(XATTRS_INDEX, prefix, null).get();

      // check whether the entry is the correct one
      while (it.hasNext()) {

        Entry<byte[], byte[]> curr = it.next();
        BufferBackedXAttr xattr = new BufferBackedXAttr(curr.getKey(), curr.getValue());
        if (uid.equals(xattr.getOwner()) && key.equals(xattr.getKey())) return xattr.getValue();
      }

      return null;

    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    } finally {
      if (it != null) it.free();
    }
  }
  @Override
  public FileMetadata getMetadata(long fileId) throws DatabaseException {

    try {

      // create the key for the file ID index lookup
      byte[] key = BabuDBStorageHelper.createFileIdIndexKey(fileId, (byte) -1);
      ByteBuffer.wrap(key).putLong(fileId);

      byte[][] valBufs = new byte[BufferBackedFileMetadata.NUM_BUFFERS][];

      // retrieve the metadata from the link index
      ResultSet<byte[], byte[]> it =
          database.prefixLookup(BabuDBStorageManager.FILE_ID_INDEX, key, null).get();

      while (it.hasNext()) {

        Entry<byte[], byte[]> curr = it.next();

        int type = BabuDBStorageHelper.getType(curr.getKey(), BabuDBStorageManager.FILE_ID_INDEX);

        // if the value is a back link, resolve it
        if (type == 3) {

          long parentId = ByteBuffer.wrap(curr.getValue()).getLong();
          String fileName = new String(curr.getValue(), 8, curr.getValue().length - 8);

          return getMetadata(parentId, fileName);
        }

        valBufs[type] = curr.getValue();
      }

      it.free();

      // if not metadata was found for the file ID, return null
      if (valBufs[FileMetadata.RC_METADATA] == null) return null;

      byte[][] keyBufs =
          new byte[][] {null, BabuDBStorageHelper.createFileKey(0, "", FileMetadata.RC_METADATA)};

      // otherwise, a hard link target is contained in the index; create a
      // new metadata object in this case
      return new BufferBackedFileMetadata(keyBufs, valBufs, BabuDBStorageManager.FILE_ID_INDEX);

    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    }
  }
  public void dump() throws BabuDBException {

    System.out.println("FILE_ID_INDEX");

    ResultSet<byte[], byte[]> it = database.prefixLookup(FILE_ID_INDEX, new byte[0], null).get();
    while (it.hasNext()) {
      Entry<byte[], byte[]> next = it.next();
      System.out.println(Arrays.toString(next.getKey()) + " = " + Arrays.toString(next.getValue()));
    }
    it.free();

    System.out.println("\nFILE_INDEX");

    it = database.prefixLookup(FILE_INDEX, new byte[0], null).get();
    while (it.hasNext()) {
      Entry<byte[], byte[]> next = it.next();
      System.out.println(Arrays.toString(next.getKey()) + " = " + Arrays.toString(next.getValue()));
    }
    it.free();
  }
  @Override
  public short delete(final long parentId, final String fileName, final AtomicDBUpdate update)
      throws DatabaseException {

    try {

      // retrieve the file metadata
      BufferBackedFileMetadata file = BabuDBStorageHelper.getMetadata(database, parentId, fileName);

      // check whether there is only one link remaining
      short newLinkCount = (short) (file.getLinkCount() - 1);
      assert (newLinkCount >= 0);

      // decrement the link count
      file.setLinkCount(newLinkCount);

      // if there will be links remaining after the deletion, update the
      // link count
      if (newLinkCount > 0)
        update.addUpdate(
            FILE_ID_INDEX,
            BabuDBStorageHelper.createFileIdIndexKey(file.getId(), FileMetadata.RC_METADATA),
            file.getRCMetadata().getValue());

      // delete all keys ...

      // remove all content from the file index
      update.addUpdate(
          BabuDBStorageManager.FILE_INDEX,
          BabuDBStorageHelper.createFileKey(parentId, fileName, FileMetadata.FC_METADATA),
          null);
      update.addUpdate(
          BabuDBStorageManager.FILE_INDEX,
          BabuDBStorageHelper.createFileKey(parentId, fileName, FileMetadata.RC_METADATA),
          null);

      // if the last link to the file is supposed to be deleted, remove
      // the remaining metadata, including ACLs and XAttrs
      if (newLinkCount == 0) {

        // remove the back link from the file ID index
        update.addUpdate(
            BabuDBStorageManager.FILE_ID_INDEX,
            BabuDBStorageHelper.createFileIdIndexKey(file.getId(), (byte) 3),
            null);

        // remove potentially existing metadata from the file ID index
        update.addUpdate(
            BabuDBStorageManager.FILE_ID_INDEX,
            BabuDBStorageHelper.createFileIdIndexKey(file.getId(), FileMetadata.FC_METADATA),
            null);
        update.addUpdate(
            BabuDBStorageManager.FILE_ID_INDEX,
            BabuDBStorageHelper.createFileIdIndexKey(file.getId(), FileMetadata.RC_METADATA),
            null);

        byte[] idBytes = new byte[8];
        ByteBuffer.wrap(idBytes).putLong(file.getId());

        // remove all ACLs
        ResultSet<byte[], byte[]> it =
            database.prefixLookup(BabuDBStorageManager.ACL_INDEX, idBytes, null).get();
        while (it.hasNext())
          update.addUpdate(BabuDBStorageManager.ACL_INDEX, it.next().getKey(), null);
        it.free();

        // remove all extended attributes
        it = database.prefixLookup(BabuDBStorageManager.XATTRS_INDEX, idBytes, null).get();
        while (it.hasNext())
          update.addUpdate(BabuDBStorageManager.XATTRS_INDEX, it.next().getKey(), null);
        it.free();

        // if a file is deleted, update file count and volume size
        if (file.isDirectory()) {
          updateCount(NUM_DIRS_KEY, false, update);
        } else if (file.getXLocList() != null) {
          volume.updateVolumeSize(-file.getSize(), update);
          updateCount(NUM_FILES_KEY, false, update);
        }
      }

      return file.getLinkCount();

    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    }
  }