Kea 3.1.1
expression_cache.cc
Go to the documentation of this file.
1// Copyright (C) 2022-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <expression_cache.h>
10#include <eval/token.h>
11#include <eval/eval_context.h>
13
14using namespace isc;
15using namespace isc::dhcp;
16using namespace isc::eval;
17using namespace std;
18
19namespace isc {
20namespace ddns_tuning {
21
22bool
24 if (util::MultiThreadingMgr::instance().getMode()) {
25 std::lock_guard<std::mutex> lk(mutex_);
26 return (findExpressionInternal(subnet_id, expression));
27 } else {
28 return (findExpressionInternal(subnet_id, expression));
29 }
30}
31
32bool
33ExpressionCache::findExpressionInternal(const SubnetID& subnet_id, ExpressionPtr& expression) const {
34 auto it = expressions_.find(subnet_id);
35 if (it != expressions_.end()) {
36 expression = it->second;
37 return (true);
38 }
39
40 expression = ExpressionPtr();
41 return (false);
42}
43
45ExpressionCache::parseAndCacheExpression(const SubnetID& subnet_id, const string& expression_str,
46 uint32_t family) {
47 ExpressionPtr expression;
48 try {
49 // We allow empty expressions so higher precedence scopes may
50 // override lower precedence scopes.
51 if (expression_str.empty()) {
52 expression.reset(new Expression());
53 } else {
54 EvalContext eval_ctx(family == AF_INET ? Option::V4 : Option::V6);
55 eval_ctx.parseString(expression_str, EvalContext::PARSER_STRING);
56 expression.reset(new Expression(eval_ctx.expression_));
57 }
58 } catch (const std::exception& ex) {
59 isc_throw(BadValue, "error parsing expression: ["
60 << expression_str << "] : " << ex.what());
61 }
62
63 cacheExpression(subnet_id, expression);
64 return (expression);
65}
66
67void
69 if (util::MultiThreadingMgr::instance().getMode()) {
70 std::lock_guard<std::mutex> lk(mutex_);
71 expressions_[subnet_id] = expression;
72 } else {
73 expressions_[subnet_id] = expression;
74 }
75}
76
77void
79 if (util::MultiThreadingMgr::instance().getMode()) {
80 std::lock_guard<std::mutex> lk(mutex_);
81 // Discard the contents.
82 expressions_.clear();
83
84 // We use modification time to remember the last time we flushed.
86 } else {
87 // Discard the contents.
88 expressions_.clear();
89
90 // We use modification time to remember the last time we flushed.
92 }
93}
94
95size_t
97 if (util::MultiThreadingMgr::instance().getMode()) {
98 std::lock_guard<std::mutex> lk(mutex_);
99 return (expressions_.size());
100 } else {
101 return (expressions_.size());
102 }
103}
104
105boost::posix_time::ptime
107 if (util::MultiThreadingMgr::instance().getMode()) {
108 std::lock_guard<std::mutex> lk(mutex_);
110 } else {
112 }
113}
114
115} // end of namespace ddns_tuning
116} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
void updateModificationTime()
Sets timestamp to the current time.
void clear()
Delete all the entries in the cache.
dhcp::ExpressionPtr parseAndCacheExpression(const dhcp::SubnetID &subnet_id, const std::string &expression_str, uint32_t family)
Parses an expression string and caches for the given subnet.
void cacheExpression(const dhcp::SubnetID &subnet_id, dhcp::ExpressionPtr &expression)
Adds (or replaces) the expression for a given subnet to the cache.
size_t size()
Returns number of entries in the cache.
boost::posix_time::ptime getLastFlushTime()
Returns the last time the cache was flushed (or the time it was created if it has never been flushed)...
bool findExpression(const dhcp::SubnetID &subnet_id, dhcp::ExpressionPtr &expression)
Fetches the expression for a given subnet.
Evaluation context, an interface to the expression evaluation.
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
@ PARSER_STRING
expression is expected to evaluate to string
isc::dhcp::Expression expression_
Parsed expression (output tokens are stored here)
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition token.h:29
Defines the logger used by the top-level component of kea-lfc.