Skip to content
Permalink
Browse files

Lock mutations of each type to the data and the log to prevent simult…

…aneous commands on different threads from corrupting the data
  • Loading branch information
chelseatroy committed Dec 16, 2019
1 parent 857856d commit d36399b9583563b5ea6c0993f5760253af4ebea9
Showing with 18 additions and 13 deletions.
  1. +18 −13 key_value_operations.py
@@ -1,4 +1,8 @@
import threading

class KeyValueStore:
client_lock = threading.Lock()

def __init__(self):
self.data = {}

@@ -17,19 +21,20 @@ def execute(self, string_operation):

response = "Sorry, I don't understand that command."

if operands[command] == "get":
response = self.get(operands[key])
elif operands[command] == "set":
value = " ".join(operands[2:])
self.set(operands[key], value)
response = f"key {operands[key]} set to {value}"
elif operands[command] == "delete":
self.delete(operands[key])
response = f"key {key} deleted"
elif operands[command] == "show":
response = str(self.data)
else:
pass
with self.client_lock:
if operands[command] == "get":
response = self.get(operands[key])
elif operands[command] == "set":
value = " ".join(operands[2:])
self.set(operands[key], value)
response = f"key {operands[key]} set to {value}"
elif operands[command] == "delete":
self.delete(operands[key])
response = f"key {key} deleted"
elif operands[command] == "show":
response = str(self.data)
else:
pass


return response

0 comments on commit d36399b

Please sign in to comment.
You can’t perform that action at this time.