This is an automated email from the ASF dual-hosted git repository. haonan pushed a commit to branch speed_up_recover in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit c112a4bc63d4dcac2b78b9527f4ccd42afbdaf6b Author: HTHou <[email protected]> AuthorDate: Wed Jul 24 10:48:05 2024 +0800 dev --- .../db/utils/writelog/PartitionLogReader.java | 41 ++++++++++ .../db/utils/writelog/PartitionLogWriter.java | 94 ++++++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogReader.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogReader.java new file mode 100644 index 00000000000..83925cc69f2 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogReader.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.utils.writelog; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; + +public class PartitionLogReader { + + private DataInputStream logStream; + private String filepath; + + private byte[] buffer; + + public PartitionLogReader(File logFile) throws IOException { + logStream = new DataInputStream(new BufferedInputStream(Files.newInputStream(logFile.toPath()))); + this.filepath = logFile.getPath(); + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogWriter.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogWriter.java new file mode 100644 index 00000000000..35213626525 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/writelog/PartitionLogWriter.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iotdb.db.utils.writelog; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.FileChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * LogWriter writes the binary logs into a file using FileChannel together with check sums of each + * log calculated using CRC32. + */ +public class PartitionLogWriter implements ILogWriter { + private static final Logger logger = LoggerFactory.getLogger(PartitionLogWriter.class); + + private final File logFile; + private FileOutputStream fileOutputStream; + private FileChannel channel; + private final boolean forceEachWrite; + + public PartitionLogWriter(File logFile, boolean forceEachWrite) throws FileNotFoundException { + this.logFile = logFile; + this.forceEachWrite = forceEachWrite; + + fileOutputStream = new FileOutputStream(logFile, true); + channel = fileOutputStream.getChannel(); + } + + @Override + public void write(ByteBuffer logBuffer) throws IOException { + if (channel == null) { + fileOutputStream = new FileOutputStream(logFile, true); + channel = fileOutputStream.getChannel(); + } + + logBuffer.flip(); + + try { + channel.write(logBuffer); + if (this.forceEachWrite) { + channel.force(true); + } + } catch (ClosedChannelException ignored) { + logger.warn("someone interrupt current thread, so no need to do write for io safety"); + } + } + + @Override + public void force() throws IOException { + if (channel != null && channel.isOpen()) { + channel.force(true); + } + } + + @Override + public void close() throws IOException { + if (channel != null) { + if (channel.isOpen()) { + channel.force(true); + } + fileOutputStream.close(); + fileOutputStream = null; + channel.close(); + channel = null; + } + } + + @Override + public String toString() { + return "LogWriter{" + "logFile=" + logFile + '}'; + } +}
