nizhikov commented on code in PR #11518: URL: https://github.com/apache/ignite/pull/11518#discussion_r1812561207
########## modules/core/src/main/java/org/apache/ignite/internal/jdbc2/lob/JdbcBlobReadWriteStorage.java: ########## @@ -0,0 +1,275 @@ +/* + * 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.jdbc2.lob; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.ignite.internal.util.typedef.internal.U; + +/** + * Read-write implementation of {@link JdbcBlobStorage}. + * + * <p>Keeps data in list of byte array buffers (of different size) to avoid memory + * reallocation and data coping on write operations (which append data in particular). + */ +class JdbcBlobReadWriteStorage extends JdbcBlobStorage { + /** Max capacity when it is still reasonable to double size for new buffer. */ + private static final int MAX_CAP = 32 * 1024 * 1024; + + /** The list of buffers. */ + private List<byte[]> buffers = new ArrayList<>(); + + /** + * Creates a new empty buffer. + */ + JdbcBlobReadWriteStorage() { + // No-op + } + + /** + * Creates a new buffer enclosing data from the existing byte array. + * + * @param arr The byte array. + */ + JdbcBlobReadWriteStorage(byte[] arr) { + if (arr.length > 0) { + buffers.add(arr); + + totalCnt = arr.length; + } + } + + /** {@inheritDoc} */ + @Override public long totalCnt() { + return totalCnt; + } + + /** {@inheritDoc} */ + @Override public JdbcBlobBufferPointer createPointer() { + return new JdbcBlobBufferPointer(); + } + + /** {@inheritDoc} */ + @Override public int read(JdbcBlobBufferPointer pos) { + byte[] buf = getBuf(pos); + + if (buf == null || pos.getPos() >= totalCnt) + return -1; + + int res = buf[getBufPos(pos)] & 0xff; + + advance(pos, 1); + + return res; + } + + /** {@inheritDoc} */ + @Override public int read(JdbcBlobBufferPointer pos, byte[] res, int off, int cnt) { + byte[] buf = getBuf(pos); + + if (buf == null || pos.getPos() >= totalCnt) + return -1; + + int remaining = cnt; + + while (remaining > 0 && pos.getPos() < totalCnt && buf != null) { + int toCopy = Math.min(remaining, buf.length - getBufPos(pos)); + + if (toCopy > totalCnt - pos.getPos()) + toCopy = (int)(totalCnt - pos.getPos()); + + U.arrayCopy(buf, getBufPos(pos), res, off + (cnt - remaining), toCopy); + + remaining -= toCopy; + + advance(pos, toCopy); + buf = getBuf(pos); + } + + return cnt - remaining; + } + + /** {@inheritDoc} */ + @Override public void write(JdbcBlobBufferPointer pos, int b) throws IOException { + if (pos.getPos() >= totalCnt + 1) + throw new IOException("Writting beyond end of Blob, it probably was truncated after OutputStream was created " + + "[pos=" + pos.getPos() + ", totalCnt=" + totalCnt + "]"); + + byte[] buf = getBuf(pos); + + if (buf == null) + buf = addNewBuffer(1); Review Comment: We don't want to create byte[1] to write data. Let's create new array of reasonable size here. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org