tkalkirill commented on code in PR #920: URL: https://github.com/apache/ignite-3/pull/920#discussion_r921225536
########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/PartitionFilePageStore.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.ignite.internal.pagememory.persistence.store; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import org.apache.ignite.internal.pagememory.persistence.PartitionMeta; +import org.apache.ignite.lang.IgniteInternalCheckedException; + +/** + * File page store with partition meta. + */ +public class PartitionFilePageStore implements PageStore { + private final FilePageStore filePageStore; + + private final PartitionMeta partitionMeta; + + /** + * Constructor. + * + * @param filePageStore File page store. + * @param partitionMeta Partition meta. + */ + public PartitionFilePageStore(FilePageStore filePageStore, PartitionMeta partitionMeta) { + this.filePageStore = filePageStore; + this.partitionMeta = partitionMeta; + } + + /** {@inheritDoc} */ + @Override + public void addWriteListener(PageWriteListener listener) { + filePageStore.addWriteListener(listener); + } + + /** {@inheritDoc} */ + @Override + public void removeWriteListener(PageWriteListener listener) { + filePageStore.removeWriteListener(listener); + } + + /** {@inheritDoc} */ + @Override + public void stop(boolean clean) throws IgniteInternalCheckedException { + filePageStore.stop(clean); + } + + /** {@inheritDoc} */ + @Override + public long allocatePage() throws IgniteInternalCheckedException { + return filePageStore.allocatePage(); + } + + /** {@inheritDoc} */ + @Override + public int pages() { + return filePageStore.pages(); + } + + /** {@inheritDoc} */ + @Override + public boolean read(long pageId, ByteBuffer pageBuf, boolean keepCrc) throws IgniteInternalCheckedException { + return filePageStore.read(pageId, pageBuf, keepCrc); + } + + /** {@inheritDoc} */ + @Override + public void write(long pageId, ByteBuffer pageBuf, int tag, boolean calculateCrc) throws IgniteInternalCheckedException { + filePageStore.write(pageId, pageBuf, tag, calculateCrc); + } + + /** {@inheritDoc} */ + @Override + public void sync() throws IgniteInternalCheckedException { + filePageStore.sync(); + } + + /** {@inheritDoc} */ + @Override + public boolean exists() { + return filePageStore.exists(); + } + + /** {@inheritDoc} */ + @Override + public void ensure() throws IgniteInternalCheckedException { + filePageStore.ensure(); + } + + /** {@inheritDoc} */ + @Override + public void close() throws IOException { + filePageStore.close(); + } + + /** + * Returns partition meta. + */ + public PartitionMeta meta() { Review Comment: I'll change it later. ########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/PartitionFilePageStoreFactory.java: ########## @@ -0,0 +1,156 @@ +/* + * 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.ignite.internal.pagememory.persistence.store; + +import static org.apache.ignite.internal.pagememory.persistence.store.FilePageStoreHeader.readHeader; +import static org.apache.ignite.internal.util.GridUnsafe.bufferAddress; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.ignite.internal.fileio.FileIo; +import org.apache.ignite.internal.fileio.FileIoFactory; +import org.apache.ignite.internal.pagememory.io.PageIoRegistry; +import org.apache.ignite.internal.pagememory.persistence.PartitionMeta; +import org.apache.ignite.internal.pagememory.persistence.io.PartitionMetaIo; +import org.apache.ignite.lang.IgniteInternalCheckedException; + +/** + * Factory for creating {@link PartitionFilePageStore}. + */ +class PartitionFilePageStoreFactory { + /** Latest file page store version. */ + public final int latestVersion = FilePageStore.VERSION_1; + + private final FileIoFactory fileIoFactory; + + private final int pageSize; + + private PageIoRegistry ioRegistry; + + /** + * Constructor. + * + * @param fileIoFactory File IO factory. + * @param ioRegistry Page IO registry. + * @param pageSize Page size in bytes. + */ + public PartitionFilePageStoreFactory( + FileIoFactory fileIoFactory, + PageIoRegistry ioRegistry, + int pageSize + ) { + this.fileIoFactory = fileIoFactory; + this.ioRegistry = ioRegistry; + this.pageSize = pageSize; + } + + /** + * Creates instance of {@link PartitionFilePageStore}. + * + * <p>If the partition file exists, it will read its {@link FilePageStoreHeader header} and {@link PartitionMetaIo meta} ({@code + * pageIdx == 0}) to create the {@link PartitionFilePageStore}. + * + * @param filePath File page store path. + * @param readIntoBuffer Direct byte buffer for reading {@link PartitionMetaIo meta} from {@code filePath}. + * @return File page store. + * @throws IgniteInternalCheckedException if failed + */ + public PartitionFilePageStore createPageStore( + Path filePath, + ByteBuffer readIntoBuffer + ) throws IgniteInternalCheckedException { + assert readIntoBuffer.remaining() == pageSize : "Expected pageSize=" + pageSize + ", bufferSize=" + readIntoBuffer.remaining(); + + if (!Files.exists(filePath)) { + return createPageStore(filePath, new FilePageStoreHeader(latestVersion, pageSize), newPartitionMeta()); + } + + try (FileIo fileIo = fileIoFactory.create(filePath)) { + FilePageStoreHeader header = readHeader(fileIo); + + if (header == null) { + header = new FilePageStoreHeader(latestVersion, pageSize); + } + + PartitionMeta meta = readPartitionMeta(fileIo, header.headerSize(), readIntoBuffer); + + return createPageStore(filePath, header, meta); + } catch (IOException e) { + throw new IgniteInternalCheckedException("Error while creating file page store [file=" + filePath + "]:", e); + } + } + + private PartitionFilePageStore createPageStore( + Path filePath, + FilePageStoreHeader header, + PartitionMeta partitionMeta + ) throws IgniteInternalCheckedException { + if (header.version() == FilePageStore.VERSION_1) { + return new PartitionFilePageStore( + new FilePageStore( + header.version(), + header.pageSize(), + header.headerSize(), + partitionMeta.pageCount(), + filePath, + fileIoFactory + ), + partitionMeta + ); + } + + throw new IgniteInternalCheckedException(String.format( + "Unknown version of file page store [version=%s, file=%s]", + header.version(), + filePath + )); + } + + private PartitionMeta readPartitionMeta(FileIo fileIo, int headerSize, ByteBuffer readIntoBuffer) throws IOException { + if (fileIo.size() <= headerSize) { + return newPartitionMeta(); + } + + try { + fileIo.readFully(readIntoBuffer, headerSize); + + long pageAddr = bufferAddress(readIntoBuffer); + + if (PartitionMetaIo.getType(pageAddr) <= 0) { + return newPartitionMeta(); + } + + PartitionMetaIo partitionMetaIo = ioRegistry.resolve(pageAddr); + + return new PartitionMeta( Review Comment: I'll change it later. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
