如何快速从 Klepto 文件存档中删除多个项目?
How to delete multiple items from Klepto file archive fast?
我正在使用 Klepto 存档来索引文件夹树中的文件规格。扫描树后,我想快速删除对已删除文件的引用。但是简单地从文件存档中逐一删除一个项目是极其缓慢的。
有没有办法将更改同步到存档,或一次删除多个密钥? ('sync'方法只出现添加新项)
@Mike Mckerns 对这个问题的有用回答只涉及删除单个项目:
使用 files.sync() 或 files.dump() 似乎只是从缓存中追加数据,而不是同步删除。有没有办法从缓存中删除密钥,然后一次性同步这些更改。单个删除太慢了。
这是一个工作示例:
from klepto.archives import *
import os
class PathIndex:
def __init__(self,folder):
self.folder_path=folder
self.files=file_archive(self.folder_path+'/.filespecs',cache=False)
self.files.load() #load memory cache
def list_directory(self):
self.filelist=[]
for folder, subdirs, filelist in os.walk(self.folder_path): #go through every subfolder in a folder
for filename in filelist: #now through every file in the folder/subfolder
self.filelist.append(os.path.join(folder, filename))
def scan(self):
self.list_directory()
for path in self.filelist:
self.update_record(path)
self.files.dump() #save to file archive
def rescan(self):
self.list_directory() #rescan original disk
deletedfiles=[]
#code to ck for modified files etc
#check for deleted files
for path in self.files:
try:
self.filelist.remove(path) #self.filelist - disk files - leaving list of new files
except ValueError:
deletedfiles.append(path)
#code to add new files, the files left in self.filelist
for path in deletedfiles:
self.delete_record(path)
#looking to here sync modified index from modifed to disk
def update_record(self,path):
self.files[path]={'size':os.path.getsize(path),'modified':os.path.getmtime(path)}
#add other specs - hash of contents etc.
def delete_record(self,path):
del(self.files[path]) #delete from the memory cache
#this next line slows it all down
del(self.files.archive[path]) #delete from the disk cache
#usage
_index=PathIndex('/path/to/root')
_index.scan()
#delete, modify some files
_index.rescan()
我明白了...您真的很关心从 file_archive
.
中一次删除一个条目的 速度
好的,我同意。当您想删除多个条目时,在 file_archive
上使用 __delitem__
或 pop
有点残酷。减速是由于 file_archive
必须为您删除的每个密钥加载和重写整个文件存档。 dir_archive
或许多其他档案不是这种情况……但 file_archive
是。所以应该补救...
更新:我添加了一种新方法,可以更快地删除指定的键...
>>> import klepto as kl
>>> ar = kl.archives.file_archive('foo.pkl')
>>> ar['a'] = 1
>>> ar['b'] = 2
>>> ar['c'] = 3
>>> ar['d'] = 4
>>> ar['e'] = 5
>>> ar.dump()
>>> ar.popkeys(list('abx'), None)
[1, 2, None]
>>> ar.sync(clear=True)
>>> ar
file_archive('foo.pkl', {'c': 3, 'e': 5, 'd': 4}, cached=True)
>>> ar.archive
file_archive('foo.pkl', {'c': 3, 'e': 5, 'd': 4}, cached=False)
以前(即在已发布的版本中),您可以廉价地 pop
从本地缓存中获取所需的密钥,然后执行 ar.sync(clear=True)
以删除存档中的关联密钥。但是,这样做假设您拥有所有要保存在内存中的密钥。因此,不是将所有密钥加载到内存中,而是现在(至少在即将发布的版本中)在缓存 and/or 存档中执行 popkeys
以删除任何不需要的密钥来自两者之一。
我正在使用 Klepto 存档来索引文件夹树中的文件规格。扫描树后,我想快速删除对已删除文件的引用。但是简单地从文件存档中逐一删除一个项目是极其缓慢的。 有没有办法将更改同步到存档,或一次删除多个密钥? ('sync'方法只出现添加新项)
@Mike Mckerns 对这个问题的有用回答只涉及删除单个项目:
使用 files.sync() 或 files.dump() 似乎只是从缓存中追加数据,而不是同步删除。有没有办法从缓存中删除密钥,然后一次性同步这些更改。单个删除太慢了。
这是一个工作示例:
from klepto.archives import *
import os
class PathIndex:
def __init__(self,folder):
self.folder_path=folder
self.files=file_archive(self.folder_path+'/.filespecs',cache=False)
self.files.load() #load memory cache
def list_directory(self):
self.filelist=[]
for folder, subdirs, filelist in os.walk(self.folder_path): #go through every subfolder in a folder
for filename in filelist: #now through every file in the folder/subfolder
self.filelist.append(os.path.join(folder, filename))
def scan(self):
self.list_directory()
for path in self.filelist:
self.update_record(path)
self.files.dump() #save to file archive
def rescan(self):
self.list_directory() #rescan original disk
deletedfiles=[]
#code to ck for modified files etc
#check for deleted files
for path in self.files:
try:
self.filelist.remove(path) #self.filelist - disk files - leaving list of new files
except ValueError:
deletedfiles.append(path)
#code to add new files, the files left in self.filelist
for path in deletedfiles:
self.delete_record(path)
#looking to here sync modified index from modifed to disk
def update_record(self,path):
self.files[path]={'size':os.path.getsize(path),'modified':os.path.getmtime(path)}
#add other specs - hash of contents etc.
def delete_record(self,path):
del(self.files[path]) #delete from the memory cache
#this next line slows it all down
del(self.files.archive[path]) #delete from the disk cache
#usage
_index=PathIndex('/path/to/root')
_index.scan()
#delete, modify some files
_index.rescan()
我明白了...您真的很关心从 file_archive
.
好的,我同意。当您想删除多个条目时,在 file_archive
上使用 __delitem__
或 pop
有点残酷。减速是由于 file_archive
必须为您删除的每个密钥加载和重写整个文件存档。 dir_archive
或许多其他档案不是这种情况……但 file_archive
是。所以应该补救...
更新:我添加了一种新方法,可以更快地删除指定的键...
>>> import klepto as kl
>>> ar = kl.archives.file_archive('foo.pkl')
>>> ar['a'] = 1
>>> ar['b'] = 2
>>> ar['c'] = 3
>>> ar['d'] = 4
>>> ar['e'] = 5
>>> ar.dump()
>>> ar.popkeys(list('abx'), None)
[1, 2, None]
>>> ar.sync(clear=True)
>>> ar
file_archive('foo.pkl', {'c': 3, 'e': 5, 'd': 4}, cached=True)
>>> ar.archive
file_archive('foo.pkl', {'c': 3, 'e': 5, 'd': 4}, cached=False)
以前(即在已发布的版本中),您可以廉价地 pop
从本地缓存中获取所需的密钥,然后执行 ar.sync(clear=True)
以删除存档中的关联密钥。但是,这样做假设您拥有所有要保存在内存中的密钥。因此,不是将所有密钥加载到内存中,而是现在(至少在即将发布的版本中)在缓存 and/or 存档中执行 popkeys
以删除任何不需要的密钥来自两者之一。