mirrorlist-server/mirrorlist_server.py | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
New commits:
commit 5b00b84789e4c40605fae640cafe7362e49156b1
Author: Matt Domsch <Matt_Domsch(a)dell.com>
Date: Sat Jul 9 19:49:32 2011 -0500
mirrorlist_server: early exit from tree_lookup() if more results aren't needed
diff --git a/mirrorlist-server/mirrorlist_server.py
b/mirrorlist-server/mirrorlist_server.py
index f11966f..947e48d 100755
--- a/mirrorlist-server/mirrorlist_server.py
+++ b/mirrorlist-server/mirrorlist_server.py
@@ -185,8 +185,8 @@ def metalink(cache, directory, file, hosts_and_urls):
doc += '</metalink>\n'
return ('metalink', 200, doc)
-def tree_lookup(tree, ip):
- # fast lookup in the tree; if present, find all the netblocks by deleting the found
one and searching again
+def tree_lookup(tree, ip, maxResults=None):
+ # fast lookup in the tree; if present, find all the matching values by deleting the
found one and searching again
# this is safe w/o copying the tree again only because this is the only place the
tree is used, and
# we'll get a new copy of the tree from our parent the next time it fork()s.
result = []
@@ -196,13 +196,13 @@ def tree_lookup(tree, ip):
while node is not None:
result.extend(node.data['hosts'])
prefix = node.prefix
- tree.delete(prefix)
- node = tree.search_best(ip.strNormal())
+ if maxResults is None or len(result) < maxResults:
+ tree.delete(prefix)
+ node = tree.search_best(ip.strNormal())
+ else:
+ break
return result
-def client_netblocks(ip):
- return tree_lookup(host_netblocks_tree, ip)
-
def trim_by_client_country(s, clientCountry):
if clientCountry is None:
return s
@@ -293,7 +293,7 @@ def do_country(kwargs, cache, clientCountry, requested_countries,
header):
def do_netblocks(kwargs, cache, header):
hostresults = set()
if not kwargs.has_key('netblock') or kwargs['netblock'] ==
"1":
- hosts = client_netblocks(kwargs['IP'])
+ hosts = tree_lookup(host_netblocks_tree, kwargs['IP'])
for hostid in hosts:
if hostid in cache['byHostId']:
hostresults.add(hostid)
@@ -399,7 +399,7 @@ def client_ip_to_country(ip):
return None
# lookup in the cache first
- result = tree_lookup(netblock_country_tree, ip)
+ result = tree_lookup(netblock_country_tree, ip, maxResults=1)
if len(result) > 0:
clientCountry = result[0]
return clientCountry
Show replies by date