-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsstable_server.py
More file actions
39 lines (33 loc) · 1.25 KB
/
sstable_server.py
File metadata and controls
39 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import socket, os, json
from sstable import SSTable
import atexit
SOCKET_PATH = "/tmp/db_socket"
def main():
if os.path.exists(SOCKET_PATH):
os.remove(SOCKET_PATH)
db = SSTable()
atexit.register(db.flush)
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server:
server.bind(SOCKET_PATH)
server.listen()
print(f"Server started at {SOCKET_PATH}")
while True:
conn, _ = server.accept()
with conn:
data = conn.recv(4096)
if not data:
continue
cmd = json.loads(data.decode())
if cmd["type"] == "get":
print(f"GET command received for key: {cmd['key']}")
value = db.get(cmd["key"])
if value is not None:
conn.sendall(json.loads(value).encode())
else:
conn.sendall(b"null")
elif cmd["type"] == "set":
print(f"SET command received for key: {cmd['key']} with value: {cmd['value']}")
db.set(cmd["key"], json.dumps(cmd["value"]))
conn.sendall(b"OK")
if __name__ == "__main__":
main()