条件删除
Compare-and-Delete(CAD) 就是判断删除
Note: CompareAndDelete 不能用于目录,如果测试用于目录,会获得 102 “Not a file” 的错误
Item | Comment |
---|---|
prevValue | 比较当前的值 |
prevIndex | 比较当前的modifiedIndex |
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
{"action":"set","node":{"key":"/foo","value":"one","modifiedIndex":94,"createdIndex":94},"prevNode":{"key":"/foo","value":"three","modifiedIndex":93,"createdIndex":89}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=two -XDELETE
{"errorCode":101,"message":"Compare failed","cause":"[two != one]","index":94}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":94,"createdIndex":94}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo?prevIndex=3 -XDELETE
{"errorCode":101,"message":"Compare failed","cause":"[3 != 94]","index":94}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XDELETE
{"action":"compareAndDelete","node":{"key":"/foo","modifiedIndex":95,"createdIndex":94},"prevNode":{"key":"/foo","value":"one","modifiedIndex":94,"createdIndex":94}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo
{"errorCode":100,"message":"Key not found","cause":"/foo","index":95}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
{"action":"set","node":{"key":"/foo","value":"one","modifiedIndex":96,"createdIndex":96}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo?prevIndex=96 -XDELETE
{"action":"compareAndDelete","node":{"key":"/foo","modifiedIndex":97,"createdIndex":96},"prevNode":{"key":"/foo","value":"one","modifiedIndex":96,"createdIndex":96}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/foo
{"errorCode":100,"message":"Key not found","cause":"/foo","index":97}
[root@docker ~]#
创建目录
代码语言:javascript复制[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/testdir -XPUT -d dir=true
{"action":"set","node":{"key":"/testdir","dir":true,"modifiedIndex":98,"createdIndex":98}}
[root@docker ~]#