~ubuntu-branches/ubuntu/maverick/pdns/maverick-updates

« back to all changes in this revision

Viewing changes to pdns/recursor_cache.hh

  • Committer: Bazaar Package Importer
  • Author(s): Debian PowerDNS Maintainers
  • Date: 2006-05-06 10:40:44 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060506104044-v9hczzfl7dcri6qt
Tags: 2.9.20-3
Disable the recursor, this is in a separate package now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef RECURSOR_CACHE_HH
2
2
#define RECURSOR_CACHE_HH
3
 
#include <map>
4
3
#include <string>
5
4
#include <set>
6
5
#include "dns.hh"
7
6
#include "qtype.hh"
8
7
#include <iostream>
9
 
 
10
 
template<int N=14>
11
 
struct optString
12
 
{
13
 
  optString()
14
 
  {
15
 
    d_len=0;
16
 
    *buf=0;
17
 
  }
18
 
 
19
 
  optString(const string& str)
20
 
  {
21
 
    if(str.size() < N-1) {
22
 
      memcpy(buf, str.c_str(), str.size()+1);
23
 
      d_len = str.size() + 1;
24
 
    }
25
 
    else {
26
 
      new(buf) string(str);
27
 
      d_len = 0;
28
 
    }
29
 
  }
30
 
 
31
 
  operator string() const
32
 
  {
33
 
 
34
 
    if(d_len) {
35
 
      return string(buf, buf + d_len - 1);
36
 
    }
37
 
    else {
38
 
      return *((string*)buf);
39
 
    }
40
 
  }
41
 
 
42
 
  void prune() const
43
 
  {
44
 
    //    cerr<<"did a prune!"<<endl;
45
 
    if(!d_len)
46
 
      ((string*)buf)->~string();
47
 
  }
48
 
 
49
 
  bool operator<(const optString& os) const
50
 
  {
51
 
    return (string)*this < (string) os;
52
 
  }
53
 
 
54
 
  char buf[N];
55
 
  uint8_t d_len;
56
 
} __attribute__((packed));
57
 
 
58
 
 
59
 
 
60
 
 
61
 
class MemRecursorCache //  : public RecursorCache
 
8
#include <boost/utility.hpp>
 
9
#undef L
 
10
#include <boost/multi_index_container.hpp>
 
11
#include <boost/multi_index/ordered_index.hpp>
 
12
 
 
13
#include <boost/multi_index/key_extractors.hpp>
 
14
#include <boost/version.hpp>
 
15
#if BOOST_VERSION >= 103300
 
16
#include <boost/multi_index/hashed_index.hpp>
 
17
#endif
 
18
 
 
19
#define L theL()
 
20
using namespace boost;
 
21
using namespace ::boost::multi_index;
 
22
 
 
23
 
 
24
class MemRecursorCache : public boost::noncopyable //  : public RecursorCache
62
25
{
63
26
public:
64
 
  ~MemRecursorCache(){}
65
27
  unsigned int size();
 
28
  unsigned int bytes();
66
29
  int get(time_t, const string &qname, const QType& qt, set<DNSResourceRecord>* res);
67
30
  void replace(const string &qname, const QType& qt,  const set<DNSResourceRecord>& content);
68
31
  void doPrune(void);
71
34
private:
72
35
  struct StoredRecord
73
36
  {
74
 
    uint32_t d_ttd;
75
 
    optString<> d_string;
 
37
    mutable uint32_t d_ttd;
 
38
 
 
39
    string d_string;
 
40
 
76
41
    bool operator<(const StoredRecord& rhs) const
77
42
    {
78
 
      return make_pair(d_ttd, d_string) < make_pair(rhs.d_ttd, rhs.d_string);
79
 
    }
80
 
  };
81
 
  typedef map<string, set<StoredRecord> > cache_t;
82
 
 
 
43
      return d_string < rhs.d_string;
 
44
    }
 
45
 
 
46
    unsigned int size() const
 
47
    {
 
48
      return 4+d_string.size();
 
49
    }
 
50
 
 
51
  };
 
52
 
 
53
  struct predicate
 
54
  {
 
55
    predicate(time_t limit) : d_limit(limit)
 
56
    {
 
57
    }
 
58
    
 
59
    bool operator()(const StoredRecord& sr) const
 
60
    {
 
61
      return sr.d_ttd <= d_limit;
 
62
    }
 
63
    time_t d_limit;
 
64
  };
 
65
 
 
66
  //   typedef __gnu_cxx::hash_map<string, vector<StoredRecord> > cache_t;
 
67
  struct CacheEntry
 
68
  {
 
69
    CacheEntry(){}
 
70
    CacheEntry(const string& name, const vector<StoredRecord>& records) : d_name(name), d_records(records)
 
71
    {}
 
72
    string d_name;
 
73
    typedef vector<StoredRecord> records_t;
 
74
    records_t d_records;
 
75
    uint32_t getTTD() const
 
76
    {
 
77
      uint32_t earliest=numeric_limits<uint32_t>::max();
 
78
      for(records_t::const_iterator i=d_records.begin(); i != d_records.end(); ++i)
 
79
        earliest=min(earliest, i->d_ttd);
 
80
      return earliest;
 
81
    }
 
82
 
 
83
  };
 
84
 
 
85
  typedef multi_index_container<
 
86
    CacheEntry,
 
87
    indexed_by <
 
88
#if BOOST_VERSION >= 103300
 
89
                hashed_unique<member<CacheEntry,string,&CacheEntry::d_name> >,
 
90
#else
 
91
                ordered_unique<member<CacheEntry,string,&CacheEntry::d_name> >,
 
92
#endif
 
93
                ordered_non_unique<const_mem_fun<CacheEntry,uint32_t,&CacheEntry::getTTD> >
 
94
               >
 
95
  > cache_t;
 
96
 
 
97
private:
83
98
  cache_t d_cache;
 
99
 
84
100
};
85
101
 
86
102