Commits

Evgeny Podjachev committed b036310

Intermediate commit.

  • Participants
  • Parent commits 559545d

Comments (0)

Files changed (1)

File tests/benchmark.py

 import time
 
+import os
 from os import walk, getcwdu
-from os.path import join
+from os.path import join, getctime, getmtime, exists
 from fnmatch import fnmatch
 import operator
 
+import sqlite3
+
 if 1:
 	import string
 	caseMod = string.lower
 else:
 	caseMod = lambda x: x
 
-def scan_dir(path, ignoreList):
+class PathsCache(object):
+	def __init__(self, pathToCache, caseSensitivePaths):
+		self.caseSensitivePaths = caseSensitivePaths
+
+		# Check if database file exists and creates all needed paths.
+		createTable = False
+		if not exists(pathToCache):
+			createTable = True
+			try:
+				os.makedirs(os.path.split(pathToCache)[0])
+			except OSError:
+				pass
+
+		self.conn = None
+		with sqlite3.connect(pathToCache) as conn:
+			self.conn = conn
+			if createTable:
+				c = conn.cursor()
+				c.executescript("""
+					CREATE TABLE roots (id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT);
+					CREATE TABLE paths (id INTEGER PRIMARY KEY AUTOINCREMENT, root_id INTEGER SECONDARY KEY, parent INTEGER SECONDARY KEY, path TEXT, modtime INTEGER);
+				""")
+				conn.commit()
+				c.close()
+
+	def getCachedPaths(self, path):
+		if self.conn == None:
+			return []
+
+		if self.caseSensitivePaths:
+			addon = ''
+		else:
+			addon = 'COLLATE NOCASE'
+
+		c = self.conn.cursor()
+
+		# get parent id
+		c.execute('SELECT id FROM paths WHERE path = "%s" %s' % (path, addon))
+		c.close()
+
+		return []
+
+	def updateCachedPaths(self, root, paths):
+		if self.conn == None:
+			return
+
+		if self.caseSensitivePaths:
+			addon = ''
+		else:
+			addon = 'COLLATE NOCASE'
+
+		c = self.conn.cursor()
+
+		# get root id
+		c.execute('SELECT id FROM paths WHERE path = "%s" %s' % (root, addon))
+
+
+		c.execute("INSERT OR REPLACE INTO paths VALUES
+
+		self.conn.commit()
+		c.close()
+
+def scan_dir(path, ignoreList, cache_provider):
 	ignoreList = map(caseMod, ignoreList)
 	def in_ignore_list(f):
 		for i in ignoreList:
 	ignore_list = ['.*', '*.bak', '~*', '*.obj', '*.pdb', '*.res', '*.dll', '*.idb', '*.exe', '*.lib', '*.so']
 	symbols = caseMod('root')
 
-	timing(scan_dir, 2, {'path' : path, 'ignoreList' : ignore_list})
+#	timing(scan_dir, 2, {'path' : path, 'ignoreList' : ignore_list})
 	
 	file_list = scan_dir(path, ignore_list)
 
 
 	#timing(filterFileList, 5, {'fileList':file_list})
 
+	init_cache('K:/home_projects/fastfileselector/tests/db.db')