Account DB 和 Container DB

DB

Swift 的数据库代码

swift.common.db.BROKER_TIMEOUT = 25

尝试连接到数据库的超时时间

swift.common.db.DB_PREALLOCATION = False

是否将调用以预分配数据库文件的磁盘空间。

exception swift.common.db.DatabaseAlreadyExists(path)

基类: DatabaseError

更友好的数据库错误消息。

class swift.common.db.DatabaseBroker(db_file, timeout=25, logger=None, account=None, container=None, pending_timeout=None, stale_reads_ok=False, skip_commits=False)

基类: object

封装了与数据库的交互。

property db_file
delete_db(timestamp)

标记数据库为已删除

参数:

timestamp – 内部删除时间戳

delete_meta_whitelist = []
empty()

检查 broker 抽象是否包含任何未删除的记录。

get()

与“with”语句一起使用;返回数据库连接。

get_device_path()
get_info()
get_items_since(start, count)

获取数据库中 start 和 end 之间的对象列表。

参数:
  • start – start ROWID

  • count – 获取的数量

返回值:

start 和 end 之间的对象列表

get_max_row(table=None)
get_raw_metadata()
get_replication_info()

获取复制所需的数据库信息。

返回值:

包含来自 get_info 的键的字典,以及 max_row 和 metadata

注意:get_info 的 <db_contains_type>_count 被转换为

“count”,metadata 是原始字符串。

get_sync(id, incoming=True)

从 sync 表中获取服务器的最新同步点。

参数:
  • id – 要获取同步点的远程 ID

  • incoming – 如果为 True,则获取上次传入的同步,否则获取上次传出的同步

返回值:

同步点,或者如果 ID 不存在则为 -1。

get_syncs(incoming=True, include_timestamp=False)

获取 sync 表的序列化副本。

参数:
  • incoming – 如果为 True,则获取上次传入的同步,否则获取上次传出的同步

  • include_timestamp – 如果为 True,则包含 updated_at 时间戳

返回值:

包含 {‘remote_id’, ‘sync_point’} 或 {‘remote_id’, ‘sync_point’, ‘updated_at’}(如果 include_timestamp 为 True)的列表。

initialize(put_timestamp=None, storage_policy_index=None)

创建数据库

storage_policy_index 将传递给子类的 _initialize 方法。它被 AccountBroker 忽略。

参数:
  • put_timestamp – 初始 PUT 请求的内部时间戳

  • storage_policy_index – 仅适用于容器

is_deleted()

检查数据库是否被视为已删除。

返回值:

如果数据库被视为已删除,则为 True,否则为 False

is_reclaimable(now, reclaim_age)

检查经纪人抽象是否为空,并且是否已标记为已删除至少 reclaim_age 时间。

lock()

与“with”语句一起使用;锁定数据库。

make_tuple_for_pickle(record)

将此数据库记录字典转换为此服务用于待定 pickles 的格式。

maybe_get(conn)
merge_items(item_list, source=None)

将 :param:item_list 保存到数据库。

merge_syncs(sync_points, incoming=True)

将同步点列表与传入的 sync 表合并。

参数:
  • sync_points – 同步点列表,其中同步点是 {‘sync_point’, ‘remote_id’} 的字典

  • incoming – 如果为 True,则获取上次传入的同步,否则获取上次传出的同步

merge_timestamps(created_at, put_timestamp, delete_timestamp)

用于复制以处理更新时间戳。

参数:
  • created_at – 创建时间戳

  • put_timestamp – 放入时间戳

  • delete_timestamp – 删除时间戳

property metadata

返回数据库的元数据字典。元数据字典值是 (值,时间戳) 的元组,其中时间戳表示将该键设置为该值的时刻。

newid(remote_id)

重新标识数据库。这应在 rsync 之后调用。

参数:

remote_id – 要 rsync 的远程数据库的 ID

possibly_quarantine(err)

检查异常信息,查看它是否指示隔离情况(数据库格式错误或损坏)。如果不是,则会重新引发原始异常。如果是,则数据库将被隔离,并引发一个新的 sqlite3.DatabaseError,指示所采取的操作。

put_record(record)

将记录放入 DB。如果 DB 具有关联的 pending 文件和空间,则该记录将附加到该文件,并且对 DB 的提交将被推迟。如果其 pending 文件已满,则该记录将立即提交。

参数:

record – 要添加到 DB 的记录。

引发:
quarantine(reason)

数据库将被隔离,并引发一个新的 sqlite3.DatabaseError,指示所采取的操作。

reclaim(age_timestamp, sync_timestamp)

从 db 中删除可回收的行和元数据。

默认情况下,此方法将删除 db_contains_type 表中标记为已删除且 created_at 时间戳小于 age_timestamp 的行,以及 updated_at 时间戳小于 sync_timestamp 的 incoming_sync 和 outgoing_sync 中的行。此外,它还会调用 _reclaim_metadata() 方法。

子类可以通过重写 _reclaim() 来回收其他项目。

参数:
  • age_timestamp – 对象行要删除的最大 created_at 时间戳

  • sync_timestamp – 要删除的 sync 行的最大 update_at 时间戳

update_metadata(metadata_updates, validate_metadata=False)

更新数据库的元数据字典。元数据字典值是 (值,时间戳) 的元组,其中时间戳表示将该键设置为该值的时刻。只有当时间戳较新时,才会覆盖键/值。要删除键,请将其值设置为(‘’,时间戳)。这些空键最终将被 reclaim() 删除

update_put_timestamp(timestamp)

更新 put_timestamp。仅当它大于当前时间戳时才进行修改。

参数:

timestamp – 内部 put 时间戳

update_status_changed_at(timestamp)

更新 stat 表中的 status_changed_at 字段。仅当时间戳大于当前的 status_changed_at 时间戳时才修改 status_changed_at。

参数:

timestamp – 内部时间戳

updated_timeout(new_timeout)

与“with”语句一起使用;更新块内的 timeout

static validate_metadata(metadata)

验证元数据是否在可接受的限制范围内。

参数:

metadata – 要验证的元数据

引发:

HTTPBadRequest – 如果超过 MAX_META_COUNT 或 MAX_META_OVERALL_SIZE,或者如果元数据包含非 UTF-8 数据

异常 swift.common.db.DatabaseConnectionError(path, msg, timeout=0)

基类: DatabaseError

更友好的数据库错误消息。

swift.common.db.GreenDBConnection(database, timeout=None, *args, **kwargs)

基类: Connection

与 eventlet 配合良好的 SQLite DB 连接处理程序。

commit()

提交任何待处理的事务到数据库。

如果没有打开的事务,此方法将不执行任何操作。

cursor(cls=None)

返回连接的游标。

db_file
execute(*args, **kwargs)

执行 SQL 语句。

timeout
swift.common.db.GreenDBCursor(*args, **kwargs)

基类: Cursor

与 eventlet 配合良好的 SQLite 游标处理程序。

db_file
execute(*args, **kwargs)

执行 SQL 语句。

timeout
swift.common.db.PICKLE_PROTOCOL = 2

要使用的 pickle 协议

swift.common.db.QUERY_LOGGING = False

是否将进行调用以记录查询 (仅限 py3)

swift.common.db.TombstoneReclaimer(broker, age_timestamp)

基类: object

封装数据库中已删除行的回收。

get_tombstone_count()

返回比 age_timestamp 更新的剩余墓碑数量。如果尚未在此实例上调用 reclaim 方法,则执行该方法。

返回值:

broker 中比 age_timestamp 更新的墓碑数量。

reclaim()

执行删除比 age_timestamp 更新的行的回收。

swift.common.db.chexor(old, name, timestamp)

帐户和容器数据库中的每个条目在插入或删除时都通过 128 位哈希进行 XOR。这充当滚动、与顺序无关的内容哈希。(检查 + XOR)

参数:
  • old – 当前 DB 哈希的十六进制表示

  • name – 正在插入的对象的名称

  • timestamp – 新记录的内部时间戳

返回值:

新的哈希值的十六进制表示

swift.common.db.dict_factory(crs, row)

只有在需要实际字典时才应使用此方法,例如,当要序列化结果时。

swift.common.db.get_db_connection(path, timeout=30, logger=None, okay_to_create=False)

返回配置良好的 SQLite 数据库连接。

参数:
  • path – DB 的路径

  • timeout – 连接超时时间

  • okay_to_create – 如果为 True,则在数据库不存在时创建数据库

返回值:

DB 连接对象

swift.common.db.native_str_keys_and_values(metadata)
swift.common.db.zero_like(count)

我们已经对消费者进行了文化模仿,使其能够容忍数据库中各种零的表示形式,以与纪律性较差的生产者保持向后兼容。

DB 复制器

swift.common.db_replicator.ReplConnection(node, partition, hash_, logger)

基类: BufferedHTTPConnection

辅助类,用于简化到远程服务器的 REPLICATE 操作。

replicate(*args)

发出 HTTP REPLICATE 请求

参数:

args – json 可编码对象的列表

返回值:

bufferedhttp 响应对象

swift.common.db_replicator.Replicator(conf, logger=None)

基类: Daemon

实现数据库复制逻辑。

cleanup_post_replicate(broker, orig_info, responses)

如果需要,清理非主数据库中的磁盘。

参数:
  • broker – 我们正在复制的数据库的代理

  • orig_info – 在复制之前拍摄的代理复制信息字典的快照

  • responses – 每个复制请求到其他节点的布尔成功值列表

返回成功:

如果尝试删除数据库但未成功,则返回 False,否则返回 True。

delete_db(broker)
extract_device(object_file)

从对象路径中提取设备名称。如果由于某种原因无法成功提取路径,则返回“UNKNOWN”。

参数:

object_file – 数据库文件的路径。

report_up_to_date(full_info)
roundrobin_datadirs(dirs)
run_forever(*args, **kwargs)

在无限循环中复制给定根目录下的数据库。

run_once(*args, **kwargs)

运行一次复制过程。

swift.common.db_replicator.ReplicatorRpc(root, datadir, broker_class, mount_check=True, logger=None)

基类: object

处理复制 RPC 调用。TODO(redbo): 请记录 :)

complete_rsync(drive, db_file, args)
debug_timing(name)
dispatch(replicate_args, args)
merge_items(broker, args)
merge_syncs(broker, args)
rsync_then_merge(drive, db_file, args)
sync(broker, args)
swift.common.db_replicator.looks_like_partition(dir_name)

如果目录名是有效的分区编号,则返回 True,否则返回 False。

swift.common.db_replicator.quarantine_db(object_file, server_type)

如果发现损坏的文件,将其移动到隔离区,以便复制可以修复它。

参数:
  • object_file – 损坏文件的路径

  • server_type – 损坏文件的类型('container' 或 'account')

swift.common.db_replicator.roundrobin_datadirs(datadirs)

生成器,以轮询方式遍历数据目录,均匀地命中系统上的每个设备,并产生找到的任何 .db 文件(在正确的位置)。 但是,每个数据目录中的分区是随机遍历的。

参数:

datadirs – 要遍历的元组列表,格式为 (path, context, partition_filter)。 context 可以是任何对象;此函数不使用 context,但将其包含在每个产生的元组中。

返回值:

一个 (partition, path_to_db_file, context) 生成器