<commit_before>package de.fernunihagen.dna.jkn.scalephant.network.packages.request;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fernunihagen.dna.jkn.scalephant.network.NetworkConst;
import de.fernunihagen.dna.jkn.scalephant.network.NetworkPackageDecoder;
import de.fernunihagen.dna.jkn.scalephant.network.NetworkPackageEncoder;
import de.fernunihagen.dna.jkn.scalephant.network.packages.NetworkRequestPackage;
import de.fernunihagen.dna.jkn.scalephant.storage.BoundingBox;
import de.fernunihagen.dna.jkn.scalephant.storage.sstable.SSTableHelper;

public class InsertTupleRequest implements NetworkRequestPackage {

	/**
	 * The name of the table
	 */
	protected final String table;
	
	/**
	 * The key to insert
	 */
	protected final String key;
	
	/**
	 * The timestamp of the tuple
	 */
	protected final long timestamp;
	
	/**
	 * The bounding box of the tuple
	 */
	protected final BoundingBox bbox;
	
	/**
	 * The data 
	 */
	protected final byte[] data;
	
	/**
	 * The Logger
	 */
	private final static Logger logger = LoggerFactory.getLogger(InsertTupleRequest.class);
	
	
	/**
	 * Create package from parameter
	 * 
	 * @param table
	 * @param key
	 * @param timestamp
	 * @param bbox
	 * @param data
	 */
	public InsertTupleRequest(final String table, final String key, final long timestamp,
			final BoundingBox bbox, final byte[] data) {
		this.table = table;
		this.key = key;
		this.timestamp = timestamp;
		this.bbox = bbox;
		this.data = data;
	}
	
	/**
	 * Decode the encoded tuple into a object
	 * 
	 * @param encodedPackage
	 * @return
	 */
	public static InsertTupleRequest decodeTuple(final ByteBuffer encodedPackage) {

		final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_INSERT_TUPLE);
		
		if(decodeResult == false) {
			logger.warn("Unable to decode package");
			return null;
		}
		
		short tableLength = encodedPackage.getShort();
		short keyLength = encodedPackage.getShort();
		int bBoxLength = encodedPackage.getInt();
		int dataLength = encodedPackage.getInt();
		long timestamp = encodedPackage.getLong();
		
		final byte[] tableBytes = new byte[tableLength];
		encodedPackage.get(tableBytes, 0, tableBytes.length);
		final String table = new String(tableBytes);
		
		final byte[] keyBytes = new byte[keyLength];
		encodedPackage.get(keyBytes, 0, keyBytes.length);
		final String key = new String(keyBytes);
		
		final byte[] boxBytes = new byte[bBoxLength];
		encodedPackage.get(boxBytes, 0, boxBytes.length);

		final byte[] dataBytes = new byte[dataLength];
		encodedPackage.get(dataBytes, 0, dataBytes.length);

		if(encodedPackage.remaining() != 0) {
			logger.error("Some bytes are left after encoding: " + encodedPackage.remaining());
		}
		
		final long[] longArray = SSTableHelper.readLongArrayFromByte(boxBytes);
		final BoundingBox boundingBox = new BoundingBox(longArray);

		return new InsertTupleRequest(table, key, timestamp, boundingBox, dataBytes);
	}
	
	/**
	 * Check validity of the entries
	 * @return
	 */
	protected boolean isValied() {
		if(table.getBytes().length > 16) {
			logger.warn("Tablename to long: " + table);
			return false;
		}
		
		return true;
	}

	@Override
	public byte[] getByteArray(final short sequenceNumber) {
		
		final NetworkPackageEncoder networkPackageEncoder 
			= new NetworkPackageEncoder();
		
		final ByteArrayOutputStream bos = networkPackageEncoder.getOutputStreamForRequestPackage(sequenceNumber, getPackageType());
		
		try {
			final byte[] tableBytes = table.getBytes();
			final byte[] keyBytes = key.getBytes();<commit_msg>Removed unused method<commit_after>package de.fernunihagen.dna.jkn.scalephant.network.packages.request;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fernunihagen.dna.jkn.scalephant.network.NetworkConst;
import de.fernunihagen.dna.jkn.scalephant.network.NetworkPackageDecoder;
import de.fernunihagen.dna.jkn.scalephant.network.NetworkPackageEncoder;
import de.fernunihagen.dna.jkn.scalephant.network.packages.NetworkRequestPackage;
import de.fernunihagen.dna.jkn.scalephant.storage.BoundingBox;
import de.fernunihagen.dna.jkn.scalephant.storage.sstable.SSTableHelper;

public class InsertTupleRequest implements NetworkRequestPackage {

	/**
	 * The name of the table
	 */
	protected final String table;
	
	/**
	 * The key to insert
	 */
	protected final String key;
	
	/**
	 * The timestamp of the tuple
	 */
	protected final long timestamp;
	
	/**
	 * The bounding box of the tuple
	 */
	protected final BoundingBox bbox;
	
	/**
	 * The data 
	 */
	protected final byte[] data;
	
	/**
	 * The Logger
	 */
	private final static Logger logger = LoggerFactory.getLogger(InsertTupleRequest.class);
	
	
	/**
	 * Create package from parameter
	 * 
	 * @param table
	 * @param key
	 * @param timestamp
	 * @param bbox
	 * @param data
	 */
	public InsertTupleRequest(final String table, final String key, final long timestamp,
			final BoundingBox bbox, final byte[] data) {
		this.table = table;
		this.key = key;
		this.timestamp = timestamp;
		this.bbox = bbox;
		this.data = data;
	}
	
	/**
	 * Decode the encoded tuple into a object
	 * 
	 * @param encodedPackage
	 * @return
	 */
	public static InsertTupleRequest decodeTuple(final ByteBuffer encodedPackage) {

		final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_INSERT_TUPLE);
		
		if(decodeResult == false) {
			logger.warn("Unable to decode package");
			return null;
		}
		
		short tableLength = encodedPackage.getShort();
		short keyLength = encodedPackage.getShort();
		int bBoxLength = encodedPackage.getInt();
		int dataLength = encodedPackage.getInt();
		long timestamp = encodedPackage.getLong();
		
		final byte[] tableBytes = new byte[tableLength];
		encodedPackage.get(tableBytes, 0, tableBytes.length);
		final String table = new String(tableBytes);
		
		final byte[] keyBytes = new byte[keyLength];
		encodedPackage.get(keyBytes, 0, keyBytes.length);
		final String key = new String(keyBytes);
		
		final byte[] boxBytes = new byte[bBoxLength];
		encodedPackage.get(boxBytes, 0, boxBytes.length);

		final byte[] dataBytes = new byte[dataLength];
		encodedPackage.get(dataBytes, 0, dataBytes.length);

		if(encodedPackage.remaining() != 0) {
			logger.error("Some bytes are left after encoding: " + encodedPackage.remaining());
		}
		
		final long[] longArray = SSTableHelper.readLongArrayFromByte(boxBytes);
		final BoundingBox boundingBox = new BoundingBox(longArray);

		return new InsertTupleRequest(table, key, timestamp, boundingBox, dataBytes);
	}

	@Override
	public byte[] getByteArray(final short sequenceNumber) {
		
		final NetworkPackageEncoder networkPackageEncoder 
			= new NetworkPackageEncoder();
		
		final ByteArrayOutputStream bos = networkPackageEncoder.getOutputStreamForRequestPackage(sequenceNumber, getPackageType());
		
		try {
			final byte[] tableBytes = table.getBytes();
			final byte[] keyBytes = key.getBytes();
{'fraction_non_alphanumeric': 0.10236979884265637, 'fraction_numerical': 0.002204464039680353, 'mean_word_length': 4.393016344725112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4675106', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2096, 'n_words': 534}