Skip to content

Commit

Permalink
Lock mutations of each type to the data and the log to prevent simult…
Browse files Browse the repository at this point in the history
…aneous commands on different threads from corrupting the data
  • Loading branch information
chelseatroy committed Dec 16, 2019
1 parent 857856d commit d36399b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions key_value_operations.py
@@ -1,4 +1,8 @@
import threading

class KeyValueStore:
client_lock = threading.Lock()

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

Expand All @@ -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.