1212from pysonata .client import SonataClient
1313from pysonata .admin import SonataAdmin
1414
15- class provDBshard :
16- def __openCollection (self , coll_name , create = False ):
15+ #Base class for a Sonata client connection to a database
16+ class provDBclientBase :
17+ def _openCollection (self , coll_name , create = False ):
1718 if (self .database .exists (coll_name ) == False ):
1819 if (create == True ):
1920 print ("Shard %s creating '%s' collection" % (self .db_name , coll_name ) )
@@ -23,16 +24,28 @@ def __openCollection(self, coll_name, create=False):
2324 sys .exit (1 )
2425 return self .database .open (coll_name )
2526
27+ #Apply a jx9 filter to a collection
28+ def filter (self , which_coll , query ):
29+ return self .getCollection (which_coll ).filter (query )
30+
31+ def execute (self , code , variables ):
32+ return self .database .execute (code ,variables )
2633
34+ def store (self , which_coll , record ):
35+ return self .getCollection (which_coll ).store (record , commit = True )
36+
37+
38+ #Sonata client connection to a database shard
39+ class provDBshard (provDBclientBase ):
2740 def __init__ (self ,client ,address ,db_name ,create = False ):
2841 #Open database as a client
2942 self .db_name = db_name
3043 self .database = client .open (address , 0 , db_name )
3144
3245 #Initialize collections
33- self .anomalies = self .__openCollection ('anomalies' ,create = create )
34- self .normalexecs = self .__openCollection ('normalexecs' ,create = create )
35- self .metadata = self .__openCollection ('metadata' ,create = create )
46+ self .anomalies = self ._openCollection ('anomalies' ,create = create )
47+ self .normalexecs = self ._openCollection ('normalexecs' ,create = create )
48+ self .metadata = self ._openCollection ('metadata' ,create = create )
3649
3750 def __del__ (self ):
3851 del self .anomalies
@@ -53,16 +66,34 @@ def getCollection(self, which_coll):
5366 sys .exit (1 )
5467 return col
5568
56- #Apply a jx9 filter to a collection
57- def filter (self , which_coll , query ):
58- return self .getCollection (which_coll ).filter (query )
5969
60- def execute (self , code , variables ):
61- return self .database .execute (code ,variables )
6270
63- def store (self , which_coll , record ):
64- return self .getCollection (which_coll ).store (record , commit = True )
65-
71+ class provDBglobal (provDBclientBase ):
72+ def __init__ (self ,client ,address ,db_name ,create = False ):
73+ #Open database as a client
74+ self .db_name = db_name
75+ self .database = client .open (address , 0 , db_name )
76+
77+ #Initialize collections
78+ self .func_stats = self ._openCollection ('func_stats' ,create = create )
79+ self .counter_stats = self ._openCollection ('counter_stats' ,create = create )
80+
81+ def __del__ (self ):
82+ del self .func_stats
83+ del self .counter_stats
84+
85+ def getCollection (self , which_coll ):
86+ col = None
87+ if which_coll == "func_stats" :
88+ col = self .func_stats
89+ elif which_coll == "counter_stats" :
90+ col = self .counter_stats
91+ else :
92+ print ("Invalid collection" )
93+ sys .exit (1 )
94+ return col
95+
96+
6697
6798
6899class provDBinterface :
@@ -99,6 +130,27 @@ def __init__(self,engine,filename,nshards,create=False):
99130
100131 self .db_shards .append ( provDBshard (self .client , self .address , db_name , create = create ) )
101132
133+ #Connect to global database if available
134+ db_name = "provdb.global"
135+ self .db_global_name = db_name
136+ db_file = re .sub (r'\%d' ,"global" ,filename )
137+ if os .path .exists (db_file ) == False :
138+ if create == True :
139+ print ("Creating global database as %s from file %s" % (db_name ,db_file ))
140+ self .admin .create_database (self .address , 0 , db_name , 'unqlite' , "{ \" path\" : \" %s\" }" % db_file )
141+ self .use_global_db = True
142+ else :
143+ self .use_global_db = False
144+ else :
145+ print ("Attaching global database as %s from file %s" % (db_name ,db_file ))
146+ self .admin .attach_database (self .address , 0 , db_name , 'unqlite' , "{ \" path\" : \" %s\" }" % db_file )
147+ self .use_global_db = True
148+
149+ if self .use_global_db :
150+ self .db_global = provDBglobal (self .client , self .address , db_name , create = create )
151+ else :
152+ self .db_global = None
153+
102154 def getNshards (self ):
103155 return len (self .db_shards )
104156
@@ -108,11 +160,20 @@ def getShard(self, i):
108160 def getShards (self ):
109161 return self .db_shards
110162
163+ #Get the global database client. Returns None if the global database doesn't exit
164+ def getGlobalDB (self ):
165+ return self .db_global
166+
111167 def __del__ (self ):
112- del self .db_shards
113- del self .client
168+ del self .db_shards
114169 for n in self .db_names :
115170 self .admin .detach_database (self .address , 0 , n )
171+
172+ if self .use_global_db :
173+ del self .db_global
174+ self .admin .detach_database (self .address , 0 , self .db_global_name )
175+
176+ del self .client
116177 del self .admin
117178 del self .provider
118179
0 commit comments