def test_creation(self): """Tests record creation and low level scanning to retrieve records from the database.""" rec = wgdb.create_record(self.d, 3) self.assertTrue(wgdb.is_record(rec)) l = wgdb.get_record_len(self.d, rec) self.assertEqual(l, 3) rec2 = wgdb.create_raw_record(self.d, 678) self.assertTrue(wgdb.is_record(rec2)) l = wgdb.get_record_len(self.d, rec2) self.assertEqual(l, 678) # wgdb module only allows comparing records by contents # so we need to use recognizable data for this test. wgdb.set_field(self.d, rec, 0, 99531179) wgdb.set_field(self.d, rec2, 0, 55498756) # XXX: the following relies on certain assumptions on memory # management of WhiteDB. By the API description, the records # are not necessarily fetched in order of creation, it is just # useful for the current test case that it happens to be the case. # cand = wgdb.get_first_record(self.d) self.assertEqual(wgdb.get_field(self.d, cand, 0), 99531179) cand = wgdb.get_next_record(self.d, cand) self.assertEqual(wgdb.get_field(self.d, cand, 0), 55498756) # This, however, should always work correctly wgdb.delete_record(self.d, rec) cand = wgdb.get_first_record(self.d) self.assertEqual(wgdb.get_field(self.d, cand, 0), 55498756)
def entry_upvote(entry_id): try: query = wgdb.make_query(g.wdb, arglist=[(0, wgdb.COND_EQUAL, entry_id)]) rec = wgdb.fetch(g.wdb, query) except Exception, e: rec = wgdb.create_record(g.wdb, 10) wgdb.set_field(g.wdb, rec, 0, entry_id)
def runTest(): field_count = 100 db = wgdb.attach_database("1", 1073741824); for x in range(0, 680000): record = wgdb.create_record(db, field_count) for j in range(0,field_count): wgdb.set_field(db, record, j, j) wgdb.detach_database(db); wgdb.delete_database("1");
def add_entry(): if not session.get('logged_in'): abort(401) cursor = g.db.cursor() cursor.execute('insert into entries (title, text) values (?, ?)', [request.form['title'], request.form['text']]) flash('New entry was successfully posted') g.db.commit() record = wgdb.create_record(g.wdb, 10) wgdb.set_field(g.wdb, record, 0, cursor.lastrowid) return redirect(url_for('show_entries'))
def create_record(self, size): """Create new record with given size.""" if size <= 0: raise DataError("Invalid record size") if self.locking: self.start_write() try: r = wgdb.create_record(self._db, size) finally: if self.locking: self.end_write() return self._new_record(r)
def show_entry(entry_id): cur = g.db.execute('select id, title, text from entries where id=? order by id desc', (str(entry_id))) row = cur.fetchone() entry = dict(eid=row[0], title=row[1], text=row[2]) meta = dict(views=0, upvotes=0, downvotes=0) try: query = wgdb.make_query(g.wdb, arglist=[(0, wgdb.COND_EQUAL, entry_id)]) rec = wgdb.fetch(g.wdb, query) except Exception, e: rec = wgdb.create_record(g.wdb, 10) wgdb.set_field(g.wdb, rec, 0, entry_id)
def make_testdata(self, dbsize): """Generates patterned test data for the query.""" for i in range(dbsize): for j in range(50): for k in range(50): rec = wgdb.create_record(self.d, 3) c1 = str(10 * i) c2 = 100 * j c3 = float(1000 * k) wgdb.set_field(self.d, rec, 0, c1) wgdb.set_field(self.d, rec, 1, c2) wgdb.set_field(self.d, rec, 2, c3)
def _set(self, key, stamp, value): l = wgdb.start_read(self.db) q = wgdb.make_query(self.db, arglist=[(0, wgdb.COND_EQUAL, key)]) rec = wgdb.fetch(self.db, q) wgdb.end_read(self.db, l) wgdb.free_query(self.db, q) l = wgdb.start_write(self.db) ## Transaction to Cache if not rec: rec = wgdb.create_record(self.db, 3) wgdb.set_field(self.db, rec, 0, key) wgdb.set_field(self.db, rec, 1, stamp) wgdb.set_field(self.db, rec, 2, value) wgdb.end_write(self.db, l)
count += 1 try: record = wgdb.get_next_record(db, record); except wgdb.error: record = None db = wgdb.attach_database("1", 1073741824); with open('../worldcitiespop.txt', 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') firstRow = True for row in reader: if firstRow: firstRow = False continue record = wgdb.create_record(db, 5) wgdb.set_field(db, record, 0, row[0]) wgdb.set_field(db, record, 1, row[1]) wgdb.set_field(db, record, 2, row[2]) wgdb.set_field(db, record, 3, row[3]) wgdb.set_field(db, record, 4, row[4]) for i in range(0,20): start = time.clock(); runTest(); print time.clock() - start wgdb.detach_database(db); wgdb.delete_database("1");
import time import wgdb field_count = 100 def runTest(): record = wgdb.get_first_record(db); while record is not None: for j in range(0,field_count): wgdb.set_field(db, record, j, j + 1); try: record = wgdb.get_next_record(db, record); except wgdb.error: record = None db = wgdb.attach_database("1", 1073741824); for x in range(0, 680000): record = wgdb.create_record(db, field_count) for j in range(0,field_count): wgdb.set_field(db, record, j, j) for i in range(0,20): start = time.clock(); runTest(); print time.clock() - start wgdb.detach_database(db); wgdb.delete_database("1");
def test_field_data(self): """Tests field data encoding and decoding.""" # BLOBTYPE not supported yet #blob = "\045\120\104\106\055\061\056\065\012\045"\ # "\265\355\256\373\012\063\040\060\040\157\142\152\012\074\074"\ # "\040\057\114\145\156\147\164\150\040\064\040\060\040\122\012"\ # "\040\040\040\057\106\151\154\164\145\162\040\057\106\154\141"\ # "\164\145\104\145\143\157\144\145\012\076\076\012\163\164\162"\ # "\145\141\155\012\170\234\255\227\333\152\334\060\020\100\337"\ # "\375\025\372\001\357\152\106\067\013\312\076\024\112\041\120"\ # "\150\132\103\037\102\036\366\032\010\064\064\027\350\357\167"\ # "\106\222\327\326\156\222\125\152\141\144\153\155\315\350\150"\ # "\146\064\243\175\154\224\025\316\130\141\264\024\255\103\051"\ # "\236\366\342\227\170\150\100\360\365\164\047\226\153\051\356" s1 = "Qly9y63M84Qly9y63M84Qly9y63M84Qly9y63M84Qly9y63M84Qly9y63M84" s2 = "2O15At13Iu" s3 = "A Test String" s4 = "#testobject" s5 = "http://example.com/testns" s6 = "9091270" s7 = "xsd:integer" rec = wgdb.create_record(self.d, 16) # BLOBTYPE not supported yet #wgdb.set_field(self.d, rec, 0, blob, wgdb.BLOBTYPE, "blob.pdf") #val = wgdb.get_field(self.d, rec, 0) #self.assertEqual(type(val), type(())) #self.assertEqual(len(val), 3) #self.assertEqual(val[0], blob) #self.assertEqual(val[1], wgdb.BLOBTYPE) #self.assertEqual(val[2], "blob.pdf") # CHARTYPE wgdb.set_field(self.d, rec, 1, "c", wgdb.CHARTYPE) val = wgdb.get_field(self.d, rec, 1) self.assertEqual(val, "c") # DATETYPE wgdb.set_field(self.d, rec, 2, datetime.date(2040, 7, 24)) val = wgdb.get_field(self.d, rec, 2) self.assertTrue(isinstance(val, datetime.date)) self.assertEqual(val.day, 24) self.assertEqual(val.month, 7) self.assertEqual(val.year, 2040) # DOUBLETYPE wgdb.set_field(self.d, rec, 3, -0.94794830) val = wgdb.get_field(self.d, rec, 3) self.assertAlmostEqual(val, -0.94794830) # FIXPOINTTYPE wgdb.set_field(self.d, rec, 4, 549.8390, wgdb.FIXPOINTTYPE) val = wgdb.get_field(self.d, rec, 4) self.assertEqual(val, 549.8390) # INTTYPE wgdb.set_field(self.d, rec, 5, 2073741877) val = wgdb.get_field(self.d, rec, 5) self.assertEqual(val, 2073741877) wgdb.set_field(self.d, rec, 6, -10) val = wgdb.get_field(self.d, rec, 6) self.assertEqual(val, -10) # NULLTYPE wgdb.set_field(self.d, rec, 7, None) val = wgdb.get_field(self.d, rec, 7) self.assertIsNone(val) # RECORDTYPE rec2 = wgdb.create_record(self.d, 1) wgdb.set_field(self.d, rec, 8, rec2) wgdb.set_field(self.d, rec2, 0, 30755904) val = wgdb.get_field(self.d, rec, 8) self.assertTrue(wgdb.is_record(val)) self.assertEqual(wgdb.get_field(self.d, val, 0), 30755904) # STRTYPE wgdb.set_field(self.d, rec, 9, s1) val = wgdb.get_field(self.d, rec, 9) self.assertEqual(val, s1) wgdb.set_field(self.d, rec, 10, s2, wgdb.STRTYPE) val = wgdb.get_field(self.d, rec, 10) self.assertEqual(val, s2) # extra string not supported yet #wgdb.set_field(self.d, rec, 11, s3, ext_str="en") #val = wgdb.get_field(self.d, rec, 11) #self.assertEqual(val, s3) # TIMETYPE wgdb.set_field(self.d, rec, 12, datetime.time(23, 44, 6)) val = wgdb.get_field(self.d, rec, 12) self.assertTrue(isinstance(val, datetime.time)) self.assertEqual(val.hour, 23) self.assertEqual(val.minute, 44) self.assertEqual(val.second, 6) # URITYPE wgdb.set_field(self.d, rec, 13, s4, wgdb.URITYPE, s5) val = wgdb.get_field(self.d, rec, 13) self.assertEqual(val, s5 + s4) # XMLLITERALTYPE wgdb.set_field(self.d, rec, 14, s6, wgdb.XMLLITERALTYPE, s7) val = wgdb.get_field(self.d, rec, 14) self.assertEqual(val, s6) # VARTYPE # when decoded, a tuple is returned that contains the # value and database (kind of a representation of vartype). wgdb.set_field(self.d, rec, 15, 2, wgdb.VARTYPE) val = wgdb.get_field(self.d, rec, 15) self.assertEqual(type(val), type(())) self.assertEqual(len(val), 2) self.assertEqual(val[0], 2) self.assertEqual(val[1], wgdb.VARTYPE)
def test_params(self): """Test encoding parameters""" marker = "This is a marker" s1 = "ctGXioJeeUkTrxiSGaWxqFujCyWHJkmveMQXEnrHAMomjuPjKqUHlUtCVjOT" s2 = "zjXNNGYUBjmdCrLaAaKv" s3 = "GRvWOVYBMObOzWPqVFCt" s4 = "#eNijRGUJbuHoJEMxRUCQ" s5 = "http://example.com/?UQCOtBzWkdipHplZqwQF" s6 = "KqKVvVhVcxbLssirtydJ" s7 = "xsd:Name" # this row shouldn't be returned by the queries (except # the NULL query) rec0 = wgdb.create_record(self.d, 15) wgdb.set_field(self.d, rec0, 0, "This is not a marker") # this row should be returned by the queries rec = wgdb.create_record(self.d, 15) wgdb.set_field(self.d, rec, 0, marker) # CHARTYPE wgdb.set_field(self.d, rec, 1, "Z", wgdb.CHARTYPE) # DATETYPE wgdb.set_field(self.d, rec, 2, datetime.date(1943, 2, 28)) # DOUBLETYPE wgdb.set_field(self.d, rec, 3, 105819.387451) # FIXPOINTTYPE wgdb.set_field(self.d, rec, 4, 783.799, wgdb.FIXPOINTTYPE) # INTTYPE wgdb.set_field(self.d, rec, 5, -871043) # NULLTYPE wgdb.set_field(self.d, rec, 7, None) # RECORDTYPE wgdb.set_field(self.d, rec, 8, rec0) # STRTYPE wgdb.set_field(self.d, rec, 9, s1) wgdb.set_field(self.d, rec, 10, s2, wgdb.STRTYPE) wgdb.set_field(self.d, rec, 11, s3, ext_str="en") # TIMETYPE wgdb.set_field(self.d, rec, 12, datetime.time(11, 22, 33)) # URITYPE wgdb.set_field(self.d, rec, 13, s4, wgdb.URITYPE, s5) # XMLLITERALTYPE wgdb.set_field(self.d, rec, 14, s6, wgdb.XMLLITERALTYPE, s7) # CHARTYPE query = wgdb.make_query(self.d, arglist = [(1, wgdb.COND_EQUAL, ("Z", wgdb.CHARTYPE))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # DATETYPE query = wgdb.make_query(self.d, arglist = [(2, wgdb.COND_EQUAL, datetime.date(1943, 2, 28))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # DOUBLETYPE query = wgdb.make_query(self.d, arglist = [(3, wgdb.COND_EQUAL, 105819.387451)]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # FIXPOINTTYPE query = wgdb.make_query(self.d, arglist = [(4, wgdb.COND_EQUAL, (783.799, wgdb.FIXPOINTTYPE))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # INTTYPE query = wgdb.make_query(self.d, arglist = [(5, wgdb.COND_EQUAL, -871043)]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # NULLTYPE query = wgdb.make_query(self.d, arglist = [(7, wgdb.COND_EQUAL, None)]) self.assertEqual(query.res_count, 2) self.assertIsNotNone(self.fetch(query)) self.assertIsNotNone(self.fetch(query)) self.assertIsNone(self.fetch(query)) # RECORDTYPE query = wgdb.make_query(self.d, arglist = [(8, wgdb.COND_EQUAL, rec0)]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # STRTYPE query = wgdb.make_query(self.d, arglist = [(9, wgdb.COND_EQUAL, s1)]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) query = wgdb.make_query(self.d, arglist = [(10, wgdb.COND_EQUAL, (s2, wgdb.STRTYPE))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) query = wgdb.make_query(self.d, arglist = [(11, wgdb.COND_EQUAL, (s3, wgdb.STRTYPE, "en"))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # TIMETYPE query = wgdb.make_query(self.d, arglist = [(12, wgdb.COND_EQUAL, datetime.time(11, 22, 33))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # URITYPE query = wgdb.make_query(self.d, arglist = [(13, wgdb.COND_EQUAL, (s4, wgdb.URITYPE, s5))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query)) # XMLLITERALTYPE query = wgdb.make_query(self.d, arglist = [(14, wgdb.COND_EQUAL, (s6, wgdb.XMLLITERALTYPE, s7))]) self.assertEqual(query.res_count, 1) rec = self.fetch(query) self.assertEqual(wgdb.get_field(self.d, rec, 0), marker) self.assertIsNone(self.fetch(query))