Faster implementation

This commit is contained in:
ville rantanen
2013-04-10 13:53:41 +03:00
parent bff8991803
commit b43254bdc8

View File

@@ -57,7 +57,6 @@ def createdb(fname):
def delete_nonexisting(sqlfile,options): def delete_nonexisting(sqlfile,options):
conn=sqlite3.connect(sqlfile) conn=sqlite3.connect(sqlfile)
conn.text_factory=str conn.text_factory=str
#conn.row_factory=sqlite3.Row
db=conn.cursor() db=conn.cursor()
dbdel=conn.cursor() dbdel=conn.cursor()
db.execute('SELECT file FROM list') db.execute('SELECT file FROM list')
@@ -81,13 +80,15 @@ def add_recurse(options):
db=conn.cursor() db=conn.cursor()
for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks): for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks):
dirs=clean_dirs(dirs) dirs=clean_dirs(dirs)
db_files=get_folder_contents(db,os.path.abspath(path)+'/')
if not options.symlinks: if not options.symlinks:
files=clean_syms(files,path) files=clean_syms(files,path)
for file in files: for file in files:
filename=os.path.abspath(os.path.join(path,file)) filename=os.path.abspath(os.path.join(path,file))
if file==options.sqlfile: if file==options.sqlfile:
continue continue
if not is_listed(db,filename): #if not is_listed(db,filename):
if file not in db_files:
if options.add: if options.add:
add_single(conn,filename,change=False) add_single(conn,filename,change=False)
else: else:
@@ -96,7 +97,7 @@ def add_recurse(options):
if not ftime_match(db,filename,ftime): if not ftime_match(db,filename,ftime):
#file content changed #file content changed
add_single(conn,filename,change=True) add_single(conn,filename,change=True)
conn.commit()
return return
@@ -116,7 +117,6 @@ def add_single(conn,filename,change=False,hash=None,minsize=0):
else: else:
db.execute("INSERT INTO list(file,date,hash,size,mime)\ db.execute("INSERT INTO list(file,date,hash,size,mime)\
VALUES(?,?,?,?,?)",(filename,ftime,hash,fsize,mime)) VALUES(?,?,?,?,?)",(filename,ftime,hash,fsize,mime))
conn.commit()
return return
def is_listed(db,filename): def is_listed(db,filename):
@@ -124,6 +124,16 @@ def is_listed(db,filename):
count=db.fetchall() count=db.fetchall()
return count[0][0]>0 return count[0][0]>0
def get_folder_contents(db,path):
''' return the contents of the folder '''
files=[]
db.execute("SELECT file FROM list where file LIKE ?",(path+'%',))
for row in db:
base=row[0].replace(path,'',1)
if base.find('/')==-1:
files.append(base)
return files
def ftime_match(db,filename,ftime): def ftime_match(db,filename,ftime):
db.execute("SELECT date FROM list where file == ?",(filename,)) db.execute("SELECT date FROM list where file == ?",(filename,))
count=db.fetchall() count=db.fetchall()