Browse Source

Merge "Adds the lazy seek"

Jenkins 2 years ago
parent
commit
d95ffe7014

+ 22
- 0
hadoop-swiftfs/src/main/java/org/apache/hadoop/fs/swift/snative/SwiftNativeInputStream.java View File

@@ -89,6 +89,8 @@ class SwiftNativeInputStream extends FSInputStream {
89 89
    */
90 90
   private long rangeOffset = 0;
91 91
 
92
+  private long nextReadPosition = 0;
93
+
92 94
   public SwiftNativeInputStream(SwiftNativeFileSystemStore storeNative,
93 95
       FileSystem.Statistics statistics, Path path, long bufferSize)
94 96
           throws IOException {
@@ -138,6 +140,7 @@ class SwiftNativeInputStream extends FSInputStream {
138 140
     verifyOpen();
139 141
     int result = -1;
140 142
     try {
143
+      seekStream();
141 144
       result = httpStream.read();
142 145
     } catch (IOException e) {
143 146
       String msg = "IOException while reading " + path
@@ -297,6 +300,13 @@ class SwiftNativeInputStream extends FSInputStream {
297 300
    */
298 301
   @Override
299 302
   public synchronized void seek(long targetPos) throws IOException {
303
+    if (targetPos < 0) {
304
+      throw new IOException("Negative Seek offset not supported");
305
+    }
306
+    nextReadPosition = targetPos;
307
+  }
308
+
309
+  public synchronized void realSeek(long targetPos) throws IOException {
300 310
     if (targetPos < 0) {
301 311
       throw new IOException("Negative Seek offset not supported");
302 312
     }
@@ -344,6 +354,18 @@ class SwiftNativeInputStream extends FSInputStream {
344 354
     fillBuffer(targetPos);
345 355
   }
346 356
 
357
+  /**
358
+   * Lazy seek.
359
+   * @throws IOException
360
+   */
361
+  private void seekStream() throws IOException {
362
+    if (httpStream != null && nextReadPosition == pos) {
363
+      // already at specified position
364
+      return;
365
+    }
366
+    realSeek(nextReadPosition);
367
+  }
368
+
347 369
   /**
348 370
    * Fill the buffer from the target position
349 371
    * If the target position == current position, the

Loading…
Cancel
Save