-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGPUdbFileHandler.java
941 lines (813 loc) · 38.8 KB
/
GPUdbFileHandler.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
package com.gpudb.filesystem;
import com.gpudb.GPUdb;
import com.gpudb.GPUdbException;
import com.gpudb.GPUdbLogger;
import com.gpudb.filesystem.common.FileOperation;
import com.gpudb.filesystem.common.KifsDirectoryInfo;
import com.gpudb.filesystem.common.KifsFileInfo;
import com.gpudb.filesystem.download.DownloadOptions;
import com.gpudb.filesystem.download.FileDownloadListener;
import com.gpudb.filesystem.download.FileDownloader;
import com.gpudb.filesystem.ingest.FileIngestor;
import com.gpudb.filesystem.ingest.IngestOptions;
import com.gpudb.filesystem.ingest.IngestResult;
import com.gpudb.filesystem.ingest.TableCreationOptions;
import com.gpudb.filesystem.upload.FileUploadListener;
import com.gpudb.filesystem.upload.FileUploader;
import com.gpudb.filesystem.upload.UploadOptions;
import com.gpudb.protocol.*;
import java.util.*;
/**
* This is the main class which exposes the API to be used by the end users.
* This class exposes methods to upload e.g.,
* ({@link #upload(String, String, UploadOptions, FileUploadListener)},
* {@link #upload(List, String, UploadOptions, FileUploadListener)})
* and similarly for download.
*
* The entire filesystem API is exposed through this class and an instance of
* this class is enough to consume all functionalities provided by the KIFS.
*
* Apart from this class the other classes which could be used by the users of
* this API are:
* {@link UploadOptions}
* {@link DownloadOptions}
* {@link FileUploadListener}
* {@link FileDownloadListener}
*/
public class GPUdbFileHandler {
private static final int DEFAULT_THREAD_POOL_TERMINATION_TIMEOUT = 90;
private static final int DEFAULT_FULL_FILE_DISPATCHER_THREADPOOL_SIZE = 5;
private static final long DEFAULT_FILE_SIZE_TO_SPLIT = 62914560; //60 MB
private final GPUdb db;
private final GPUdbFileHandler.Options options;
/**
* Constructs a {@link GPUdbFileHandler} object that allows the user to
* upload and download files.
* @param db - {@link GPUdb} instance
* @param options - An instance of {@link GPUdbFileHandler.Options} class
*/
public GPUdbFileHandler(GPUdb db, Options options) {
this.db = db;
if( options != null ) {
this.options = options;
} else {
this.options = new Options();
}
}
/**
* Constructor using default {@link Options}
* @param db - the {@link GPUdb} instance
*/
public GPUdbFileHandler( GPUdb db ) {
this.db = db;
options = new Options();
}
public static int getDefaultThreadPoolTerminationTimeout() {
return DEFAULT_THREAD_POOL_TERMINATION_TIMEOUT;
}
public static int getDefaultFullFileDispatcherThreadpoolSize() {
return DEFAULT_FULL_FILE_DISPATCHER_THREADPOOL_SIZE;
}
/**
* Returns the {@link GPUdbFileHandler.Options} instance
*/
public Options getOptions() {
return options;
}
/** This method facilitates the upload of a single file from a given
* local directory to a given remote directory. The filename passed in
* is preserved between the client and the server. This method uses the
* default Upload options and does not use a callback.
*
* @param fileName - Name of the file along with the full path
* e.g., "/home/user1/dir1/dir2/a.txt"
* @param remoteDirName - Name of the remote directory to which the
* file is to be saved.
* @throws GPUdbException - from the method {@link #upload(List, String, UploadOptions, FileUploadListener)}
*
* @see FileUploadListener
* @see UploadOptions
* @see UploadOptions#defaultOptions()
* @see #upload(String, String, UploadOptions, FileUploadListener)
*
*/
public void upload(final String fileName, final String remoteDirName) throws GPUdbException {
upload(fileName, remoteDirName, UploadOptions.defaultOptions(), null );
}
/** This method facilitates the upload of a single file from a given
* local directory to a given remote directory. The filename passed in
* is preserved between the client and the server.
*
* @param fileName - Name of the file along with the full path
* e.g., "/home/user1/dir1/dir2/a.txt"
* @param remoteDirName - Name of the remote directory to which the
* file is to be saved.
* @param options - {@link UploadOptions} - Various options to be used for
* uploading a file. If the value passed is null then
* default values would be used. The default values for
* the options can also be set explicitly using the method
* {@link UploadOptions#defaultOptions()}.
*
* @param callback - An instance of {@link FileUploadListener} class which
* is used to report status of an ongoing/completed upload
* to the caller of the method.
*
* @throws GPUdbException - from the method {@link #upload(List, String, UploadOptions, FileUploadListener)}
*
* @see FileUploadListener
* @see UploadOptions
*
*/
public void upload(final String fileName,
final String remoteDirName,
UploadOptions options,
FileUploadListener callback) throws GPUdbException {
upload( Collections.singletonList( fileName ), remoteDirName, options, callback );
}
/** This method facilitates uploading of multiple files from the same
* local directory to a given remote directory. The filenames are preserved
* between the client and the server. This method uses default Upload options
* and does not use a callback.
*
* @param fileNames - List<String> - Names of the files to be uploaded
* including the full local path.
* @param remoteDirName - Name of the remote directory to which the
* file is to be saved. This indicates a KIFS directory
* @throws GPUdbException - thrown from {@link #upload(List, String, UploadOptions, FileUploadListener)}
*
* @see FileUploadListener
* @see UploadOptions
* @see UploadOptions#defaultOptions()
* @see #upload(List, String, UploadOptions, FileUploadListener)
*/
public void upload(final List<String> fileNames, final String remoteDirName) throws GPUdbException {
upload(fileNames, remoteDirName, UploadOptions.defaultOptions(), null );
}
/** This method facilitates uploading of multiple files from the same
* local directory to a given remote directory. The filenames are preserved
* between the client and the server.
*
* @param fileNames - List<String> - Names of the files to be uploaded
* including the full local path.
* @param remoteDirName - Name of the remote directory to which the
* file is to be saved. This indicates a KIFS directory
* @param uploadOptions - {@link UploadOptions} - Various uploadOptions to be used for
* uploading a file. If the value passed is null then
* default values would be used. The default values for
* the uploadOptions can also be set explicitly using the method
* {@link UploadOptions#defaultOptions()}.
* @param callback - An instance of {@link FileUploadListener} class which
* is used to report status of an ongoing/completed upload
* to the caller of the method.
*
* @throws GPUdbException - Where parameters value checks fail
*
* @see FileUploadListener
* @see UploadOptions
*/
public void upload(final List<String> fileNames,
final String remoteDirName,
UploadOptions uploadOptions,
FileUploadListener callback) throws GPUdbException {
// This method will throw an exception if one or more files are not
// found and will log an error message with the ones that are not found.
final List<String> nonExistentFileNames = new ArrayList<>();
if( fileNames != null && fileNames.size() > 0 ) {
for (String fileName : fileNames) {
if( fileName != null && !fileName.isEmpty() ) {
if ( !FileOperation.localFileExists( fileName ) ) {
nonExistentFileNames.add( fileName );
}
} else {
throw new GPUdbException( "Name of local file to upload cannot be null or empty" );
}
}
} else {
throw new GPUdbException( "List of local files to upload cannot be null or empty" );
}
if( nonExistentFileNames.size() > 0 ) {
throw new GPUdbException( String.format( "Input file names [%s] are not found ", nonExistentFileNames ) );
}
if( uploadOptions == null ) {
uploadOptions = UploadOptions.defaultOptions();
}
FileUploader fileUploader = new FileUploader(
db,
fileNames,
remoteDirName,
uploadOptions,
callback, this.options
);
fileUploader.upload();
}
/**
* This method downloads files from a KIFS
* directory to a local directory. The local directory must exist or else
* this method will throw an exception. The 'fileNames' argument contains
* a list of fully qualified file names on a KIFS directory. This method
* uses default download options and doesn't use a callback.
*
* @param fileNames - A list of fully qualified file names
* residing on the KIFS directory e.g., 'a/b/c/d.txt'
* where 'a/b/c' is a full path on the KIFS.
* @param localDirName - String - This is the name of a local directory where the
* files will be saved. This directory must exist on the
* local filesystem.
*
* @see DownloadOptions
* @see DownloadOptions#defaultOptions()
* @see FileDownloadListener
*/
public void download(List<String> fileNames, String localDirName) throws GPUdbException {
download(fileNames, localDirName, DownloadOptions.defaultOptions(), null );
}
/**
* This method downloads files from a KIFS
* directory to a local directory. The local directory must exist or else
* this method will throw an exception. The 'fileNames' argument contains
* a list of fully qualified file names on a KIFS directory.
*
* @param fileNames - A list of fully qualified file names
* residing on the KIFS directory e.g., 'a/b/c/d.txt'
* where 'a/b/c' is a full path on the KIFS.
* @param localDirName - String - This is the name of a local directory where the
* files will be saved. This directory must exist on the
* local filesystem.
* @param downloadOptions - {@link DownloadOptions} - Options to be used for
* downloading files from KIFS to the local directory.
* if the value passed is null then default values
* would be used. The default values for the options
* can also be set explicitly using the method
* {@link DownloadOptions#defaultOptions()}
* @param callback - An instance of {@link FileDownloadListener} class which
* is used to report status of an ongoing/completed download
* to the caller of the method.
*/
public void download(List<String> fileNames,
String localDirName,
DownloadOptions downloadOptions,
FileDownloadListener callback) throws GPUdbException {
if( fileNames!= null && fileNames.size() > 0 ) {
for ( String fileName: fileNames ) {
if( fileName == null || fileName.isEmpty() ) {
throw new GPUdbException( "KIFS file name cannot be null or empty" );
}
}
} else {
throw new GPUdbException( "List of KIFS file names cannot be null or empty" );
}
// This method will throw an exception if the local directory name
// passed using 'localDirName' is not found
if( !FileOperation.localDirExists( localDirName ) ) {
throw new GPUdbException( String.format( "Local directory %s does not exist", localDirName ) );
}
if( downloadOptions == null ) {
downloadOptions = DownloadOptions.defaultOptions();
}
FileDownloader downloader = new FileDownloader(
db,
fileNames,
localDirName,
downloadOptions,
callback,
options
);
downloader.download();
}
/**
* This method downloads a single file from a KIFS
* directory to a local directory. The local directory must exist or else
* this method will throw an exception. The 'fileNames' argument contains
* a list of fully qualified file names on a KIFS directory.
* @param fileName - String - A fully qualified file name
* residing on the KIFS directory e.g., 'a/b/c/d.txt'
* where 'a/b/c' is a full path on the KIFS.
* @param localDirName - String - This is the name of a local directory where the
* files will be saved. This directory must exist on the
* local filesystem.
*
*/
public void download(String fileName, String localDirName) throws GPUdbException {
download(fileName, localDirName, DownloadOptions.defaultOptions(), null );
}
/**
* This method downloads a single file from a KIFS
* directory to a local directory. The local directory must exist or else
* this method will throw an exception. The 'fileNames' argument contains
* a list of fully qualified file names on a KIFS directory.
*
* @param fileName - String - A fully qualified file name
* residing on the KIFS directory e.g., 'a/b/c/d.txt'
* where 'a/b/c' is a full path on the KIFS.
* @param localDirName - String - This is the name of a local directory where the
* files will be saved. This directory must exist on the
* local filesystem.
* @param downloadOptions - {@link DownloadOptions} - Options to be used for
* downloading files from KIFS to the local directory.
* if the value passed is null then default values
* would be used. The default values for the options
* can also be set explicitly using the method
* {@link DownloadOptions#defaultOptions()}
*
*/
public void download(String fileName,
String localDirName,
DownloadOptions downloadOptions,
FileDownloadListener callback) throws GPUdbException {
download( Collections.singletonList( fileName ), localDirName, downloadOptions, callback );
}
/**
* This method will download all files in a KIFS directory to a directory on
* the local filesystem.
*
* @param remoteDirName - Name of the KIFS directory
* @param localDirName - Name of the local directory
* @param downloadOptions - A {@link DownloadOptions} object
* @throws GPUdbException - throws a GPUdbException if
* 1. the remoteDirName is not existing
* 2. the localDirName is not existing
* 3. something goes wrong with the download
*
* @see DownloadOptions
*/
public void downloadDir(String remoteDirName,
String localDirName,
DownloadOptions downloadOptions,
FileDownloadListener callback) throws GPUdbException {
// This method will throw an exception if the local directory name
// passed using 'localDirName' is not found
if( !FileOperation.localDirExists( localDirName ) ) {
throw new GPUdbException( String.format( "Local directory %s does not exist", localDirName ) );
}
// Get the list of all files in the remote directory
// If there is an error here, an exception is handled and re-thrown
ShowFilesResponse showFilesResponse = db.showFiles( Collections.singletonList(remoteDirName), new HashMap<String, String>() );
// Download all the files found if there is no error
// The call to the download method could throw exceptions
// while checking for existence of local directory to save the
// downloaded files.
if( !showFilesResponse.getFileNames().isEmpty() ) {
download(showFilesResponse.getFileNames(), localDirName, downloadOptions, callback);
}
}
/**
* This method will ingest several files in one go. It will use the
* method {@link #upload(List, String, UploadOptions, FileUploadListener)} to
* upload the files to the directory 'sys_temp' on the KIFS and use the
* method {@link GPUdb#insertRecordsFromFiles(String, List, Map, Map, Map)}
* to update the table data.
*
* @param fileNames - Names of the local files to be ingested
* @param tableName - Name of the table to ingest into
* @param ingestOptions - Options for ingestion
* @param createTableOptions - Options for table creation
*
* @throws - {@link GPUdbException} - if the ingestion or the underlying upload fails
*
* @see IngestOptions
* @see TableCreationOptions
*/
public void ingest( final List<String> fileNames,
final String tableName,
final IngestOptions ingestOptions,
final TableCreationOptions createTableOptions ) throws GPUdbException{
// This method will throw an exception if one or more files are not
// found and will log an error message with the ones that are not found.
final List<String> nonExistentFileNames = new ArrayList<>();
if( fileNames != null && fileNames.size() > 0 ) {
for (String fileName : fileNames) {
if( fileName != null && !fileName.isEmpty() ) {
if ( !FileOperation.localFileExists( fileName ) ) {
nonExistentFileNames.add( fileName );
}
} else {
throw new GPUdbException( "Name of file to ingest cannot be null or empty" );
}
}
} else {
throw new GPUdbException( "List of files to ingest cannot be null or empty" );
}
if( nonExistentFileNames.size() > 0 ) {
throw new GPUdbException( String.format( "Input file names [%s] to ingest are not found ", nonExistentFileNames ) );
}
FileIngestor ingestor = new FileIngestor(
db,
tableName,
fileNames,
ingestOptions,
createTableOptions
);
IngestResult ingestResult = ingestor.ingestFromFiles();
if(ingestResult.getException() != null ) {
throw new GPUdbException( ingestResult.getErrorMessage() );
}
}
/**
* This method takes a list of fully qualified KIFS file paths and deletes
* the files in one go.
* @param fileNames - a list of files on the KIFS
*
*/
public void deleteFiles(List<String> fileNames) throws GPUdbException {
deleteFiles(fileNames, true);
}
/**
* This method takes a list of fully qualified KIFS file paths and deletes
* the files in one go.
*
* @param fileNames - a list of files on the KIFS
* @param noErrorIfNotExists - true indicates not to throw an error if the
* files are not found on KIFS and false otherwise
*/
public void deleteFiles(List<String> fileNames, boolean noErrorIfNotExists) throws GPUdbException {
Map<String, String> options = new HashMap<>();
options.put( DeleteFilesRequest.Options.NO_ERROR_IF_NOT_EXISTS,
noErrorIfNotExists ? DeleteFilesRequest.Options.TRUE : DeleteFilesRequest.Options.FALSE );
db.deleteFiles( fileNames, options );
}
/**
* This method deletes files in the KIFS directory whose name is passed in
* using {@code remoteDirName}. This method uses default options
*
* @param remoteDirName - Name of the KIFS directory
* @throws GPUdbException - from {@link #deleteFilesInDir(String, boolean)}
*/
public void deleteFilesInDir(String remoteDirName) throws GPUdbException {
deleteFilesInDir(remoteDirName, true);
}
/**
* This method deletes files in the KIFS directory whose name is passes in
* using {@code remoteDirName}.
* @param remoteDirName - Name of the KIFS directory
* @param noErrorIfNotExists - Indicates whether to allow deletion of files
* in non-existent KIFS directory or not.
* @throws GPUdbException - from downstream API calls
*/
public void deleteFilesInDir(String remoteDirName, boolean noErrorIfNotExists) throws GPUdbException {
// Retrieve the files in the directory and delete them
ShowFilesResponse showFilesResponse = db.showFiles( Collections.singletonList( remoteDirName ), new HashMap<String, String>());
Map<String, String> deleteFilesOptions = new HashMap<>();
deleteFilesOptions.put( DeleteFilesRequest.Options.NO_ERROR_IF_NOT_EXISTS ,
noErrorIfNotExists ? DeleteFilesRequest.Options.TRUE : DeleteFilesRequest.Options.FALSE );
db.deleteFiles( showFilesResponse.getFileNames(), deleteFilesOptions );
}
/**
* This method shows the files
* @param remoteDirNames - List of KIFS directory names
* @return - List of {@link KifsFileInfo} objects
*/
public List<KifsFileInfo> showFiles(List<String> remoteDirNames) throws GPUdbException {
List<KifsFileInfo> resp = new ArrayList<>();
if( remoteDirNames == null || remoteDirNames.isEmpty() ) {
throw new GPUdbException( "List of KIFS directory names cannot be null or empty list" );
}
if( kifsDirectoriesExist( new LinkedHashSet<>( remoteDirNames ) ) ) {
ShowFilesResponse sfResp = db.showFiles( remoteDirNames, new HashMap<String, String>() );
int count = sfResp.getFileNames().size();
for( int i = 0; i < count; i++ ) {
KifsFileInfo fileInfo = new KifsFileInfo();
fileInfo.setFileName( sfResp.getFileNames().get( i ) );
fileInfo.setFileSize( sfResp.getSizes().get( i ) );
fileInfo.setCreationTime( sfResp.getCreationTimes().get( i ) );
fileInfo.setCreatedBy( sfResp.getUsers().get( i ) );
fileInfo.setInfo( sfResp.getInfo() );
resp.add( fileInfo );
}
return resp;
} else {
GPUdbLogger.error( String.format( "One or more KIFS directories/files in [%s] does not exist", remoteDirNames ) );
return null;
}
}
/**
* This method retrieves the directory information for a given list of KIFS
* directories and returns the information as a list of {@link KifsDirectoryInfo}
* objects.
*
* @param remoteDirNames - Set of KIFS directory names
* @return - list of {@link KifsDirectoryInfo} objects
* @throws GPUdbException - from {@link #showDirectories(Set, Map)}
* @see #showDirectories(Set, Map)
*/
public List<KifsDirectoryInfo> showDirectories(Set<String> remoteDirNames) throws GPUdbException {
return showDirectories(remoteDirNames, new HashMap<String, String>());
}
/**
* This method retrieves the directory information for a given list of KIFS
* directories and returns the information as a list of {@link KifsDirectoryInfo}
* objects.
* @param remoteDirNames - Set of names of the KIFS directories
* @param options - Map of String to String; the endpoint expects an empty
* map.
* @return - List of {@link KifsDirectoryInfo} objects.
*/
public List<KifsDirectoryInfo> showDirectories(Set<String> remoteDirNames,
Map<String, String> options) throws GPUdbException {
if( remoteDirNames == null || remoteDirNames.isEmpty() ) {
throw new GPUdbException( "Set of KIFS directory names cannot be null or empty" );
}
List<KifsDirectoryInfo> showDirResp = new ArrayList<>();
for( String dirName: remoteDirNames ) {
if( dirName == null || dirName.trim().isEmpty() ) {
throw new GPUdbException( "Set of KIFS directory names cannot contain null or empty directory name" );
} else {
ShowDirectoriesResponse resp = db.showDirectories( dirName, options );
KifsDirectoryInfo dirInfo = new KifsDirectoryInfo();
dirInfo.setKifsPath( resp.getDirectories().get(0) );
dirInfo.setCreatedBy( resp.getUsers().get(0) );
dirInfo.setPermission( resp.getPermissions().get(0) );
dirInfo.setCreationTime( resp.getCreationTimes().get(0) );
showDirResp.add( dirInfo );
}
}
return showDirResp;
}
/**
* This method retrieves the directory information for a given KIFS
* directory and returns the information as a list of {@link KifsDirectoryInfo}
* objects. This method uses default options.
*
* @param remoteDirName - Name of the KIFS directory, cannot be null or empty
* @return - List of {@link KifsDirectoryInfo} objects.
* @see #showDirectory(String, Map)
*/
public List<KifsDirectoryInfo> showDirectory(String remoteDirName) throws GPUdbException {
return showDirectory(remoteDirName, new HashMap<String, String>());
}
/**
* This method retrieves the directory information for a given KIFS
* directory and returns the information as a list of {@link KifsDirectoryInfo}
* objects.
* @param remoteDirName - Name of the KIFS directory, cannot be null or empty
* @param options - Map of String to String; the endpoint expects an empty
* map.
* @return - List of {@link KifsDirectoryInfo} objects.
*/
public List<KifsDirectoryInfo> showDirectory(String remoteDirName,
Map<String, String> options) throws GPUdbException {
if( remoteDirName == null || remoteDirName.isEmpty() ) {
throw new GPUdbException( "KIFS directory name [remoteDirName] cannot be null or empty" );
}
List<KifsDirectoryInfo> showDirResp = new ArrayList<>();
ShowDirectoriesResponse resp = db.showDirectories( remoteDirName, options );
int count = resp.getDirectories().size();
for( int i=0; i < count; i++) {
KifsDirectoryInfo dirInfo = new KifsDirectoryInfo();
dirInfo.setKifsPath( resp.getDirectories().get(i) );
dirInfo.setCreatedBy( resp.getUsers().get(i) );
dirInfo.setPermission( resp.getPermissions().get(i) );
dirInfo.setCreationTime( resp.getCreationTimes().get(i) );
showDirResp.add( dirInfo );
}
return showDirResp;
}
/**
* This method retrieves the directory information all KIFS
* directories and returns the information as a list of {@link KifsDirectoryInfo}
* objects.
*
* @return - a list of {@link KifsDirectoryInfo} objects
* @throws GPUdbException - from {@link #showAllDirectories(Map)}
* @see #showAllDirectories(Map)
*/
public List<KifsDirectoryInfo> showAllDirectories() throws GPUdbException {
return showAllDirectories(new HashMap<String, String>());
}
/**
* This method retrieves the directory information all KIFS
* directories and returns the information as a list of {@link KifsDirectoryInfo}
* objects.
* @param options - Map of String to String; the endpoint expects an empty
* map.
* @return - List of {@link KifsDirectoryInfo} objects.
* @throws GPUdbException - from downstream API call
*/
public List<KifsDirectoryInfo> showAllDirectories( Map<String, String> options) throws GPUdbException {
List<KifsDirectoryInfo> showDirResp = new ArrayList<>();
ShowDirectoriesResponse resp = db.showDirectories( "", options );
int count = resp.getDirectories().size();
for( int i=0; i < count; i++) {
KifsDirectoryInfo dirInfo = new KifsDirectoryInfo();
dirInfo.setKifsPath( resp.getDirectories().get(i) );
dirInfo.setCreatedBy( resp.getUsers().get(i) );
dirInfo.setPermission( resp.getPermissions().get(i) );
dirInfo.setCreationTime( resp.getCreationTimes().get(i) );
showDirResp.add( dirInfo );
}
return showDirResp;
}
/**
* This method will create a KIFS directory with default options
*
* @param remoteDirName - String - Name of the KIFS directory to create
* @throws GPUdbException - from {@link #createDirectory(String, boolean)}
*/
public void createDirectory(String remoteDirName) throws GPUdbException {
createDirectory(remoteDirName, true);
}
/**
* This method will create a KIFS directory with options as explained
* below.
*
* @param remoteDirName - String - Name of the KIFS directory to create
* @param noErrorIfExists - boolean - true means if the directory exists
* there will be no error thrown and false means
* an existing directory name will throw an error
*/
public void createDirectory(String remoteDirName, boolean noErrorIfExists) throws GPUdbException {
Map<String, String> options = new HashMap<>();
options.put( CreateDirectoryRequest.Options.NO_ERROR_IF_EXISTS ,
noErrorIfExists ? CreateDirectoryRequest.Options.TRUE : CreateDirectoryRequest.Options.FALSE);
db.createDirectory(remoteDirName, options);
}
/**
* This method deletes a KIFS directory. This method uses default options
*
* @param remoteDirName - Name of the KIFS directory
* @throws GPUdbException - from {@link #deleteDirectory(String, boolean, boolean)}
* @see #deleteDirectory(String, boolean, boolean)
*/
public void deleteDirectory(String remoteDirName) throws GPUdbException {
deleteDirectory(remoteDirName, true, true);
}
/**
* This method deletes a KIFS directory. It will do a recursive delete if
* such an option is given.
*
* @param remoteDirName - Name of the KIFS directory
* @param recursive - Indicates deletion of all sub-directories
* @param noErrorIfNotExists - If set to true will ignore if the directory
* does not exist
*/
public void deleteDirectory(String remoteDirName,
boolean recursive,
boolean noErrorIfNotExists) throws GPUdbException {
Map<String, String> options = new HashMap<>();
options.put( DeleteDirectoryRequest.Options.RECURSIVE,
recursive ? DeleteDirectoryRequest.Options.TRUE : DeleteDirectoryRequest.Options.FALSE);
options.put( DeleteDirectoryRequest.Options.NO_ERROR_IF_NOT_EXISTS,
noErrorIfNotExists ? DeleteDirectoryRequest.Options.TRUE : DeleteDirectoryRequest.Options.FALSE);
db.deleteDirectory(remoteDirName, options);
}
/**
* This class models the options available for modifying some behaviours
* of the {@link GPUdbFileHandler} class
*
* The two options available right now are:
* 1. threadPoolTerminationTimeout - in seconds
* 2. fullFileDispatcherThreadpoolSize - an integer
*/
public static final class Options {
private long fileSizeToSplit;
private int fullFileDispatcherThreadpoolSize;
/**
* Constructor with default values for the class members
*/
public Options() {
this.fullFileDispatcherThreadpoolSize = DEFAULT_FULL_FILE_DISPATCHER_THREADPOOL_SIZE;
this.fileSizeToSplit = DEFAULT_FILE_SIZE_TO_SPLIT;
}
/**
* A copy constructor
* @param other - {@link Options} instance to copy from
*/
public Options( Options other ) {
this.fullFileDispatcherThreadpoolSize = other.fullFileDispatcherThreadpoolSize;
this.fileSizeToSplit = other.fileSizeToSplit;
}
/**
* This value is used to configure the size of the thread pool used
* internally to handle batches of full file uploads. The default value
* is set to 5
* @return Return the value of the 'fullFileDispatcherThreadpoolSize'
*
* @see #setFullFileDispatcherThreadpoolSize(int)
*/
public int getFullFileDispatcherThreadpoolSize() {
return fullFileDispatcherThreadpoolSize;
}
/**
* This value is used to configure the size of the thread pool used
* internally to handle batches of full file uploads. The default value
* is set to 5
* @param fullFileDispatcherThreadpoolSize - an Integer value indicating
* the size of the thread pool
* @return - the current Options instance
*
* @see #getFullFileDispatcherThreadpoolSize()
*/
public Options setFullFileDispatcherThreadpoolSize(int fullFileDispatcherThreadpoolSize) throws GPUdbException {
if( fullFileDispatcherThreadpoolSize <= 0
|| fullFileDispatcherThreadpoolSize > Runtime.getRuntime().availableProcessors() ) {
throw new GPUdbException(String.format("Thread pool size must be between 1 and %d", Runtime.getRuntime().availableProcessors()));
}
this.fullFileDispatcherThreadpoolSize = fullFileDispatcherThreadpoolSize;
return this;
}
/**
* Split files greater than this size so that they are uploaded multi-part.
* This value is also used to split multiple small files into batches
* where the total size of the files in each batch is determined by this
* value.
*
* Gets the value of 'fileSizeToSplit'
*
* @return - the file size in bytes
*
* @see #setFileSizeToSplit(long)
*/
public long getFileSizeToSplit() {
return fileSizeToSplit;
}
/**
* Split files greater than this size so that they are uploaded multi-part.
* This value is also used to split multiple small files into batches
* where the total size of the files in each batch is determined by this
* value.
*
* Sets the value of 'fileSizeToSplit'
*
* @param fileSizeToSplit - the file size in bytes
*/
public void setFileSizeToSplit( long fileSizeToSplit ) throws GPUdbException {
if( fileSizeToSplit <=0 || fileSizeToSplit > DEFAULT_FILE_SIZE_TO_SPLIT ) {
throw new GPUdbException( String.format( "FileSizeToSplit : Value must be a positive value less than or equal to %s", DEFAULT_FILE_SIZE_TO_SPLIT ));
}
this.fileSizeToSplit = fileSizeToSplit;
}
}
/**
* This method checks whether KIFS paths exist or not.
*
* @param dirNames - the set of full KIFS paths
* @return - true if all the directories exist and false if one or more of them
* does not exist
*/
public boolean kifsDirectoriesExist( Set<String> dirNames ) {
if( dirNames == null || dirNames.isEmpty() ) {
GPUdbLogger.error( "List of Directory names cannot be null or empty" );
return false;
}
// Get all the KIFS directories and put them into a Set
ShowDirectoriesResponse sdResp;
try {
sdResp = db.showDirectories( "", new HashMap<String, String>());
Set<String> remoteDirNames = new LinkedHashSet<>( sdResp.getDirectories() );
// Find the intersection of the two sets
remoteDirNames.retainAll( dirNames );
// Return true if the intersection equals the input set
return remoteDirNames.equals( dirNames );
} catch (GPUdbException e) {
GPUdbLogger.error( e.getMessage() );
return false;
}
}
/**
* This method checks whether a KIFS path exists or not.
*
* @param dirName - the full KIFS paths
* @return - true if the directory exists and false otherwise
* Also returns false if the GPUdb instance is null.
*/
public boolean kifsDirectoryExists( String dirName ) {
// 'show/directories' will only return the KIFS roots so an embedded
// '/' will always result in a failed match.
if( dirName == null || dirName.isEmpty() || dirName.contains( "/" )) {
GPUdbLogger.error( "KIFS directory name cannot be null or empty and cannot contain an embedded '/' " );
return false;
}
// Get all the KIFS directories and put them into a Set
ShowDirectoriesResponse sdResp = null;
try {
sdResp = db.showDirectories( dirName, new HashMap<String, String>());
// Return true if the list of directories returned contain the input
// directory name
return sdResp.getDirectories().contains( dirName );
} catch (GPUdbException e) {
GPUdbLogger.error( e.getMessage() );
return false;
}
}
/**
* This method checks whether a KIFS file exists or not.
*
* @param fileName - the full KIFS path to a file
* @return - true if the file exists and false otherwise
*/
public boolean kifsFileExists( String fileName ) {
if( fileName == null || fileName.isEmpty() ) {
GPUdbLogger.error( "KIFS file name cannot be null" );
return false;
}
ShowFilesResponse sfResp = null;
try {
sfResp = db.showFiles( Collections.singletonList( fileName ), new HashMap<String, String>());
// Return true if the list of directories returned contain the input
// directory name
return sfResp.getFileNames().contains( fileName );
} catch (GPUdbException e) {
GPUdbLogger.error( e.getMessage() );
return false;
}
}
}