container.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2012 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 "container.h"
  15. #include "client.h"
  16. #include "factory.h"
  17. #include <memory>
  18. #include <boost/foreach.hpp>
  19. using namespace isc::data;
  20. using namespace std;
  21. namespace isc {
  22. namespace datasrc {
  23. ConfigurableContainer::ConfigurableContainer(const ConstElementPtr&, bool,
  24. const ConstContainerPtr&)
  25. {
  26. // TODO: Implement
  27. }
  28. Container::SearchResult
  29. ConfigurableContainer::search(const dns::Name& name, bool want_exact_match,
  30. bool ) const
  31. {
  32. // Nothing found yet.
  33. // Pointer is used as the SearchResult can't be assigned.
  34. auto_ptr<SearchResult> candidate(new SearchResult());
  35. BOOST_FOREACH(const DataSourceInfo& info, data_sources_) {
  36. // TODO: Once we have support for the caches, consider them too here
  37. // somehow. This would probably get replaced by a function, that
  38. // checks if there's a cache available, if it is, checks the loaded
  39. // zones and zones expected to be in the real data source. If it is
  40. // the cached one, provide the cached one. If it is in the external
  41. // data source, use the datasource and don't provide the finder yet.
  42. DataSourceClient::FindResult result(info.data_src_->findZone(name));
  43. switch (result.code) {
  44. case result::SUCCESS: {
  45. // If we found an exact match, we have no hope to getting
  46. // a better one. Stop right here.
  47. // TODO: In case we have only the datasource and not the finder
  48. // and the need_updater parameter is true, get the zone there.
  49. return (SearchResult(info.data_src_, result.zone_finder,
  50. name.getLabelCount(), true));
  51. }
  52. case result::PARTIALMATCH: {
  53. if (!want_exact_match) {
  54. // In case we have a partial match, check if it is better
  55. // than what we have. If so, replace it.
  56. uint8_t labels(
  57. result.zone_finder->getOrigin().getLabelCount());
  58. if (labels > candidate->matched_labels_) {
  59. // This one is strictly better. Replace it.
  60. candidate.reset(new SearchResult(info.data_src_,
  61. result.zone_finder,
  62. labels, false));
  63. }
  64. }
  65. break;
  66. }
  67. default: {
  68. // Nothing found, nothing to do.
  69. ;
  70. }
  71. }
  72. }
  73. // TODO: In case we have only the datasource and not the finder
  74. // and the need_updater parameter is true, get the zone there.
  75. // Return the partial match we have. In case we didn't want a partial
  76. // match, this surely contains the original empty result.
  77. return (*candidate);
  78. }
  79. // NOTE: This function is not tested, it would be complicated. However, the
  80. // purpose of the function is to provide a very thin wrapper to be able to
  81. // replace the call to DataSourceClientContainer constructor in tests.
  82. ConfigurableContainer::DataSourcePair
  83. ConfigurableContainer::getDataSource(const string& type,
  84. const ::ConstElementPtr& configuration)
  85. {
  86. DataSourceClientContainerPtr
  87. container(new DataSourceClientContainer(type, configuration));
  88. return (DataSourcePair(&container->getInstance(), container));
  89. }
  90. }
  91. }