WIP: index query, updated cursor connection code

This commit is contained in:
JohnE 2016-01-01 02:00:38 -08:00
parent a39dad4f18
commit eaba709b46
1 changed files with 49 additions and 50 deletions

View File

@ -35,18 +35,23 @@ class FingerprintIndex:
logging.info("DB Open SUCCESSFUL") logging.info("DB Open SUCCESSFUL")
else: else:
logging.info("No index file found, creating index now...") logging.info("No index file found, creating index now...")
self.createIndex(fp_dir) self.__createIndex(fp_dir)
self.db_conn = sql.connect(fq_fpidx) try:
self.cur = self.db_conn.cursor() self.cur = self.db_conn.cursor()
except: self.__populateIndex(fp_dir)
except Exception as ex:
raise FingerprintIndexWrite("Error populating index file\n{}".format(ex))
logging.info("Successfully populated the index")
self.db_conn = sql.connect(fq_fpidx)
except Exception as ex:
if self.db_conn: if self.db_conn:
self.db_conn.close() self.db_conn.close()
self.db_conn = None self.db_conn = None
raise FingerprintIndexOpen("Error opening/creating an index file") logging.error(ex)
raise FingerprintIndexOpen("Error opening/creating an index file\n")
# #
def createIndex(self, fp_dir): def __createIndex(self, fp_dir):
fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME
try: try:
self.db_conn = sql.connect(fq_fpidx) self.db_conn = sql.connect(fq_fpidx)
@ -64,14 +69,8 @@ class FingerprintIndex:
fp_count INTEGER); fp_count INTEGER);
''') ''')
logging.info("Successfully created index table") logging.info("Successfully created index table")
self.__populateIndex(fp_dir)
logging.info("Successfully populated the index")
except Exception as ex: except Exception as ex:
raise FingerprintIndexWrite("Error creating an index file\n{}".format(ex)) raise FingerprintIndexWrite("Error creating index file\n{}".format(ex))
finally:
if self.db_conn:
self.db_conn.close()
self.db_conn = None
# #
def qDatabaseMD5(self, md5_db): def qDatabaseMD5(self, md5_db):
@ -112,11 +111,11 @@ def qTableMD5(self, md5_table):
finCount = finCount+1 finCount = finCount+1
if ((finCount % 5) == 0): if ((finCount % 5) == 0):
self.db_conn.commit() self.db_conn.commit()
except Exception as e: except Exception as ex:
logging.error(e) logging.error(ex)
failCount = failCount+1 failCount = failCount+1
except: except Exception as ex:
pass logging.error(ex)
finally: finally:
self.db_conn.commit() self.db_conn.commit()
logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount))) logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount)))
@ -139,10 +138,10 @@ def qTableMD5(self, md5_table):
''' '''
UPDATE md5_all SET fp_list=?, fp_count=? WHERE md5_db=? UPDATE md5_all SET fp_list=?, fp_count=? WHERE md5_db=?
''', [fp_list, fp_count, md5_db]) ''', [fp_list, fp_count, md5_db])
except Exception as e: except Exception as ex:
raise FingerprintIndexWrite("Error updating a row\n{}".format(e)) raise FingerprintIndexWrite("Error updating a row\n{}".format(ex))
except Exception as e: except Exception as ex:
raise FingerprintIndexWrite("Error inserting a row\n{}".format(e)) raise FingerprintIndexWrite("Error inserting a row\n{}".format(ex))
# #
def __insertMod_md5_tables(self, md5_db, filename): def __insertMod_md5_tables(self, md5_db, filename):
@ -162,10 +161,10 @@ def qTableMD5(self, md5_table):
''' '''
UPDATE md5_tables SET fp_list=?, fp_count=? WHERE md5_table=? UPDATE md5_tables SET fp_list=?, fp_count=? WHERE md5_table=?
''', [fp_list, fp_count, md5_table]) ''', [fp_list, fp_count, md5_table])
except Exception as e: except Exception as ex:
raise FingerprintIndexWrite("Error updating a row\n{}".format(e)) raise FingerprintIndexWrite("Error updating a row\n{}".format(ex))
except Exception as e: except Exception as ex:
raise FingerprintIndexWrite("Error inserting a row\n{}".format(e)) raise FingerprintIndexWrite("Error inserting a row\n{}".format(ex))
# #
def __selectFileList(self, md5_db): def __selectFileList(self, md5_db):
@ -176,8 +175,8 @@ def qTableMD5(self, md5_table):
''', [md5_db]) ''', [md5_db])
for row in rows: for row in rows:
return (row[0], row[1]) return (row[0], row[1])
except: except Exception as ex:
raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(e)) raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(ex))
# #
def __selectFileList222(self, md5_table): def __selectFileList222(self, md5_table):
@ -188,8 +187,8 @@ def qTableMD5(self, md5_table):
''', [md5_table]) ''', [md5_table])
for row in rows: for row in rows:
return (row[0], row[1]) return (row[0], row[1])
except: except Exception as ex:
raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(e)) raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(ex))
# #
def __checkIntegrity(self): def __checkIntegrity(self):