summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/filedistribution/FileDBRegistry.java
blob: d921d8d4f8d6afb938fe7baff4d48bd08278c25b (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;

import com.yahoo.config.FileReference;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.net.HostName;
import com.yahoo.vespa.filedistribution.FileDistributionManager;
import com.yahoo.config.model.application.provider.FileReferenceCreator;

import java.util.*;

/**
 * @author tonytv
 */
public class FileDBRegistry implements FileRegistry {

    private final FileDistributionManager manager;
    private List<Entry> entries = new ArrayList<>();
    private final Map<String, FileReference> fileReferenceCache = new HashMap<>();

    public FileDBRegistry(FileDistributionManager manager) {
        this.manager = manager;
    }

    @Override
    public synchronized FileReference addFile(String relativePath) {
        Optional<FileReference> cachedReference = Optional.ofNullable(fileReferenceCache.get(relativePath));
        return cachedReference.orElseGet(() -> {
            FileReference newRef = FileReferenceCreator.create(manager.addFile(relativePath));
            entries.add(new Entry(relativePath, newRef));
            fileReferenceCache.put(relativePath, newRef);
            return newRef;
        });
    }

    @Override
    public String fileSourceHost() {
        return HostName.getLocalhost();
    }

    @Override
    public synchronized List<Entry> export() {
        return entries;
    }

}