| Kea 3.0.2-git

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>
12#include <util/multi_threading_mgr.h>
13
14 using namespace isc;
15 using namespace isc::dhcp;
16 using namespace isc::eval;
17 using namespace std;
18
19 namespace isc {
20 namespace ddns_tuning {
21
22 bool
23 ExpressionCache::findExpression(const SubnetID& subnet_id, ExpressionPtr& expression) {
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
32 bool
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
44 ExpressionPtr
45 ExpressionCache::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
67 void
68 ExpressionCache::cacheExpression(const SubnetID& subnet_id, ExpressionPtr& expression) {
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
77 void
78 ExpressionCache::clear() {
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.
85 updateModificationTime();
86 } else {
87 // Discard the contents.
88 expressions_.clear();
89
90 // We use modification time to remember the last time we flushed.
91 updateModificationTime();
92 }
93}
94
95 size_t
96 ExpressionCache::size() {
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
106 ExpressionCache::getLastFlushTime() {
107 if (util::MultiThreadingMgr::instance().getMode()) {
108 std::lock_guard<std::mutex> lk(mutex_);
109 return (BaseStampedElement::getModificationTime());
110 } else {
111 return (BaseStampedElement::getModificationTime());
112 }
113}
114
115} // end of namespace ddns_tuning
116} // end of namespace isc
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::data::BaseStampedElement::getModificationTime
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
Definition base_stamped_element.h:61
isc::data::BaseStampedElement::updateModificationTime
void updateModificationTime()
Sets timestamp to the current time.
Definition base_stamped_element.cc:21
isc::ddns_tuning::ExpressionCache::clear
void clear()
Delete all the entries in the cache.
Definition expression_cache.cc:78
isc::ddns_tuning::ExpressionCache::parseAndCacheExpression
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.
Definition expression_cache.cc:45
isc::ddns_tuning::ExpressionCache::cacheExpression
void cacheExpression(const dhcp::SubnetID &subnet_id, dhcp::ExpressionPtr &expression)
Adds (or replaces) the expression for a given subnet to the cache.
Definition expression_cache.cc:68
isc::ddns_tuning::ExpressionCache::size
size_t size()
Returns number of entries in the cache.
Definition expression_cache.cc:96
isc::ddns_tuning::ExpressionCache::getLastFlushTime
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)...
Definition expression_cache.cc:106
isc::ddns_tuning::ExpressionCache::findExpression
bool findExpression(const dhcp::SubnetID &subnet_id, dhcp::ExpressionPtr &expression)
Fetches the expression for a given subnet.
Definition expression_cache.cc:23
isc::dhcp::Option::V6
@ V6
Definition option.h:90
isc::dhcp::Option::V4
@ V4
Definition option.h:90
isc::eval::EvalContext
Evaluation context, an interface to the expression evaluation.
Definition eval_context.h:35
isc::eval::EvalContext::parseString
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
Definition eval_context.cc:37
isc::eval::EvalContext::PARSER_STRING
@ PARSER_STRING
expression is expected to evaluate to string
Definition eval_context.h:41
isc::eval::EvalContext::expression_
isc::dhcp::Expression expression_
Parsed expression (output tokens are stored here).
Definition eval_context.h:68
isc::util::MultiThreadingMgr::instance
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
Definition multi_threading_mgr.cc:25
eval_context.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
expression_cache.h
multi_threading_mgr.h
isc::ddns_tuning
Definition ddns_tuning.cc:27
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::ExpressionPtr
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::Expression
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition token.h:29
isc::eval
Definition eval_context.h:24
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
token.h