Search in sources :

Example 1 with IllegalDocumentModificationException

use of io.atomix.core.tree.IllegalDocumentModificationException in project atomix by atomix.

the class DocumentTreeService method update.

protected DocumentTreeResult<Versioned<byte[]>> update(Commit<? extends Update> commit) {
    DocumentTreeResult<Versioned<byte[]>> result = null;
    DocumentPath path = commit.value().path();
    // If the path is locked by a transaction, return a WRITE_LOCK error.
    if (isLocked(path)) {
        return DocumentTreeResult.writeLock();
    }
    Versioned<byte[]> currentValue = docTree.get(path);
    try {
        Match<Long> versionMatch = commit.value().versionMatch();
        Match<byte[]> valueMatch = commit.value().valueMatch();
        if (versionMatch.matches(currentValue == null ? null : currentValue.version()) && valueMatch.matches(currentValue == null ? null : currentValue.value())) {
            if (commit.value().value() == null) {
                Versioned<byte[]> oldValue = docTree.removeNode(path);
                result = new DocumentTreeResult<>(Status.OK, oldValue);
                if (oldValue != null) {
                    notifyListeners(new DocumentTreeEvent<>(path, Type.DELETED, Optional.empty(), Optional.of(oldValue)));
                }
            } else {
                Versioned<byte[]> oldValue = docTree.set(path, commit.value().value().orElse(null));
                Versioned<byte[]> newValue = docTree.get(path);
                result = new DocumentTreeResult<>(Status.OK, newValue);
                if (oldValue == null) {
                    notifyListeners(new DocumentTreeEvent<>(path, Type.CREATED, Optional.of(newValue), Optional.empty()));
                } else {
                    notifyListeners(new DocumentTreeEvent<>(path, Type.UPDATED, Optional.of(newValue), Optional.of(oldValue)));
                }
            }
        } else {
            result = new DocumentTreeResult<>(commit.value().value() == null ? Status.INVALID_PATH : Status.NOOP, currentValue);
        }
    } catch (IllegalDocumentModificationException e) {
        result = DocumentTreeResult.illegalModification();
    } catch (NoSuchDocumentPathException e) {
        result = DocumentTreeResult.invalidPath();
    } catch (Exception e) {
        getLogger().error("Failed to apply {} to state machine", commit.value(), e);
        throw Throwables.propagate(e);
    }
    return result;
}
Also used : Versioned(io.atomix.utils.time.Versioned) AtomicLong(java.util.concurrent.atomic.AtomicLong) DocumentPath(io.atomix.core.tree.DocumentPath) NoSuchDocumentPathException(io.atomix.core.tree.NoSuchDocumentPathException) IllegalDocumentModificationException(io.atomix.core.tree.IllegalDocumentModificationException) NoSuchDocumentPathException(io.atomix.core.tree.NoSuchDocumentPathException) IllegalDocumentModificationException(io.atomix.core.tree.IllegalDocumentModificationException)

Example 2 with IllegalDocumentModificationException

use of io.atomix.core.tree.IllegalDocumentModificationException in project atomix by atomix.

the class DefaultDocumentTree method create.

@Override
public boolean create(DocumentPath path, V value) {
    checkRootModification(path);
    DocumentTreeNode<V> node = getNode(path);
    if (node != null) {
        return false;
    }
    DocumentPath parentPath = path.parent();
    DefaultDocumentTreeNode<V> parentNode = getNode(parentPath);
    if (parentNode == null) {
        throw new IllegalDocumentModificationException();
    }
    parentNode.addChild(simpleName(path), value, versionSupplier.get());
    return true;
}
Also used : DocumentPath(io.atomix.core.tree.DocumentPath) IllegalDocumentModificationException(io.atomix.core.tree.IllegalDocumentModificationException)

Example 3 with IllegalDocumentModificationException

use of io.atomix.core.tree.IllegalDocumentModificationException in project atomix by atomix.

the class DefaultDocumentTree method createRecursive.

@Override
public boolean createRecursive(DocumentPath path, V value) {
    checkRootModification(path);
    DocumentTreeNode<V> node = getNode(path);
    if (node != null) {
        return false;
    }
    DocumentPath parentPath = path.parent();
    if (getNode(parentPath) == null) {
        createRecursive(parentPath, null);
    }
    DefaultDocumentTreeNode<V> parentNode = getNode(parentPath);
    if (parentNode == null) {
        throw new IllegalDocumentModificationException();
    }
    parentNode.addChild(simpleName(path), value, versionSupplier.get());
    return true;
}
Also used : DocumentPath(io.atomix.core.tree.DocumentPath) IllegalDocumentModificationException(io.atomix.core.tree.IllegalDocumentModificationException)

Aggregations

DocumentPath (io.atomix.core.tree.DocumentPath)3 IllegalDocumentModificationException (io.atomix.core.tree.IllegalDocumentModificationException)3 NoSuchDocumentPathException (io.atomix.core.tree.NoSuchDocumentPathException)1 Versioned (io.atomix.utils.time.Versioned)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1