[hamster-applet] avoid committing midair, thus saving time and using power of transactions



commit 635872a0c40aaf50d5781d627cff11ac079e96f0
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat Dec 26 04:36:28 2009 +0000

    avoid committing midair, thus saving time and using power of transactions

 hamster/db.py      |   37 ++++++++++++++++++++-----------------
 hamster/storage.py |   12 +++++++++---
 2 files changed, 29 insertions(+), 20 deletions(-)
---
diff --git a/hamster/db.py b/hamster/db.py
index ede7b75..2f3a133 100644
--- a/hamster/db.py
+++ b/hamster/db.py
@@ -46,7 +46,6 @@ DB_FILE = 'hamster.db'
 
 class Storage(storage.Storage):
     con = None # Connection will be created on demand
-
     def __setup(self):
         """
         Delayed setup so we don't do everything at the same time
@@ -54,6 +53,9 @@ class Storage(storage.Storage):
         if self.__setup.im_func.complete:
             return
 
+        self.__con = None
+        self.__cur = None
+
         from configuration import runtime, GconfStore
 
         db_file = runtime.database_path
@@ -717,18 +719,6 @@ class Storage(storage.Storage):
             
         return activities
 
-    def __get_sorted_activities(self):
-        """returns list of acitivities that have categories"""
-        query = """
-                   SELECT a.*, b.name as category, b.category_order
-                     FROM activities a
-                LEFT JOIN categories b on coalesce(b.id, -1) = a.category_id
-                    WHERE a.category_id > -1
-                      AND a.deleted is null
-                 ORDER BY category_order, activity_order
-        """
-        return self.fetchall(query)
-        
     def __get_autocomplete_activities(self):
         """returns list of activities for autocomplete,
            activity names converted to lowercase"""
@@ -846,8 +836,8 @@ class Storage(storage.Storage):
         from configuration import runtime
         self.__setup()
 
-        con = self.connection
-        cur = con.cursor()
+        con = self.__con or self.connection
+        cur = self.__cur or con.cursor()
         
         if isinstance(statement, list) == False: #we kind of think that we will get list of instructions
             statement = [statement]
@@ -859,9 +849,22 @@ class Storage(storage.Storage):
          
                 res = cur.execute(statement[i], params[i])
 
-        con.commit()
-        cur.close()
+        if not self.__con:
+            con.commit()
+            cur.close()
+            runtime.register_modification()
         
+        
+    def start_transaction(self):
+        # will give some hints to execute not to close or commit anything
+        self.__con = self.connection
+        self.__cur = self.__con.cursor()
+    
+    def end_transaction(self):
+        self.__con.commit()
+        self.__cur.close()
+        self.__con = None
+        from configuration import runtime
         runtime.register_modification()
         
     def run_fixtures(self):
diff --git a/hamster/storage.py b/hamster/storage.py
index 8cf81a2..e4fe975 100644
--- a/hamster/storage.py
+++ b/hamster/storage.py
@@ -56,7 +56,10 @@ class Storage(object):
     def add_fact(self, activity_name, tags, start_time = None, end_time = None,
                                       category_name = None, description = None):
 
+        self.start_transaction()
         result = self.__add_fact(activity_name, tags, start_time, end_time, category_name, description)
+        self.end_transaction()
+
         if result:
             self.dispatch('day_updated', result['start_time'])
         return result
@@ -80,11 +83,17 @@ class Storage(object):
             self.dispatch('day_updated', fact['start_time'])
 
     def update_fact(self, fact_id, activity_name, tags, start_time, end_time):
+        now = datetime.datetime.now()
+        self.start_transaction()
+        
         fact = self.get_fact(fact_id)
         if fact:
             self.__remove_fact(fact_id)
         
         result = self.__add_fact(activity_name, tags, start_time, end_time)
+
+        self.end_transaction()
+        
         if result:
             self.dispatch('day_updated', result['start_time'])
         return result
@@ -92,9 +101,6 @@ class Storage(object):
     def get_activities(self, category_id = None):
         return self.__get_activities(category_id = category_id)
     
-    def get_sorted_activities(self):
-        return self.__get_sorted_activities()
-        
     def get_autocomplete_activities(self):
         return self.__get_autocomplete_activities()
     



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]