rpuch commented on code in PR #1405:
URL: https://github.com/apache/ignite-3/pull/1405#discussion_r1049315988


##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/BlobStorage.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.storage.pagememory.mv;
+
+import java.util.Objects;
+import org.apache.ignite.internal.pagememory.PageIdAllocator;
+import org.apache.ignite.internal.pagememory.PageMemory;
+import org.apache.ignite.internal.pagememory.datastructure.DataStructure;
+import org.apache.ignite.internal.pagememory.io.PageIo;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.pagememory.reuse.LongListReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseList;
+import org.apache.ignite.internal.pagememory.util.PageHandler;
+import org.apache.ignite.internal.pagememory.util.PageLockListenerNoOp;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobFragmentIo;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Used to store a limited number of blobs (just byte arrays) per partition. 
Each blob is stored in a sequence
+ * of pages forming a linked list (a previous page links to the next one).
+ *
+ * <p>If a lot of blobs (comparable with the number of rows) needs to be 
stored in a partition, another mechanism
+ * (probably using a {@link 
org.apache.ignite.internal.pagememory.freelist.FreeList}) should be used.
+ */
+public class BlobStorage extends DataStructure {
+    static final long NO_PAGE_ID = 0;
+
+    private final IoStatisticsHolder statisticsHolder;
+
+    private final RecycleAndAddToReuseBag recycleAndAddToReuseBag = new 
RecycleAndAddToReuseBag();
+
+    private final ReadFragment readFragment = new ReadFragment();
+
+    private final WriteFragment writeFragment = new WriteFragment();
+
+    /**
+     * Creates a new instance.
+     */
+    public BlobStorage(ReuseList reuseList, PageMemory pageMemory, int 
groupId, int partitionId, IoStatisticsHolder statisticsHolder) {
+        super("BlobStorage", groupId, null, partitionId, pageMemory, 
PageLockListenerNoOp.INSTANCE, PageIdAllocator.FLAG_AUX);
+
+        super.reuseList = reuseList;
+        this.statisticsHolder = statisticsHolder;
+    }
+
+    /**
+     * Reads a blob stored starting at a page with the given ID.
+     *
+     * @param firstPageId ID of first page.
+     * @return Byte array for the blob.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public byte[] readBlob(long firstPageId) throws 
IgniteInternalCheckedException {
+        ReadState readState = new ReadState();
+
+        long pageId = firstPageId;
+
+        while (pageId != NO_PAGE_ID) {
+            Boolean ok = PageHandler.readPage(
+                    pageMem,
+                    grpId,
+                    pageId,
+                    PageLockListenerNoOp.INSTANCE,
+                    readFragment,
+                    readState,
+                    0,
+                    false,
+                    IoStatisticsHolderNoOp.INSTANCE
+            );
+
+            assert ok : pageId;
+
+            pageId = readState.nextPageId;
+        }
+
+        assert readState.bytes != null;
+
+        return readState.bytes;
+    }
+
+    /**
+     * Adds a new blob to the storage.
+     *
+     * @param bytes Blob bytes.
+     * @return ID of the page starting the chain representing the blob.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public long addBlob(byte[] bytes) throws IgniteInternalCheckedException {
+        return doStore(NO_PAGE_ID, bytes);
+    }
+
+    /**
+     * Updates the blob content.
+     *
+     * @param firstPageId ID of the first page in the chain storing the blob.
+     * @param bytes New blob content.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public void updateBlob(long firstPageId, byte[] bytes) throws 
IgniteInternalCheckedException {
+        doStore(firstPageId, bytes);
+    }
+
+    private long doStore(long maybeFirstPageId, byte[] bytes) throws 
IgniteInternalCheckedException {
+        Objects.requireNonNull(bytes, "bytes is null");
+
+        long firstPageId = allocatePageIfNeeded(maybeFirstPageId);
+
+        WriteState state = new WriteState(bytes);
+        state.pageId = firstPageId;
+
+        do {
+            Boolean ok = PageHandler.writePage(
+                    pageMem,
+                    grpId,
+                    state.pageId,
+                    PageLockListenerNoOp.INSTANCE,
+                    writeFragment,
+                    null,
+                    state,
+                    0,
+                    false,
+                    statisticsHolder
+            );
+
+            assert ok : state.pageId;
+        } while (!state.stop);
+
+        freePagesStartingWith(state.firstPageToFreeId);
+
+        return firstPageId;
+    }
+
+    private long allocatePageIfNeeded(long maybePageId) throws 
IgniteInternalCheckedException {

Review Comment:
   Inlined the method



-- 
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]

Reply via email to