分布式冗余存储系统,PyCharm Community Edition 2024.2.1,python版

分布式冗余存储系统,PyCharm Community Edition 2024.2.1,python版

主函数

import look
import storage
import read
import remove
print("这是一个文件分布式冗余存储的程序:\n存文件输入1,取文件输入2,查看输入3,删除输入4,退出程序输入0\n")
while True:
    print("\n\n")
    print("您想进行什么操作?")
    a = int(input())

    if a == 0:
        print("下次再见!")
        break

    if a == 1:
        storage.storage()
    elif a == 2:
        read.read()
    elif a == 3:
        look.look()
    elif a == 4:
        remove.dismiss()
    else:
        print("不是哥们")
        print("存文件输入1,取文件输入2,查看输入3,删除输入4,退出程序输入0")

存储模块

import os
import json
import random
import time

# Constants
KID = 16777216  # 16MB

def generate_unique_random(data):
    while True:
        random_num = str(random.randint(0, 2023011000))
        if not is_random_in_filenames(data, random_num):
            return random_num

def get_file_suffix(filename):
    return os.path.splitext(filename)[1]

def is_random_in_filenames(data, random_str):
    for file_data in data.values():
        for paths in file_data.values():
            for path in paths:
                if os.path.splitext(os.path.basename(path))[0] == random_str:
                    return True
    return False

def confirm_location(input_str, disk0, disk1, disk2):
    locations = [disk0, disk1, disk2]
    selected_locs = random.sample(locations, 2)

    file_name = generate_unique_random({})
    location_str1 = os.path.join(selected_locs[0], f"{file_name}{input_str}")
    location_str2 = os.path.join(selected_locs[1], f"{file_name}{input_str}")

    return location_str1, location_str2

def save_json(data):
    with open("save.json", "w", encoding="utf-8") as file:
        json.dump(data, file, indent=4, ensure_ascii=False)  # Ensure proper encoding

def load_json():
    if os.path.exists("save.json"):
        with open("save.json", "r", encoding="utf-8") as file:
            return json.load(file)
    else:
        return {}

def storage():
    # Load or initialize JSON data
    data = load_json()

    # Input disk locations
    disk0 = input("请确定存储位置(1/3),以斜杠结尾:").strip()
    disk1 = input("请确定存储位置(2/3),以斜杠结尾:").strip()
    disk2 = input("请确定存储位置(3/3),以斜杠结尾:").strip()

    while True:
        file_location = input("输入文件地址(输入#退出): ").strip()
        if file_location == '#':
            break

        if not os.path.exists(file_location):
            print("文件不存在,请重试!")
            continue

        # Get file suffix
        suffix = get_file_suffix(file_location)
        print(f"文件后缀名为: {suffix}")

        # Get file size
        file_size = os.path.getsize(file_location)
        print(f"文件大小为: {file_size} 字节")

        with open(file_location, "rb") as file:
            buffer_size = KID
            num_chunks = (file_size // buffer_size) + (1 if file_size % buffer_size != 0 else 0)

            big_file = {}
            for i in range(num_chunks):
                chunk_size = buffer_size if i < num_chunks - 1 else file_size % buffer_size
                buffer = file.read(chunk_size)

                # Get storage locations
                c, d = confirm_location(suffix, disk0, disk1, disk2)

                # Save buffer to two locations
                with open(c, "wb") as c_file:
                    c_file.write(buffer)
                with open(d, "wb") as d_file:
                    d_file.write(buffer)

                big_file[str(i)] = [c, d]
                data[file_location] = big_file

            print("存储成功!")

        # Save the updated JSON data
        save_json(data)

if __name__ == "__main__":
    storage()

恢复模块

import os
import json

# Function to load JSON with UTF-8 encoding
def load_json():
    if os.path.exists("save.json"):
        with open("save.json", "r", encoding="utf-8") as file:
            return json.load(file)
    else:
        return {}

# Function to save JSON with UTF-8 encoding
def save_json(data):
    with open("save.json", "w", encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

def get_file_name(file_path):
    """Extract the filename from the full path."""
    return os.path.basename(file_path)


def write_to_file(root, big_file, file_location):
    """Write the content of the files listed in the JSON object to the specified output file."""
    if file_location not in root:
        print(f"文件地址 {file_location} json里没有")
        return

    file_data = root[file_location]
    previous_file_name = None

    # Iterate through each file path in the array
    buffer_size = 16 * 1024 * 1024  # 16 MB buffer size
    buffer = bytearray(buffer_size)

    for path in file_data:
        # Debugging: Print the path
        print(f"正尝试打开: {path}")

        if not os.path.exists(path):
            print(f"文件 {path} 找不到")
            continue

        current_file_name = get_file_name(path)

        # Avoid duplicate file writes
        if current_file_name == previous_file_name:
            continue

        with open(path, 'rb') as input_file:
            while chunk := input_file.read(buffer_size):
                big_file.write(chunk)

        previous_file_name = current_file_name



def read():
    data = load_json()

    if not data:
        print("这里什么也没有")
        return

    while True:
        file_location = input("请输入要恢复的文件原始地址,输入#结束恢复操作:").strip()
        if file_location == "#":
            break

        if file_location not in data:
            print(f"文件{file_location} 无法在记录中找到")
            continue

        save_location = input("请输入恢复文件存储位置,以斜杠结尾:").strip()

        with open(os.path.join(save_location, os.path.basename(file_location)), "wb") as restored_file:
            for chunk_paths in data[file_location].values():
                for path in chunk_paths:
                    if os.path.exists(path):
                        with open(path, "rb") as chunk_file:
                            restored_file.write(chunk_file.read())
                        break  # Only need one copy of the chunk
                    else:
                        print(f"小文件 {path} 找不到了,已跳过")

        print(f"文件 {file_location} 已保存至 {save_location}.")



查看模块

import os
import json

# Function to load JSON with UTF-8 encoding
def load_json():
    if os.path.exists("save.json"):
        with open("save.json", "r", encoding="utf-8") as file:
            return json.load(file)
    else:
        return {}

# Function to save JSON with UTF-8 encoding
def save_json(data):
    with open("save.json", "w", encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=4)


def look():
    data = load_json()

    if not data:
        print("这里什么也没有")
        return

    print("已存储的文件有:")
    for filename, file_data in data.items():
        print(filename)
        # for file_chunks in file_data.values():
        #     for path in file_chunks:
        #         print(path)



    return 0

删除模块

import os
import json

# Function to load JSON with UTF-8 encoding
def load_json():
    if os.path.exists("save.json"):
        with open("save.json", "r", encoding="utf-8") as file:
            return json.load(file)
    else:
        return {}

# Function to save JSON with UTF-8 encoding
def save_json(data):
    with open("save.json", "w", encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

def dismiss():
    data = load_json()

    if not data:
        print("这里什么也没有")
        return

    while True:
        file_location = input("请输入要删除的文件原始地址,输入#结束:").strip()
        if file_location == "#":
            break

        if file_location not in data:
            print(f"文件 {file_location} 没有记录")
            continue

        for file_chunks in data[file_location].values():
            for path in file_chunks:
                try:
                    os.remove(path)
                    print(f"删除{path}")
                except FileNotFoundError:
                    print(f"{path} 没找到,跳过")

        # Remove entry from JSON data
        del data[file_location]

        # Save the updated JSON
        save_json(data)

        print(f"文件{file_location}已从记录里清除.")

没有使用按类封装,其逻辑与C语言版相同,略显繁杂,没有完全发挥python优势。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值