insndb/core: introduce Records type
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 9 Jun 2023 18:27:57 +0000 (21:27 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Fri, 9 Jun 2023 19:57:32 +0000 (22:57 +0300)
src/openpower/insndb/core.py
src/openpower/insndb/db.py

index 5472502f9b12b25d412edb3b45b640f7ef2c245a..26e3c1a9841932fffa4c4e17cda5a836e726d21b 100644 (file)
@@ -3720,6 +3720,19 @@ class SVP64Database:
         return None
 
 
+class Records(tuple):
+    def __new__(cls, records):
+        return super().__new__(cls, sorted(records))
+
+    def subnodes(self, match=None):
+        if match is None:
+            match = lambda subnode: True
+
+        for record in self:
+            if match(record):
+                yield record
+
+
 class Database(Node):
     def __init__(self, root):
         root = _pathlib.Path(root)
@@ -3748,7 +3761,7 @@ class Database(Node):
             names[record.name] = record
             opcodes[section][record.PO].add(record)
 
-        self.__db = sorted(db)
+        self.__db = Records(db)
         self.__names = dict(sorted(names.items()))
         self.__opcodes = dict(sorted(opcodes.items()))
 
@@ -3758,7 +3771,8 @@ class Database(Node):
         if match is None:
             match = lambda subnode: True
 
-        yield from filter(match, self)
+        if match(self.__db):
+            yield self.__db
 
     def __repr__(self):
         return repr(self.__db)
index 2fce21b158efcc88b1ad990b167a9f551160beef..33586152d3bddc345e29c850a6811078ed3215e0 100644 (file)
@@ -10,6 +10,7 @@ from openpower.insndb.core import (
     Database,
     Extra,
     Record,
+    Records,
     Visitor,
     visit,
 )
@@ -138,6 +139,7 @@ def main():
     visitor = commands[command][0]()
 
     db = Database(find_wiki_dir())
+    records = next(db.subnodes(match=lambda node: isinstance(node, Records)))
     if command in ("list",):
         match = None
     else:
@@ -145,7 +147,7 @@ def main():
         def match(record):
             return (isinstance(record, Record) and (record.name == insn))
 
-    for node in db.subnodes(match=match):
+    for node in records.subnodes(match=match):
         visit(visitor=visitor, node=node)