Created
July 20, 2023 15:29
-
-
Save CharlyJazz/e9037f4bf407a592097f9b0393a5ddf5 to your computer and use it in GitHub Desktop.
lol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
import redis | |
import json | |
import logging | |
app = Flask(__name__) | |
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0) | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.INFO) | |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
file_handler = logging.FileHandler('bptree_operations.log') | |
file_handler.setFormatter(formatter) | |
logger.addHandler(file_handler) | |
# Sample B+ tree implementation | |
class BPlusTree: | |
def __init__(self): | |
# Initialize your B+ tree data structure here | |
pass | |
def insert(self, key, value): | |
# Implement B+ tree insert operation here | |
pass | |
def search(self, key): | |
# Implement B+ tree search operation here | |
pass | |
# Helper function to serialize data to JSON and store in Redis | |
def save_to_redis(key, data): | |
serialized_data = json.dumps(data) | |
redis_client.set(key, serialized_data) | |
# Helper function to retrieve data from Redis and deserialize from JSON | |
def load_from_redis(key): | |
serialized_data = redis_client.get(key) | |
if serialized_data: | |
return json.loads(serialized_data) | |
return None | |
@app.route('/insert', methods=['POST']) | |
def insert_data(): | |
data = request.json | |
# Assuming data contains 'key' and 'value' fields | |
key = data.get('key') | |
value = data.get('value') | |
if key is None or value is None: | |
return jsonify({'message': 'Key and Value fields are required'}), 400 | |
# Perform the B+ tree insert operation | |
# bptree.insert(key, value) | |
# Save data to Redis | |
save_to_redis(key, value) | |
# Log the operation | |
logger.info(f"Inserted key: {key}, value: {value}") | |
return jsonify({'message': 'Data inserted successfully'}), 201 | |
@app.route('/search/<key>', methods=['GET']) | |
def search_data(key): | |
# Perform the B+ tree search operation | |
# result = bptree.search(key) | |
# Retrieve data from Redis | |
result = load_from_redis(key) | |
if result: | |
# Log the operation | |
logger.info(f"Data found for key: {key}, value: {result}") | |
return jsonify(result), 200 | |
else: | |
logger.info(f"No data found for key: {key}") | |
return jsonify({'message': 'Data not found'}), 404 | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment