aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorOperations.java
blob: 59ee322beb38a1e280501eb21aa82b3deb894c22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator.transaction;

import com.yahoo.path.Path;
import com.yahoo.vespa.curator.Curator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Factory for transactional ZooKeeper operations.
 * This mirrors the operations which are actually available in Curator, which unfortunately does not include
 * variants that deletes children, creates parents etc. in a single operation.
 *
 * @author Ulf Lilleengen
 * @author bratseth
 */
public class CuratorOperations {

    /**
     * Sets data at this path.
     *
     * @throws IllegalStateException in check() if the path does not exist
     */
    public static CuratorOperation setData(String path, byte[] bytes) {
        return new CuratorSetDataOperation(path, bytes);
    }

    /**
     * Creates this path with data.
     *
     * @throws IllegalStateException in check() if the parent does not exist
     */
    public static CuratorOperation create(String path, byte[] bytes) {
        return new CuratorCreateOperation(path, Optional.of(bytes));
    }

    /**
     * Creates this path with no data.
     *
     * @throws IllegalStateException in check() if the parent does not exist
     */
    public static CuratorOperation create(String path) {
        return new CuratorCreateOperation(path, Optional.empty());
    }

    /** 
     * Deletes this path. This does not delete children.
     * 
     * @throws IllegalStateException in check() if the path does not exist.
     */
    public static CuratorOperation delete(String path) {
        return new CuratorDeleteOperation(path);
    }

    /**
     * Returns operations deleting this path and everything below it, in an order where a parent
     * is ordered after all its children,
     * such that the operations will succeed when executed in the returned order.
     * This does not fail, but returns an empty list if the path does not exist.
     */
    public static List<CuratorOperation> deleteAll(String path, Curator curator) {
        if ( ! curator.exists(Path.fromString(path))) return Collections.emptyList();

        List<CuratorOperation> operations = new ArrayList<>();
        deleteRecursively(Path.fromString(path), operations, curator);
        return operations;
    }
    
    private static void deleteRecursively(Path path, List<CuratorOperation> operations, Curator curator) {
        for (String childName : curator.getChildren(path))
            deleteRecursively(path.append(childName), operations, curator);
        operations.add(delete(path.getAbsolute()));
    }

}