9 def __init__(self, base_dir, exclude_dir, db_file):
10 self._base_dir = base_dir
11 self._exclude_dir = exclude_dir
12 if not os.path.isfile(db_file):
13 self._conn = sqlite3.connect(db_file, check_same_thread=False)
16 self._conn = sqlite3.connect(db_file, check_same_thread=False)
17 logging.info("Opened locator for %s.", self._base_dir)
19 def locate_file(self, name, size, meta_time):
20 c = self._conn.cursor()
21 c.execute("SELECT file_dir FROM cache WHERE name=? AND size=? AND meta_time=?",
22 (name, size, meta_time))
27 file_dir = misc.find_file(self._base_dir, name, size, meta_time,
31 self._add_to_cache(name, size, meta_time, file_dir)
35 logging.info("Closing locator for %s...", self._base_dir)
38 def _update_cache_access_time(self, name, size, meta_time):
39 c = self._conn.cursor()
40 c.execute("UPDATE cache SET access_time=? WHERE name=? AND size=? AND meta_time=?",
41 (int(time.time()), name, size, meta_time))
44 def _remove_from_cache(self, name, size, meta_time):
45 c = self._conn.cursor()
46 c.execute("DELETE FROM cache WHERE name=? AND size=? AND meta_time=?",
47 (name, size, meta_time))
50 def _add_to_cache(self, name, size, meta_time, file_dir):
51 c = self._conn.cursor()
52 c.execute('''INSERT INTO cache (name, size, meta_time, file_dir, access_time)
53 VALUES (?, ?, ?, ?, ?)''',
54 (name, size, meta_time, file_dir, int(time.time())))
57 def _clean_cache(self):
58 c = self._conn.cursor()
59 c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800,))
63 logging.info("Creating locator database for %s...", self._base_dir)
64 c = self._conn.cursor()
65 c.execute('''CREATE TABLE cache(
66 name TEXT, size INTEGER, meta_time INTEGER, file_dir TEXT,
68 PRIMARY KEY (name, size, meta_time))''')