Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include "UrlMappingPathIndex.h"
00024 
00025 UrlMappingPathIndex::~UrlMappingPathIndex()
00026 {
00027   for (UrlMappingGroup::iterator group_iter = m_tries.begin(); group_iter != m_tries.end(); ++group_iter)
00028     delete group_iter->second; 
00029   m_tries.clear();
00030 }
00031 
00032 bool
00033 UrlMappingPathIndex::Insert(url_mapping *mapping)
00034 {
00035   int scheme_idx;
00036   int port = (mapping->fromURL).port_get();
00037   UrlMappingTrie *trie;
00038   int from_path_len;
00039   const char *from_path;
00040 
00041   trie = _GetTrie(&(mapping->fromURL), scheme_idx, port);
00042 
00043   if (!trie) {
00044     trie = new UrlMappingTrie();
00045     m_tries.insert(UrlMappingGroup::value_type(UrlMappingTrieKey(scheme_idx, port), trie));
00046     Debug("UrlMappingPathIndex::Insert", "Created new trie for scheme index, port combo <%d, %d>",
00047           scheme_idx, port);
00048   }
00049 
00050   from_path = mapping->fromURL.path_get(&from_path_len);
00051   if (!trie->Insert(from_path, mapping, mapping->getRank(), from_path_len)) {
00052     Error("Couldn't insert into trie!");
00053     return false;
00054   }
00055   Debug("UrlMappingPathIndex::Insert", "Inserted new element!");
00056   return true;
00057 }
00058 
00059 url_mapping *
00060 UrlMappingPathIndex::Search(URL *request_url, int request_port, bool normal_search ) const
00061 {
00062   url_mapping *retval = 0;
00063   int scheme_idx;
00064   UrlMappingTrie *trie;
00065   int path_len;
00066   const char *path;
00067 
00068   trie = _GetTrie(request_url, scheme_idx, request_port, normal_search);
00069 
00070   if (!trie) {
00071     Debug("UrlMappingPathIndex::Search", "No mappings exist for scheme index, port combo <%d, %d>",
00072           scheme_idx, request_port);
00073     goto lFail;
00074   }
00075 
00076   path = request_url->path_get(&path_len);
00077   if (!(retval = trie->Search(path, path_len))) {
00078     Debug("UrlMappingPathIndex::Search", "Couldn't find entry for url with path [%.*s]", path_len, path);
00079     goto lFail;
00080   }
00081   return retval;
00082 
00083 lFail:
00084   return 0;
00085 }
00086 
00087 void
00088 UrlMappingPathIndex::Print()
00089 {
00090   for (UrlMappingGroup::iterator group_iter = m_tries.begin(); group_iter != m_tries.end(); ++group_iter)
00091     group_iter->second->Print();
00092 }