comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 557:ce2b616ed22e onclient

Add a cancellation callback to UploadHandler
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Thu, 16 Sep 2021 15:12:26 +0200 (2021-09-16)
parents 87feb93330a6
children 7f10d662a788
comparison
equal deleted inserted replaced
556:87feb93330a6 557:ce2b616ed22e
3234 if (uploadHandler == null) { 3234 if (uploadHandler == null) {
3235 return "No file upload handler has been registered with the JDBC driver"; 3235 return "No file upload handler has been registered with the JDBC driver";
3236 } 3236 }
3237 3237
3238 long linesToSkip = offset >= 1 ? offset - 1 : 0; 3238 long linesToSkip = offset >= 1 ? offset - 1 : 0;
3239 Upload handle = new Upload(server); 3239 Upload handle = new Upload(server, uploadHandler::uploadCancelled);
3240 boolean wasFaking = server.setInsertFakePrompts(false); 3240 boolean wasFaking = server.setInsertFakePrompts(false);
3241 try { 3241 try {
3242 uploadHandler.handleUpload(handle, path, textMode, linesToSkip); 3242 uploadHandler.handleUpload(handle, path, textMode, linesToSkip);
3243 if (!handle.hasBeenUsed()) { 3243 if (!handle.hasBeenUsed()) {
3244 String message = "Call to " + uploadHandler.getClass().getCanonicalName() + ".handleUpload for path '" + path + "' sent neither data nor an error message"; 3244 String message = "Call to " + uploadHandler.getClass().getCanonicalName() + ".handleUpload for path '" + path + "' sent neither data nor an error message";
3288 * 0 means upload everything, 1 means skip the first line, etc. 3288 * 0 means upload everything, 1 means skip the first line, etc.
3289 * Note: this is different from the OFFSET option of the COPY INTO, 3289 * Note: this is different from the OFFSET option of the COPY INTO,
3290 * where both 0 and 1 mean 'upload everything' 3290 * where both 0 and 1 mean 'upload everything'
3291 */ 3291 */
3292 void handleUpload(Upload handle, String name, boolean textMode, long linesToSkip) throws IOException; 3292 void handleUpload(Upload handle, String name, boolean textMode, long linesToSkip) throws IOException;
3293
3294 /**
3295 * Called when the upload is cancelled halfway by the server.
3296 *
3297 * The default implementation does nothing.
3298 */
3299 default void uploadCancelled() {}
3293 } 3300 }
3294 3301
3295 /** 3302 /**
3296 * Callback for receiving files with COPY ON CLIENT 3303 * Callback for receiving files with COPY ON CLIENT
3297 * 3304 *
3314 /** 3321 /**
3315 * Handle passed to {@link UploadHandler} to allow communication with the server 3322 * Handle passed to {@link UploadHandler} to allow communication with the server
3316 */ 3323 */
3317 public static class Upload { 3324 public static class Upload {
3318 private final MapiSocket server; 3325 private final MapiSocket server;
3326 private final Runnable cancellationCallback;
3319 private PrintStream print = null; 3327 private PrintStream print = null;
3320 private String error = null; 3328 private String error = null;
3321 private int customChunkSize = -1; 3329 private int customChunkSize = -1;
3322 3330
3323 Upload(MapiSocket server) { 3331 Upload(MapiSocket server, Runnable cancellationCallback) {
3324 this.server = server; 3332 this.server = server;
3333 this.cancellationCallback = cancellationCallback;
3325 } 3334 }
3326 3335
3327 /** 3336 /**
3328 * Send an error message to the server 3337 * Send an error message to the server
3329 * 3338 *
3360 throw new IOException("Cannot send data after an error has been sent"); 3369 throw new IOException("Cannot send data after an error has been sent");
3361 } 3370 }
3362 if (print == null) { 3371 if (print == null) {
3363 try { 3372 try {
3364 MapiSocket.UploadStream up = customChunkSize >= 0 ? server.uploadStream(customChunkSize) : server.uploadStream(); 3373 MapiSocket.UploadStream up = customChunkSize >= 0 ? server.uploadStream(customChunkSize) : server.uploadStream();
3374 up.setCancellationCallback(cancellationCallback);
3365 print = new PrintStream(up, false, "UTF-8"); 3375 print = new PrintStream(up, false, "UTF-8");
3366 up.write('\n'); 3376 up.write('\n');
3367 } catch (UnsupportedEncodingException e) { 3377 } catch (UnsupportedEncodingException e) {
3368 throw new RuntimeException("The system is guaranteed to support the UTF-8 encoding but apparently it doesn't", e); 3378 throw new RuntimeException("The system is guaranteed to support the UTF-8 encoding but apparently it doesn't", e);
3369 } 3379 }