-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDataPartition.java
610 lines (563 loc) · 17.4 KB
/
DataPartition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*******************************************************************************
* HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems®.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.hpccsystems.dfs.client;
import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.hpccsystems.commons.ecl.FileFilter;
import org.hpccsystems.commons.errors.HpccFileException;
import org.hpccsystems.dfs.cluster.ClusterRemapper;
import org.hpccsystems.ws.client.wrappers.wsdfu.DFUFilePartWrapper;
import org.hpccsystems.ws.client.wrappers.wsdfu.DFUFileTypeWrapper;
/**
* A partition of data. One physical file or key accessed by HPCC Systems remote read.
*/
public class DataPartition implements Serializable
{
public static final long serialVersionUID = 1L;
private String[] copyLocations;
private String[] copyPaths;
private int this_part;
private int num_parts;
private int rowservicePort;
private boolean useSSL;
private FileFilter fileFilter;
private String fileAccessBlob;
private FileType fileType;
private boolean isTLK;
private String fileName;
public static enum FileType
{
FLAT (
"disk", true
), // disk is the prefix used in dafilesrv for flat files
INDEX (
"index", true
), INDEX_LOCAL (
"indexlocal", true
), INDEX_PARTITIONED (
"indexpartitioned", true
), JSON (
"json", true
), CSV (
"csv", false
), XML (
"xml", false
);
private String name = null;
private boolean typeCanBeDeduced = false;
/**
* Instantiates a new file type.
*
* @param type
* the type
* @param deduced
* the deduced
*/
FileType(String type, boolean deduced)
{
name = type;
typeCanBeDeduced = deduced;
}
/*
* (non-Javadoc)
*
* @see java.lang.Enum#toString()
*/
public String toString()
{
return name;
}
/**
* Type can be deduced.
*
* @return true, if successful
*/
public boolean typeCanBeDeduced()
{
return typeCanBeDeduced;
}
public static FileType fromWrappedFileType(DFUFileTypeWrapper wrappedType) throws Exception
{
switch (wrappedType)
{
case Flat:
return FileType.FLAT;
case Index:
return FileType.INDEX;
case Xml:
return FileType.XML;
case Csv:
return FileType.CSV;
case Json:
return FileType.JSON;
case IndexLocal:
return FileType.INDEX_LOCAL;
case IndexPartitioned:
return FileType.INDEX_PARTITIONED;
default:
throw new Exception("Unknown file type: " + wrappedType);
}
}
};
/**
* Instantiates a new data partition.
*
* @param copyLocations
* the copy locations
* @param copyPaths
* the copy paths
* @param partNum
* the part num
* @param numParts
* the num parts
* @param rowServicePort
* the row service port
* @param shouldUseSSL
* the should use SSL
* @param fileAccessBlob
* the file access blob
*/
public DataPartition(String[] copyLocations, String[] copyPaths, int partNum, int numParts, int rowServicePort, boolean shouldUseSSL,
String fileAccessBlob)
{
this(copyLocations, copyPaths, partNum, numParts, rowServicePort, shouldUseSSL, null, fileAccessBlob);
}
/**
* Construct the data part, used by makeParts.
*
* @param copyLocations
* the copy locations
* @param copyPaths
* the copy paths
* @param partNum
* part number
* @param numParts
* number of parts
* @param rowServicePort
* the row service port
* @param shouldUseSSL
* the should use SSL
* @param filter
* the file filter object
* @param fileAccessBlob
* file access token
*/
private DataPartition(String[] copyLocations, String[] copyPaths, int partNum, int numParts, int rowServicePort, boolean shouldUseSSL,
FileFilter filter, String fileAccessBlob)
{
this(copyLocations, copyPaths, partNum, numParts, rowServicePort, shouldUseSSL, null, fileAccessBlob, FileType.FLAT);
}
/**
* Construct the data part, used by makeParts.
*
* @param copylocations
* locations of all copies of this file part
* @param copyPaths
* the copy paths
* @param this_part
* part number
* @param num_parts
* number of parts
* @param clearport
* port number of clear communications
* @param sslport
* port number of ssl communications
* @param filter
* the file filter object
* @param fileAccessBlob
* file access token
* @param fileType
* the file type
*/
private DataPartition(String[] copylocations, String[] copyPaths, int this_part, int num_parts, int clearport, boolean sslport, FileFilter filter,
String fileAccessBlob, FileType fileType) {
this(copylocations,copyPaths,this_part,num_parts,clearport,sslport,filter,fileAccessBlob,fileType,null);
}
/**
* Construct the data part, used by makeParts.
*
* @param copylocations
* locations of all copies of this file part
* @param copyPaths
* the copy paths
* @param this_part
* part number
* @param num_parts
* number of parts
* @param clearport
* port number of clear communications
* @param sslport
* port number of ssl communications
* @param filter
* the file filter object
* @param fileAccessBlob
* file access token
* @param fileType
* the file type
* @param fileName
* the file name
*/
private DataPartition(String[] copylocations, String[] copyPaths, int this_part, int num_parts, int clearport, boolean sslport, FileFilter filter,
String fileAccessBlob, FileType fileType, String fileName)
{
this.this_part = this_part;
this.num_parts = num_parts;
this.rowservicePort = clearport;
this.useSSL = sslport;
this.fileFilter = filter;
this.fileName=fileName;
if (this.fileFilter == null)
{
this.fileFilter = new FileFilter();
}
this.fileType = fileType;
this.fileAccessBlob = fileAccessBlob;
this.copyLocations = copylocations;
this.copyPaths = copyPaths;
}
/**
* Security access blob.
*
* @return security access blob
*/
public String getFileAccessBlob()
{
return this.fileAccessBlob;
}
/**
* Set the security access blob.
*
* @param accessBlob security access blob
*/
public void setFileAccessBlob(String accessBlob)
{
this.fileAccessBlob = accessBlob;
}
/**
* The underying file format for this partition .
*
* @return file type
*/
public FileType getFileType()
{
return this.fileType;
}
/**
* File part copy locations.
*
* @return copy locations
*/
public String[] getCopyLocations()
{
return this.copyLocations;
}
/**
* Location of the ith copy of this file part.
*
* @param copyindex
* the copyindex
* @return ip address
*/
public String getCopyIP(int copyindex)
{
int copiescount = copyLocations.length;
if (copyindex < 0 || copyindex >= copiescount)
{
return null;
}
return copyLocations[copyindex];
}
/**
* Set the copy IP
*
* @param copyIndex
* the copyindex
* @param copyIP The IP of the file part copy
*/
public void setCopyIP(int copyIndex, String copyIP)
{
if (copyIndex < 0 || copyIndex >= copyLocations.length)
{
return;
}
copyLocations[copyIndex] = copyIP;
}
/**
* Add file part copy
*
* @param index The index at which to insert the file part copy
* @param copyIP The IP of the new file part copy
* @param copyPath The path of the new file part copy
* @throws Exception The exception
*/
public void add(int index, String copyIP, String copyPath) throws Exception
{
if (index < 0 || index > copyLocations.length)
{
throw new InvalidParameterException("Insertion index: " + index + " is invalid."
+ "Expected index in range of: [0," + copyLocations.length + "]");
}
if (copyIP == null || copyPath == null)
{
throw new InvalidParameterException("Copy IP or Path are invalid, must be non-null.");
}
List<String> copyLocationsList = new ArrayList<>(Arrays.asList(copyLocations));
copyLocationsList.add(index, copyIP);
copyLocations = copyLocationsList.toArray(new String[0]);
List<String> copyPathList = new ArrayList<>(Arrays.asList(copyPaths));
copyPathList.add(index, copyPath);
copyPaths = copyPathList.toArray(new String[0]);
}
/**
* Count of copies available for this file part.
* @return copy locations size
*/
public int getCopyCount()
{
return copyLocations.length;
}
/**
* Port used for communication in clear.
* @return port number
*/
public int getPort()
{
return rowservicePort;
}
/**
* Port used for SSL communication.
*
* @return port
*/
public boolean getUseSsl()
{
return useSSL;
}
/**
* File name being read
*
* @return filename
*/
public String getFileName()
{
return fileName;
}
/**
* Copy Path.
*
* @param index
* the index
* @return name
*/
public String getCopyPath(int index)
{
if (index >= copyPaths.length)
{
return null;
}
return copyPaths[index];
}
/**
* This part's id.
*
* @return the id of this part
*/
public int getThisPart()
{
return this.this_part;
}
/**
* Number of parts for this file.
*
* @return number of parts
*/
public int getNumParts()
{
return this.num_parts;
}
/**
* The filter object to select specific rows.
*
* @return the filter object.
*/
public FileFilter getFilter()
{
return this.fileFilter;
}
/**
* Set the filter object to select specific rows.
*
* @param filter file filter
* @return the partition
*/
public DataPartition setFilter(FileFilter filter)
{
this.fileFilter = filter;
return this;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object
*/
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("part ").append(this.getThisPart()).append(", copy locations: {");
for (int copyindex = 0; copyindex < getCopyCount(); copyindex++)
{
if (copyindex > 0) sb.append(", ");
sb.append(this.getCopyIP(copyindex));
}
sb.append("} :");
sb.append(this.getPort());
return sb.toString();
}
/**
* Make an array of data partitions for the supplied HPCC File.
*
* @param dfupartcopies
* the dfupartcopies
* @param clusterremapper
* the clusterremapper
* @param max_parts
* the maximum number of partitions or zero for no limit
* @param fileAccessBlob
* the file access blob
* @return an array of partitions
* @throws HpccFileException
* the hpcc file exception
*/
public static DataPartition[] createPartitions(DFUFilePartWrapper[] dfupartcopies, ClusterRemapper clusterremapper, int max_parts,
String fileAccessBlob) throws HpccFileException
{
return createPartitions(dfupartcopies, clusterremapper, max_parts, FileFilter.nullFilter(), fileAccessBlob);
}
/**
* Creates the partitions.
*
* @param dfuparts
* the dfuparts
* @param clusterremapper
* the clusterremapper
* @param max_parts
* the max parts
* @param filter
* the filter
* @param fileAccessBlob
* the file access blob
* @return the data partition[]
* @throws HpccFileException
* the hpcc file exception
*/
public static DataPartition[] createPartitions(DFUFilePartWrapper[] dfuparts, ClusterRemapper clusterremapper, int max_parts, FileFilter filter,
String fileAccessBlob) throws HpccFileException
{
return createPartitions(dfuparts, clusterremapper, max_parts, FileFilter.nullFilter(), fileAccessBlob, FileType.FLAT);
}
/**
* Creates the partitions.
*
* @param dfuparts
* the dfuparts
* @param clusterremapper
* the clusterremapper
* @param max_parts
* the max parts
* @param filter
* the filter
* @param fileAccessBlob
* the file access blob
* @param fileType
* the file type
* @return the data partition[]
* @throws HpccFileException
* the hpcc file exception
*/
public static DataPartition[] createPartitions(DFUFilePartWrapper[] dfuparts, ClusterRemapper clusterremapper, int max_parts, FileFilter filter,
String fileAccessBlob, FileType fileType) throws HpccFileException {
return createPartitions(dfuparts,clusterremapper,max_parts,filter,fileAccessBlob,fileType,null);
}
/**
* Creates the partitions.
*
* @param dfuparts
* the dfuparts
* @param clusterremapper
* the clusterremapper
* @param max_parts
* the max parts
* @param filter
* the filter
* @param fileAccessBlob
* the file access blob
* @param fileType
* the file type
* @param fileName
* the file name
* @return the data partition[]
* @throws HpccFileException
* the hpcc file exception
*/
public static DataPartition[] createPartitions(DFUFilePartWrapper[] dfuparts, ClusterRemapper clusterremapper, int max_parts, FileFilter filter,
String fileAccessBlob, FileType fileType, String fileName) throws HpccFileException
{
DataPartition[] rslt = new DataPartition[dfuparts.length];
try
{
for (int i = 0; i < dfuparts.length; i++)
{
int numCopies = dfuparts[i].getCopies().length;
String[] copyPaths = new String[numCopies];
for (int j = 0; j < numCopies; j++)
{
copyPaths[j] = dfuparts[i].getCopies()[j].getCopyPath();
}
DataPartition new_dp = new DataPartition(clusterremapper.reviseIPs(dfuparts[i].getCopies()), copyPaths, dfuparts[i].getPartIndex(),
dfuparts.length, clusterremapper.revisePort(null), clusterremapper.getUsesSSLConnection(null), filter, fileAccessBlob,
fileType,fileName);
new_dp.isTLK = dfuparts[i].isTopLevelKey();
rslt[i] = new_dp;
}
}
catch (Exception e)
{
throw new HpccFileException("Could not create DataPartition!\n" + e.getMessage());
}
return rslt;
}
/**
* Index.
*
* @return the int
*/
public int index()
{
return this.this_part - 1;
}
/**
* Is this data partition the TLK
*
* @return isTLK
*/
public boolean isTLK()
{
return this.isTLK;
}
}