You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2022/01/28 01:21:09 UTC

[iotdb] branch master updated: Avoid flush stream in get position() to improve flush performance (#4963)

This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 7614350  Avoid flush stream in get position() to improve flush performance (#4963)
7614350 is described below

commit 76143508036b8bffe1d9de162ffc4c9205979653
Author: Jianyun Cheng <ch...@360.cn>
AuthorDate: Fri Jan 28 09:20:35 2022 +0800

    Avoid flush stream in get position() to improve flush performance (#4963)
---
 .../tsfile/write/writer/LocalTsFileOutput.java     | 41 ++++++++++++++--------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
index 392ee98..e69915f 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/LocalTsFileOutput.java
@@ -19,8 +19,6 @@
 package org.apache.iotdb.tsfile.write.writer;
 
 import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -30,40 +28,51 @@ import java.nio.ByteBuffer;
  * a TsFileOutput implementation with FileOutputStream. If the file is not existed, it will be
  * created. Otherwise the file will be written from position 0.
  */
-public class LocalTsFileOutput implements TsFileOutput {
+public class LocalTsFileOutput extends OutputStream implements TsFileOutput {
 
   private FileOutputStream outputStream;
   private BufferedOutputStream bufferedStream;
-
-  LocalTsFileOutput(File file, boolean append) throws FileNotFoundException {
-    this.outputStream = new FileOutputStream(file, append);
-    this.bufferedStream = new BufferedOutputStream(outputStream);
-  }
+  private long position;
 
   public LocalTsFileOutput(FileOutputStream outputStream) {
     this.outputStream = outputStream;
     this.bufferedStream = new BufferedOutputStream(outputStream);
+    position = 0;
+  }
+
+  @Override
+  public synchronized void write(int b) throws IOException {
+    bufferedStream.write(b);
+    position++;
   }
 
   @Override
-  public void write(byte[] b) throws IOException {
+  public synchronized void write(byte[] b) throws IOException {
     bufferedStream.write(b);
+    position += b.length;
   }
 
   @Override
-  public void write(byte b) throws IOException {
+  public synchronized void write(byte b) throws IOException {
     bufferedStream.write(b);
+    position++;
+  }
+
+  @Override
+  public synchronized void write(byte[] buf, int start, int offset) throws IOException {
+    bufferedStream.write(buf, start, offset);
+    position += offset;
   }
 
   @Override
-  public void write(ByteBuffer b) throws IOException {
+  public synchronized void write(ByteBuffer b) throws IOException {
     bufferedStream.write(b.array());
+    position += b.array().length;
   }
 
   @Override
-  public long getPosition() throws IOException {
-    bufferedStream.flush();
-    return outputStream.getChannel().position();
+  public long getPosition() {
+    return position;
   }
 
   @Override
@@ -74,7 +83,7 @@ public class LocalTsFileOutput implements TsFileOutput {
 
   @Override
   public OutputStream wrapAsStream() {
-    return bufferedStream;
+    return this;
   }
 
   @Override
@@ -84,6 +93,8 @@ public class LocalTsFileOutput implements TsFileOutput {
 
   @Override
   public void truncate(long size) throws IOException {
+    bufferedStream.flush();
     outputStream.getChannel().truncate(size);
+    position = outputStream.getChannel().position();
   }
 }