From: Scott Baker Date: Tue, 9 Sep 2008 01:40:03 +0000 (+0000) Subject: split tables from records X-Git-Tag: sfa-0.9-0@14641~856 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;ds=sidebyside;h=9fe81c328a9e413fde0e1b537510ab1849c97bbb;p=sfa.git split tables from records --- diff --git a/util/genitable.py b/util/genitable.py new file mode 100644 index 00000000..53756aa1 --- /dev/null +++ b/util/genitable.py @@ -0,0 +1,118 @@ +# genitable.py +# +# implements support for geni records stored in db tables +# +# TODO: Use existing PLC database methods? or keep this separate? + +import report + +from pg import DB +from gid import * +from record import * + +GENI_TABLE_PREFIX = "geni$" + +class GeniTable(): + def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None): + global GENI_TABLE_PREFIX + + self.hrn = hrn + + # pgsql doesn't like table names with "." in them, to replace it with "$" + self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$") + + # establish a connection to the pgsql server + self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password']) + + # if asked to create the table, then create it + if create: + self.create() + + def exists(self): + tableList = self.cnx.get_tables() + if 'public.' + self.tablename in tableList: + return True + if 'public."' + self.tablename + '"' in tableList: + return True + return False + + def create(self): + querystr = "CREATE TABLE " + self.tablename + " ( \ + key text, \ + name text, \ + gid text, \ + type text, \ + pointer integer);" + + self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename) + self.cnx.query(querystr) + + def remove(self, record): + query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'" + self.cnx.query(query_str) + + def insert(self, record): + fieldnames = ["key"] + record.get_field_names() + fieldvals = record.get_field_value_strings(fieldnames) + query_str = "INSERT INTO " + self.tablename + \ + "(" + ",".join(fieldnames) + ") " + \ + "VALUES(" + ",".join(fieldvals) + ")" + #print query_str + self.cnx.query(query_str) + + def update(self, record): + names = record.get_field_names() + pairs = [] + for name in names: + val = record.get_field_value_string(name) + pairs.append(name + " = " + val) + update = ", ".join(pairs) + + query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'" + #print query_str + self.cnx.query(query_str) + + def resolve_dict(self, type, hrn): + query_str = "SELECT * FROM " + self.tablename + " WHERE name = '" + hrn + "'" + dict_list = self.cnx.query(query_str).dictresult() + result_dict_list = [] + for dict in dict_list: + if (type=="*") or (dict['type'] == type): + result_dict_list.append(dict) + return result_dict_list + + def resolve(self, type, hrn): + result_dict_list = self.resolve_dict(type, hrn) + result_rec_list = [] + for dict in result_dict_list: + result_rec_list.append(GeniRecord(dict=dict)) + return result_rec_list + + def list_dict(self): + query_str = "SELECT * FROM " + self.tablename + result_dict_list = self.cnx.query(query_str).dictresult() + return result_dict_list + + def list(self): + result_dict_list = self.list_dict() + result_rec_list = [] + for dict in result_dict_list: + result_rec_list.append(GeniRecord(dict=dict)) + return result_rec_list + +def set_geni_table_prefix(x): + global GENI_TABLE_PREFIX + + GENI_TABLE_PREFIX = x + +def geni_records_purge(cninfo): + global GENI_TABLE_PREFIX + + cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password']) + tableList = cnx.get_tables() + for table in tableList: + if table.startswith(GENI_TABLE_PREFIX) or \ + table.startswith('public.' + GENI_TABLE_PREFIX) or \ + table.startswith('public."' + GENI_TABLE_PREFIX): + report.trace("dropping table " + table) + cnx.query("DROP TABLE " + table) diff --git a/util/record.py b/util/record.py index 9149deb2..bf9156c5 100644 --- a/util/record.py +++ b/util/record.py @@ -4,9 +4,7 @@ # # TODO: Use existing PLC database methods? or keep this separate? -from pg import DB - -GENI_TABLE_PREFIX = "geni$" +import report # Record is a tuple (Name, GID, Type, Info) # info is comprised of the following sub-fields @@ -42,7 +40,10 @@ class GeniRecord(): self.dirty = True def set_gid(self, gid): - self.gid = gid + if isinstance(gid, str): + self.gid = gid + else: + self.gid = gid.save_to_string(save_parents=True) self.dirty = True def set_type(self, type): @@ -73,8 +74,18 @@ class GeniRecord(): else: return {} + def get_name(self): + return self.name + + def get_type(self): + return self.type + def get_pointer(self): - return pointer + return self.pointer + + # TODO: not the best name for the function, because we have things called gidObjects in the Cred + def get_gid_object(self): + return GID(string=self.gid) def get_key(self): return self.name + "#" + self.type @@ -102,7 +113,7 @@ class GeniRecord(): dict = {} names = self.get_field_names() for name in names: - dict[name] = self.getattr(name) + dict[name] = getattr(self, name) if self.pl_info: dict['pl_info'] = self.pl_info @@ -112,104 +123,24 @@ class GeniRecord(): return dict -# GeniTable -# -# Represents a single table on a registry for a single authority. - -class GeniTable(): - def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None): - global GENI_TABLE_PREFIX - - self.hrn = hrn - - # pgsql doesn't like table names with "." in them, to replace it with "$" - self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$") - - # establish a connection to the pgsql server - self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password']) - - # if asked to create the table, then create it - if create: - self.create() - - def exists(self): - tableList = self.cnx.get_tables() - return (self.tablename in tableList) - - def create(self): - querystr = "CREATE TABLE " + self.tablename + " ( \ - key text, \ - name text, \ - gid text, \ - type text, \ - pointer integer);" - - self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename) - self.cnx.query(querystr) - - def remove(self, record): - query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'" - self.cnx.quert(query_str) - - def insert(self, record): - fieldnames = ["key"] + record.get_field_names() - fieldvals = record.get_field_value_strings(fieldnames) - query_str = "INSERT INTO " + self.tablename + \ - "(" + ",".join(fieldnames) + ") " + \ - "VALUES(" + ",".join(fieldvals) + ")" - #print query_str - self.cnx.query(query_str) - - def update(self, record): - names = record.get_field_names() - pairs = [] - for name in names: - val = record.get_field_value_string(name) - pairs.append(name + " = " + val) - update = ", ".join(pairs) - - query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'" - #print query_str - self.cnx.query(query_str) - - def resolve_dict(self, type, hrn): - query_str = "SELECT * FROM " + self.tablename + " WHERE name = '" + hrn + "'" - dict_list = self.cnx.query(query_str).dictresult() - result_dict_list = [] - for dict in dict_list: - if (type=="*") or (dict['type'] == type): - result_dict_list.append(dict) - return result_dict_list - - def resolve(self, type, hrn): - result_dict_list = self.resolve_dict(type, hrn) - result_rec_list = [] - for dict in result_dict_list: - result_rec_list.append(GeniRecord(dict=dict)) - return result_rec_list - - def list_dict(self): - query_str = "SELECT * FROM " + self.tablename - result_dict_list = self.cnx.query(query_str).dictresult() - return result_dict_list - - def list(self): - result_dict_list = self.list_dict() - result_rec_list = [] - for dict in result_dict_list: - result_rec_list.append(GeniRecord(dict=dict)) - return result_rec_list - -def set_geni_table_prefix(x): - global GENI_TABLE_PREFIX - - GENI_TABLE_PREFIX = x - -def geni_records_purge(cninfo): - global GENI_TABLE_PREFIX - - cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password']) - tableList = cnx.get_tables() - for table in tableList: - if table.startswith(GENI_TABLE_PREFIX): - cnx.query("DROP TABLE " + table) + def dump(self, dump_parents=False): + print "RECORD", self.name + print " hrn:", self.name + print " type:", self.type + print " gid:" + self.get_gid_object().dump(8, dump_parents) + print " pointer:", self.pointer + + print " geni_info:" + geni_info = getattr(self, "geni_info", {}) + if geni_info: + for key in geni_info.keys(): + print " ", key, ":", geni_info[key] + + print " pl_info:" + pl_info = getattr(self, "pl_info", {}) + if pl_info: + for key in pl_info.keys(): + print " ", key, ":", pl_info[key] + +