aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoreinitializer.cpp
blob: bade54a2adcfb9f61243b9d8dc4c65deac01cd60 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentmetastoreinitializer.h"
#include "documentmetastore.h"
#include <vespa/searchlib/common/indexmetainfo.h>
#include <vespa/searchcore/proton/common/eventlogger.h>
#include <vespa/vespalib/util/exceptions.h>
#include <filesystem>

using search::GrowStrategy;
using search::IndexMetaInfo;
using vespalib::IllegalStateException;
using proton::initializer::InitializerTask;
using vespalib::make_string;

namespace proton::documentmetastore {

DocumentMetaStoreInitializer::
DocumentMetaStoreInitializer(const vespalib::string baseDir,
                             const vespalib::string &subDbName,
                             const vespalib::string &docTypeName,
                             std::shared_ptr<DocumentMetaStore> dms)
    : _baseDir(baseDir),
      _subDbName(subDbName),
      _docTypeName(docTypeName),
      _dms(std::move(dms))
{ }

namespace {
vespalib::string
failedMsg(const char * msg) {
    return make_string("Failed to load document meta store for document type '%s' from disk", msg);
}
}

void
DocumentMetaStoreInitializer::run()
{
    vespalib::string name = DocumentMetaStore::getFixedName();
    IndexMetaInfo info(_baseDir);
    if (info.load()) {
        IndexMetaInfo::Snapshot snap = info.getBestSnapshot();
        if (snap.valid) {
            vespalib::string attrFileName = _baseDir + "/" + snap.dirName + "/" + name;
            _dms->setBaseFileName(attrFileName);
            assert(_dms->hasLoadData());
            vespalib::Timer stopWatch;
            EventLogger::loadDocumentMetaStoreStart(_subDbName);
            if (!_dms->load()) {
                throw IllegalStateException(failedMsg(_docTypeName.c_str()));
            } else {
                _dms->commit(search::CommitParam(snap.syncToken));
            }
            EventLogger::loadDocumentMetaStoreComplete(_subDbName, stopWatch.elapsed());
        }
    } else {
        std::filesystem::create_directory(std::filesystem::path(_baseDir));
        info.save();
    }
}

}