MemDB test driver, bug fixes
[python-rwhoisd.git] / rwhoisd / MemDB.py
index 47cba41..488e3e6 100644 (file)
@@ -25,12 +25,12 @@ class MemDB:
 
     def __init__(self):
 
-        # a dictonary holding the various attribute indexes.  The keys
-        # are lowercase attribute names, values are MemIndex or
-        # CidrMemIndex objects.
+        # a dictionary holding the various attribute indexes.  The keys
+        # are lowercase attribute names, values are MemIndex,
+        # CidrMemIndex, or ComboMemIndex objects.
         self.indexes = {}
 
-        # a dictonary holding the actual rwhoisobjects.  keys are
+        # a dictionary holding the actual rwhoisobjects.  keys are
         # string IDs, values are rwhoisobject instances.
         self.main_index = {}
 
@@ -47,7 +47,7 @@ class MemDB:
         self.normal_indexes = []
         self.cidr_indexes   = []
 
-        # dictonary holding all of the seen class names.  keys are
+        # dictionary holding all of the seen class names.  keys are
         # lowercase classnames, value is always None.
         self.classes = {}
 
@@ -58,8 +58,8 @@ class MemDB:
     def init_schema(self, schema_file):
         """Initialize the schema from a schema file.  Currently the
         schema file is a list of 'attribute_name = index_type' pairs,
-        one per line.  index_type is one of N or C, where N means a
-        normal string index, and C means a CIDR index.
+        one per line.  index_type is one of A (all), N (normal) or C
+        (cidr).
 
         It should be noted that this database implementation
         implements a global namespace for attributes, which isn't
@@ -69,10 +69,10 @@ class MemDB:
 
         # initialize base schema
 
-        self.attrs['id']         = "N"
-        self.attrs['auth-area']  = None
+        self.attrs['id'] = "N"
+        self.attrs['auth-area'] = None
         self.attrs['class-name'] = None
-        self.attrs['updated']    = None
+        self.attrs['updated'] = None
         self.attrs['referred-auth-area'] = "R"
 
         sf = open(schema_file, "r")
@@ -81,7 +81,7 @@ class MemDB:
             line = line.strip()
             if not line or line.startswith("#"): continue
 
-            attr, it = line.split("=")
+            attr, it = line.split("=", 1)
             self.attrs[attr.strip().lower()] = it.strip()[0].upper()
 
         for attr, index_type in self.attrs.items():
@@ -96,7 +96,7 @@ class MemDB:
                 self.cidr_indexes.append(attr)
             elif index_type == "R":
                 # referral index, an all index that must be searched
-                # explictly by attribute
+                # explicitly by attribute
                 self.indexes[attr] = MemIndex.ComboMemIndex()
             elif index_type == "C":
                 # a cidr index
@@ -167,7 +167,7 @@ class MemDB:
 
     def is_indexed_attr(self, attr):
         if self.is_attribute(attr):
-            return self.attrs[attr.lower()]
+            return self.indexes.has_key(attr.lower())
         return False
 
     def is_objectclass(self, objectclass):
@@ -244,7 +244,7 @@ class MemDB:
         return res
 
     def search_referral(self, value, max = 0):
-        """Given a heirarchal value, search for referrals.  Returns a
+        """Given a hierarchical value, search for referrals.  Returns a
         list of object ids or an empty list."""
 
         return self.search_attr("referred-auth-area", value, max)
@@ -275,57 +275,3 @@ class IndexResult:
         to_del = self.data[n:]
         for i in to_del: del self._dict[i]
         self.data = self.data[:n]
-
-
-# test driver
-if __name__ == "__main__":
-    import sys
-    db = MemDB()
-
-    print "loading schema:", sys.argv[1]
-    db.init_schema(sys.argv[1])
-    for data_file in sys.argv[2:]:
-        print "loading data file:", data_file
-        db.load_data(data_file)
-    db.index_data()
-
-    print "Schema: authority areas"
-    for a in db.authareas.keys():
-        print "   %s" % a
-    print "Schema: classes"
-    for c in db.classes.keys():
-        print "   %s" % c
-    print "Schema: attributes"
-    for a in db.attrs.keys():
-        print "   %s" % a
-
-    print "Is 'Network' a class?", db.is_objectclass("Network")
-        
-#    for k, v in db.main_index.items():
-#        print "main_index[", k, "]:", v
-
-    print "searching for a.com"
-    res = db.search_attr("domain-name", "a.com")
-    print res.list()
-    print [ str(x) for x in db.fetch_objects(res.list()) ]
-
-    print "searching for doe"
-    res = db.search_normal("doe")
-    print res.list()
-    print [ str(x) for x in db.fetch_objects(res.list()) ]
-
-    print "searching for 10.0.0.2"
-    res = db.search_cidr("10.0.0.2")
-    print res.list()
-    print [ str(x) for x in db.fetch_objects(res.list()) ]
-
-    print "searching for fddi.a.com"
-    res = db.search_normal("fddi.a.com")
-    print res.list()
-
-    print "searching referral index for fddi.a.com"
-    res = db.search_attr("referred-auth-area", "fddi.a.com")
-    print res.list()
-    print [ str(x) for x in db.fetch_objects(res.list()) ]
-
-