Sunteți pe pagina 1din 20

package com.

assignment3;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

java.io.BufferedReader;
java.io.File;
java.util.Iterator;
java.io.FileInputStream;
java.io.FileNotFoundException;
java.io.FileOutputStream;
java.io.FileReader;
java.io.IOException;
java.nio.channels.FileChannel;
java.nio.file.Files;
java.nio.file.Path;
java.nio.file.StandardCopyOption;
java.util.ArrayList;
java.util.HashMap;
java.util.List;
java.util.Map;
java.util.NavigableSet;
java.util.TreeMap;

import
import
import
import

com.assignment3.ColumnVo;
com.assignment3.DataBuffer;
com.assignment3.FileDataVO;
com.assignment3.MetaData;

public class BTreeTest {


private static String metaDataFilePath = "C:\\Users\\mmarthi.PKRMT\\Desk
top\\pgssp\\asst2\\data\\Newfolder4\\Assignment2eval\\metadata1.txt";
private static String inputFilePath1 = "Data/new1.txt";// "C:\\Users\\mm
arthi.PKRMT\\Desktop\\pgssp\\asst2\\data\\Newfolder4\\Assignment2eval\\input1Tes
t.txt";
private static String inputFilePath2 = "Data/new2.txt";// "C:\\Users\\mm
arthi.PKRMT\\Desktop\\pgssp\\asst2\\data\\Newfolder4\\Assignment2eval\\input2Tes
t.txt";
private static String outputFilePath = "C:\\Users\\mmarthi.PKRMT\\Deskto
p\\pgssp\\asst2\\data\\Newfolder4\\Assignment2eval\\Output1Test.txt";
private static String sortColumns = "col0,col1,col2";
private static String outputColumns = "col0,col1,col2";
private static double xmxSize = 2000;
private static String order = "asc";
private static TupleBTree tree;
private static List<String> sortColumnList = new ArrayList<String>();
private static List<Integer> sortIndexes = new ArrayList<>();
private static List<Integer> outputIndexes = new ArrayList<Integer>();
private static List<String> outputColumnList = new ArrayList<String>();
private
private
private
private
private

static
static
static
static
static

MetaData metaInfo;
String sortedFilesPath = "Sort";
String outputFilesPath = "OutPut";
double numberOfSortFiles = 0;
double bufferSizeFromEachFile = 0;

private static double mainMemoryBuffers = 10;


private static Map<Integer, ArrayList<String>> metaDataMap = new HashMap
<Integer, ArrayList<String>>();;
// function union(Table1,Table2,n,M,type_of_index)

public static void main(String args[]) throws Exception {


// inputFilePath1 = args[0];
// inputFilePath2 = args[1];
// mainMemoryBuffers = Integer.parseInt(args[3]);
String typeOfIndex = "BPlusTre";// args[4];
// tree.insert("abc0,def2,hij1");
// String a = tree.search("abc,def2,hij1");
// String a1 = tree.search("wer");
clearFolders();
if (typeOfIndex == "BPlusTree")
useBtree();
// or
else
useHash();
return;
}
private static void useHash() throws Exception {
sortTree(inputFilePath1);
sortTree(inputFilePath2);
for (int i = 0; i < mainMemoryBuffers; i++) {
metaDataMap.put(i, new ArrayList<String>());
}
// Read M sized input. Apply hash ,goto corresponding bucket.
// if not present ,store in appropriate bucket. else discard.
// after completion, send it to output file.
Double sizeAvailableforInput = (mainMemoryBuffers) * 999 * 1000;
String tempStore;
Double lineNumber = (double) 0;
Double tempsize = (double) 0;
Double outputListSize = (double) 0;
List<String> tempInputList = new ArrayList<String>();
List<String> tempOutputList = new ArrayList<String>();
Boolean inputAEmpty = false;
// Read TableA into inputBuffers and hash
FileReader fileReader1 = new FileReader("Sort/new1.txt");
BufferedReader bufferedReader1 = new BufferedReader(fileReader1)
;
try {
{
while (bufferedReader1.ready()) {
// long fileLength = new System.IO.FileI
nfo("Sort/new" +
// filenameCount.ToString() + ".txt").Le
ngth;
tempStore = bufferedReader1.readLine();
tempInputList.add(tempStore);
}
}
} finally {
if (bufferedReader1 != null)
bufferedReader1.close();
if (fileReader1 != null)
fileReader1.close();
}

if (!(tempInputList.isEmpty())) {
performHash(tempInputList);
tempInputList.clear();
}
// Read tableB into inputBuffers and hash
while (true) {
FileReader fileReader = new FileReader("Sort/new2.txt");
BufferedReader bufferedReader = new BufferedReader(fileR
eader);
try {
{
// traverse to the line
for (double i = 0; i < lineNumber; i++)
{
if (bufferedReader.ready()) {
bufferedReader.readLine(
);
} else
break;
}
if (!(bufferedReader.ready())) {
inputAEmpty = true;
}
while (bufferedReader.ready()) {
// long fileLength = new System.
IO.FileInfo("Sort/new" +
// filenameCount.ToString() + ".
txt").Length;
tempStore = bufferedReader.readL
ine();
if (tempsize + tempStore.getByte
s().length < sizeAvailableforInput) {
tempInputList.add(tempSt
ore);
tempsize = tempsize + te
mpStore.getBytes().length;
lineNumber++;
} else {
break;
}
}
}
} finally {
if (bufferedReader != null)
bufferedReader.close();
if (fileReader != null)
fileReader.close();
}
// PerformHashing on inputFiles
if (!(tempInputList.isEmpty())) {
performHash(tempInputList);
tempInputList.clear();
tempsize = (double) 0;
}
// End of file

if (inputAEmpty) {
break;
}
}
// write to outputfile
File file2 = new File("OutPut/new1.txt");
FileOutputStream fop = new FileOutputStream(file2);
// StreamWriter sw = new StreamWriter("Sort/new"
// + filenameCount + ".txt");
try {
Iterator it = metaDataMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
for (String str : (ArrayList<String>) (pair.getV
alue())) {
// outSize = outSize + str.getBytes().le
ngth;
fop.write(str.getBytes());
fop.write(System.getProperty("line.separ
ator").getBytes());
}
// System.out.println(pair.getKey() + " = " + pa
ir.getValue());
// it.remove(); // avoids a ConcurrentModificati
onException
}
eliminateDuplicates("OutPut/new1.txt");
// sw.WriteLine(item.getLineData());
} finally {
if (fop != null)
fop.close();
}
}
private static void performHash(List<String> tempInputList) throws Excep
tion {
// TODO Auto-generated method stub
// metaDataMap = new HashMap<Integer, ArrayList<String>>();
for (String str : tempInputList) {
Integer hCode = str.hashCode(); // 0,1,2
Integer remiander = (int) (hCode % mainMemoryBuffers);
List<String> tmp = metaDataMap.get(Math.abs(remiander));
// if (!(tmp.contains(str))) {
tmp.add(str);
// }
}
}
private static void useBtree() throws Exception {
sortTree(inputFilePath1);
// Move file1 from sort to output
moveTableAtoOutput();

sortTree(inputFilePath2);
GenerateTreeA();
performUnion();
}
private static void performUnion() throws Exception {
// read TableB into M-1 buffers each of 999kb size .
Double sizeAvailableforInput = (mainMemoryBuffers - 1) * 999 * 1
000;
Double sizeAvailableforOutput = (double) (999 * 1000);
String tempStore;
Double lineNumber = (double) 0;
Double tempsize = (double) 0;
Double outputListSize = (double) 0;
Boolean inputEmpty = false;
List<String> tempInputList = new ArrayList<String>();
List<String> tempOutputList = new ArrayList<String>();
while (true) {
FileReader fileReader = new FileReader("Sort/new2.txt");
BufferedReader bufferedReader = new BufferedReader(fileR
eader);
// fill input buffers
try {
{
// traverse to the line
for (double i = 0; i < lineNumber; i++)
{
if (bufferedReader.ready()) {
bufferedReader.readLine(
);
} else
break;
}
if (!(bufferedReader.ready())) {
inputEmpty = true;
}
while (bufferedReader.ready()) {
// long fileLength = new System.
IO.FileInfo("Sort/new" +
// filenameCount.ToString() + ".
txt").Length;
tempStore = bufferedReader.readL
ine();
if (tempsize + tempStore.getByte
s().length < sizeAvailableforInput) {
tempInputList.add(tempSt
ore);
tempsize = tempsize + te
mpStore.getBytes().length;
lineNumber++;
} else
break;
}
}

} finally {
if (bufferedReader != null)
bufferedReader.close();
if (fileReader != null)
fileReader.close();
}
// check each tuple in the Tree. If present , Ignore. If
not , move
// to Mth Buffer
// Constantly check Mth buffer if it reaches highest.
// If highest is reached, Move to output file.
File file1 = new File("Output/new1.txt");
FileOutputStream outputStream = new FileOutputStream(fil
e1, true);
// FileOutputStream sw = new FileOutputStream(outputFile
Path,
// true);
try {
for (String str : tempInputList) {
// if Mth buffer is full
if (str.getBytes().length + outputListSi
ze >= sizeAvailableforOutput) {
// write data to outputfile + cl
ear output temp list.
for (String string1 : tempOutput
List) {
outputStream.write(strin
g1.getBytes());
outputStream.write(Syste
m.getProperty("line.separator").getBytes());
}
tempOutputList.clear();
outputListSize = (double) 0;
}
String search = tree.search(str);
if (search == null) {
tempOutputList.add(str);
outputListSize = outputListSize
+ str.getBytes().length;
}
// outputStream.write(System.getProperty
("line.separator").getBytes());
}
} catch (Exception e) {
break;
} finally {
if (outputStream != null)
outputStream.close();
tempInputList.clear();
tempsize = (double) 0;
}
if (inputEmpty) {
break;
}
}
// fill remainign outputs if available

if (!(tempOutputList.isEmpty())) {
File file1 = new File("Output/new1.txt");
FileOutputStream outputStream = new FileOutputStream(fil
e1, true);
try {
for (String string1 : tempOutputList) {
outputStream.write(string1.getBytes());
outputStream.write(System.getProperty("l
ine.separator").getBytes());
}
} finally {
if (outputStream != null)
outputStream.close();
}
}
}
private static void moveTableAtoOutput() {
File folder3 = new File(outputFilesPath);
folder3.mkdir();
File sourceFile = new File("Sort/new1.txt");
File destFile = new File("Output/new1.txt");
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileChannel source = null;
FileChannel destination = null;
try {
/**
* getChannel() returns unique FileChannel object associ
ated a file
* output stream.
*/
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel(
);
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size(
));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (source != null) {
try {

source.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (destination != null) {
try {
destination.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void clearFolders() {
File folder = new File(sortedFilesPath);
if (folder.exists() && folder.isDirectory()) {
File[] totalFiles = folder.listFiles();
for (int i = 0; i < totalFiles.length; i++) {
totalFiles[i].delete();
}
}
File folder1 = new File(outputFilesPath);
if (folder1.exists() && folder1.isDirectory()) {
File[] totalFiles = folder1.listFiles();
for (int i = 0; i < totalFiles.length; i++) {
totalFiles[i].delete();
}
}
}
private static void GenerateTreeA() throws Exception {
// TODO Auto-generated method stub
tree = new TupleBTree();
FileReader fileReader = new FileReader("Sort/new1.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
{
while (bufferedReader.ready()) {
// long fileLength = new System.IO.FileI
nfo("Sort/new" +
// filenameCount.ToString() + ".txt").Le
ngth;
String currentLine = bufferedReader.read
Line();
tree.insert(currentLine);
}
}
} finally {
if (bufferedReader != null)
bufferedReader.close();
if (fileReader != null)
fileReader.close();
}
}
private static void sortTree(String inputFilePath) throws Exception {
// TODO Auto-generated method stub

Runtime runtime = Runtime.getRuntime();


// to get the maximum amount of memory that your Java applicatio
n can
// use:
// System.out.println("max memory in MB: "
// + (float) (runtime.maxMemory() / 1024) / 1024);
// to get how much memory that JVM has allocated for your applic
ation
// System.out.println("allocated memory: "
// + (float) (runtime.totalMemory() / 1024) / 1024);
// to get how much memory is being used by your application:
// System.out.println("free memory: "
// + (float) (runtime.freeMemory() / 1024) / 1024);
readMetaData();
// split sort and outputs
setSortColumnsList();
setOutputList();
getSortColumnIndexes();
getOutputColumnIndexes();
phase1(inputFilePath);
Runtime runtime1 = Runtime.getRuntime();
}
/*
*
*
*
*
*
*

private static void phase2() throws Exception {


List<String> fileNames = new ArrayList<>(); File folder = new
File(sortedFilesPath); if (folder.exists() && folder.isDirectory()) {
File[] totalFiles = folder.listFiles(); for (int i = 0; i <
totalFiles.length; i++) { if (totalFiles[i].getName().endsWith(".txt"

)) {
*
*
*
*

fileNames.add(totalFiles[i].getPath()); } } } numberOfSortFiles =
fileNames.size(); bufferSizeFromEachFile = (xmxSize * 1000 * 1000) /
numberOfSortFiles; List<DataBuffer> dataBuffers =
getInitialBuffersFormSortedLists(fileNames); List<String> outputData

=
* new ArrayList<String>(); DataBuffer sortedBuffer; DataBuffer bufferTo
Sort
* = new DataBuffer(); bufferToSort.setfileDataVos(new
* ArrayList<FileDataVO>()); for (DataBuffer dataBuffer : dataBuffers) {
//
* take first of all buffers
* bufferToSort.getfileDataVos().add(dataBuffer.getfileDataVos().get(0))
;
*
*
*
*
*
*
*
*
*
*

dataBuffer.getfileDataVos().remove(0);
} sortedBuffer = sortBuffersPhase2(bufferToSort); while (true) { if
(outputData.size() == 100) { // write to output file File file1 = new
File(outputFilePath); FileOutputStream outputStream = new
FileOutputStream(file1, true); // FileOutputStream sw = new
FileOutputStream(outputFilePath, // true); try { for (String str :
outputData) { String strng = setColsForOutput(str);
outputStream.write(strng.getBytes());
outputStream.write(System.getProperty("line.separator").getBytes());

} }
* finally { if (outputStream != null) outputStream.close();

*
*
*
*
*
*

} outputData.clear(); }
outputData.add(sortedBuffer.getfileDataVos().get(0).getLineData());
String removedFileName =
sortedBuffer.getfileDataVos().get(0).getFilePath(); // Moved to outpu

t ,
* so remove from buffer.
* bufferToSort.getfileDataVos().remove(sortedBuffer.getfileDataVos().ge
t(0)
*
*
*
*
*

); FileDataVO fileData = getNextLineFromDataBuffer(dataBuffers,


removedFileName); if (fileData != null) {
bufferToSort.getfileDataVos().add(fileData); }

*
*
*
*
*
*
*
*

0) { break; }

sortedBuffer = sortBuffersPhase2(bufferToSort); if (dataBuffers.size(

) ==
} if (outputData.size() > 0) { File file1 = new File(outputFilePath);
FileOutputStream outputStream = new FileOutputStream(file1, true); //
StreamWriter sw = new StreamWriter(outputFilePath, true); try { { for
(String str : outputData) { String strng = setColsForOutput(str);
outputStream.write(strng.getBytes());
outputStream.write(System.getProperty("line.separator").getBytes());

} }
* } finally { if (outputStream != null) outputStream.close();
*
* } outputData.clear(); }
*
* }
*/
private static String setColsForOutput(String str) throws Exception {
String[] strs = str.split(",");
StringBuilder sb = new StringBuilder();
for (Object __dummyForeachVar5 : outputIndexes) {
int index = (Integer) __dummyForeachVar5;
sb.append(strs[index]);
sb.append(',');
}
String string = sb.toString();
return string.substring(0, string.length() - 1);
}
private static FileDataVO getNextLineFromDataBuffer(List<DataBuffer> dat
aBuffers, String filename)
throws Exception {
DataBuffer dataBuffer = null;
for (DataBuffer dB : dataBuffers) {
if (filename.equals(dB.getfileName())) {
dataBuffer = dB;
}
}
if (dataBuffer.getfileDataVos().size() == 0) {
dataBuffer = getBufferFromFile(dataBuffer);
dataBuffers.remove(dataBuffer);
if (!(dataBuffer.getfileDataVos().size() == 0)) {
dataBuffers.add(dataBuffer);
}

}
FileDataVO temp = null;
if (dataBuffer.getfileDataVos().size() != 0) {
temp = dataBuffer.getfileDataVos().get(0);
dataBuffer.getfileDataVos().remove(0);
}
return temp;
}
private static List<DataBuffer> getInitialBuffersFormSortedLists(List<St
ring> fileNames) throws Exception {
List<DataBuffer> dataBuffers = new ArrayList<DataBuffer>();
for (String fileName : fileNames) {
// Get buffers initially
DataBuffer databuffer = new DataBuffer();
databuffer.setfileName(fileName);
databuffer.setLineNumber(Double.valueOf(0));
dataBuffers.add(getBufferFromFile(databuffer));
}
return dataBuffers;
}
/**
* Gets the lines from files from LineNumber to bufferSizeFromEachFile
*
* @param databuffer
*/
private static DataBuffer getBufferFromFile(DataBuffer databuffer) throw
s Exception {
try {
double LineCountWithInBuffer = 0;
double currentBufferSize = 0;
if (databuffer != null && databuffer.getfileDataVos().si
ze() > 0) {
databuffer.getfileDataVos().clear();
}
FileReader fileReader = new FileReader(databuffer.getfil
eName());
BufferedReader bufferedReader = new BufferedReader(fileR
eader);
// StreamReader sr = new StreamReader(databuffer.getfile
Name());
try {
{
for (double i = 0; i < databuffer.getLin
eNumber(); i++) {
if (bufferedReader.ready()) {
bufferedReader.readLine(
);
} else
break;
}
if (bufferedReader.ready()) {
List<FileDataVO> fileDataVos = n
ew ArrayList<>();
while (true) {
String currentLine = buf

feredReader.readLine();
if (currentBufferSize +
currentLine.getBytes().length + 2 < bufferSizeFromEachFile) {
LineCountWithInB
uffer++;
databuffer.setLi
neNumber(databuffer.getLineNumber() + 1);
FileDataVO fileD
ataVO = new FileDataVO();
fileDataVO.setLi
neData(currentLine);
fileDataVO.setLi
neNumber(LineCountWithInBuffer);
fileDataVO.setFi
lePath(databuffer.getfileName());
fileDataVos.add(
fileDataVO);
currentBufferSiz
e = currentBufferSize + currentLine.getBytes().length + 2;
} else
break;
databuffer.setfileDataVo
s(fileDataVos);
}
}
}
} finally {
if (bufferedReader != null)
bufferedReader.close();
fileReader.close();
}
} catch (Exception ex) {
System.out.println(ex.getCause());
}
return databuffer;
}
// private static void IsTwoWayMergePossible()
// {
// //if adding all 1st
// if (Directory.Exists(sortedFilesPath))
// {
// Directory.GetFiles(sortedFilesPath);
// }
// }
private static void phase1(String inputFilePath) throws Exception {
// split and sort
File folder = new File(sortedFilesPath);
folder.mkdir();
// Directory.CreateDirectory(sortedFilesPath);
readInputFileSplitAndSort(inputFilePath);
}
// Sort and remove duplicates

private static Integer tableCount = 1;


private static void eliminateDuplicates(String inputFilePath) throws Exc
eption {
// File file = new File("Sort/new" + tableCount + ".txt");
// tableCount++;
// file.createNewFile();
/*
* IDisposable __newVar0 = File.Create("Sort/new1.txt"); try { {
} }
* finally { if (__newVar0 != null)
* Disposable.mkDisposable(__newVar0).dispose();
*
* }
*/
int filenameCount = 1;
double LineCountWithInBuffer = 0;
double currentBufferSize = 0;
// long fileLength = new System.IO.FileInfo("Sort/new.txt").Leng
th;
String tempLineStore = "";
DataBuffer databuffer = new DataBuffer();
databuffer.setfileDataVos(new ArrayList<FileDataVO>());
FileReader fileReader = new FileReader(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// StreamReader sr = new StreamReader(inputFilePath);
try {
{
while (bufferedReader.ready()) {
// long fileLength = new System.IO.FileI
nfo("Sort/new" +
// filenameCount.ToString() + ".txt").Le
ngth;
String currentLine = bufferedReader.read
Line();
if (currentBufferSize + currentLine.getB
ytes().length + 2 < xmxSize * 1000 * 1000) {
if (tempLineStore != null && tem
pLineStore.length() != 0) {
LineCountWithInBuffer++;
FileDataVO vo = new File
DataVO();
vo.setLineData(currentLi
ne);
vo.setFilePath("Sort/new
" + filenameCount + ".txt");
vo.setLineNumber(LineCou
ntWithInBuffer);
databuffer.getfileDataVo
s().add(vo);
tempLineStore = "";
currentBufferSize = curr
entBufferSize + (tempLineStore.length()) + 2;
}
LineCountWithInBuffer++;
FileDataVO vo2 = new FileDataVO(
);
vo2.setLineData(currentLine);

vo2.setFilePath("Sort/new" + fil
enameCount + ".txt");
vo2.setLineNumber(LineCountWithI
nBuffer);
databuffer.getfileDataVos().add(
vo2);
currentBufferSize = currentBuffe
rSize + (currentLine.length()) + 2;
}
}
if (databuffer.getfileDataVos().size() > 0) {
databuffer = sortBuffers(databuffer);
File file2 = new File("OutPut/new2.txt")
;
tableCount++;
FileOutputStream fop = new FileOutputStr
eam(file2);
/*
* StreamWriter sw = new StreamWriter("S
ort/new" +
* filenameCount + ".txt");
*/
try {
{
for (FileDataVO item : d
atabuffer.getfileDataVos()) {
fop.write(item.g
etLineData().getBytes());
fop.write(System
.getProperty("line.separator").getBytes());
}
}
} finally {
if (fop != null)
fop.close();
}
databuffer.getfileDataVos().clear();
}
}
} finally {
if (bufferedReader != null)
bufferedReader.close();
}
}
/**
* Reads the input file , Splits and Sorts them. to /Sort/fileame(i).txt
*/
private static void readInputFileSplitAndSort(String inputFilePath) thro
ws Exception {
// File file = new File("Sort/new" + tableCount + ".txt");
// tableCount++;
// file.createNewFile();
/*
* IDisposable __newVar0 = File.Create("Sort/new1.txt"); try { {
} }
* finally { if (__newVar0 != null)

* Disposable.mkDisposable(__newVar0).dispose();
*
* }
*/
int filenameCount = 1;
double LineCountWithInBuffer = 0;
double currentBufferSize = 0;
// long fileLength = new System.IO.FileInfo("Sort/new.txt").Leng
th;
String tempLineStore = "";
DataBuffer databuffer = new DataBuffer();
databuffer.setfileDataVos(new ArrayList<FileDataVO>());
FileReader fileReader = new FileReader(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// StreamReader sr = new StreamReader(inputFilePath);
try {
{
while (bufferedReader.ready()) {
// long fileLength = new System.IO.FileI
nfo("Sort/new" +
// filenameCount.ToString() + ".txt").Le
ngth;
String currentLine = bufferedReader.read
Line();
if (currentBufferSize + currentLine.getB
ytes().length + 2 < xmxSize * 1000 * 1000) {
if (tempLineStore != null && tem
pLineStore.length() != 0) {
LineCountWithInBuffer++;
FileDataVO vo = new File
DataVO();
vo.setLineData(currentLi
ne);
//vo.setFilePath("Sort/n
ew" + filenameCount + ".txt");
//vo.setLineNumber(LineC
ountWithInBuffer);
databuffer.getfileDataVo
s().add(vo);
tempLineStore = "";
currentBufferSize = curr
entBufferSize + (tempLineStore.length()) + 2;
}
LineCountWithInBuffer++;
FileDataVO vo2 = new FileDataVO(
);
vo2.setLineData(currentLine);
//vo2.setFilePath("Sort/new" + f
ilenameCount + ".txt");
//vo2.setLineNumber(LineCountWit
hInBuffer);
databuffer.getfileDataVos().add(
vo2);
currentBufferSize = currentBuffe
rSize + (currentLine.length()) + 2;
}
}
if (databuffer.getfileDataVos().size() > 0) {
databuffer = sortBuffers(databuffer);

File file2 = new File("Sort/new" + table


Count + ".txt");
tableCount++;
FileOutputStream fop = new FileOutputStr
eam(file2);
/*
* StreamWriter sw = new StreamWriter("S
ort/new" +
* filenameCount + ".txt");
*/
try {
{
for (FileDataVO item : d
atabuffer.getfileDataVos()) {
fop.write(item.g
etLineData().getBytes());
fop.write(System
.getProperty("line.separator").getBytes());
}
}
} finally {
if (fop != null)
fop.close();
}
databuffer.getfileDataVos().clear();
}
}
} finally {
if (bufferedReader != null)
bufferedReader.close();
}
}
/**
* Sort the input file based on sortIndex
*
* @param buffer
* @return
*/
private static DataBuffer sortBuffers(DataBuffer buffer) throws Exceptio
n {
TreeMap<String, String> map = new TreeMap<>();
for (FileDataVO fileVO : buffer.getfileDataVos()) {
try {
map.put(getKey(fileVO), fileVO.getLineData());
} catch (Exception e) {
}
}
buffer.getfileDataVos().clear();
if (order.equals("asc")) {
for (String key : map.keySet()) {
FileDataVO fileDataVO = new FileDataVO();
fileDataVO.setLineData(map.get(key));
buffer.getfileDataVos().add(fileDataVO);
}

} else {
NavigableSet<String> set = map.descendingKeySet();
for (String key2 : set) {
FileDataVO fileDataVO = new FileDataVO();
fileDataVO.setLineData(map.get(key2));
buffer.getfileDataVos().add(fileDataVO);
}
}
return buffer;
}
private static DataBuffer sortBuffersPhase2(DataBuffer buffer) throws Ex
ception {
TreeMap<String, String> map2 = new TreeMap<>();
for (FileDataVO fileVO : buffer.getfileDataVos()) {
try {
map2.put(getKey(fileVO), fileVO.getLineData() +
"~" + fileVO.getFilePath());
} catch (Exception e) {
}
}
buffer.getfileDataVos().clear();
if (order.equals("asc")) {
for (String key : map2.keySet()) {
FileDataVO fileDataVO = new FileDataVO();
String values = map2.get(key);
String[] splitStrings = values.split("~");
fileDataVO.setLineData(splitStrings[0]);
fileDataVO.setFilePath(splitStrings[1]);
buffer.getfileDataVos().add(fileDataVO);
}
} else {
NavigableSet<String> set2 = map2.descendingKeySet();
for (String key : set2) {
FileDataVO fileDataVO = new FileDataVO();
String values = map2.get(key);
String[] splitStrings = values.split("~");
fileDataVO.setLineData(splitStrings[0]);
fileDataVO.setFilePath(splitStrings[1]);
buffer.getfileDataVos().add(fileDataVO);
}
}
return buffer;
}
// static double keyCount = 0;
/**
* takes sorted indexes , gets corresponding columns and forms a key for
* sort.
*
* @param dataVO
* @return
*/
private static String getKey(FileDataVO dataVO) throws Exception {
/*
* StringBuilder sb = new StringBuilder(); String[] cols =
* dataVO.getLineData().split(","); for (Integer inte : sortInde
xes) {

* String col = cols[inte.intValue()]; boolean isNumber =


* isNumeric(col); if (isNumber) { // make it length 6 if (col.l
ength()
* < 6) { while (true) { col = "0" + col; if (col.length() == 6)
break;
*
* } }
*
* sb.append(col); sb.append(','); } else // check if date if
* (col.contains("/")) { String[] dateSplits = col.split("/"); c
ol =
* dateSplits[2] + "/" + dateSplits[1] + "/" + dateSplits[0];
* sb.append(col); sb.append(','); } else { sb.append(col);
* sb.append(','); }
*
* }
*/
// sb.append(String.valueOf(keyCount));
// keyCount++;
// String returnStr = sb.toString();
// return returnStr;// .substring(0, returnStr.length() - 1);
return dataVO.getLineData();
}
private static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static void getOutputColumnIndexes() throws Exception {
outputIndexes = new ArrayList<Integer>();
for (ColumnVo item : metaInfo.getcolumns()) {
if (outputColumnList.contains(item.getColumnName())) {
outputIndexes.add(item.getColumnNumber());
}
}
}
private static void getSortColumnIndexes() throws Exception {
sortIndexes = new ArrayList<Integer>();
for (ColumnVo item : metaInfo.getcolumns()) {
if (sortColumnList.contains(item.getColumnName())) {
sortIndexes.add(item.getColumnNumber());
}
}
}
private static void readMetaData() throws IOException {
metaInfo = new MetaData();
metaInfo.setcolumns(new ArrayList<ColumnVo>());
String line = null;
BufferedReader brTest = new BufferedReader(new FileReader(inputF
ilePath1));

String firstLine = brTest.readLine();


String[] splitData = firstLine.split(",");
for (int i = 0; i < splitData.length; i++) {
ColumnVo columnVo = new ColumnVo();
columnVo.setColumnName(splitData[i]);
columnVo.setType("CHAR(20)");
columnVo.setColumnNumber(i);
metaInfo.getcolumns().add(columnVo);
}
/*
* FileReader sr = new FileReader(metaDataFilePath); BufferedRea
der
* bufferedReader = new BufferedReader(sr); int i = 0; while ((l
ine =
* bufferedReader.readLine()) != null) { String[] metaData =
* line.split(","); ColumnVo columnVo = new ColumnVo();
* columnVo.setColumnName(metaData[0]); columnVo.setType(metaDat
a[1]);
* columnVo.setColumnNumber(i); metaInfo.getcolumns().add(column
Vo);
* i++; } bufferedReader.close(); sr.close();
*/
}
private static void setSortColumnsList() throws Exception {
/*
* String[] cols = sortColumns.split(","); sortColumnList = new
* ArrayList<String>(); for (Object __dummyForeachVar18 : cols)
{ String
* col = (String) __dummyForeachVar18; sortColumnList.add(col);
}
*/
for (ColumnVo item : metaInfo.getcolumns()) {
sortColumnList.add("col" + item.getColumnNumber());
}
}
private static void setOutputList() throws Exception {
/*
* String[] cols = outputColumns.split(","); outputColumnList =
new
* ArrayList<String>(); for (Object __dummyForeachVar19 : cols)
{ String
* col = (String) __dummyForeachVar19; outputColumnList.add(col)
; }
*/
for (ColumnVo item : metaInfo.getcolumns()) {
outputColumnList.add("col" + item.getColumnNumber());
}
}
}
class TupleBTree extends BTree<String, String> {
public void insert(String key) {
this.insert(key, key);
}

public void remove(String key) {


this.delete(key);
}
}

S-ar putea să vă placă și