Search in sources :

Example 1 with IgfsDirectoryNotEmptyException

use of org.apache.ignite.igfs.IgfsDirectoryNotEmptyException in project ignite by apache.

the class IgfsMetaManager method softDelete.

/**
 * Move path to the trash directory.
 *
 * @param path Path.
 * @param recursive Recursive flag.
 * @param secondaryFs Secondary file system (optional).
 * @return ID of an entry located directly under the trash directory.
 * @throws IgniteCheckedException If failed.
 */
IgfsDeleteResult softDelete(final IgfsPath path, final boolean recursive, @Nullable IgfsSecondaryFileSystem secondaryFs) throws IgniteCheckedException {
    while (true) {
        if (busyLock.enterBusy()) {
            try {
                validTxState(false);
                IgfsPathIds pathIds = pathIds(path);
                if (!pathIds.allExists() && secondaryFs == null)
                    return new IgfsDeleteResult(false, null);
                IgniteUuid victimId = pathIds.lastId();
                String victimName = pathIds.lastPart();
                if (IgfsUtils.isRootId(victimId))
                    throw new IgfsException("Cannot remove root directory");
                // Prepare IDs to lock.
                SortedSet<IgniteUuid> allIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
                pathIds.addExistingIds(allIds, relaxed);
                IgniteUuid trashId = IgfsUtils.randomTrashId();
                allIds.add(trashId);
                try (GridNearTxLocal tx = startTx()) {
                    // Lock participants.
                    Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(allIds);
                    if (secondaryFs != null && isRetryForSecondary(pathIds, lockInfos))
                        continue;
                    // Ensure that all participants are still in place.
                    if (!pathIds.allExists() || !pathIds.verifyIntegrity(lockInfos, relaxed)) {
                        // For DUAL mode we will try to update the underlying FS still. Note we do that inside TX.
                        if (secondaryFs != null) {
                            boolean res = secondaryFs.delete(path, recursive);
                            return new IgfsDeleteResult(res, null);
                        } else
                            return new IgfsDeleteResult(false, null);
                    }
                    IgfsEntryInfo victimInfo = lockInfos.get(victimId);
                    // Cannot delete non-empty directory if recursive flag is not set.
                    if (!recursive && victimInfo.hasChildren())
                        throw new IgfsDirectoryNotEmptyException("Failed to remove directory (directory is not " + "empty and recursive flag is not set).");
                    // Prepare trash data.
                    IgfsEntryInfo trashInfo = lockInfos.get(trashId);
                    final String trashName = IgfsUtils.composeNameForTrash(path, victimId);
                    assert !trashInfo.hasChild(trashName) : "Failed to add file name into the " + "destination directory (file already exists) [destName=" + trashName + ']';
                    IgniteUuid parentId = pathIds.lastParentId();
                    IgfsEntryInfo parentInfo = lockInfos.get(parentId);
                    // Propagate call to the secondary file system.
                    if (secondaryFs != null && !secondaryFs.delete(path, recursive))
                        return new IgfsDeleteResult(false, null);
                    transferEntry(parentInfo.listing().get(victimName), parentId, victimName, trashId, trashName);
                    tx.commit();
                    signalDeleteWorker();
                    return new IgfsDeleteResult(true, victimInfo);
                }
            } finally {
                busyLock.leaveBusy();
            }
        } else
            throw new IllegalStateException("Failed to perform soft delete because Grid is " + "stopping [path=" + path + ']');
    }
}
Also used : IgfsDirectoryNotEmptyException(org.apache.ignite.igfs.IgfsDirectoryNotEmptyException) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgfsException(org.apache.ignite.igfs.IgfsException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) TreeSet(java.util.TreeSet)

Aggregations

TreeSet (java.util.TreeSet)1 IgfsDirectoryNotEmptyException (org.apache.ignite.igfs.IgfsDirectoryNotEmptyException)1 IgfsException (org.apache.ignite.igfs.IgfsException)1 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)1 IgniteUuid (org.apache.ignite.lang.IgniteUuid)1