@Override
  public void setMetadata(FileMetadata metadata, byte type, AtomicDBUpdate update)
      throws DatabaseException {

    assert (metadata instanceof BufferBackedFileMetadata);
    BufferBackedFileMetadata md = (BufferBackedFileMetadata) metadata;

    int index = md.getIndexId();
    if (type == -1)
      for (byte i = 0; i < BufferBackedFileMetadata.NUM_BUFFERS; i++) {
        byte[] valBuf = md.getValueBuffer(i);
        assert (valBuf != null);
        update.addUpdate(
            index,
            index == FILE_ID_INDEX
                ? BabuDBStorageHelper.createFileIdIndexKey(metadata.getId(), i)
                : md.getKeyBuffer(i),
            valBuf);
      }
    else {
      byte[] valBuf = md.getValueBuffer(type);
      assert (valBuf != null);
      update.addUpdate(
          index,
          index == FILE_ID_INDEX
              ? BabuDBStorageHelper.createFileIdIndexKey(metadata.getId(), type)
              : md.getKeyBuffer(type),
          valBuf);
    }
  }
  @Override
  public void startRequest(MRCRequest rq) throws Throwable {

    final removexattrRequest rqArgs = (removexattrRequest) rq.getRequestArgs();

    final VolumeManager vMan = master.getVolumeManager();
    final FileAccessManager faMan = master.getFileAccessManager();

    Path p = new Path(rqArgs.getVolumeName(), rqArgs.getPath());

    validateContext(rq);

    StorageManager sMan = vMan.getStorageManagerByName(p.getComp(0));
    PathResolver res = new PathResolver(sMan, p);

    // check whether the path prefix is searchable
    faMan.checkSearchPermission(
        sMan, res, rq.getDetails().userId, rq.getDetails().superUser, rq.getDetails().groupIds);

    // check whether file exists
    res.checkIfFileDoesNotExist();

    // retrieve and prepare the metadata to return
    FileMetadata file = res.getFile();

    AtomicDBUpdate update = sMan.createAtomicDBUpdate(master, rq);

    // if the attribute is a system attribute, set it

    final String attrKey = rqArgs.getName();

    // set a system attribute
    if (attrKey.startsWith("xtreemfs.")) {

      // check whether the user has privileged permissions to set
      // system attributes
      faMan.checkPrivilegedPermissions(
          sMan, file, rq.getDetails().userId, rq.getDetails().superUser, rq.getDetails().groupIds);

      MRCHelper.setSysAttrValue(
          sMan, vMan, faMan, res.getParentDirId(), file, attrKey.substring(9), "", update);
    }

    // set a user attribute
    else {

      sMan.setXAttr(file.getId(), rq.getDetails().userId, attrKey, null, update);
    }

    // update POSIX timestamps
    int time = (int) (TimeSync.getGlobalTime() / 1000);
    MRCHelper.updateFileTimes(res.getParentDirId(), file, false, true, false, sMan, time, update);

    // set the response
    rq.setResponse(timestampResponse.newBuilder().setTimestampS(time).build());

    update.execute();
  }
  @Override
  public FileMetadata createSymLink(
      long fileId,
      long parentId,
      String fileName,
      int atime,
      int ctime,
      int mtime,
      String userId,
      String groupId,
      String ref,
      AtomicDBUpdate update) {

    // create metadata
    BufferBackedFileMetadata fileMetadata =
        new BufferBackedFileMetadata(
            parentId,
            fileName,
            userId,
            groupId,
            fileId,
            atime,
            ctime,
            mtime,
            ref.length(),
            0777,
            0,
            (short) 1,
            0,
            0,
            false);

    // create link target (XAttr)
    BufferBackedXAttr lt =
        new BufferBackedXAttr(fileId, SYSTEM_UID, LINK_TARGET_ATTR_NAME, ref.getBytes(), (short) 0);
    update.addUpdate(XATTRS_INDEX, lt.getKeyBuf(), lt.getValBuf());

    // update main metadata in the file index
    update.addUpdate(
        FILE_INDEX, fileMetadata.getFCMetadataKey(), fileMetadata.getFCMetadataValue());
    update.addUpdate(
        FILE_INDEX, fileMetadata.getRCMetadata().getKey(), fileMetadata.getRCMetadata().getValue());

    // add an entry to the file ID index
    update.addUpdate(
        FILE_ID_INDEX,
        BabuDBStorageHelper.createFileIdIndexKey(fileId, (byte) 3),
        BabuDBStorageHelper.createFileIdIndexValue(parentId, fileName));

    return fileMetadata;
  }
  @Override
  public void setACLEntry(long fileId, String entity, Short rights, AtomicDBUpdate update)
      throws DatabaseException {

    BufferBackedACLEntry entry =
        new BufferBackedACLEntry(fileId, entity, rights == null ? 0 : rights);
    update.addUpdate(ACL_INDEX, entry.getKeyBuf(), rights == null ? null : entry.getValBuf());
  }
  @Override
  public void setLastFileId(long fileId, AtomicDBUpdate update) throws DatabaseException {

    byte[] idBytes = new byte[8];
    ByteBuffer.wrap(idBytes).putLong(0, fileId);

    update.addUpdate(VOLUME_INDEX, LAST_ID_KEY, idBytes);
  }
  private void initCount(byte[] key, AtomicDBUpdate update) {

    byte[] countBytes = new byte[Long.SIZE / 8];
    ByteBuffer countBuf = ByteBuffer.wrap(countBytes);
    countBuf.putLong(0, 1);

    update.addUpdate(VOLUME_INDEX, key, countBytes);
  }
  protected void updateVolumeSize(long diff, AtomicDBUpdate update) throws DatabaseException {

    long newSize = getVolumeSize() + diff;

    byte[] sizeBytes = new byte[8];
    ByteBuffer.wrap(sizeBytes).putLong(0, newSize);

    update.addUpdate(VOLUME_INDEX, VOL_SIZE_KEY, sizeBytes);
  }
  @Override
  public FileMetadata createFile(
      long fileId,
      long parentId,
      String fileName,
      int atime,
      int ctime,
      int mtime,
      String userId,
      String groupId,
      int perms,
      long w32Attrs,
      long size,
      boolean readOnly,
      int epoch,
      int issEpoch,
      AtomicDBUpdate update)
      throws DatabaseException {

    // create metadata
    BufferBackedFileMetadata fileMetadata =
        new BufferBackedFileMetadata(
            parentId, fileName, userId, groupId, fileId, atime, ctime, mtime, size, perms, w32Attrs,
            (short) 1, epoch, issEpoch, readOnly);

    // update main metadata in the file index
    update.addUpdate(
        FILE_INDEX, fileMetadata.getFCMetadataKey(), fileMetadata.getFCMetadataValue());
    update.addUpdate(
        FILE_INDEX, fileMetadata.getRCMetadata().getKey(), fileMetadata.getRCMetadata().getValue());

    // add an entry to the file ID index
    update.addUpdate(
        FILE_ID_INDEX,
        BabuDBStorageHelper.createFileIdIndexKey(fileId, (byte) 3),
        BabuDBStorageHelper.createFileIdIndexValue(parentId, fileName));

    volume.updateVolumeSize(size, update);
    updateCount(NUM_FILES_KEY, true, update);

    return fileMetadata;
  }
  private void updateCount(byte[] key, boolean increment, AtomicDBUpdate update)
      throws DatabaseException {

    try {
      byte[] countBytes = BabuDBStorageHelper.getVolumeMetadata(database, key);
      ByteBuffer countBuf = ByteBuffer.wrap(countBytes);
      countBuf.putLong(0, countBuf.getLong() + (increment ? 1 : -1));

      update.addUpdate(VOLUME_INDEX, key, countBytes);
    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    }
  }
  @Override
  public short unlink(final long parentId, final String fileName, final AtomicDBUpdate update)
      throws DatabaseException {

    try {

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

      // determine and set the new link count
      short newLinkCount = (short) (file.getLinkCount() - 1);
      file.setLinkCount(newLinkCount);

      // if there will be links remaining after the deletion, update the
      // link count; it must be in the FILE_ID_INDEX, because there have
      // been at least two links
      if (newLinkCount > 0)
        update.addUpdate(
            FILE_ID_INDEX,
            BabuDBStorageHelper.createFileIdIndexKey(file.getId(), FileMetadata.RC_METADATA),
            file.getRCMetadata().getValue());

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

      return newLinkCount;

    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    }
  }
  public FileMetadata createDir(
      long fileId,
      long parentId,
      String fileName,
      int atime,
      int ctime,
      int mtime,
      String userId,
      String groupId,
      int perms,
      long w32Attrs,
      boolean initCount,
      AtomicDBUpdate update)
      throws DatabaseException {

    // create metadata
    BufferBackedFileMetadata fileMetadata =
        new BufferBackedFileMetadata(
            parentId, fileName, userId, groupId, fileId, atime, ctime, mtime, perms, w32Attrs,
            (short) 1);

    // update main metadata in the file index
    update.addUpdate(
        FILE_INDEX, fileMetadata.getFCMetadataKey(), fileMetadata.getFCMetadataValue());
    update.addUpdate(
        FILE_INDEX, fileMetadata.getRCMetadata().getKey(), fileMetadata.getRCMetadata().getValue());

    // add an entry to the file ID index
    update.addUpdate(
        FILE_ID_INDEX,
        BabuDBStorageHelper.createFileIdIndexKey(fileId, (byte) 3),
        BabuDBStorageHelper.createFileIdIndexValue(parentId, fileName));

    if (initCount) initCount(NUM_DIRS_KEY, update);
    else updateCount(NUM_DIRS_KEY, true, update);

    return fileMetadata;
  }
  public void setXAttr(
      long fileId, String uid, String key, byte[] value, boolean init, AtomicDBUpdate update)
      throws DatabaseException {

    try {
      short collNumber =
          init ? -1 : BabuDBStorageHelper.findXAttrCollisionNumber(database, fileId, uid, key);

      BufferBackedXAttr xattr = new BufferBackedXAttr(fileId, uid, key, value, collNumber);
      update.addUpdate(XATTRS_INDEX, xattr.getKeyBuf(), value == null ? null : xattr.getValBuf());

      if (key.startsWith(SYS_ATTR_KEY_PREFIX + MRCHelper.POLICY_ATTR_PREFIX))
        notifyAttributeSet(volume.getId(), key, value == null ? null : new String(value));

    } catch (BabuDBException exc) {
      throw new DatabaseException(exc);
    }
  }
  @Override
  public void link(
      final FileMetadata metadata,
      final long newParentId,
      final String newFileName,
      final AtomicDBUpdate update) {

    // get the link source
    BufferBackedFileMetadata md = (BufferBackedFileMetadata) metadata;

    // increment the link count
    short links = metadata.getLinkCount();
    md.setLinkCount((short) (links + 1));

    // insert the whole metadata of the original file in the file ID
    // index
    update.addUpdate(
        FILE_ID_INDEX,
        BabuDBStorageHelper.createFileIdIndexKey(metadata.getId(), FileMetadata.FC_METADATA),
        md.getFCMetadataValue());
    update.addUpdate(
        FILE_ID_INDEX,
        BabuDBStorageHelper.createFileIdIndexKey(metadata.getId(), FileMetadata.RC_METADATA),
        md.getRCMetadata().getValue());

    // remove the back link
    update.addUpdate(
        FILE_ID_INDEX, BabuDBStorageHelper.createFileIdIndexKey(metadata.getId(), (byte) 3), null);

    // if the metadata was retrieved from the FILE_INDEX and hasn't
    // been deleted before (i.e. links == 0), ensure that the original
    // file in the file index now points to the file ID index, and
    // remove the FC and XLoc metadata entries
    if (links != 0 && md.getIndexId() == FILE_INDEX) {

      update.addUpdate(
          FILE_INDEX,
          md.getRCMetadata().getKey(),
          BabuDBStorageHelper.createLinkTarget(metadata.getId()));
      update.addUpdate(FILE_INDEX, md.getFCMetadataKey(), null);
    }

    // create an entry for the new link to the metadata in the file
    // index
    update.addUpdate(
        FILE_INDEX,
        BabuDBStorageHelper.createFileKey(newParentId, newFileName, FileMetadata.RC_METADATA),
        BabuDBStorageHelper.createLinkTarget(metadata.getId()));
  }
  @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);
    }
  }