This is an automated email from the ASF dual-hosted git repository. jackietien pushed a commit to branch MLogBug in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 6f449f7093026df69c6551f8781d69ab26573c1e Author: JackieTien97 <[email protected]> AuthorDate: Mon Oct 26 17:06:51 2020 +0800 init --- .../org/apache/iotdb/db/metadata/MLogWriter.java | 45 ++++++++++------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/MLogWriter.java b/server/src/main/java/org/apache/iotdb/db/metadata/MLogWriter.java index d6745f0..5f77527 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/MLogWriter.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/MLogWriter.java @@ -29,6 +29,7 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory; import org.apache.iotdb.db.qp.physical.sys.CreateTimeSeriesPlan; import org.apache.iotdb.tsfile.fileSystem.FSFactoryProducer; @@ -38,11 +39,12 @@ import org.slf4j.LoggerFactory; public class MLogWriter { private static final Logger logger = LoggerFactory.getLogger(MLogWriter.class); - private static final String STRING_TYPE = "%s,%s,%s"; - private File logFile; + private static final String STRING_TYPE = "%s,%s,%s" + System.lineSeparator(); + private static final String LINE_SEPARATOR = System.lineSeparator(); + private final File logFile; private FileOutputStream fileOutputStream; private FileChannel channel; - private int lineNumber; + private final AtomicInteger lineNumber; public MLogWriter(String schemaDir, String logFileName) throws IOException { File metadataDir = SystemFileFactory.INSTANCE.getFile(schemaDir); @@ -57,10 +59,10 @@ public class MLogWriter { logFile = SystemFileFactory.INSTANCE.getFile(schemaDir + File.separator + logFileName); fileOutputStream = new FileOutputStream(logFile, true); channel = fileOutputStream.getChannel(); + lineNumber = new AtomicInteger(0); } public void close() throws IOException { - channel.close(); fileOutputStream.close(); } @@ -92,51 +94,50 @@ public class MLogWriter { if (offset >= 0) { buf.append(offset); } - buf.append(System.getProperty("line.separator")); + buf.append(LINE_SEPARATOR); channel.write(ByteBuffer.wrap(buf.toString().getBytes())); - ++lineNumber; + lineNumber.incrementAndGet(); } public void deleteTimeseries(String path) throws IOException { - String outputStr = MetadataOperationType.DELETE_TIMESERIES + "," + path; + String outputStr = MetadataOperationType.DELETE_TIMESERIES + "," + path + LINE_SEPARATOR; ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); } public void setStorageGroup(String storageGroup) throws IOException { - String outputStr = MetadataOperationType.SET_STORAGE_GROUP + "," + storageGroup; + String outputStr = MetadataOperationType.SET_STORAGE_GROUP + "," + storageGroup + LINE_SEPARATOR; ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); + lineNumber.incrementAndGet(); } public void deleteStorageGroup(String storageGroup) throws IOException { - String outputStr = MetadataOperationType.DELETE_STORAGE_GROUP + "," + storageGroup; + String outputStr = MetadataOperationType.DELETE_STORAGE_GROUP + "," + storageGroup + LINE_SEPARATOR; ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); + lineNumber.incrementAndGet(); } public void setTTL(String storageGroup, long ttl) throws IOException { String outputStr = String.format(STRING_TYPE, MetadataOperationType.SET_TTL, storageGroup, ttl); ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); + lineNumber.incrementAndGet(); } public void changeOffset(String path, long offset) throws IOException { String outputStr = String.format(STRING_TYPE, MetadataOperationType.CHANGE_OFFSET, path, offset); ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); + lineNumber.incrementAndGet(); } public void changeAlias(String path, String alias) throws IOException { String outputStr = String.format(STRING_TYPE, MetadataOperationType.CHANGE_ALIAS, path, alias); ByteBuffer buff = ByteBuffer.wrap(outputStr.getBytes()); channel.write(buff); - newLine(); + lineNumber.incrementAndGet(); } public static void upgradeMLog(String schemaDir, String logFileName) throws IOException { @@ -158,7 +159,7 @@ public class MLogWriter { } // upgrading try (BufferedReader reader = new BufferedReader(new FileReader(logFile)); - BufferedWriter writer = new BufferedWriter(new FileWriter(tmpLogFile, true));) { + BufferedWriter writer = new BufferedWriter(new FileWriter(tmpLogFile, true))) { String line; while ((line = reader.readLine()) != null) { StringBuilder buf = new StringBuilder(); @@ -179,23 +180,17 @@ public class MLogWriter { Files.delete(logFile.toPath()); fileOutputStream = new FileOutputStream(logFile, true); channel = fileOutputStream.getChannel(); - lineNumber = 0; - } - - private void newLine() throws IOException { - channel.write(ByteBuffer.wrap(System.lineSeparator().getBytes())); - channel.force(true); - ++lineNumber; + lineNumber.set(0); } int getLineNumber() { - return lineNumber; + return lineNumber.get(); } /** * only used for initialize a mlog file writer. */ void setLineNumber(int number) { - lineNumber = number; + lineNumber.set(number); } }
