Kea  2.1.7-git
rrset_collection.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2020 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 <dns/rrset_collection.h>
11 #include <dns/master_loader.h>
12 #include <dns/rrcollator.h>
13 
14 #include <exceptions/exceptions.h>
15 
16 #include <functional>
17 
18 using namespace isc;
19 namespace ph = std::placeholders;
20 
21 namespace isc {
22 namespace dns {
23 
24 void
25 RRsetCollection::loaderCallback(const std::string&, size_t, const std::string&)
26 {
27  // We just ignore callbacks for errors and warnings.
28 }
29 
30 void
32  const CollectionKey key(rrset->getClass(), rrset->getType(),
33  rrset->getName());
34  CollectionMap::const_iterator it = rrsets_.find(key);
35  if (it != rrsets_.end()) {
37  "RRset for " << rrset->getName() << "/" << rrset->getClass()
38  << " with type " << rrset->getType() << " already exists");
39  }
40 
41  rrsets_.insert(std::pair<CollectionKey, RRsetPtr>(key, rrset));
42 }
43 
44 template<typename T>
45 void
46 RRsetCollection::constructHelper(T source, const isc::dns::Name& origin,
47  const isc::dns::RRClass& rrclass)
48 {
49  RRCollator collator(std::bind(&RRsetCollection::addRRset, this, ph::_1));
50  MasterLoaderCallbacks callbacks
51  (std::bind(&RRsetCollection::loaderCallback, this, ph::_1, ph::_2, ph::_3),
52  std::bind(&RRsetCollection::loaderCallback, this, ph::_1, ph::_2, ph::_3));
53  MasterLoader loader(source, origin, rrclass, callbacks,
54  collator.getCallback(),
56  loader.load();
57  collator.flush();
58 }
59 
60 RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
61  const RRClass& rrclass)
62 {
63  constructHelper(filename, origin, rrclass);
64 }
65 
66 RRsetCollection::RRsetCollection(std::istream& input_stream, const Name& origin,
67  const RRClass& rrclass)
68 {
69  constructHelper<std::istream&>(input_stream, origin, rrclass);
70 }
71 
73 RRsetCollection::find(const Name& name, const RRClass& rrclass,
74  const RRType& rrtype) {
75  const CollectionKey key(rrclass, rrtype, name);
76  CollectionMap::iterator it = rrsets_.find(key);
77  if (it != rrsets_.end()) {
78  return (it->second);
79  }
80  return (RRsetPtr());
81 }
82 
84 RRsetCollection::find(const Name& name, const RRClass& rrclass,
85  const RRType& rrtype) const
86 {
87  const CollectionKey key(rrclass, rrtype, name);
88  CollectionMap::const_iterator it = rrsets_.find(key);
89  if (it != rrsets_.end()) {
90  return (it->second);
91  }
92  return (ConstRRsetPtr());
93 }
94 
95 bool
96 RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
97  const RRType& rrtype)
98 {
99  const CollectionKey key(rrclass, rrtype, name);
100 
101  CollectionMap::iterator it = rrsets_.find(key);
102  if (it == rrsets_.end()) {
103  return (false);
104  }
105 
106  rrsets_.erase(it);
107  return (true);
108 }
109 
112  CollectionMap::iterator it = rrsets_.begin();
113  return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
114 }
115 
118  CollectionMap::iterator it = rrsets_.end();
119  return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
120 }
121 
122 } // end of namespace dns
123 } // end of namespace isc
The Name class encapsulates DNS names.
Definition: name.h:223
boost::shared_ptr< Iter > IterPtr
Wraps Iter with a reference count.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
A class able to load DNS master files.
Definition: master_loader.h:36
A converter from a stream of RRs to a stream of collated RRsets.
Definition: rrcollator.h:45
bool removeRRset(const isc::dns::Name &name, const isc::dns::RRClass &rrclass, const isc::dns::RRType &rrtype)
Remove an RRset from the collection.
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
AddRRCallback getCallback()
Return MasterLoader compatible callback.
Definition: rrcollator.cc:91
virtual RRsetCollectionBase::IterPtr getBeginning()
Returns an IterPtr wrapping an Iter pointing to the beginning of the collection.
void addRRset(isc::dns::RRsetPtr rrset)
Add an RRset to the collection.
Defines the logger used by the top-level component of kea-lfc.
RRsetCollection()
Constructor.
virtual RRsetCollectionBase::IterPtr getEnd()
Returns an IterPtr wrapping an Iter pointing past the end of the collection.
The RRType class encapsulates DNS resource record types.
Definition: rrtype.h:106
boost::shared_ptr< const AbstractRRset > ConstRRsetPtr
A pointer-like type pointing to an (immutable) RRset object.
Definition: rrset.h:60
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
void load()
Load everything.
void flush()
Call the callback on the remaining RRset, if any.
Definition: rrcollator.cc:97
Set of issue callbacks for a loader.
virtual isc::dns::ConstRRsetPtr find(const isc::dns::Name &name, const isc::dns::RRClass &rrclass, const isc::dns::RRType &rrtype) const
Find a matching RRset in the collection.