<commit_before>package com.decentralizeddatabase.reno;

import java.io.File;
import java.util.*;

import com.decentralizeddatabase.errors.BadRequest;
import com.decentralizeddatabase.errors.EncryptionError;
import com.decentralizeddatabase.utils.DecentralizedDBRequest;
import com.decentralizeddatabase.utils.DecentralizedDBResponse;
import com.decentralizeddatabase.utils.FileBlock;

import java.util.ArrayList;
import java.util.List;

import static com.decentralizeddatabase.utils.Constants.*;

public class Reno {

    private final CryptoBlock cryptoBlock;

    public Reno() throws EncryptionError {
	try {
	    this.cryptoBlock = new CryptoBlock();
	} catch (Exception e) {
	    throw new EncryptionError(500, "There has been an error initializing our encryption scheme");
	}
    }

    public void listAll(final DecentralizedDBRequest request,
			final DecentralizedDBResponse response) throws BadRequest {

    }

    public void read(final DecentralizedDBRequest request,
		     final DecentralizedDBResponse response) throws BadRequest, EncryptionError {
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<String> keys = createKeys(secretKey, filename, user, 4); //TODO: use multimap to get #blocks)
        List<FileBlock> blocks = retrieve(keys);
        String file = makeFile(blocks, secretKey);

        response.setData(file);
    }

    public void write(final DecentralizedDBRequest request,
		      final DecentralizedDBResponse response) throws BadRequest, EncryptionError {
        final String file = request.getFile();
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<FileBlock> blocks = createBlocks(secretKey, file);
        final int numBlocks = blocks.size();
        final List<String> keys = createKeys(secretKey, filename, user, numBlocks);

        send(blocks, keys);

        // TODO: After successful request, save new filename
    }

    public void delete(final DecentralizedDBRequest request,
		       final DecentralizedDBResponse response) throws BadRequest {
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<String> blockKeys = createKeys(secretKey, filename, user, (int) NUM_BLOCKS);

        //TODO: List of keys for blocks created, need to send to jailcell to retrieve blocks
        send(blockKeys);

    }

    private List<FileBlock> createBlocks(final String secretKey, final String file) throws EncryptionError {
        final List<FileBlock> blocks = new ArrayList<>();

        final List<byte[]> splitFiles = DataManipulator.breakFile(file);
        final int numBlocks = splitFiles.size();
        final List<Long> blockOrders = DataManipulator.getBlockOrderValues(numBlocks);

        final Iterator<byte[]> it1 = splitFiles.iterator();
        final Iterator<Long> it2 = blockOrders.iterator();

        while (it1.hasNext() && it2.hasNext()) {
            byte[] encrypted = null;
            try {<commit_msg>Add file table<commit_after>package com.decentralizeddatabase.reno;

import java.io.File;
import java.util.*;

import com.decentralizeddatabase.errors.BadRequest;
import com.decentralizeddatabase.errors.EncryptionError;
import com.decentralizeddatabase.utils.DecentralizedDBRequest;
import com.decentralizeddatabase.utils.DecentralizedDBResponse;
import com.decentralizeddatabase.utils.FileBlock;

import java.util.ArrayList;
import java.util.List;

import static com.decentralizeddatabase.utils.Constants.*;

public class Reno {

    private final CryptoBlock cryptoBlock;
    private final FileTable fileTable;

    public Reno() throws EncryptionError {
	try {
	    this.cryptoBlock = new CryptoBlock();
	} catch (Exception e) {
	    throw new EncryptionError(500, "There has been an error initializing our encryption scheme");
	}
	this.fileTable = new FileTable();
    }

    public void listAll(final DecentralizedDBRequest request,
			final DecentralizedDBResponse response) throws BadRequest {

    }

    public void read(final DecentralizedDBRequest request,
		     final DecentralizedDBResponse response) throws BadRequest, EncryptionError {
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<String> keys = createKeys(secretKey, filename, user, 4); //TODO: use multimap to get #blocks)
        List<FileBlock> blocks = retrieve(keys);
        String file = makeFile(blocks, secretKey);

        response.setData(file);
    }

    public void write(final DecentralizedDBRequest request,
		      final DecentralizedDBResponse response) throws BadRequest, EncryptionError {
        final String file = request.getFile();
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<FileBlock> blocks = createBlocks(secretKey, file);
        final int numBlocks = blocks.size();
        final List<String> keys = createKeys(secretKey, filename, user, numBlocks);

        send(blocks, keys);

	fileTable.addFile(user, filename, numBlocks);	
    }

    public void delete(final DecentralizedDBRequest request,
		       final DecentralizedDBResponse response) throws BadRequest {
        final String filename = request.getFilename();
        final String user = request.getUser();
        final String secretKey = request.getSecretKey();

        final List<String> blockKeys = createKeys(secretKey, filename, user, (int) NUM_BLOCKS);

        //TODO: List of keys for blocks created, need to send to jailcell to retrieve blocks
        send(blockKeys);

	fileTable.removeFile(user, filename);
    }

    private List<FileBlock> createBlocks(final String secretKey, final String file) throws EncryptionError {
        final List<FileBlock> blocks = new ArrayList<>();

        final List<byte[]> splitFiles = DataManipulator.breakFile(file);
        final int numBlocks = splitFiles.size();
        final List<Long> blockOrders = DataManipulator.getBlockOrderValues(numBlocks);

        final Iterator<byte[]> it1 = splitFiles.iterator();
        final Iterator<Long> it2 = blockOrders.iterator();

        while (it1.hasNext() && it2.hasNext()) {
            byte[] encrypted = null;
            try {
{'fraction_non_alphanumeric': 0.08763432722869684, 'fraction_numerical': 0.0024216739821401544, 'mean_word_length': 3.5984690327070283, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '526961', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1716, 'n_words': 501}