-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkv.py
105 lines (87 loc) · 2.73 KB
/
dkv.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
#!/usr/bin/env python3
import os
import sys
# import numpy as np
# import time
import collections
#
# import numpy as np
import random
class KeyValueStore:
""" Distributed key value store
TODO(@dain): <<TO BE REMOVED BEFORE ALPHA LAUNCH>>
# General <Key,Value> Store Implementation
#
- [x] Ability to set a key and value
- [x] Ability to retrieve a value via the key
- [x] Ability to retrieve all keys
- [x] Ability to get all values
- [x] Ability to update a value by key
# Distributed <Key,Value> Store Integration
#
- [x] Ability to add a key and value, and it automatically adds URL to
system as key
{
URL_HERE_TO_SERVER: {
}
}
- [ ] Ability to store the data on the server - the value should point
to a route with d structure
- [ ] Route should point to a controller method
- [ ] Controller method should save it to the DB (in this case redis)
- [ ] Consider abstracting the storage implementation to allow any store
- [ ] Abstract the code into a library for a few languages (python
first, ruby second)
- [ ] Library should contain all of the code above, and provide
clear instructions
- [ ] Allow to inheret from controller/storage model/etc so you can
easily override and provide custom functionality
"""
SERVERS = [
'https://localhost:3000',
'https://localhost:5000',
'https://localhost:6000'
]
def __init__(self):
self.kv = collections.OrderedDict()
def set(self, key, value):
if key and value:
self.kv[key] = value
def update(self, key, value):
for k in self.kv:
if k == key:
self.kv[k] = value
return "Updated"
def getValueByKey(self, key):
for k in self.kv:
if k == key:
return self.kv[k]
def getAllKeys(self):
keys = []
for k in self.kv:
keys.append(k)
return keys
def getAllValues(self):
values = []
for k in self.kv:
values.append(self.kv[k])
return values
def distributedSet(self, key, value):
serverQuantity = len(self.SERVERS)
randomNumber = random.randrange(0, serverQuantity)
self.set(self.SERVERS[randomNumber], {
key: value
})
# END OF FILE
v = KeyValueStore()
v.set('a', 1)
v.set('b', 3)
print(v.kv)
print(v.getValueByKey('a'))
print(v.getValueByKey('b'))
print(v.getAllKeys())
print(v.getAllValues())
print(v.update('a', 2))
print(v.getValueByKey('a'))
v.distributedSet('a', 2)
print(v.kv)