filedb.filestore 模块

基类

class whoosh.filedb.filestore.Storage

存储对象的抽象基类。

存储对象是一个虚拟的平面文件系统,允许创建和检索类似文件的对象。( StructFile 对象)。默认实现( FileStorage )使用目录中的实际文件。

所有对whoosh中文件的访问都会通过这个对象。This allows more different forms of storage (for example, in RAM, in a database, in a single file) to be used transparently.

例如,要创建 FileStorage 对象:

# Create a storage object
st = FileStorage("indexdir")
# Create the directory if it doesn't already exist
st.create()

这个 Storage.create() 方法使交换存储实现变得更加容易。这个 create() 方法处理存储对象的设置。例如, FileStorage.create() 创建目录。数据库实现可能会创建表。这是为了避免在应用程序中放入特定于实现的设置代码。

close()

关闭此存储对象打开的所有资源。对于某些存储实现,这将是一个禁止操作,但对于其他存储实现,需要释放锁和/或防止泄漏,因此在完成存储对象操作后最好调用它。

create()

创建任何必需的特定于实现的资源。例如,基于文件系统的实现可能创建目录,而数据库实现可能创建表。例如::

from whoosh.filedb.filestore import FileStorage
# Create a storage object
st = FileStorage("indexdir")
# Create any necessary resources
st.create()

此方法返回 self 所以你也可以说:

st = FileStorage("indexdir").create()

应编写存储实现,以便在同一存储上再次调用create()。

返回

Storage 实例。

create_file(name)

在此存储中创建具有给定名称的文件。

参数

name -- 新文件的名称。

返回

whoosh.filedb.structfile.StructFile 实例。

create_index(schema, indexname='MAIN', indexclass=None)

在此存储中创建新索引。

>>> from whoosh import fields
>>> from whoosh.filedb.filestore import FileStorage
>>> schema = fields.Schema(content=fields.TEXT)
>>> # Create the storage directory
>>> st = FileStorage.create("indexdir")
>>> # Create an index in the storage
>>> ix = st.create_index(schema)
参数
  • schema -- 这个 whoosh.fields.Schema 用于新索引的对象。

  • indexname -- 存储对象中索引的名称。可以使用此选项将多个索引存储在同一个存储中。

  • indexclass -- 可选自定义 Index 用于创建索引文件的子类。默认值为 whoosh.index.FileIndex . This method will call the create 用于创建索引的给定类的类方法。

返回

whoosh.index.Index 实例。

delete_file(name)

从此存储中删除给定的文件。

参数

name -- the name to delete.

destroy(*args, **kwargs)

删除与此存储对象相关的任何特定于实现的资源。例如,基于文件系统的实现可能删除目录,数据库实现可能删除表。

这些参数是特定于实现的。

file_exists(name)

如果给定文件存在于此存储中,则返回true。

参数

name -- 要检查的名称。

返回类型

bool

file_length(name)

返回此存储中给定文件的大小(字节)。

参数

name -- 要检查的名称。

返回类型

int

file_modified(name)

返回此存储中给定文件的上次修改时间(作为“ctime”unix时间戳)。

参数

name -- 要检查的名称。

返回

“CTIME”号码。

index_exists(indexname=None)

如果此存储中存在非空索引,则返回true。

参数

indexname -- 存储对象中索引的名称。可以使用此选项将多个索引存储在同一个存储中。

返回类型

bool

list()

返回此存储中的文件名列表。

返回

字符串列表

lock(name)

返回命名锁对象(实现 .acquire().release() 方法)。不同的存储实现可以使用具有不同保证的不同锁类型。例如,ramstorage对象使用python线程锁,而filestorage对象使用基于文件系统的锁,这些锁在不同的进程中是有效的。

参数

name -- 锁的名称。

返回

像锁一样的物体。

open_file(name, *args, **kwargs)

在此存储中打开具有给定名称的文件。

参数

name -- 新文件的名称。

返回

whoosh.filedb.structfile.StructFile 实例。

open_index(indexname='MAIN', schema=None, indexclass=None)

打开现有索引(创建时使用 create_index() )在这个仓库里。

>>> from whoosh.filedb.filestore import FileStorage
>>> st = FileStorage("indexdir")
>>> # Open an index in the storage
>>> ix = st.open_index()
参数
  • indexname -- 存储对象中索引的名称。可以使用此选项将多个索引存储在同一个存储中。

  • schema -- 如果你通过 whoosh.fields.Schema 对象使用此参数,它将重写与索引一起存储的架构。

  • indexclass -- 可选自定义 Index 用于打开索引文件的子类。默认值为 whoosh.index.FileIndex . 此方法将用此存储对象实例化类。

返回

whoosh.index.Index 实例。

optimize()

Optimizes the storage object. The meaning and cost of "optimizing" will vary by implementation. For example, a database implementation might run a garbage collection procedure on the underlying database.

rename_file(frm, to, safe=False)

重命名此存储中的文件。

参数
  • frm -- 文件的当前名称。

  • to -- 文件的新名称。

  • safe -- 如果为true,则在已存在具有新名称的文件时引发异常。

temp_storage(name=None)

为临时文件创建新的存储对象。你可以调用 Storage.destroy() 当你用完新的储藏室。

参数

name -- 新存储的名称。这可能是可选的,也可能是必需的,具体取决于存储实现。

返回类型

Storage

实现类

class whoosh.filedb.filestore.FileStorage(path, supports_mmap=True, readonly=False, debug=False)

将索引存储为磁盘目录中的文件的存储对象。

在版本3之前,如果目录不存在,初始值设定项将引发IOERROR。从版本3开始,对象不检查初始化时目录是否存在。此更改是为了支持使用 FileStorage.create() 方法。

参数
  • path -- 目录的路径。

  • supports_mmap -- 如果为真(默认值),则使用 mmap 打开内存映射文件的模块。可以使用打开存储对象 supports_mmap=False to force Whoosh to open files normally instead of with mmap .

  • readonly -- 如果 True 如果尝试创建或重命名文件,则对象将引发异常。

class whoosh.filedb.filestore.RamStorage

将索引保存在内存中的存储对象。

帮助程序函数

whoosh.filedb.filestore.copy_storage(sourcestore, deststore)

使用将文件从源存储对象复制到目标存储对象 shutil.copyfileobj .

whoosh.filedb.filestore.copy_to_ram(storage)

将给定的filestore对象复制到新的ramstorage对象中。

返回类型

RamStorage

例外情况

exception whoosh.filedb.filestore.ReadOnlyError