rrset_cache.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <string>
  16. #include "rrset_cache.h"
  17. #include <nsas/nsas_entry_compare.h>
  18. #include <nsas/hash_table.h>
  19. #include <nsas/hash_deleter.h>
  20. using namespace isc::nsas;
  21. using namespace isc::dns;
  22. using namespace std;
  23. namespace isc {
  24. namespace cache {
  25. RRsetCache::RRsetCache(uint32_t cache_size,
  26. uint16_t rrset_class):
  27. class_(rrset_class),
  28. rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
  29. rrset_lru_((3 * cache_size),
  30. new HashDeleter<RRsetEntry>(rrset_table_))
  31. {
  32. }
  33. RRsetEntryPtr
  34. RRsetCache::lookup(const isc::dns::Name& qname,
  35. const isc::dns::RRType& qtype)
  36. {
  37. const string entry_name = genCacheEntryName(qname, qtype);
  38. RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name, RRClass(class_)));
  39. //If the rrset entry has expired, return NULL.
  40. if(entry_ptr && (time(NULL) > entry_ptr->getExpireTime())) {
  41. return (RRsetEntryPtr());
  42. }
  43. return (entry_ptr);
  44. }
  45. RRsetEntryPtr
  46. RRsetCache::update(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) {
  47. // TODO: If the RRset is an NS, we should update the NSAS as well
  48. // lookup first
  49. RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
  50. if(!entry_ptr) {
  51. // rrset entry doesn't exist, create one rrset entry for the rrset
  52. // and add it directly.
  53. entry_ptr.reset(new RRsetEntry(rrset, level));
  54. // Replace the expired rrset entry if it exists.
  55. rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
  56. //TODO , lru list touch.
  57. return (entry_ptr);
  58. } else {
  59. // there is one rrset entry in the cache, need to check whether
  60. // the new rrset is more authoritative.
  61. if (entry_ptr->getTrustLevel() > level) {
  62. // existed rrset entry is more authoritative, do nothing,
  63. // just return it.
  64. //TODO, lru list touch
  65. return (entry_ptr);
  66. } else {
  67. HashKey key = entry_ptr->hashKey();
  68. entry_ptr.reset(new RRsetEntry(rrset, level));
  69. //TODO, lru list touch.
  70. // Replace the expired rrset entry if it exists.
  71. rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
  72. return (entry_ptr);
  73. }
  74. }
  75. }
  76. #if 0
  77. void
  78. RRsetCache::dump(const std::string&) {
  79. //TODO
  80. }
  81. void
  82. RRsetCache::load(const std::string&) {
  83. //TODO
  84. }
  85. bool
  86. RRsetCache::resize(uint32_t) {
  87. //TODO
  88. return (true);
  89. }
  90. #endif
  91. } // namespace cache
  92. } // namespace isc