Commits

Nacho Álvarez committed 5fa62be

Fixing errors pytables backend and complete zodb backend

Comments (0)

Files changed (4)

backends/pybsddb.py

     if self.mode == "w":
       self.test_db_items.close()
 
+  def getTestDBItems(self):
+    return self.test_db_items.values()

backends/pytables.py

     if self.mode == "w":
       self.h5file.close()
 
+  def getTestDBItems(self):
+    return self.test_db_items 

backends/pyzodb.py

+from ZODB.FileStorage import FileStorage
+from ZODB.DB import DB
+import transaction
+
+
+class TestBackend:
+
+  def __init__(self, filename, mode):
+
+    self.mode = mode
+
+    if mode == "w":
+      self.storage = FileStorage(filename)
+      db = DB(self.storage)
+      connection = db.open()
+      self.test_db_items = connection.root()
+
+    elif mode == "r":
+      self.storage = FileStorage(filename)
+      db = DB(self.storage)
+      connection = db.open()
+      self.test_db_items = connection.root()
+
+      self.next_rec_num = 0   # Initialise next record counter
+      self.num_records = len(self.test_db_items)
+
+  def __setitem__(self, key, value):
+
+    self.test_db_items[key] = value
+
+  def __getitem__(self, key):
+
+    return self.test_db_items[str(key)]
+
+  def __len__(self):
+
+    return len(self.test_db_items)
+
+  def first(self):
+
+    return self.test_db_items[0]
+
+  def iteritems(self):
+
+    while(self.next_rec_num < self.num_records):
+      value = self.test_db_items[self.next_rec_num]
+  
+      self.next_rec_num += 1
+
+      yield value
+
+  def close(self):
+    transaction.commit()
+    self.storage.close()
+
+  def getTestDBItems(self):
+    return self.test_db_items.values()
 import imp
 import os
 
-FILENAME='/tmp/fichero1.csv'
+# CSV to process
+FILENAME = '/tmp/fichero1.csv'
+
 
 class Test:
 
   def __init__(self, csv_reader, module):
 
     # import backend
-    f, filename, description = imp.find_module('backends')
-    example_package = imp.load_module('backends', f, filename, description)
-
-    f, filename, description = imp.find_module(module, example_package.__path__)
+    f, filename, description = imp.find_module(module, ['backends'])
     try:
-        moduleObj = imp.load_module('backend.module', f, filename, description)
+        moduleObj = imp.load_module(module, f, filename, description)
     finally:
         f.close()
 
     # open test write database for chosen backend
-    test_file = '/tmp/test.db' 
+    test_file = '/tmp/test' + module + '.db'
     test_db = moduleObj.TestBackend(test_file, "w")
 
     # start write test to disk
     start_write = time.time()
-    
+
     i = 0
     for row in csv_reader:
       test_db[str(i)] = str(row)
 
     end_write = time.time()
 
-    print "Module writing time: " + str(end_write - start_write)
-    
+    print module + " writing time: " + str(end_write - start_write)
+
     # start read test to memory from disk
     start_read = time.time()
 
     test_db = moduleObj.TestBackend(test_file, "r")
 
-    i = 0
-    for (key, value) in test_db.iteritems():
-      memvar = value
-      i += 1
+    # Read records. PyTables uses a different way based on class description
+    for item in test_db.getTestDBItems():
+      if module == 'pytables':
+        a = item['key']
+        b = item['value']
+      else:
+        memvar = item
+
+    #Another way to read records
+    #for (key, value) in test_db.iteritems():
+    #  memvar = value
+    #  i += 1
 
     test_db.close()
 
     end_read = time.time()
 
-    print "Module reading time: " + str(end_read - start_read)
+    print module + " reading time: " + str(end_read - start_read) + "\n"
+
 
 def getCSVReader():
   # CSV dialect where lines end in "\n"
 
 # test experiment
 tester = Test(getCSVReader(), "pytables")
+#tester = Test(getCSVReader(), "pybsddb")
+tester = Test(getCSVReader(), "pyzodb")