从 python 检查 aerospike 集是否为空的最简单方法是什么?

What is the easiest way to check if aerospike set is empty from python?

我在 Python client 中没有看到检查给定集合是否为空的方法。

作为命令行中的文档says,我可以使用:

asinfo -h <host> -v 'sets/<namespace>/<set>'

但它仅适用于单个主机。

另一种方法是查看输出:

asadm -h <host> -e 'info set'

你会如何在 Python 中实现这样的方法?

在客户端使用"info"API:https://www.aerospike.com/apidocs/python/client.html#aerospike.Client.info_all

client.info_all("sets/<namespace>/<set>")

然后总结您感兴趣的统计数据。

这是我的 python 实现:

def _info_result_prop_value(info_result, prop_name, default):
    result_split = info_result.split(':')
    return next((s.strip().split('=', 2)[1] for s in result_split
                 if s.strip().startswith(f'{prop_name}=')), default)


def count_objects(client, ns, set_name):
    count = 0
    for info_result in client.info_all(f"sets/{ns}/{set_name}").values():
        count += int(_info_result_prop_value(info_result[1], 'objects', '0'))
    return count


def is_empty_set(client, ns, set_name):
    return count_objects(client, ns, set_name) == 0