rrset_collection.cc 4.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <dns/rrset_collection.h>
#include <dns/master_loader_callbacks.h>
#include <dns/master_loader.h>
18
#include <dns/rrcollator.h>
19

20
21
#include <exceptions/exceptions.h>

22
23
#include <boost/bind.hpp>

24
25
using namespace isc;

26
27
28
29
30
31
namespace isc {
namespace dns {

void
RRsetCollection::loaderCallback(const std::string&, size_t, const std::string&)
{
32
     // We just ignore callbacks for errors and warnings.
33
34
35
36
}

void
RRsetCollection::addRRset(RRsetPtr rrset) {
37
38
    const CollectionKey key(rrset->getClass(), rrset->getType(),
                            rrset->getName());
39
40
41
42
43
44
45
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        isc_throw(InvalidParameter,
                  "RRset for " << rrset->getName() << "/" << rrset->getClass()
                  << " with type " << rrset->getType() << " already exists");
    }

46
47
48
    rrsets_.insert(std::pair<CollectionKey, RRsetPtr>(key, rrset));
}

49
50
51
52
template<typename T>
void
RRsetCollection::constructHelper(T source, const isc::dns::Name& origin,
                                 const isc::dns::RRClass& rrclass)
53
{
54
    RRCollator collator(boost::bind(&RRsetCollection::addRRset, this, _1));
55
56
57
    MasterLoaderCallbacks callbacks
        (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
         boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
58
    MasterLoader loader(source, origin, rrclass, callbacks,
59
                        collator.getCallback(),
60
61
                        MasterLoader::DEFAULT);
    loader.load();
62
    collator.flush();
63
64
}

65
66
67
68
69
70
RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
                                 const RRClass& rrclass)
{
    constructHelper<const char*>(filename, origin, rrclass);
}

71
72
73
RRsetCollection::RRsetCollection(std::istream& input_stream, const Name& origin,
                                 const RRClass& rrclass)
{
74
    constructHelper<std::istream&>(input_stream, origin, rrclass);
75
76
}

77
const AbstractRRset*
78
79
80
RRsetCollection::find(const Name& name, const RRType& rrtype,
                      const RRClass& rrclass) const {
    const CollectionKey key(rrclass, rrtype, name);
81
82
83
84
85
86
87
88
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (&(*it->second));
    }
    return (NULL);
}

RRsetPtr
89
90
91
RRsetCollection::find(const Name& name, const RRClass& rrclass,
                      const RRType& rrtype) {
    const CollectionKey key(rrclass, rrtype, name);
92
93
94
95
96
97
98
99
    CollectionMap::iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (it->second);
    }
    return (RRsetPtr());
}

ConstRRsetPtr
100
RRsetCollection::find(const Name& name, const RRClass& rrclass,
101
102
                      const RRType& rrtype) const
{
103
    const CollectionKey key(rrclass, rrtype, name);
104
105
106
107
108
109
110
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (it->second);
    }
    return (ConstRRsetPtr());
}

111
bool
112
RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
113
114
                             const RRType& rrtype)
{
115
    const CollectionKey key(rrclass, rrtype, name);
116
117
118
119
120
121
122
123

    CollectionMap::iterator it = rrsets_.find(key);
    if (it == rrsets_.end()) {
        return (false);
    }

    rrsets_.erase(it);
    return (true);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}

RRsetCollectionBase::IterPtr
RRsetCollection::getBeginning() {
    CollectionMap::iterator it = rrsets_.begin();
    return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
}

RRsetCollectionBase::IterPtr
RRsetCollection::getEnd() {
    CollectionMap::iterator it = rrsets_.end();
    return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
}

} // end of namespace dns
} // end of namespace isc