Esempio n. 1
0
    def getFileId(self, src_path):
        if src_path:
            query = "select file_id from file_meta where file_path=\'" + src_path + "\';"
            db_logger.info(query)
            cursor = self.connnection.cursor()
            cursor.execute(query)
            result = cursor.fetchone()

            if result is None:
                file_id = str(uuid.uuid1())
                result = [file_id]
                FileMeta.path_to_uuid_map[src_path] = file_id

                last_update_time = last_move_time = create_time = str(
                    time.time())
                access_count = write_count = "0"
                volume_info = DiskUtil.getDiskId(src_path)
                file_tag = "tada!"
                query = "insert into file_meta values( \'" + str(file_id)+"\', \'" \
                + src_path +"\', \'0\',\'" + create_time+ "\', \'" + last_update_time+"\', \'" + last_move_time\
                +"\', \'" + str(access_count)+"\', \'" + str(write_count)+"\', \'" + volume_info+"\', \'" + file_tag+"\');"
                db_logger.info("Executing: " + query)
                self.insert(query)

            cursor.close()
            self.connnection.commit()
            return result[0]
Esempio n. 2
0
 def resetCounts(self):
     curr_time = str(time.time() - 1800)
     query = "update file_meta set write_count=\'0\' where last_update_time < \'" + curr_time + "\';"
     db_logger.info(query)
     cursor = self.connnection.cursor()
     cursor.execute(query)
     cursor.close()
     self.connnection.commit()
Esempio n. 3
0
 def query(self, query):
     cursor = self.connnection.cursor()
     db_logger.info("executing: " + query)
     cursor.execute(query)
     result = cursor.fetchall()
     cursor.close()
     self.connnection.commit()
     return result
Esempio n. 4
0
 def updateSize(self, src_path, size):
     if src_path:
         query = "update file_meta set file_size=\'" + size + "\' where file_path=\'" + src_path + "\';"
         db_logger.info(query)
         cursor = self.connnection.cursor()
         cursor.execute(query)
         cursor.close()
         self.connnection.commit()
Esempio n. 5
0
 def removeStaleEntry(self, src_path):
     if src_path:
         query = "delete from file_meta where file_path=\'" + src_path + "\';"
         db_logger.info(query)
         cursor = self.connnection.cursor()
         cursor.execute(query)
         cursor.close()
         self.connnection.commit()
Esempio n. 6
0
 def cleanupStaleEntries(self):
     query = "select file_path from file_meta limit 100;"
     db_logger.info("cleanupStaleEntries: " + query)
     cursor = self.connnection.cursor()
     cursor.execute(query)
     result = cursor.fetchall()
     for row in result:
         if not os.path.exists(row[0]):
             self.removeStaleEntry(row[0])
Esempio n. 7
0
 def getHotRow(self, disk_id=None):
     if disk_id:
         query = "select file_path,file_size,write_count,volume_info from file_meta \
         where volume_info=\'" + disk_id + "\' and file_size>'5242880' order by write_count desc limit 1;"
         db_logger.info("getHotRow: " + query)
         cursor = self.connnection.cursor()
         cursor.execute(query)
         result = cursor.fetchone()
         cursor.close()
         self.connnection.commit()
         return result
Esempio n. 8
0
 def updateFilePath(self, file_id, dst_path):
     if file_id and dst_path:
         query = "update file_meta set file_path=\'" + dst_path + "\',\
         volume_info=\'" + DiskUtil.getDiskId(
             dst_path) + "\' where file_id=\'" + file_id + "\';"
         db_logger.info(query)
         cursor = self.connnection.cursor()
         cursor.execute(query)
         cursor.close()
         self.connnection.commit()
     else:
         db_logger.error(str(file_id) + dst_path)
Esempio n. 9
0
    def writeToDB():
        #Write to DB only if any dictionary is not empty
        if len(FileMeta.access_count_map) > 0 or len(
                FileMeta.write_count_map) > 0:
            db_logger.info("Updating counts in DB...")
            db_conn = DBUtil()
            #loop on all file locks
            for path in list(FileMeta.path_to_uuid_map):
                realpath = os.path.realpath(path)
                file_id = FileMeta.path_to_uuid_map[realpath]

                if not file_id:
                    file_id = db_conn.getFileId(realpath)
                    FileMeta.path_to_uuid_map[realpath] = file_id

                if file_id:
                    #Aquire lock so that policy thread wont interfere. Unlock in release method
                    lock = FileMeta.lock_map[file_id]
                    lock.acquire()
                    realpath = os.path.realpath(path)
                    old_counts = db_conn.getCounts(file_id)
                else:
                    file_id = str(uuid.uuid1())
                    FileMeta.path_to_uuid_map[realpath] = file_id
                    lock = FileMeta.lock_map[file_id]
                    lock.acquire()
                    realpath = os.path.realpath(path)
                    last_update_time = last_move_time = create_time = str(
                        time.time())
                    access_count = write_count = "0"
                    volume_info = DiskUtil.getDiskId(realpath)
                    file_tag = "tada!"
                    query = "insert into file_meta values( \'" + str(file_id)+"\', \'" \
                    + realpath +"\', \'" + str(os.path.getsize(realpath)) + "\',\'" + create_time+ "\', \'" + last_update_time+"\', \'" + last_move_time\
                    +"\', \'" + str(access_count)+"\', \'" + str(write_count)+"\', \'" + volume_info+"\', \'" + file_tag+"\');"
                    db_logger.info("Executing: " + query)
                    db_conn.insert(query)
                    old_counts = [0, 0]

                if not old_counts:
                    old_counts = [0, 0]

                #only write if atleast one count is non zero. (This condition should not occur)
                if FileMeta.access_count_map[
                        file_id] > 0 or FileMeta.write_count_map[file_id] > 0:
                    #write to DB
                    update_query = "update file_meta set access_count=\'" + \
                    str(FileMeta.access_count_map[file_id] + old_counts[0])+"\', write_count=\'" \
                    + str(FileMeta.write_count_map[file_id] + old_counts[0])+"\', file_size=\'" + str(os.path.getsize(realpath)) + "\', \
                    last_update_time=\'"                                         + str(time.time()) +"\' where file_id=\'" + str(file_id)+ "\';"
                    db_logger.info("Executing: " + update_query)
                    db_conn.insert(update_query)

                lock.release()
            #Clear both count dictionaries. Note that we dont need to clear lock dictionary.
            FileMeta.access_count_map.clear()
            FileMeta.write_count_map.clear()
Esempio n. 10
0
 def insert(self, query):
     cursor = self.connnection.cursor()
     db_logger.info("executing: " + query)
     cursor.execute(query)
     cursor.close()
     self.connnection.commit()