Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
[repo] Dead database record removal.
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangyu Bu <xybu92@live.com>
  • Loading branch information
xybu committed Jan 13, 2017
1 parent 2127ba2 commit a8946b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion onedrived/od_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ItemRecordType:

class ItemRecordStatus:
OK = 0
MOVING_FROM = 1
MARKED = 255


class OneDriveLocalRepository:
Expand Down Expand Up @@ -118,6 +118,12 @@ def delete_item(self, item_name, parent_relpath, is_folder=False):
self._cursor.execute('DELETE FROM items WHERE parent_path=? AND name=?', (parent_relpath, item_name))
self._conn.commit()

def update_status(self, item_name, parent_relpath, status=ItemRecordStatus.OK):
with self._lock:
self._cursor.execute('UPDATE items SET status=? WHERE parent_path=? AND name=?',
(status, parent_relpath, item_name))
self._conn.commit()

def update_item(self, item, parent_relpath, size_local=0, status=ItemRecordStatus.OK):
"""
:param onedrivesdk.model.item.Item item:
Expand Down Expand Up @@ -149,3 +155,14 @@ def update_item(self, item, parent_relpath, size_local=0, status=ItemRecordStatu
item.size, size_local, created_time_str, modified_time_str, status, sha1_hash,
str(datetime.utcnow().isoformat()) + 'Z'))
self._conn.commit()

def mark_all_items(self):
with self._lock:
self._cursor.execute('UPDATE items SET status=?', (ItemRecordStatus.MARKED, ))
self._conn.commit()

def sweep_marked_items(self):
with self._lock:
self._cursor.execute('DELETE FROM items WHERE status=?', (ItemRecordStatus.MARKED, ))
self._conn.commit()
logging.info('Deleted %d dead records from database.', self._cursor.rowcount)
4 changes: 4 additions & 0 deletions onedrived/tasks/start_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def __repr__(self):
def handle(self):
try:
if os.path.isdir(self.repo.local_root):
# Clean up dead records in the database.
self.repo.sweep_marked_items()
self.repo.mark_all_items()
# And add a recursive merge task to task queue.
item_request = self.repo.authenticator.client.item(drive=self.repo.drive.id, path='/')
self.task_pool.add_task(_merge_dir.MergeDirectoryTask(self.repo, self.task_pool, '', item_request))
else:
Expand Down

0 comments on commit a8946b9

Please sign in to comment.