[Neo-report] r1993 gregory - in /trunk/neo: storage/ storage/database/ storage/handlers/ t...

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Apr 1 16:10:36 CEST 2010


Author: gregory
Date: Thu Apr  1 16:10:36 2010
New Revision: 1993

Log:
Fix load/store of None configuration values with database manager.

- Store None values as NULL column values
- Raise KeyError if the configuration entry is not found

Modified:
    trunk/neo/storage/app.py
    trunk/neo/storage/database/mysqldb.py
    trunk/neo/storage/handlers/verification.py
    trunk/neo/tests/storage/testStorageMySQLdb.py

Modified: trunk/neo/storage/app.py
==============================================================================
--- trunk/neo/storage/app.py [iso-8859-1] (original)
+++ trunk/neo/storage/app.py [iso-8859-1] Thu Apr  1 16:10:36 2010
@@ -87,34 +87,46 @@
         """Load persistent configuration data from the database.
         If data is not present, generate it."""
 
+        def NoneOnKeyError(getter):
+            try:
+                return getter()
+            except KeyError:
+                return None
         dm = self.dm
 
-        self.uuid = dm.getUUID()
-        num_partitions = dm.getNumPartitions()
-        num_replicas = dm.getNumReplicas()
-
+        # check cluster name
+        try:
+            if dm.getName() != self.name:
+                raise RuntimeError('name does not match with the database')
+        except KeyError:
+            dm.setName(self.name)
+
+        # load configuration
+        self.uuid = NoneOnKeyError(dm.getUUID)
+        num_partitions = NoneOnKeyError(dm.getNumPartitions)
+        num_replicas = NoneOnKeyError(dm.getNumReplicas)
+        ptid = NoneOnKeyError(dm.getPTID)
+
+        # check partition table configuration
         if num_partitions is not None and num_replicas is not None:
             if num_partitions <= 0:
                 raise RuntimeError, 'partitions must be more than zero'
             # create a partition table
             self.pt = PartitionTable(num_partitions, num_replicas)
 
-        name = dm.getName()
-        if name is None:
-            dm.setName(self.name)
-        elif name != self.name:
-            raise RuntimeError('name does not match with the database')
-        ptid = dm.getPTID()
         logging.info('Configuration loaded:')
         logging.info('UUID      : %s', dump(self.uuid))
         logging.info('PTID      : %s', dump(ptid))
-        logging.info('Name      : %s', name)
+        logging.info('Name      : %s', self.name)
         logging.info('Partitions: %s', num_partitions)
         logging.info('Replicas  : %s', num_replicas)
 
     def loadPartitionTable(self):
         """Load a partition table from the database."""
-        ptid = self.dm.getPTID()
+        try:
+            ptid = self.dm.getPTID()
+        except KeyError:
+            ptid = None
         cell_list = self.dm.getPartitionTable()
         new_cell_list = []
         for offset, uuid, state in cell_list:

Modified: trunk/neo/storage/database/mysqldb.py
==============================================================================
--- trunk/neo/storage/database/mysqldb.py [iso-8859-1] (original)
+++ trunk/neo/storage/database/mysqldb.py [iso-8859-1] Thu Apr  1 16:10:36 2010
@@ -135,7 +135,7 @@
         # persistent data.
         q("""CREATE TABLE IF NOT EXISTS config (
                  name VARBINARY(16) NOT NULL PRIMARY KEY,
-                 value VARBINARY(255) NOT NULL
+                 value VARBINARY(255) NULL
              ) ENGINE = InnoDB""")
 
         # The table "pt" stores a partition table.
@@ -191,18 +191,20 @@
         q = self.query
         e = self.escape
         key = e(str(key))
-        r = q("""SELECT value FROM config WHERE name = '%s'""" % key)
-        try:
-            return r[0][0]
+        try:
+            return q("SELECT value FROM config WHERE name = '%s'" % key)[0][0]
         except IndexError:
-            return None
+            raise KeyError, key
 
     def _setConfiguration(self, key, value):
         q = self.query
         e = self.escape
         key = e(str(key))
-        value = e(str(value))
-        q("""REPLACE INTO config VALUES ('%s', '%s')""" % (key, value))
+        if value is None:
+            value = 'NULL'
+        else:
+            value = "'%s'" % (e(str(value)), )
+        q("""REPLACE INTO config VALUES ('%s', %s)""" % (key, value))
 
     def getPartitionTable(self):
         q = self.query

Modified: trunk/neo/storage/handlers/verification.py
==============================================================================
--- trunk/neo/storage/handlers/verification.py [iso-8859-1] (original)
+++ trunk/neo/storage/handlers/verification.py [iso-8859-1] Thu Apr  1 16:10:36 2010
@@ -27,8 +27,14 @@
 
     def askLastIDs(self, conn):
         app = self.app
-        oid = app.dm.getLastOID()
-        tid = app.dm.getLastTID()
+        try:
+            oid = app.dm.getLastOID()
+        except KeyError:
+            oid = None
+        try:
+            tid = app.dm.getLastTID()
+        except KeyError:
+            tid = None
         conn.answer(Packets.AnswerLastIDs(oid, tid, app.pt.getID()))
 
     def askPartitionTable(self, conn, offset_list):

Modified: trunk/neo/tests/storage/testStorageMySQLdb.py
==============================================================================
--- trunk/neo/tests/storage/testStorageMySQLdb.py [iso-8859-1] (original)
+++ trunk/neo/tests/storage/testStorageMySQLdb.py [iso-8859-1] Thu Apr  1 16:10:36 2010
@@ -151,9 +151,8 @@
     def test_10_getConfiguration(self):
         # check if a configuration entry is well read
         self.db.setup()
-        result = self.db.getConfiguration('a')
-        # doesn't exists, None expected
-        self.assertEquals(result, None)
+        # doesn't exists, raise
+        self.assertRaises(KeyError, self.db.getConfiguration, 'a')
         self.db.query("insert into config values ('a', 'b');")
         result = self.db.getConfiguration('a')
         # exists, check result
@@ -169,7 +168,7 @@
     def checkConfigEntry(self, get_call, set_call, value):
         # generic test for all configuration entries accessors
         self.db.setup()
-        self.assertEquals(get_call(), None)
+        self.assertRaises(KeyError, get_call)
         set_call(value)
         self.assertEquals(get_call(), value)
         set_call(value * 2)
@@ -194,13 +193,10 @@
             value='TEST_NAME')
 
     def test_15_PTID(self):
-        test = '\x01' * 8
-        self.db.setup()
-        self.assertEquals(self.db.getPTID(), None)
-        self.db.setPTID(test)
-        self.assertEquals(self.db.getPTID(), test)
-        self.db.setPTID(test * 2)
-        self.assertEquals(self.db.getPTID(), test * 2)
+        self.checkConfigEntry(
+            get_call=self.db.getPTID,
+            set_call=self.db.setPTID,
+            value=self.getPTID(1))
 
     def test_16_getPartitionTable(self):
         # insert an entry and check it





More information about the Neo-report mailing list