aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/nowfeature.cpp
blob: 5dab98eb82f49e07832ac986a6bf15005592b132 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "nowfeature.h"
#include <vespa/searchlib/fef/queryproperties.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/vespalib/util/stash.h>
#include <chrono>

namespace search::features {

NowExecutor::NowExecutor(int64_t timestamp) :
    fef::FeatureExecutor(),
    _timestamp(timestamp)
{
}

void
NowExecutor::execute(uint32_t) {
    outputs().set_number(0, _timestamp);
}

void
NowBlueprint::visitDumpFeatures(const fef::IIndexEnvironment &, fef::IDumpFeatureVisitor &) const
{
}

bool
NowBlueprint::setup(const fef::IIndexEnvironment &, const fef::ParameterList &)
{
    describeOutput("out", "The timestamp (seconds since epoch) of query execution.");
    return true;
}

fef::Blueprint::UP
NowBlueprint::createInstance() const
{
    return std::make_unique<NowBlueprint>();
}

using namespace std::chrono;

fef::FeatureExecutor &
NowBlueprint::createExecutor(const fef::IQueryEnvironment &env, vespalib::Stash &stash) const
{
    int64_t timestamp;
    const fef::Property &prop = env.getProperties().lookup(fef::queryproperties::now::SystemTime::NAME);
    if (prop.found()) {
        timestamp = atoll(prop.get().c_str());
    } else {
        timestamp = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
    }
    return stash.create<NowExecutor>(timestamp);
}

}