Attachment 'gns_core.py'
Download 1 import sys
2 import types
3
4 from time import time as _time
5
6 import sqlite
7
8 import gnserr
9
10 serverstatue = {
11 'S_REGED': 1,
12 'S_WORKING': 2,
13 'S_PAUSED': 3,
14 'S_STOPED': 4
15 }
16
17 class Namepool:
18 """
19 Namepool: a name generater.
20 """
21 def __init__(self, mode = 'String' ,prefix='', postfix='', start=0, step=1):
22 assert(mode in ['String', 'Integer', 'Long'])
23 self.mode = mode
24 if mode == 'String':
25 self.prefix, self.postfix = prefix, postfix
26 self.counter, self.step = start, step
27 def get(self):
28 r = self.hook[self.mode](self)
29 self.counter += self.step
30 return r
31 hook = {
32 'String': lambda self: self.prefix + str(self.counter) + self.postfix,
33 'Integer': lambda self: int(self.counter),
34 'Long': lambda self: long(self.counter)
35 }
36
37 class Partition:
38 def __init__(self, dbname=None):
39 self.opendb(dbname)
40 self.np = Namepool(mode = 'String',
41 prefix='server://anonymous/s'+int(_time()).__str__()+'c'
42 )
43 self.idp = Namepool(mode = 'Long', start=_time()*1000)
44 def opendb(self, dbname=None):
45 if not dbname:
46 raise gnserr.dbnameerr
47 self.con = sqlite.connect(dbname, encoding='utf-8')
48 return self
49 def close(self):
50 if not self.con.closed:
51 self.con.close()
52 return self
53 def __del__(self):
54 self.close()
55 def create_db(self):
56 assert((self.con)
57 and (not self.con.closed)
58 )
59 cu = self.con.cursor()
60 """
61 sev_id: server's ident
62 uri: server's uri, 129.0.0.1:8080, ftp://www.xxx.xxx/, ect. it's just a name.
63 url: server's url, http://www.ddd.aaa:8080/, dns://kkk.ddd.ccc, ect. can figure out server's real address.
64 statue: server statue, work|pause|stop|death
65 checktime: last update timestamp.
66 """
67 SQL_create_server_tb = \
68 """
69 create table servers (
70 sev_id int primary key,
71 uri text(255),
72 url text not null,
73 statue int,
74 checktime int
75 )
76 """
77 """
78 res_id: resource id
79 server: id of the server which countain the resource
80 uri: resource uri
81 """
82 SQL_create_resource_tb = \
83 """
84 create table resources (
85 res_id long primary key,
86 server int,
87 uri varchar(255)
88 )
89 """
90 cu.execute(SQL_create_server_tb)
91 cu.execute(SQL_create_resource_tb)
92 self.con.commit()
93 return self
94 def drop_db(self):
95 cu = self.con.cursor()
96 cu.executemany("drop table %s",
97 [('servers', ),
98 ('resources', )
99 ]
100 )
101 self.con.commit()
102 return self
103 def dropall(self):
104 self.drop_db()
105 return self
106 def addserver(self, serverinfo = None):
107 cu = self.con.cursor()
108 serverurl = serverinfo.get('url', None)
109 if not serverurl:
110 raise gnserr.urlerr(url)
111 serveruri = serverinfo.get('uri', None)
112 if serveruri:
113 cu.execute("select count(*) from servers where uri='%(uri)s'"
114 %{'uri': serveruri})
115 if int(cu.fetchone()[0]) >= 1:
116 raise gnserr.haveserver(serveruri)
117 else:
118 serveruri = self.np.get()
119 serverid = self.idp.get()
120 cu.execute(
121 """
122 insert into servers
123 (sev_id, uri, url, statue, checktime)
124 values
125 (%(sev_id)u,
126 '%(uri)s',
127 '%(url)s',
128 %(statue)u,
129 %(checktime)u
130 )
131 """%
132 {'sev_id': serverid,
133 'uri': serveruri,
134 'url': serverurl,
135 'statue': serverstatue['S_REGED'],
136 'checktime': int(_time())
137 }
138 )
139 self.con.commit()
140 return serverid
141 def dropserver(self, id):
142 cu = self.con.cursor()
143 cu.execute(
144 """
145 delete from servers
146 where sev_id == %s
147 """
148 %(id)
149 )
150 self.con.commit()
151 return self
152 def _Pcase(self, case):
153 wherecase = ' 1 '
154 if len(case) <1 :
155 raise ERROR
156 for k, v in case.items():
157 if type(v) in types.StringTypes:
158 v = "'" + v + "'"
159 wherecase += ' and '+ k +'=='+ str(v) +' '
160 return wherecase
161 def Qserver_case(self, case):
162 wherecase = self._Pcase(case)
163 cu = self.con.cursor()
164 cu.execute("-- types long, str, str, int, long")
165 cu.execute(
166 """select sev_id, uri, url, statue, checktime
167 from servers where %s"""
168 %(wherecase)
169 )
170 res = cu.fetchall()
171 res = map(dict,
172 map(lambda x:
173 zip(('id', 'uri', 'url', 'statue', 'checktime'),x),
174 res
175 )
176 )
177 return res
178 def Qserver4uri(self, uri):
179 res = self.Qserver_case({'uri': uri})
180 if len(res) > 1:
181 raise gnserr.urierr(uri)
182 return res
183 def Qserver4id(self, id):
184 res = self.Qserver_case({'sev_id': id})
185 if len(res) > 1:
186 raise gnserr.urierr(uri)
187 return res
188 def _Pattrs(self, attrs):
189 if len(attrs) <1:
190 raise ERROR
191 assignments = ''
192 for k, v in attrs.items():
193 if type(v) in types.StringTypes:
194 v = "'" + v + "'"
195 assignments += ' '+k+'='+str(v)+','
196 return assignments[:-1]
197 def Sattrs4server(self, attrs, id):
198 assignments = self._Pattrs(attrs)
199 cu = self.con.cursor()
200 cu.execute(
201 """
202 update servers
203 set %s
204 where sev_id = %s
205 """
206 %(assignments, id)
207 )
208 self.con.commit()
209 return True
210 def Sstatue4server(self, statue, id):
211 res = self.Sattrs4server({'statue': statue}, id)
212 return res
213 def Surl4server(self, url, id):
214 res = self.Sattrs4server({'url': url}, id)
215 return res
216 def QSattrs4server_case(self, attrs, case):
217 wherecase = self._Pcase(case)
218 assignments = self._Pattrs(attrs)
219 cu = self.con.cursor()
220 #cu.execute("begin exclusive")
221 cu.execute("-- types long")
222 cu.execute(
223 """
224 select sev_id from servers
225 where %s
226 """
227 %(wherecase)
228 )
229 res = cu.fetchone()
230 if res:
231 sid = res[0]
232 else:
233 return None
234 cu.execute(
235 """
236 update servers set %s
237 where sev_id=%s
238 """
239 %(assignments, sid)
240 )
241 #cu.execute("commit")
242 #cu.execute("end")
243 self.con.commit()
244 return sid
245 class GNScore:
246 def __init__(self):
247 self.pdict = dict()
248 def addPartition(self, pname, describe):
249 if self.pdict.has_key(pname):
250 raise ERROR
251 self.pdict[pname] = [Partition(pname).create_db(), describe]
252 return True
253 def dropPartition(self, pname):
254 if not self.pdict.has_key(pname):
255 raise ERROR
256 self.pdict.get(pname)[0].dropall()
257 return True
258 def setPartition(self, pname, describe):
259 if not self.pdict.has_key(pname):
260 raise ERROR
261 self.pdict[pname][1] = describe
262 def addServer(self, sinfo):
263 if not self.pdict.has_key(sinfo.get('part','')):
264 raise ERROR
265 self.pdict.get(sinfo.get('part')
266 )[0].addserver(sinfo)
267
268 def test_Partition(tempdbname):
269 import pprint
270 pp = pprint.PrettyPrinter(indent=4)
271
272 p=Partition(dbname=tempdbname)
273 p.create_db()
274 try:
275 sid=list()
276 sinfo = {
277 'uri': 'hello://ddd.1',
278 'url': '127.0.0.1:8081',
279 }
280 sid.append(p.addserver(sinfo))
281 sinfo['uri'] = 'hello://dd.2'
282 sid.append(p.addserver(sinfo))
283 del sinfo['uri']
284 sid.append(p.addserver(sinfo))
285 pp.pprint(sid)
286 pp.pprint(p.Qserver4uri('hello://ddd.1'))
287 pp.pprint(p.Qserver4id(sid[1]))
288 pp.pprint(p.Qserver_case({'1':1}))
289 p.Sstatue4server(10, sid[1])
290 p.Surl4server('hello://abc.def/', sid[1])
291 pp.pprint(p.Qserver4id(sid[1]))
292 p.dropserver(sid[1])
293 pp.pprint(p.Qserver_case({'1':1}))
294 sid = p.QSattrs4server_case({'statue': 20},
295 {'statue': 1}
296 )
297 pp.pprint(p.Qserver4id(sid))
298 finally:
299 p.drop_db()
300 p.close()
301
302 def test_GNScore():
303 gns = GNScore()
304 gns.addPartition("test", "test Partition")
305 gns.dropPartition("test")
306 sinfo = {
307 "part": "test",
308 "uri": "test://server1",
309 "url": "127.0.0.1:8080"
310 }
311 gns.addServer(sinfo)
312 sinfo['uri'] = 'test://server2'
313 gns.addServer(sinfo)
314 gns.qserver()
315
316 if __name__ == "__main__":
317 test_Partition("testdb.sdb")
318 test_GNScore()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.