Example #1
0
 def test_find():
     query = db.makeRecord("20 ROOT ROOT 2000 24")
     for i in db._attribute:
         indexname = i + '_' + name + '.json'
         res = db.find(query[i], indexname)
         if not res == None:
             lines = db.retrieve(res[0].keys[res[1]], database)
             print ('Records found in ' + indexname)
             db.printRecords(lines)
Example #2
0
def full_testbench(name, amount):
    """ This testbench tests the full functionality of the b-tree database and its relying functions

    :param name: Name of the database
    :type name: string
    :param amount: Amount of records that will be created in database
    :type amount: integer

    .. note::
        This function is fully commented in the source. click [source] on the right
    """
    database = name + '.db'

    # CreateDatabase
    db.createDatabase(database,amount)

    # For the time i use padding to store the nodes
    # in the file although its a bad technique
    # here i set the db.SIZE_SMALL and db.SIZE_BIG according to
    # the amount of records in the database
    # db.SIZE_SMALL = 200 + amount//1000
    # db.SIZE_BIG = 1000 + amount

    # CreatePrimaryIndex and CreateSecondary Index
    for i in db._attribute:
        indexname = i + '_' + name + '.json'
        x = db.createIndex(database,indexname, i)

    # Insert record in database
    record = db.makeRecord("10070 seimenis souloumenis 50 80")
    num = db.insertInDatabase(record, database)

    # Insert record in database with duplicate primary key
    record_error = db.makeRecord("20 error error 2 20")
    num_error = db.insertInDatabase(record_error, database)                   # ERROR 1

    # Insert
    if num != -1:
        for i in db._attribute:
            indexname = i + '_' + name + '.json'
            db.insertInIndex([record[i], num], indexname)
            db.insertInIndex([record_error[i], num_error], indexname)

    # All of the following function can be called in order to test their
    # functionality

    # Test function for finding in Primary and Secondary key indexes
    def test_find():
        query = db.makeRecord("20 ROOT ROOT 2000 24")
        for i in db._attribute:
            indexname = i + '_' + name + '.json'
            res = db.find(query[i], indexname)
            if not res == None:
                lines = db.retrieve(res[0].keys[res[1]], database)
                print ('Records found in ' + indexname)
                db.printRecords(lines)

    # Test function for finding in Primary and Secondary key indexes with range find
    def test_rangefind():
        range_query = {'ID':(0,10),
                       'surname':('pelopiou', 'ROOT'),
                       'name':('john', 'sotiris'),
                       'year':(2000, 2005),
                       'age':(20, 21)}

        for i in db._attribute:
            indexname = i + '_' + name + '.json'
            key1 = range_query[i][0]
            key2 = range_query[i][1]
            list = db.findRange(key1, key2, indexname)

            (MIN, MAX) = (key1, key2)
            if (key1>key2): (MIN, MAX) = (key2, key1)

            if not list == None:
                print ('--------------RECORDS FOUND IN ' + indexname + ' IN RANGE \'' + str(MIN) + '\', \'' + str(MAX) + '\'--------------')
                for i in list:
                    lines = db.retrieve(i[0].keys[i[1]], database)
                    db.printRecords(lines)

    # Test function for deleting in Primary and Secondary key indexes
    def test_delete():
        # Deletes a record from the primary index
        indexname = 'ID' + '_' + name + '.json'
        db.deleteFromIndex(indexname, 5)
        db.deleteFromIndex(indexname, 1000)          # this does not exist   # ERROR 2

        # Deletes a record from the secondary index
        indexname = 'name' + '_' + name + '.json'
        db.deleteFromIndex(indexname, 1, 'seimenis') # this does not exist   # ERROR 3
        db.deleteFromIndex(indexname, 5070, 'souloumeni')
        db.deleteFromIndex(indexname, 5, 'hadfadd')

        # deletes completely the primary key from all the indexes of the given database
        db.delete(5070, database)

    #test_find()
    #test_rangefind()
    #test_delete()

    print_all(name)