-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_server.py
390 lines (309 loc) · 11.1 KB
/
database_server.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""Database Server.
A server that is accessible on http://localhost:4000/. When your server
receives a request on http://localhost:4000/set?somekey=somevalue it should
store the passed key and value in memory. When it receives a request on
http://localhost:4000/get?key=somekey it should return the value stored at
somekey.
Class diagram:
+------------+ +--------------------+
| BaseServer |------------------| BaseRequestHandler |
+------------+ +--------------------+
^ ^
| |
+-----+-----+ +---------+------------+
| TCPServer |-------------------| StreamRequestHandler |
+-----------+ +----------------------+
^ ^
| |
+-----+------+ +---------+----------+
| HTTPServer |------------------| HTTPRequestHandler |
+------------+ +--------------------+
^ ^
| |
+-----+----------+ +---------+--------------+
| DatabaseServer |--------------| DatabaseRequestHandler |
+----------------+ +------------------------+
|
|
+-----+---+
| Storage |
+---------+
^
|
+-----------------------------------+
| |
+-----+--------------+ +--------+--------+
| NonVolatileStorage | | VolatileStorage |
+--------------------+ +-----------------+
BaseServer/BaseRequestHandler defines the interface, but does not implement
most of the methods, which is done in subclasses.
TCPServer/StreamRequestHandler uses the Internet TCP protocol, which
provides for continuous streams of data between the client and server.
HTTPServer/HTTPRequestHandler dispatches the HTTP requests to a handler and
implements the HTTP protocol.
DatabaseServer/DatabaseRequestHandler dispatches the get/set requests
(set?somekey=somevalue, get?key=somekey) to a method handler and makes the
apropriate calls to Storage.
Storage defines the interface, but does not implement most of the methods, which
is done in sublcasses Volatile and NonVolatile. Volatile
will save the key, value pair in memory. NonVolatile will save the
key-value pair in disk.
Example:
Server::
$ python -m database_server
Attributes:
FILENAME::
Filename for non-volatile storage.
"""
import http.server
import urllib.parse
import socketserver
import threading
import os
import json
import tempfile
import sqlite3
FILENAME = "{0}/data.dat".format(os.path.dirname(os.path.abspath(__file__)))
class RWLock:
def __init__(self):
self.__counter = 0
self.__counter_semaphore = threading.BoundedSemaphore(1)
self.__reader_semaphore = threading.BoundedSemaphore(1)
self.__writer_semaphore = threading.BoundedSemaphore(1)
def reader_acquire(self):
self.__reader_semaphore.acquire()
self.__counter_semaphore.acquire()
self.__counter += 1
if self.__counter == 1:
self.__writer_semaphore.acquire()
self.__counter_semaphore.release()
self.__reader_semaphore.release()
def reader_release(self):
self.__counter_semaphore.acquire()
self.__counter -= 1
if self.__counter == 0:
self.__writer_semaphore.release()
self.__counter_semaphore.release()
def writer_acquire(self):
self.__reader_semaphore.acquire()
self.__writer_semaphore.acquire()
def writer_release(self):
self.__writer_semaphore.release()
self.__reader_semaphore.release()
class DatabaseServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
"""Datababse Server.
Since we are building an HTTP server with no disk I/O where all data will be
stored in memory, a synchronous server class is correct.
Knowing though that the class Storage could potentially be extended with disk
I/O it would better to anticipate for that. An HTTP server with disk I/0
could render the service "deaf" while one request is being handled. A
threading or forking server would be appropriate.
I choose not to implement as asynchronous server class for the purposes of
the interview.
Attributes:
storage: Server storage.
"""
def __init__(self, address, handler):
super().__init__(address, handler)
# Database server storage
self.storage = NonVolatile()
class DatabaseRequestHandler(http.server.BaseHTTPRequestHandler):
"""Database request handler."""
def do_GET(self):
"""Handle HTTP GET request.
Handle HTTP GET request by parsing the path to method, arguments and
passing them for dispatch.
"""
method, args = self.parse(self.path)
self.dispatch(method, args)
def parse(self, path):
"""Parse url.
Parse url to path and query. Path is the method name and query are the
method arguments.
Returns:
path: Method name.
query: Key/Value pairs.
"""
url = urllib.parse.urlparse(path)
path = url.path[1:]
query = urllib.parse.parse_qsl(url.query)[0]
return path, query
def dispatch(self, method, args):
"""Dispatch method.
Dispatch method if exists else send error to client. This will enable
extention of the database server interface through extention and not
modification.
"""
try:
method = getattr(self, method)
except AttributeError:
self.send_error(400)
else:
method(args)
def get(self, args):
"""Get value by key.
Get value by key from server storage and send response with value to
client.
Since there is no clear status code for that case, if key has no value
then a 200 response with an empty string as body is sent.
"""
# Get value by key from server storage
value = self.server.storage.get(args[1])
# Send response with value to client
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(value.encode())
def set(self, args):
"""Set value by key.
Set value by key to server storage and send response to client.
"""
# Set value by key to server storage
self.server.storage.set(args[0], args[1])
# Send response to client
self.send_response(200)
self.end_headers()
class Storage:
"""Storage interface.
Storage defines an interface for setting and getting values from keys.
Subclasses should implement the set and get methods.
"""
def set(self, key, value):
"""Set value by key.
"""
raise NotImplementedError
def get(self, key):
"""Get value by key.
Returns:
value: If key exist return value else return empty string.
"""
raise NotImplementedError
class Volatile(Storage):
"""Volatile storage.
Volatitle storage implements set and get methods for storing values from
keys in memory.
"""
def __init__(self):
self.__data = {}
def set(self, key, value):
self.__data[key] = value
def get(self, key):
return self.__data.get(key, "")
class NonVolatile(Storage):
def __init__(self):
super().__init__()
self.__lock = RWLock()
try:
with open(FILENAME, "r") as f:
self.__data = json.load(f)
except IOError:
self.__data = {}
def get(self, key):
self.__lock.reader_acquire()
try:
return self.__data.get(key, "")
finally:
self.__lock.reader_release()
def set(self, key, value):
self.__lock.writer_acquire()
try:
self.__data[key] = value
with tempfile.NamedTemporaryFile("w", dir=os.path.dirname(FILENAME), delete=False) as tf:
json.dump(self.__data, tf)
tf.flush()
os.fsync(tf)
tempname = tf.name
os.rename(tempname, FILENAME)
dirfd = os.open(os.path.dirname(FILENAME), os.O_DIRECTORY)
os.fsync(dirfd)
os.close(dirfd)
finally:
self.__lock.writer_release()
#class NonVolatile(Storage):
#
# def __init__(self):
# super().__init__()
#
# self.__lock = RWLock()
#
# def get(self, key):
# self.__lock.reader_acquire()
# try:
# with open(FILENAME, "r") as f:
# for line in f:
# tempkey, tempvalue = line.split("=")
# if key == tempkey:
# return tempvalue
# return ""
# finally:
# self.__lock.reader_release()
#
# def set(self, key, value):
# self.__lock.writer_acquire()
# try:
# with open(FILENAME, "r") as f:
# with tempfile.NamedTemporaryFile("w", dir=os.path.dirname(FILENAME), delete=False) as tf:
#
# templine = "{0}={1}\n".format(key, value)
#
# updated = False
# for line in f:
# if key == line.split("=")[0]:
# tf.write(templine)
# updated = True
# else:
# tf.write(line)
# if not updated:
# tf.write(templine)
#
# tf.flush()
# os.fsync(tf)
# tempname = tf.name
# os.rename(tempname, FILENAME)
# dirfd = os.open(os.path.dirname(FILENAME), os.O_DIRECTORY)
# os.fsync(dirfd)
# os.close(dirfd)
# finally:
# self.__lock.writer_release()
#class NonVolatile(Storage):
#
# def __init__(self):
# super().__init__()
#
# with sqlite3.connect(FILENAME, isolation_level=None) as conn:
# cursor = conn.cursor()
# cursor.execute("""
# create table if not exists data(
# key text primary key not null,
# value text
# )
# """)
#
# def get(self, key):
# with sqlite3.connect(FILENAME, isolation_level=None) as conn:
# cursor = conn.cursor()
# cursor.execute("""
# select *
# from data
# where key=?
# """, (key,))
# results = cursor.fetchone()
#
# return "" if results is None else results[1]
#
# def set(self, key, value):
# with sqlite3.connect(FILENAME, isolation_level=None) as conn:
# cursor = conn.cursor()
# cursor.execute("""
# insert or replace
# into data
# (key, value) values(?, ?)
# """, (key, value))
if __name__ == "__main__":
HOST = "localhost"
PORT = 4000
server = DatabaseServer((HOST, PORT), DatabaseRequestHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()