气钉 - 检查/增加列表中的 NIL 值?



如果我运行此代码,它会递增索引 7 中存在的任何值,如果 7 之前的任何索引不存在,它只需将 NIL 作为其值

法典

Operation operation = ListOperation.increment( "incre", 7 );
client.operate( policy, key, operation );

AQL

aql> select * from test.users
+----+----------------------------------------+
| PK | incre                                  |
+----+----------------------------------------+
| 2  | LIST('[1, 1, 1, 2, 1, 1, NIL, 1]') |
+----+----------------------------------------+
1 row in set (0.095 secs)

如您所见,索引 6 不存在,因此 aerospike 自动将 NIL 放在其位置。 如果我尝试增加索引 6,则会出现此错误。

Exception in thread "main" com.aerospike.client.AerospikeException: Error 4,1,30000,0,0,BB955892AFD8CA0 127.0.0.1 3000: Parameter error

我的问题:-

1(是否可以为不存在的索引放置任何类型的默认值而不是NIL?如果无法检查 NIL,是否可以增加 NIL?

2( 有没有办法在增加之前检查零值?

增量操作假定数据类型为整数。因此,当您尝试在位置 7 处递增时,那里还没有元素,它从整数 0 开始并递增到 1。但是,正如您所注意到的,第 6 位由 NIL 填充,因为列表需要连续。现在的问题是 NIL 不是一个整数。因此,您不能递增它。

要回答您的具体问题:

  1. 不可以,无法填充特定的默认值。
  2. 您可以在某个位置读取元素,您可以使用 Value 类的 getType(( 来了解该元素是否为 null 类型。

通过使用LIST_WRITE_INSERT_BOUNDED标志作为列表操作策略的一部分,可以避免更新插入此类列表。这种操作将引发特定的错误代码 26。例如(Python(:

from __future__ import print_function
import aerospike
import sys
from aerospike_helpers.operations import list_operations as lh
config = {"hosts": [("127.0.0.1", 3000)]}
try:
client = aerospike.client(config).connect()
except e.ClientError:
print("Error: {0} [{1}]".format(e.msg, e.code))
sys.exit(1)
key = ("test", "example", "here")
try:
client.remove(key)
except:
pass
try:
ops = [
# increment the seventh element of a non-existent record with boundary
# restriction
lh.list_increment(
"incre", 7, 2, {"write_flags": aerospike.LIST_WRITE_INSERT_BOUNDED}
)
]
k, m, b = client.operate(key, ops)
except Exception as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
print("Could not increment outside the boundary, in this case no recordn")
# try again, without limit on inserting into the bounds of the list
try:
ops = [
# increment the seventh element of a non-existent record
lh.list_increment(
"incre", 7, 2, {}
)
]
k, m, b = client.operate(key, ops)
(key, meta, bins) = client.get(key)
print(bins)
ops = [
# increment the sixth element of the newly created record
lh.list_increment(
"incre", 6, 1, {}
)
]
k, m, b = client.operate(key, ops)
except Exception as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
print("Can't increment a None (NIL) valuen")
client.close()

哪些输出

Error: 127.0.0.1:3000 AEROSPIKE_ERR_OP_NOT_APPLICABLE [26]
Could not increment outside the boundary, in this case no record
{'incre': [None, None, None, None, None, None, None, 2]}
Error: 127.0.0.1:3000 AEROSPIKE_ERR_REQUEST_INVALID [4]
Can't increment a None (NIL) value

在 Java 客户端中,这是ListPolicyListWriteFlag.INSERT_BOUNDED标志。

但实际上,如果你有一个元组,其中第 0-6 个位置具有特定的含义,你应该用[0, 0, 0, 0, 0, 0, 0]初始化 bin .

最新更新