1
1
package com .webank .wedpr .components .scheduler .dag .utils ;
2
2
3
+ import com .webank .wedpr .common .utils .WeDPRException ;
3
4
import java .io .BufferedReader ;
4
5
import java .io .FileReader ;
5
6
import java .io .FileWriter ;
@@ -31,23 +32,24 @@ public class MpcResultFileResolver {
31
32
32
33
public static final String CSV_SEP = "," ;
33
34
public static final String BLANK_SEP = " " ;
35
+ public static final String MPC_ID_FIELD = "id" ;
34
36
35
37
public MpcResult doParseMpcResultFile (String mpcOutputFile , boolean onlyField )
36
- throws IOException {
38
+ throws IOException , WeDPRException {
37
39
38
- int valueCount = 0 ;
40
+ int mpcResultRowCount = 0 ;
41
+ int mpcResultFieldCount = 0 ;
39
42
40
- int fieldCount = 0 ;
41
43
boolean needAddFields = true ;
42
44
MpcResult mpcResult = new MpcResult ();
43
- String strResultFields = "id" ;
45
+ String strResultFields = MPC_ID_FIELD ;
44
46
try (BufferedReader mpcOutputBufferedReader =
45
47
new BufferedReader (new FileReader (mpcOutputFile ))) {
46
48
String line ;
47
49
while ((line = mpcOutputBufferedReader .readLine ()) != null ) {
48
50
line = line .trim ();
49
51
if (line .startsWith (PPC_RESULT_FIELDS_FLAG )) {
50
- strResultFields += "," ;
52
+ strResultFields += CSV_SEP ;
51
53
strResultFields +=
52
54
line .substring (line .indexOf ('=' ) + 1 )
53
55
.trim ()
@@ -56,13 +58,13 @@ public MpcResult doParseMpcResultFile(String mpcOutputFile, boolean onlyField)
56
58
mpcResult .setMpcResultFields (strResultFields );
57
59
needAddFields = false ;
58
60
} else if (line .startsWith (PPC_RESULT_VALUES_FLAG )) {
59
- fieldCount = line .split ("=" )[1 ].trim ().split (BLANK_SEP ).length ;
60
- logger .info ("## {}:{}" , PPC_RESULT_VALUES_FLAG , fieldCount );
61
- mpcResult .setMpcResultFieldCount (fieldCount );
61
+ mpcResultFieldCount = line .split ("=" )[1 ].trim ().split (BLANK_SEP ).length ;
62
+ logger .info ("## {}:{}" , PPC_RESULT_VALUES_FLAG , mpcResultFieldCount );
63
+ mpcResult .setMpcResultFieldCount (mpcResultFieldCount );
62
64
if (onlyField ) {
63
65
break ;
64
66
}
65
- valueCount ++;
67
+ mpcResultRowCount ++;
66
68
} else if (line .startsWith (PPC_RESULT_TIME_FLAG )) {
67
69
logger .info ("## {}:{}" , PPC_RESULT_TIME_FLAG , line );
68
70
mpcResult .setMpcResultTimeLine (line );
@@ -79,19 +81,29 @@ public MpcResult doParseMpcResultFile(String mpcOutputFile, boolean onlyField)
79
81
if (needAddFields ) {
80
82
StringBuilder stringBuilder = new StringBuilder ();
81
83
stringBuilder .append (strResultFields );
82
- for (int i = 0 ; i < fieldCount ; i ++) {
83
- stringBuilder .append ("," + "result" ).append (i );
84
+ for (int i = 0 ; i < mpcResultFieldCount ; i ++) {
85
+ stringBuilder .append (CSV_SEP + "result" ).append (i );
84
86
}
85
87
mpcResult .setMpcResultFields (stringBuilder .toString ());
88
+
89
+ logger .error (
90
+ "Not found \" {}\" flag in mpc result file, fields: {}, mpcOutputFile: {}" ,
91
+ PPC_RESULT_FIELDS_FLAG ,
92
+ stringBuilder ,
93
+ mpcOutputFile );
94
+
95
+ throw new WeDPRException (
96
+ "Not found \" " + PPC_RESULT_FIELDS_FLAG + "\" flag in mpc result file" );
86
97
}
87
98
88
- mpcResult .setMpcResultValueCount (valueCount );
89
- logger .info ("mpc result: {}" , mpcResult );
99
+ mpcResult .setMpcResultValueCount (mpcResultRowCount );
100
+ logger .info ("## mpc result: {}" , mpcResult );
90
101
return mpcResult ;
91
102
}
92
103
93
104
public void transMpcOutputFile2ResultFile (
94
- String jobId , String mpcOutputFile , String mpcResultFile ) throws IOException {
105
+ String jobId , String mpcOutputFile , String mpcResultFile )
106
+ throws IOException , WeDPRException {
95
107
96
108
long startTimeMillis = System .currentTimeMillis ();
97
109
logger .info (
@@ -102,7 +114,7 @@ public void transMpcOutputFile2ResultFile(
102
114
103
115
MpcResult mpcResult = doParseMpcResultFile (mpcOutputFile , true );
104
116
105
- int rowCount = 0 ;
117
+ int rowNumber = 0 ;
106
118
try (BufferedReader mpcOutputBufferedReader =
107
119
new BufferedReader (new FileReader (mpcOutputFile ));
108
120
FileWriter mpcResultFileWriter = new FileWriter (mpcResultFile );
@@ -124,19 +136,19 @@ public void transMpcOutputFile2ResultFile(
124
136
continue ;
125
137
}
126
138
127
- rowCount ++;
139
+ rowNumber ++;
128
140
129
- StringBuilder stringBuilder = new StringBuilder (String .valueOf (rowCount ));
141
+ StringBuilder stringBuilder = new StringBuilder (String .valueOf (rowNumber ));
130
142
131
143
String [] valuesList = line .split ("=" )[1 ].trim ().split (BLANK_SEP );
132
144
for (String value : valuesList ) {
133
- stringBuilder .append ("," ).append (value );
145
+ stringBuilder .append (CSV_SEP ).append (value );
134
146
}
135
147
136
148
csvWriter .write (stringBuilder .toString ());
137
149
138
150
if (logger .isTraceEnabled ()) {
139
- logger .trace ("result values: {}, index: {}" , stringBuilder , rowCount );
151
+ logger .trace ("result values: {}, index: {}" , stringBuilder , rowNumber );
140
152
}
141
153
142
154
// add a newline at the end of each row
@@ -146,25 +158,12 @@ public void transMpcOutputFile2ResultFile(
146
158
long endTimeMillis = System .currentTimeMillis ();
147
159
148
160
logger .info (
149
- "trans mpc output file to mpc result file end, jobId: {}, mpcOutputFile: {}, mpcResultFile: {}, rowCount : {}, costMs: {}" ,
161
+ "trans mpc output file to mpc result file end, jobId: {}, mpcOutputFile: {}, mpcResultFile: {}, rowNumber : {}, costMs: {}" ,
150
162
jobId ,
151
163
mpcOutputFile ,
152
164
mpcResultFile ,
153
- rowCount ,
165
+ rowNumber ,
154
166
endTimeMillis - startTimeMillis );
155
167
}
156
168
}
157
-
158
- public static void main (String [] args ) throws IOException {
159
- MpcResultFileResolver mpcResultFileResolver = new MpcResultFileResolver ();
160
- // MpcResult mpcResult =
161
- // mpcResultFileResolver.doParseMpcResultFile(
162
- // "/Users/octopus/Desktop/mpc_result_1.txt", true);
163
-
164
- mpcResultFileResolver .transMpcOutputFile2ResultFile (
165
- "" ,
166
- "/Users/octopus/Desktop/mpc_result_1.txt" ,
167
- "/Users/octopus/Desktop/mpc_result.csv" );
168
- // System.out.println(mpcResult);
169
- }
170
169
}
0 commit comments