Mercurial > hg > monetdb-java
comparison example/OnClientExample.java @ 551:5ce6a942aff3 onclient
Last minute API fix: 'int offset' -> 'long linesToSkip'
Int -> long because there might be many lines
Offset -> linesToSkip to save users some work.
The COPY INTO statement has an OFFSET modifier which is 1-based
but also allows 0. This means both 0 and 1 mean 'upload the whole
file' and any N > 1 means skip N-1 lines and upload the rest.
To avoid having to deal with this over and over in every implementation
of MonetConnection.UploadHandler#handleUpload(), parameter 'offset'
has been replaced with 'linesToSkip' where the adjustment has already
been performed.
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Tue, 14 Sep 2021 10:52:04 +0200 (2021-09-14) |
parents | d462000fc410 |
children | 87feb93330a6 |
comparison
equal
deleted
inserted
replaced
550:c5cf3f00c4c5 | 551:5ce6a942aff3 |
---|---|
94 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize(); | 94 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize(); |
95 this.filesAreUtf8 = filesAreUtf8; | 95 this.filesAreUtf8 = filesAreUtf8; |
96 } | 96 } |
97 | 97 |
98 @Override | 98 @Override |
99 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { | 99 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException { |
100 | 100 |
101 // COPY OFFSET line numbers are 1-based but 0 is also allowed. | 101 // COPY OFFSET line numbers are 1-based but 0 is also allowed. |
102 // Compute the number of lines to skip | 102 // Compute the number of lines to skip |
103 long toSkip = offset <= 1 ? 0 : offset - 1; | |
104 | 103 |
105 // We can upload data read from the file system but also make up our own data | 104 // We can upload data read from the file system but also make up our own data |
106 if (name.equals("generated.csv")) { | 105 if (name.equals("generated.csv")) { |
107 uploadGenerated(handle, toSkip); | 106 uploadGenerated(handle, linesToSkip); |
108 return; | 107 return; |
109 } | 108 } |
110 | 109 |
111 // Validate the path, demonstrating two ways of dealing with errors | 110 // Validate the path, demonstrating two ways of dealing with errors |
112 Path path = securityCheck(name); | 111 Path path = securityCheck(name); |
122 } | 121 } |
123 | 122 |
124 boolean binary = !textMode; | 123 boolean binary = !textMode; |
125 if (binary) { | 124 if (binary) { |
126 uploadBinary(handle, path); | 125 uploadBinary(handle, path); |
127 } else if (toSkip == 0 && filesAreUtf8) { | 126 } else if (linesToSkip == 0 && filesAreUtf8) { |
128 // Avoid unnecessary character set conversions by pretending it's binary | 127 // Avoid unnecessary character set conversions by pretending it's binary |
129 uploadBinary(handle, path); | 128 uploadBinary(handle, path); |
130 } else { | 129 } else { |
131 // Charset and skip handling really necessary | 130 // Charset and skip handling really necessary |
132 uploadTextFile(handle, path, toSkip); | 131 uploadTextFile(handle, path, linesToSkip); |
133 } | 132 } |
134 } | 133 } |
135 | 134 |
136 private Path securityCheck(String name) { | 135 private Path securityCheck(String name) { |
137 Path p = uploadDir.resolve(name).normalize(); | 136 Path p = uploadDir.resolve(name).normalize(); |