Skip to content

Commit 4bbc307

Browse files
committed
threads
1 parent 5e60211 commit 4bbc307

File tree

5 files changed

+419
-49
lines changed

5 files changed

+419
-49
lines changed

DataFrame/src/DF/DataFrame.java

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
import java.io.BufferedReader;
77
import java.io.FileReader;
88
import java.io.IOException;
9-
import java.util.Collection;
10-
import java.util.LinkedList;
11-
import java.util.NoSuchElementException;
12-
import java.util.TreeMap;
9+
import java.util.*;
1310

1411
public class DataFrame {
15-
Kolumna[] kolumny;
12+
Kolumna[] kolumny;
1613
int ilosc_wierszy;
1714
public String[] lista_nazw;
1815
Class<? extends Value>[] lista_typow;
@@ -215,7 +212,7 @@ public DataFrame get(String[] cols, boolean copy) { // zwraca nową DataFrame z
215212

216213
public void readingFromFileFunction(String path, boolean header) throws IOException, IncoherentTypeException, ValueParseException {
217214

218-
try(BufferedReader br = new BufferedReader(new FileReader(path))) {
215+
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
219216
// Open the file
220217

221218
String[] strLine;
@@ -238,7 +235,7 @@ public void readingFromFileFunction(String path, boolean header) throws IOExcept
238235
try {
239236
wartosciZpliku[m] = (kolumny[m].typ).newInstance().create(wartoscWpostaciStringa);
240237
} catch (Exception e) {
241-
throw new ValueParseException(size(),kolumny[m].getNazwa());
238+
throw new ValueParseException(size(), kolumny[m].getNazwa());
242239
}
243240

244241

@@ -319,8 +316,10 @@ protected TreeMap<Value, DataFrame> groupByOne(String colname) {
319316
return output;
320317
}
321318

319+
// @Deprecated
322320
public GroupBy groupBy(String... colname) throws ZeroLengthException, NoSenseInGroupingByAllColumnsException {
323-
if(colname.length==this.kolumny.length){throw new NoSenseInGroupingByAllColumnsException();
321+
if (colname.length == this.kolumny.length) {
322+
throw new NoSenseInGroupingByAllColumnsException();
324323
}
325324
TreeMap<Value, DataFrame> initial = groupByOne(colname[0]);
326325

@@ -344,6 +343,54 @@ public GroupBy groupBy(String... colname) throws ZeroLengthException, NoSenseInG
344343
}
345344

346345

346+
protected class ValueGroup implements Comparable<ValueGroup> {
347+
private Value[] id;
348+
349+
@Override
350+
public String toString() {
351+
return Arrays.toString(id);
352+
}
353+
354+
protected ValueGroup(Value[] key) {
355+
id = key;
356+
}
357+
358+
public Value[] getId() {
359+
return id;
360+
}
361+
362+
@Override
363+
public boolean equals(Object o) {
364+
if (o instanceof ValueGroup) {
365+
ValueGroup other = (ValueGroup) o;
366+
367+
return Arrays.deepEquals(id, other.id);
368+
} else return false;
369+
}
370+
371+
@Override
372+
public int hashCode() {
373+
374+
return Arrays.deepHashCode(id);
375+
}
376+
377+
@Override
378+
public int compareTo(ValueGroup valueGroup) {
379+
for (int i = 0; i < id.length; i++) {
380+
if (id[i].equals(valueGroup.id[i])) continue;
381+
try {
382+
if (id[i].lte(valueGroup.id[i])) return -1;
383+
else return 1;
384+
} catch (IncoherentTypeException e) {
385+
e.printStackTrace();
386+
}
387+
}
388+
return 0;
389+
}
390+
}
391+
392+
393+
347394
public class Grupator implements GroupBy {
348395
private LinkedList<DataFrame> lista;
349396
private String[] kluczowe_nazwy_kolumn;
@@ -447,14 +494,19 @@ public DataFrame apply(Applyable funkcja) throws IncoherentTypeException, ZeroLe
447494

448495
}
449496

450-
@Deprecated
451-
public Kolumna[] getKolumny(){
452-
Kolumna[] output = new Kolumna[kolumny.length];
453-
for (int i = 0; i < kolumny.length; i++) {
454-
output[i]=kolumny[i];
497+
498+
@Deprecated
499+
public Kolumna[] getKolumny() {
500+
Kolumna[] output = new Kolumna[kolumny.length];
501+
for (int i = 0; i < kolumny.length; i++) {
502+
output[i] = kolumny[i];
503+
}
504+
return output;
455505
}
456-
return output;
457-
}
506+
507+
508+
509+
458510

459511

460512
}

DataFrame/src/DF/GroupBy.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package DF;
22

3+
import DF.Exceptions.DifferentAmountOfColumns;
34
import DF.Exceptions.IncoherentTypeException;
45
import DF.Exceptions.ZeroLengthException;
6+
import DF.Values.Value;
57

68
public interface GroupBy {
79

@@ -13,4 +15,35 @@ public interface GroupBy {
1315
default DataFrame var() throws IncoherentTypeException, ZeroLengthException { return apply(new ApplyVar());}
1416

1517
DataFrame apply(Applyable funkcja) throws IncoherentTypeException, ZeroLengthException;
18+
19+
static DataFrame getOutputDataFrame(Class<? extends Value>[] keyTypes, String[] keyNames, Class<? extends Value>[] dfTypes, String[] dfNames) {
20+
Class<? extends Value>[] fullTypes = new Class[keyTypes.length + dfTypes.length];
21+
22+
System.arraycopy(keyTypes, 0, fullTypes, 0, keyTypes.length);
23+
System.arraycopy(dfTypes, 0, fullTypes, keyTypes.length, dfTypes.length);
24+
25+
String[] fullNames = new String[keyNames.length + dfNames.length];
26+
27+
System.arraycopy(keyNames, 0, fullNames, 0, keyNames.length);
28+
System.arraycopy(dfNames, 0, fullNames, keyNames.length, dfNames.length);
29+
30+
return new DataFrame(fullNames, fullTypes);
31+
32+
}
33+
34+
static void addGroup(DataFrame output, Value[] keyValues, DataFrame group) throws DifferentAmountOfColumns, IncoherentTypeException {
35+
36+
Value[] rowValues = new Value[group.iloscKolumn() + keyValues.length];
37+
System.arraycopy(keyValues, 0, rowValues, 0, keyValues.length);
38+
39+
for (int j = 0; j < group.size(); j++) {
40+
Value[] groupValues = group.zwrocWiersz(j);
41+
42+
System.arraycopy(groupValues, 0, rowValues, keyValues.length, groupValues.length);
43+
output.dodajElement(rowValues);
44+
45+
}
46+
}
47+
48+
1649
}

DataFrame/src/DF/Kolumna.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public Kolumna(Kolumna do_skopiowania){ // konstruktor kopiujący potrzebny do
4747

4848
}
4949

50+
Kolumna copy(){
51+
Kolumna nowa = new Kolumna(nazwa,typ);
52+
nowa.dane=dane;
53+
return nowa;
54+
}
55+
5056
public int size(){
5157
return dane.size();
5258
}

DataFrame/src/DF/Main.java

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package DF;
22

33
import DF.Exceptions.*;
4+
import DF.Values.DateTimeValue;
5+
import DF.Values.DoubleValue;
6+
import DF.Values.StringValue;
47

58
import java.io.IOException;
69
import java.sql.SQLException;
10+
import java.util.concurrent.ExecutorService;
11+
12+
import static java.util.concurrent.Executors.newFixedThreadPool;
713

814
public class Main {
9-
public static void main (String[] args) throws IOException, ValueParseException, ZeroLengthException, NoSenseInGroupingByAllColumnsException, IncoherentTypeException, DifferentAmountOfColumns, SQLException, DivideByZeroException, DifferentSizeOfColumnsExcepiton {
15+
public static void main (String[] args) throws IOException, ValueParseException, ZeroLengthException, NoSenseInGroupingByAllColumnsException, IncoherentTypeException, DifferentAmountOfColumns, SQLException, DivideByZeroException, DifferentSizeOfColumnsExcepiton, CloneNotSupportedException {
1016
// DataFrame tmp = new DataFrame(new String[]{"z","g","r"}, new Class[] {IntegerValue.class,IntegerValue.class,IntegerValue.class} );
1117
// tmp.dodajElement(new IntegerValue(9),new IntegerValue(23),new IntegerValue(33));
1218
// System.out.println(tmp.get("z").getType());
@@ -32,7 +38,7 @@ public static void main (String[] args) throws IOException, ValueParseException,
3238
// // System.out.println(tmp2.toString());
3339
//
3440
//
35-
// DataFrame.Grupator group = tmp2.groupBy(new String[]{"id"});
41+
// DataFrame.Grupator group = tmp2.groupBy(new String[]{"id"});
3642
// System.out.println(group.sum());
3743
// System.out.println(group.max());
3844
// System.out.println(group.min());
@@ -43,18 +49,17 @@ public static void main (String[] args) throws IOException, ValueParseException,
4349

4450
// TEST DLA DWÓCH KLUCZY
4551

46-
// DataFrame tmp3 = new DataFrame("groubymulti.csv", new Class[]{StringValue.class,DateTimeValue.class,DoubleValue.class,DoubleValue.class});
52+
// DataFrame tmp3 = new DataFrame("groubymulti.csv", new Class[]{StringValue.class,DateTimeValue.class,DoubleValue.class,DoubleValue.class});
4753

48-
// DataFrame.Grupator group1 = tmp3.groupBy(new String []{"id","date"});
54+
// DataFrame.Grupator group1 = tmp3.groupBy(new String []{"id","date"});
4955
// System.out.println(group1.min());
5056
//System.out.println(group1.max());
5157
// System.out.println(group1.sum());
5258
// System.out.println(group1.mean());
53-
//System.out.println(group1.var());
59+
//System.out.println(group1.var());
5460
// System.out.println(group1.std());
5561

5662

57-
5863
// DataFrame my = new DataFrame(new String[]{"double","double2","double3"},new Class[]{DoubleValue.class,DoubleValue.class,DoubleValue.class});
5964
// my.addRecord(new DoubleValue(9.0),new DoubleValue(6.7),new DoubleValue(6.7));
6065
// my.addRecord(new DoubleValue(10.0),new DoubleValue(45.7),new DoubleValue(6.7));
@@ -85,11 +90,10 @@ public static void main (String[] args) throws IOException, ValueParseException,
8590

8691
// System.out.println(my2.toString());
8792

88-
// DataFrame.Grupator grupkaTest = my2.groupBy(new String[]{"string"});
89-
// System.out.println(grupkaTest.sum());
93+
// DataFrame.Grupator grupkaTest = my2.groupBy(new String[]{"string"});
94+
// System.out.println(grupkaTest.sum());
9095
//System.out.println(grupkaTest.min());
91-
// System.out.println(grupkaTest.var());
92-
96+
// System.out.println(grupkaTest.var());
9397

9498

9599
// DataFrame my1 = new DataFrame(new String[]{"data","data2","data3"},new Class[]{DateTimeValue.class,DateTimeValue.class,DateTimeValue.class});
@@ -117,7 +121,6 @@ public static void main (String[] args) throws IOException, ValueParseException,
117121
// System.out.println(grupkaTest.std());
118122

119123

120-
121124
////
122125

123126
// Kolumna test1 = new Kolumna ("nazwa",StringValue.class);
@@ -182,8 +185,7 @@ public static void main (String[] args) throws IOException, ValueParseException,
182185

183186
//---------- testy dla Data Frame DB------------
184187

185-
// DataFrameDB tmp = new DataFrameDB.DataFrameBDBuilder("jdbc:mysql://mysql.agh.edu.pl/joannabi",
186-
// "joannabi","87cz4zfRV047S5Wr","TABLE3").build();
188+
187189

188190
// DataFrameDB tmp = new DataFrameDB.DataFrameBDBuilder("jdbc:mysql://mysql.agh.edu.pl/joannabi",
189191
// "joannabi","87cz4zfRV047S5Wr","books").build();
@@ -195,37 +197,58 @@ public static void main (String[] args) throws IOException, ValueParseException,
195197
// System.out.println(tmp.get("title"));
196198
// DataFrame test = tmp.fromDataFrameDFtoDataFrame();
197199
// System.out.println(test.toString());
198-
// GroupBy grp=tmp.groupBy("id");
199-
// System.out.println(grp.apply(new ApplyMax()));
200-
// tmp.dodajElement(new StringValue("0101010101"),new StringValue("test tytul"),new StringValue("test autor"),new IntegerValue(2000));
201-
// System.out.println(tmp.toString());
202200

201+
// System.out.println(grp.apply(new ApplyMax()));
202+
// tmp.dodajElement(new StringValue("0101010101"),new StringValue("test tytul"),new StringValue("test autor"),new IntegerValue(2000));
203+
// System.out.println(tmp.toString());
203204

204-
// -----porównanie czasów dla max -------
205205

206-
// System.out.println("DataFrame DB:");
207-
// long start = System.currentTimeMillis();
208-
// grp.apply(new ApplyMax());
209-
// long stop = System.currentTimeMillis();
210-
// System.out.println(stop-start);
211-
//
212-
// DataFrame tmp2 = new DataFrame("TABLE3.csv", new Class[]{ StringValue.class,DateTimeValue.class,DoubleValue.class,DoubleValue.class});
213-
// GroupBy group = tmp2.groupBy(new String[]{"id"});
214-
// long start2 = System.currentTimeMillis();
215-
// group.max();
216-
// long stop2 = System.currentTimeMillis();
217-
//
218-
// System.out.println("DataFrame :");
219-
// System.out.println(stop2-start2);
206+
// -----porównanie czasów dla max -------
220207

221-
208+
DataFrameDB tmp = new DataFrameDB.DataFrameBDBuilder("jdbc:mysql://mysql.agh.edu.pl/joannabi",
209+
"joannabi","87cz4zfRV047S5Wr","TABLE3").build();
222210

211+
GroupBy grp=tmp.groupBy("id");
212+
213+
214+
215+
System.out.println("DataFrame DB:");
216+
long start = System.currentTimeMillis();
217+
grp.apply(new ApplyMax());
218+
long stop = System.currentTimeMillis();
219+
System.out.println(stop-start);
223220

224-
}
225221

226222

227223

224+
DataFrame tmp2 = new DataFrame("table600k.csv", new Class[]{ StringValue.class,DateTimeValue.class,DoubleValue.class,DoubleValue.class});
225+
GroupBy group = tmp2.groupBy(new String[]{"id"});
226+
long start2 = System.currentTimeMillis();
228227

228+
group.std();
229+
long stop2 = System.currentTimeMillis();
229230

231+
System.out.println("DataFrame :");
232+
System.out.println(stop2-start2);
230233

234+
235+
ExecutorService threadPool = newFixedThreadPool(4);
236+
ThreadDataFrame threadDF = new ThreadDataFrame(threadPool,tmp2);
237+
238+
GroupBy groupThread = threadDF.groupBy(new String[]{"id"});
239+
long start3 = System.currentTimeMillis();
240+
241+
groupThread.std();
242+
long stop3 = System.currentTimeMillis();
243+
threadPool.shutdown();
244+
245+
System.out.println("DataFrameThread :");
246+
System.out.println(stop3-start3);
247+
248+
249+
250+
251+
252+
253+
}
231254
}

0 commit comments

Comments
 (0)