import	time
import	btree							# see: https://docs.micropython.org/en/latest/library/btree.html

def test1():
    t1 = time.ticks_ms()
    f = open("data.btr", "w+b")
#    try   : f = open("data.btr", "r+b")	# see https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-the-built-in-open-function
#    except: f = open("data.btr", "w+b")
    db = btree.open(f)
    t2 = time.ticks_ms()
    for idx in range(2000):
        db[idx.to_bytes(2, 'big')] = b'\x00'
    t3 = time.ticks_ms()
    db.flush()
    t4 = time.ticks_ms()
    db.close()
    t5 = time.ticks_ms()
    print(f'total: {t5-t1}, open: {t2-t1}, populate: {t3-t2}, flush: {t4-t3}, close: {t5-t4}')
          
          
def test2():
    f = open("data.btr", "w+b")
    db = btree.open(f)
    db[b'\x00'] = b'\x00'
    db[b'\x80'] = b'\x02'
    db['0'] = b'\x05'
    for k, v in db.items():
        print(k, v)
    print()
    for k, v in db.items(bytes([0x30]), bytes([0x40])):
        print(list(k), v)
    db.flush()
    db.close()


test1()
test1()
#test2()
