rrset_cache.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // $Id$
  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. namespace isc {
  23. namespace cache {
  24. RRsetCache::RRsetCache(uint32_t cache_size,
  25. uint16_t rrset_class):
  26. class_(rrset_class),
  27. rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
  28. rrset_lru_((3 * cache_size),
  29. new HashDeleter<RRsetEntry>(rrset_table_))
  30. {
  31. }
  32. RRsetEntryPtr
  33. RRsetCache::lookup(const isc::dns::Name& qname,
  34. const isc::dns::RRType& qtype)
  35. {
  36. CacheEntryKey keydata = genCacheEntryKey(qname, qtype);
  37. //TODO, HashKey need to be refactored, since we don't need query class
  38. // as the parameters.
  39. RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(
  40. keydata.first, keydata.second, RRClass(class_)));
  41. //If the rrset entry has expired, return NULL.
  42. if(entry_ptr && (time(NULL) > entry_ptr->getExpireTime())) {
  43. return RRsetEntryPtr();
  44. }
  45. return entry_ptr;
  46. }
  47. RRsetEntryPtr
  48. RRsetCache::update(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) {
  49. // lookup first
  50. RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
  51. if(!entry_ptr) {
  52. // rrset entry doesn't exist, create one rrset entry for the rrset
  53. // and add it directly.
  54. entry_ptr.reset(new RRsetEntry(rrset, level));
  55. // Replace the expired rrset entry if it exists.
  56. rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
  57. //TODO , lru list touch.
  58. return entry_ptr;
  59. } else {
  60. // there is one rrset entry in the cache, need to check whether
  61. // the new rrset is more authoritative.
  62. if (entry_ptr->getTrustLevel() > level) {
  63. // existed rrset entry is more authoritative, do nothing,
  64. // just return it.
  65. //TODO, lru list touch
  66. return entry_ptr;
  67. } else {
  68. HashKey key = entry_ptr->hashKey();
  69. rrset_table_.remove(key);
  70. entry_ptr.reset(new RRsetEntry(rrset, level));
  71. //TODO, lru list touch.
  72. // Replace the expired rrset entry if it exists.
  73. rrset_table_.add(entry_ptr, key, true);
  74. return entry_ptr;
  75. }
  76. }
  77. }
  78. HashKey
  79. RRsetCache::getEntryHashKey(const Name& name, const RRType& type) const
  80. {
  81. CacheEntryKey keydata = genCacheEntryKey(name, type);
  82. return HashKey(keydata.first, keydata.second, RRClass(class_));
  83. }
  84. void
  85. RRsetCache::dump(const std::string&) {
  86. //TODO
  87. }
  88. void
  89. RRsetCache::load(const std::string&) {
  90. //TODO
  91. }
  92. bool
  93. RRsetCache::resize(uint32_t) {
  94. //TODO
  95. return true;
  96. }
  97. } // namespace cache
  98. } // namespace isc