Browse Source

basestorage.find_node(): try a direct node-id match, first

This should result in a huge performance boost, as most calls to
find_node use the node_id anyways. In order to implement this feature,
BaseStorage was extended by a get_node(id) function which implementors
must implement to get the node having the given id (or return None if no
node with that id exists).
Helge Jung 9 years ago
parent
commit
f0bd3921ab
3 changed files with 16 additions and 0 deletions
  1. 5 0
      ffstatus/basestorage.py
  2. 5 0
      ffstatus/filestorage.py
  3. 6 0
      ffstatus/redisstorage.py

+ 5 - 0
ffstatus/basestorage.py

@@ -189,6 +189,11 @@ class BaseStorage(object):
         If necessary, look through node aliases.
         """
 
+        # try direct match, first
+        node = self.get_node(rawid)
+        if node is not None:
+            return sanitize_node(node, include_raw_data=include_raw_data)
+
         # look through all nodes
         found = None
         nodes = self.get_all_nodes_raw()

+ 5 - 0
ffstatus/filestorage.py

@@ -67,6 +67,11 @@ class FileStorage(BaseStorage):
         self.storage_file = None
         BaseStorage.close(self)
 
+    def get_node(self, id):
+        """Gets the node by its id, if any."""
+
+        return self.__data.get(id)
+
     def get_all_nodes_raw(self):
         """Gets all nodes as dict."""
 

+ 6 - 0
ffstatus/redisstorage.py

@@ -27,6 +27,9 @@ class RedisStorage(BaseStorage):
     def save(self):
         self.db.save()
 
+    def get_node(self, id):
+        return self.get_node_data(id)
+
     def get_all_nodes_raw(self):
         keys = self.db.keys('node_*')
         nodes = {}
@@ -57,6 +60,9 @@ class RedisStorage(BaseStorage):
     def get_node_data(self, key):
         node = {}
         thedata = self.db.hgetall('node_' + key)
+        if thedata is None:
+            return None
+
         for item in thedata:
             if item.endswith(self.FIELDSUFFIX_TYPE):
                 continue