[#5] Use better dict-based format for write updates
This commit is contained in:
@ -35,22 +35,24 @@ def readdata(zk_conn, key):
|
||||
return data
|
||||
|
||||
# Data write function
|
||||
def writedata(zk_conn, key, data):
|
||||
# Get the current version
|
||||
orig_data_raw = zk_conn.get(key)
|
||||
def writedata(zk_conn, kv):
|
||||
# Get the current version; we base this off the first key (ordering in multi-key calls is irrelevant)
|
||||
first_key = list(kv.keys())[0]
|
||||
orig_data_raw = zk_conn.get(first_key)
|
||||
meta = orig_data_raw[1]
|
||||
if meta == None:
|
||||
ansiiprint.echo('Zookeeper key "{}" does not exist'.format(key), '', 'e')
|
||||
ansiiprint.echo('Zookeeper key "{}" does not exist'.format(first_key), '', 'e')
|
||||
return 1
|
||||
|
||||
version = meta.version
|
||||
new_version = version + 1
|
||||
zk_transaction = zk_conn.transaction()
|
||||
zk_transaction.set_data(key, data.encode('ascii'))
|
||||
for key, data in kv:
|
||||
zk_transaction.set_data(key, data.encode('ascii'))
|
||||
try:
|
||||
zk_transaction.check(key, new_version)
|
||||
zk_transaction.check(first_key, new_version)
|
||||
except TypeError:
|
||||
ansiiprint.echo('Zookeeper key "{}" does not match expected version'.format(key), '', 'e')
|
||||
ansiiprint.echo('Zookeeper key "{}" does not match expected version'.format(first_key), '', 'e')
|
||||
return 1
|
||||
zk_transaction.commit()
|
||||
return 0
|
||||
|
Reference in New Issue
Block a user