aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/PathChildrenCacheWrapper.java
blob: 644334a6fe06a548f0a5d5bf442e48138d588a4c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator;

import com.yahoo.path.Path;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * A directory cache backed by a curator path children cache.
 *
 * @author bratseth
 */
class PathChildrenCacheWrapper implements Curator.DirectoryCache {

    private final PathChildrenCache wrapped;

    public PathChildrenCacheWrapper(CuratorFramework curatorFramework, String path, boolean cacheData, boolean dataIsCompressed, ExecutorService executorService) {
        wrapped = new PathChildrenCache(curatorFramework, path, cacheData, dataIsCompressed, executorService);
    }

    @Override
    public void start() {
        try {
            wrapped.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        } catch (Exception e) {
            throw new IllegalStateException("Could not start the Curator cache", e);
        }
    }

    @Override
    public void addListener(PathChildrenCacheListener listener) {
        wrapped.getListenable().addListener(listener);

    }

    @Override
    public List<ChildData> getCurrentData() {
        return wrapped.getCurrentData();
    }

    @Override
    public ChildData getCurrentData(Path absolutePath) {
        return wrapped.getCurrentData(absolutePath.getAbsolute());
    }

    @Override
    public void close() {
        try {
            wrapped.close();
        } catch (IOException e) {
            throw new RuntimeException("Exception closing curator cache", e);
        }
    }

}