aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/tablemanager.h
blob: 3abb06e45ff51df53226b408313c75439cd05883 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "itablefactory.h"
#include "itablemanager.h"
#include <map>
#include <vector>
#include <mutex>

namespace search::fef {

/**
 * This class manages a set of tables and contains an ordered list of table factories used to create tables,
 * and a cache of allready created tables. A table is accessed by a unique name.
 **/
class TableManager : public ITableManager
{
private:
    TableManager(const TableManager &);
    TableManager &operator=(const TableManager &);

    using TableCache = std::map<vespalib::string, Table::SP>;
    std::vector<ITableFactory::SP> _factories;
    mutable TableCache             _cache;
    mutable std::mutex             _lock;

public:
    TableManager();
    ~TableManager() override;

    /**
     * Adds a table factory to this manager.
     * The table factories are used in the order they where added to create tables.
     **/
    void addFactory(ITableFactory::SP factory) { _factories.push_back(factory); }

    /**
     * Retrieves the table with the given name using the following strategy:
     * 1. Try to find the table in the cache.
     * 2. Iterate over the table factories and try to create the table.
     *    The first table that is successfully created is added it to the cache and returned.
     * 3. Return NULL.
     **/
    const Table * getTable(const vespalib::string & name) const override;
};

}