def create_index(self, field_to_index: str) -> None: index_tree = BPlusTree( f'db_files/{self.name}_order_by_{field_to_index}.db', order=50) db_data = read_from_json("db_files/db.json") if field_to_index in db_data[self.name]['indexes']: return num_of_files = db_data[self.name]['num of files'] primary_key = db_data[self.name]['primary key'] if primary_key == field_to_index: for file_num in range(num_of_files): path = f"db_files/{self.name}_{file_num + 1}.json" file_data = read_from_json(path) for k in file_data.keys(): print(k) index_tree[k] = path else: for file_num in range(num_of_files): path = f"db_files/{self.name}_{file_num + 1}.json" file_data = read_from_json(path) for v in file_data.values(): print(v) index_tree[v[field_to_index]] = path index_tree.close() db_data[self.name]['indexes'] += field_to_index
def main(): tree = BPlusTree('/tmp/bplustree.db', order=50) tree[1] = b'foo' tree[2] = b'bar' print(tree.get(3), tree[1], list(tree.keys())) tree.close()
def __init__(self, disk_name="Heap.cbd", indexBy=[], indexBTree=True, indexBPlusTree=False): maxDegreeBTree = 10 self.r_block = Block(disk_name) self.w_block = Block(disk_name) self.indexes = {} self.indexBTree = indexBTree self.indexBPlusTree = indexBPlusTree for i in indexBy: if indexBPlusTree: self.indexes.update({i:BPlusTree(getcwd()+"\\"+i+".index",serializer=StrSerializer(),key_size=128)}) else: if indexBTree: self.indexes.update({i:BTree(maxDegreeBTree)}) else: self.indexes.update({i:{}})
def demo(): bplustree = BPlusTree() print('read file ./partfile.txt') keys = [] with open("partfile.txt", 'rb') as reader: bplustree.readfile(reader) with open("partfile.txt", 'r') as reader: for line in reader: s = line.split(maxsplit=1) keys += [s[0]] for i in range(len(keys)): bplustree.delete(keys[i]) if i % 1000 == 0: print('Delete ' + keys[i] + ' item')
#!/usr/bin/env python # -*- coding: utf-8 -*- """NoSQL database written in Python""" # Standard library imports import socket import time import utils as u u.prepareCryptTable() # import b_plus_tree as bt from bplustree import BPlusTree tree = BPlusTree('./test.db', order=50) HOST = 'localhost' PORT = 50507 SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM) STATS = { 'PUT': { 'success': 0, 'error': 0 }, 'GET': { 'success': 0, 'error': 1 }, 'GETLIST': { 'success': 0, 'error': 0 }, 'PUTLIST': {
# ### 0. Tree 생성 # In[1]: from bplustree import BPlusTree from bplustree.serializer import (IntSerializer, StrSerializer, UUIDSerializer, DatetimeUTCSerializer) import os if os.path.exists("database/homework03.db"): os.remove("database/homework03.db") if os.path.exists("database/homework03.db-wal"): os.remove("database/homework03.db-wal") tree = BPlusTree('database/homework03.db', key_size=32, order=5, serializer=StrSerializer()) tree # ### 1. Insert: key, value 쌍을 tree index에 삽입 # * 삽입 과정에서 leaf node와 parent node의 split을 확인할 수 있다. # In[2]: tree.insert('a', b'1') tree.insert('b', b'2') tree.insert('c', b'3') tree.insert('d', b'4') tree.display() tree.insert('e', b'5')
def setUp(self) -> None: self.bplustree = BPlusTree() self.random_list = random.sample(range(1, 100), 20) for i in self.random_list: self.bplustree[i] = 'test' + str(i) random.shuffle(self.random_list)
class TestBPlusTree(TestCase): def setUp(self) -> None: self.bplustree = BPlusTree() self.random_list = random.sample(range(1, 100), 20) for i in self.random_list: self.bplustree[i] = 'test' + str(i) random.shuffle(self.random_list) def test_query(self): for i in self.random_list: self.assertEqual(self.bplustree.query(i), 'test' + str(i)) def test_change(self): self.assertTrue(self.bplustree.change(self.random_list[0], 'try')) self.assertEqual(self.bplustree.query(self.random_list[0]), 'try') def test_insert(self): self.bplustree.show() i = random.randint(1, 100) print('Insert ' + str(i)) self.bplustree[i] = 'test' + str(i) self.bplustree.show() def test_delete(self): print(self.random_list) for i in self.random_list: print('Delete ' + str(i)) self.bplustree.delete(i) self.bplustree.show()
def demo_bplustree(): print('Initializing B+ tree...') bplustree = BPlusTree() print('\nB+ tree with 1 item...') bplustree.insert('a', 'alpha') bplustree.show() print('\nB+ tree with 2 items...') bplustree.insert('b', 'bravo') bplustree.show() print('\nB+ tree with 3 items...') bplustree.insert('c', 'charlie') bplustree.show() print('\nB+ tree with 4 items...') bplustree.insert('d', 'delta') bplustree.show() print('\nB+ tree with 5 items...') bplustree.insert('e', 'echo') bplustree.show() for i in range(20): print('\nB+ tree with ' + str(i + 5) + ' items...') bplustree.insert(str(i), 'foxtrot' + str(i)) bplustree.show() print('\nRetrieving values with key e...') print(bplustree.query('e'))
from flask import Flask, render_template, request from bplustree import BPlusTree app = Flask(__name__) bplustree = BPlusTree() app.config['Save_Path'] = './partfile.txt' @app.route('/') def initialization(): with open(app.config['Save_Path'], 'rb') as reader: bplustree.readfile(reader) return render_template('index.html', data=display_the_next_10_parts( bplustree.leftmost_leaf()), output=bplustree.output()) @app.route("/transaction", methods=["POST"]) def transaction(): button = request.form['submit_button'] key = str(request.form["key"]) print(button, key) if button == 'query': leaf = bplustree.find(key) if key not in leaf.keys: return render_template('index.html', key=key,