Revamp renamekey function
The function had numerous bugs and didn't work. Fix them up.
This commit is contained in:
parent
4937c01075
commit
837666a15e
|
@ -83,15 +83,16 @@ def writedata(zk_conn, kv):
|
||||||
|
|
||||||
# Key rename function
|
# Key rename function
|
||||||
def renamekey(zk_conn, kv):
|
def renamekey(zk_conn, kv):
|
||||||
# Start up a transaction
|
# This one is not transactional because, inexplicably, transactions don't
|
||||||
zk_transaction = zk_conn.transaction()
|
# support either the recursive delete or recursive create operations that
|
||||||
|
# we need. Why? No explanation in the docs that I can find.
|
||||||
|
|
||||||
# Proceed one KV pair at a time
|
# Proceed one KV pair at a time
|
||||||
for key in sorted(kv):
|
for key in sorted(kv):
|
||||||
old_name = key
|
old_name = key
|
||||||
new_name = kv[key]
|
new_name = kv[key]
|
||||||
|
|
||||||
old_data = zk_conn.get(old_name)
|
old_data = zk_conn.get(old_name)[0]
|
||||||
|
|
||||||
# Find the children of old_name recursively
|
# Find the children of old_name recursively
|
||||||
child_keys = list()
|
child_keys = list()
|
||||||
|
@ -99,34 +100,26 @@ def renamekey(zk_conn, kv):
|
||||||
children = zk_conn.get_children(key)
|
children = zk_conn.get_children(key)
|
||||||
if not children:
|
if not children:
|
||||||
child_keys.append(key)
|
child_keys.append(key)
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
for ckey in children:
|
for ckey in children:
|
||||||
get_children(key)
|
get_children('{}/{}'.format(key, ckey))
|
||||||
get_children(old_name)
|
get_children(old_name)
|
||||||
|
|
||||||
# Get the data out of each of the child keys
|
# Get the data out of each of the child keys
|
||||||
child_data = dict()
|
child_data = dict()
|
||||||
for ckey in child_keys:
|
for ckey in child_keys:
|
||||||
child_data[ckey] = zk_conn.get(ckey)
|
child_data[ckey] = zk_conn.get(ckey)[0]
|
||||||
|
|
||||||
# Create the new parent key
|
# Create the new parent key
|
||||||
zk_transaction.create(new_name, old_data)
|
zk_conn.create(new_name, old_data, makepath=True)
|
||||||
|
|
||||||
# For each child key, create the key and add the data
|
# For each child key, create the key and add the data
|
||||||
for ckey in child_keys:
|
for ckey in child_keys:
|
||||||
new_ckey_name = ckey.replace(old_name, new_name)
|
new_ckey_name = ckey.replace(old_name, new_name)
|
||||||
zk_transaction.create(new_ckey_name, child_data[ckey])
|
zk_conn.create(new_ckey_name, child_data[ckey], makepath=True)
|
||||||
|
|
||||||
# Remove recursively the old key
|
# Remove recursively the old key
|
||||||
zk_transaction.delete(old_name, recursive=True)
|
zk_conn.delete(old_name, recursive=True)
|
||||||
|
|
||||||
# Commit the transaction
|
|
||||||
try:
|
|
||||||
zk_transaction.commit()
|
|
||||||
return True
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Write lock function
|
# Write lock function
|
||||||
def writelock(zk_conn, key):
|
def writelock(zk_conn, key):
|
||||||
|
|
Loading…
Reference in New Issue