log_search.py
changeset 93 48fca00689e3
parent 89 2dc6de43f317
child 96 d30c88e89a7e
--- a/log_search.py	Wed Feb 11 01:21:22 2009 +0200
+++ b/log_search.py	Wed Feb 11 02:07:07 2009 +0200
@@ -46,7 +46,7 @@
     def __init__ (self, channels, path, mode='r') :
         """
             Open the database at the given path, with the given mode:
-                r       - read-only
+                r       - read, error if not exists
                 w       - write, create if not exists
                 a       - write, error if not exists
                 c       - write, create, error if exists
@@ -88,75 +88,86 @@
             Adds a sequence of LogLines from the given LogChannel to the index, and return the number of added items
         """
         
-        # validate the LogChannel
-        assert channel.name
-
+        # count from zero
         count = 0
         
         # iterate
         for line in lines :
-            # validate the LogLine
-            assert line.offset
-            assert line.timestamp
-
-            # create new document
-            doc = hype.Document()
-
-            # line date
-            date = line.timestamp.date()
-
-            # ensure that it's not 1900
-            assert date.year != 1900
-
-            # add URI
-            doc.add_attr('@uri',        "%s/%s/%d" % (channel.id, date.strftime('%Y-%m-%d'), line.offset))
-
-            # add channel id
-            doc.add_attr('channel',     channel.id)
-
-            # add type
-            doc.add_attr('type',        str(line.type))
-
-            # add UTC timestamp
-            doc.add_attr('timestamp',   str(utils.to_utc_timestamp(line.timestamp)))
+            # insert
+            self.insert_line(channel, line)
 
-            # add source attribute?
-            if line.source :
-                source_nickname, source_username, source_hostname, source_chanflags = line.source
-
-                if source_nickname :
-                    doc.add_attr('source_nickname', source_nickname.encode('utf8'))
-                
-                if source_username :
-                    doc.add_attr('source_username', source_username.encode('utf8'))
-
-                if source_hostname :
-                    doc.add_attr('source_hostname', source_hostname.encode('utf8'))
-
-                if source_chanflags :
-                    doc.add_attr('source_chanflags', source_chanflags.encode('utf8'))
-            
-            # add target attributes?
-            if line.target :
-                target_nickname = line.target
-
-                if target_nickname :
-                    doc.add_attr('target_nickname', target_nickname.encode('utf8'))
-
-            # add data
-            if line.data :
-                doc.add_text(line.data.encode('utf8'))
-
-            # put, "clean up dispensable regions of the overwritten document"
-            if not self.db.put_doc(doc, hype.Database.PDCLEAN) :
-                raise Exeception("Index put_doc failed")
-            
             # count
             count += 1
         
         # return
         return count
 
+
+
+    def insert_line (self, channel, line) :
+        """
+            Adds a single LogLine for the given LogChannel to the index
+        """
+
+        # validate the LogChannel
+        assert channel.id
+
+        # validate the LogLine
+        assert line.offset
+        assert line.timestamp
+
+        # create new document
+        doc = hype.Document()
+
+        # line date
+        date = line.timestamp.date()
+
+        # ensure that it's not 1900
+        assert date.year != 1900
+
+        # add URI
+        doc.add_attr('@uri',        "%s/%s/%d" % (channel.id, date.strftime('%Y-%m-%d'), line.offset))
+
+        # add channel id
+        doc.add_attr('channel',     channel.id)
+
+        # add type
+        doc.add_attr('type',        str(line.type))
+
+        # add UTC timestamp
+        doc.add_attr('timestamp',   str(utils.to_utc_timestamp(line.timestamp)))
+
+        # add source attribute?
+        if line.source :
+            source_nickname, source_username, source_hostname, source_chanflags = line.source
+
+            if source_nickname :
+                doc.add_attr('source_nickname', source_nickname.encode('utf8'))
+            
+            if source_username :
+                doc.add_attr('source_username', source_username.encode('utf8'))
+
+            if source_hostname :
+                doc.add_attr('source_hostname', source_hostname.encode('utf8'))
+
+            if source_chanflags :
+                doc.add_attr('source_chanflags', source_chanflags.encode('utf8'))
+        
+        # add target attributes?
+        if line.target :
+            target_nickname = line.target
+
+            if target_nickname :
+                doc.add_attr('target_nickname', target_nickname.encode('utf8'))
+
+        # add data
+        if line.data :
+            doc.add_text(line.data.encode('utf8'))
+
+        # put, "clean up dispensable regions of the overwritten document"
+        if not self.db.put_doc(doc, hype.Database.PDCLEAN) :
+            raise Exeception("Index put_doc failed")
+            
     def search_cond (self, cond) :
         """
             Search using a raw hype.Condition. Raises NoResultsFound if there aren't any results