| 61 | |
| 62 | |
| 63 | class PostgresDB(DB): |
| 64 | |
| 65 | def __init__(self, nrows, rng, userandom): |
| 66 | DB.__init__(self, nrows, rng, userandom) |
| 67 | self.port = PORT |
| 68 | |
| 69 | def flatten(self, line): |
| 70 | """Flattens list of tuples l.""" |
| 71 | return [x[0] for x in line] |
| 72 | # return map(lambda x: x[col], line) |
| 73 | |
| 74 | # Overloads the method in DB class |
| 75 | def get_db_size(self): |
| 76 | sout = subprocess.Popen( |
| 77 | "sudo du -s %s" % DATA_DIR, shell=True, stdout=subprocess.PIPE |
| 78 | ).stdout |
| 79 | line = sout[0] |
| 80 | return int(line.split()[0]) |
| 81 | |
| 82 | def open_db(self, remove=0): |
| 83 | if remove: |
| 84 | sout = subprocess.Popen( |
| 85 | DROP_DB % self.filename, shell=True, stdout=subprocess.PIPE |
| 86 | ).stdout |
| 87 | for line in sout: |
| 88 | print(line) |
| 89 | sout = subprocess.Popen( |
| 90 | CREATE_DB % self.filename, shell=True, stdout=subprocess.PIPE |
| 91 | ).stdout |
| 92 | for line in sout: |
| 93 | print(line) |
| 94 | |
| 95 | print("Processing database:", self.filename) |
| 96 | con = db2.connect(DSN % (self.filename, self.port)) |
| 97 | self.cur = con.cursor() |
| 98 | return con |
| 99 | |
| 100 | def create_table(self, con): |
| 101 | self.cur.execute( |
| 102 | """create table %s( |
| 103 | col1 integer, |
| 104 | col2 integer, |
| 105 | col3 double precision, |
| 106 | col4 double precision)""" |
| 107 | % TABLE_NAME |
| 108 | ) |
| 109 | con.commit() |
| 110 | |
| 111 | def fill_table(self, con): |
| 112 | st = StreamChar(self) |
| 113 | self.cur.copy_from(st, TABLE_NAME) |
| 114 | con.commit() |
| 115 | |
| 116 | def index_col(self, con, colname, optlevel, idxtype, verbose): |
| 117 | self.cur.execute( |
| 118 | "create index {} on {}({})".format( |
| 119 | colname + "_idx", TABLE_NAME, colname |
| 120 | ) |