-
Notifications
You must be signed in to change notification settings - Fork 0
/
effyTable.py
189 lines (157 loc) · 5.54 KB
/
effyTable.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
#
# effyTable: simple table database module.
#
# Copyright 2010 Toshio Koide.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import bisect
from multiprocessing import Lock
from multiprocessing.managers import BaseManager
class effyTable:
__indexes = {}
__rows = {}
__maxrowid = 0
__lock = Lock()
def __setRow(self, rowid, row):
self.__rows[rowid] = row
for col in self.__indexes:
if col in row:
self.__lock.acquire()
bisect.insort(self.__indexes[col], (row[col], rowid))
self.__lock.release()
def addRow(self, row):
self.__lock.acquire()
self.__maxrowid += 1
rowid = self.__maxrowid
self.__lock.release()
self.__setRow(rowid, row)
return rowid
def getRowIds(self, col, from_value=None, to_value=None):
if col in self.__indexes:
from_i = bisect.bisect_left(self.__indexes[col], (from_value,))
if to_value:
to_i = bisect.bisect_left(self.__indexes[col], (to_value,))
return [index_row[1] for index_row in self.__indexes[col][from_i:to_i]]
else:
return [index_row[1] for index_row in self.__indexes[col][from_i:]]
def getRow(self, rowid):
try:
return self.__rows[rowid].copy()
except IndexError:
return None
def deleteRow(self, rowid):
self.__lock.acquire()
row = self.__rows[rowid]
for col in self.__indexes:
if col in row:
del self.__indexes[col][bisect.bisect_left(self.__indexes[col], (row[col], rowid))]
del self.__rows[rowid]
self.__lock.release()
def updateRow(self, rowid, row):
if rowid in self.__rows:
self.deleteRow(rowid)
self.__setRow(rowid, row)
def setIndex(self, col):
i = []
if col not in self.__indexes:
self.__lock.acquire()
for rowid,row in self.__rows.items():
if col in row:
i.append((row[col],rowid))
i.sort()
self.__indexes[col] = i
self.__lock.release()
return len(i)
def removeIndex(self, col):
if col in self.__indexes:
del self.indexes[col]
def getIndexNames(self):
return self.__indexes.keys()
def show(self):
print "rows:", self.__rows
print "indexes:", self.__indexes
print "maxrowid:", self.__maxrowid
class PeerAddresses:
__addresses = effyTable()
def __init__(self):
self.__addresses.setIndex('key')
self.__addresses.setIndex('address')
def getAddress(self, key):
id = self.__addresses.getRowIds('key', None, key)[-1]
row = self.__addresses.getRow(id)
return row['address']
def setAddress(self, key, address):
rows = self.__addresses.getRowIds('address', address)
if len(rows) > 0 and self.__addresses.getRow(rows[0])['address'] == address:
self.__addresses.updateRow(rows[0], {'key':key, 'address':address})
else:
self.__addresses.addRow({'key':key, 'address':address})
"""
usage of class Peer:
*** at manager node
from effyTable import Peer
m=Peer(address=('localhost',1000), authkey='a')
m.startManager()
*** at node A
from effyTable import Peer
m=Peer(address=('localhost',1001), authkey='a')
m.connectManager(address=('localhost', 1000))
m.startPeer(key=None)
*** at node B
from effyTable import Peer
m=Peer(address=('localhost',1002), authkey='a')
m.connectManager(address=('localhost', 1000))
m.startPeer(key='effy')
*** at node A or B
m.getTable('effy').setIndex('key')
m.getTable('effy1').setIndex('key')
(key,value) = ('sample', 12345)
m.getTable(key).addRow({'key':key, 'value':value})
"""
class Peer(BaseManager):
__manager = None
__addresses = None
__table = None
__peers = None
__address = None
__authkey = None
def __init__(self, address, authkey):
self.__address = address
self.__authkey = authkey
BaseManager.__init__(self, address, authkey)
def startManager(self):
self.__addresses = PeerAddresses()
Peer.register('addresses', callable=lambda:self.__addresses)
self.start()
def connectManager(self, address):
Peer.register('addresses')
self.__manager = Peer(address=address, authkey=self.__authkey)
self.__manager.connect()
self.__addresses = self.__manager.addresses()
def startPeer(self, key):
self.__addresses.setAddress(key, self.__address)
self.__table = effyTable()
self.__peers = {}
Peer.register('table', callable=lambda:self.__table)
self.start()
def getTable(self, key):
address = self.__addresses.getAddress(key)
if address not in self.__peers:
Peer.register('table')
peer = Peer(address=address, authkey=self.__authkey)
peer.connect()
self.__peers[address] = peer
else:
peer = self.__peers[address]
return peer.table()