pan3793 commented on code in PR #54477: URL: https://github.com/apache/spark/pull/54477#discussion_r2876275436
########## sql/catalyst/src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java: ########## @@ -0,0 +1,561 @@ +/* + * 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.datasketches.memory.internal; + +import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe; +import static org.apache.datasketches.memory.internal.Util.characterPad; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.datasketches.memory.MemoryBoundsException; +import org.apache.datasketches.memory.MemoryRequestServer; +import org.apache.datasketches.memory.ReadOnlyException; +import org.apache.datasketches.memory.Resource; + +// temp copied for testing +// https://github.com/apache/datasketches-memory/pull/272 + +/** + * Implements the root Resource methods plus some common static variables and check methods. + * + * @author Lee Rhodes + */ +@SuppressWarnings("restriction") +public abstract class ResourceImpl implements Resource { + static final String JDK; + static final int JDK_MAJOR; //8, 11, 17, etc + + //Used to convert "type" to bytes: bytes = longs << LONG_SHIFT + static final int BOOLEAN_SHIFT = 0; + static final int BYTE_SHIFT = 0; + static final long SHORT_SHIFT = 1; + static final long CHAR_SHIFT = 1; + static final long INT_SHIFT = 2; + static final long LONG_SHIFT = 3; + static final long FLOAT_SHIFT = 2; + static final long DOUBLE_SHIFT = 3; + + //class type IDs. Do not change the bit orders + //The lowest 3 bits are set dynamically + // 0000 0XXX Group 1 + static final int WRITABLE = 0; //bit 0 = 0 + static final int READONLY = 1; //bit 0 + static final int REGION = 2; //bit 1 + static final int DUPLICATE = 4; //bit 2, for Buffer only + + // 000X X000 Group 2 + static final int HEAP = 0; //bits 3,4 = 0 + static final int DIRECT = 8; //bit 3 + static final int MAP = 16; //bit 4, Map is effectively Direct + + // 00X0 0000 Group 3 ByteOrder + static final int NATIVE_BO = 0; //bit 5 = 0 + static final int NONNATIVE_BO = 32;//bit 5 + + // 0X00 0000 Group 4 + static final int MEMORY = 0; //bit 6 = 0 + static final int BUFFER = 64; //bit 6 + + // X000 0000 Group 5 + static final int BYTEBUF = 128; //bit 7 + + /** + * The java line separator character as a String. + */ + public static final String LS = System.getProperty("line.separator"); + + static final String NOT_MAPPED_FILE_RESOURCE = "This is not a memory-mapped file resource"; + static final String THREAD_EXCEPTION_TEXT = "Attempted access outside owning thread"; + + private static AtomicBoolean JAVA_VERSION_WARNING_PRINTED = new AtomicBoolean(false); + + static { + final String jdkVer = System.getProperty("java.version"); + final int[] p = parseJavaVersion(jdkVer); + JDK = p[0] + "." + p[1]; + JDK_MAJOR = (p[0] == 1) ? p[1] : p[0]; + } + + //set by the leaf nodes + long capacityBytes; + long cumOffsetBytes; + long offsetBytes; + int typeId; + Thread owner = null; + + /** + * The root of the Memory inheritance hierarchy + */ + ResourceImpl() { } + + //MemoryRequestServer logic + + /** + * User specified MemoryRequestServer. Set here and by leaf nodes. + */ + MemoryRequestServer memReqSvr = null; + + @Override + public MemoryRequestServer getMemoryRequestServer() { + return memReqSvr; + } + + @Override + public boolean hasMemoryRequestServer() { + return memReqSvr != null; + } + + @Override + public void setMemoryRequestServer(final MemoryRequestServer memReqSvr) { this.memReqSvr = memReqSvr; } + + //*** + + /** + * Check the requested offset and length against the allocated size. + * The invariants equation is: {@code 0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize}. + * If this equation is violated an {@link MemoryBoundsException} will be thrown. + * @param reqOff the requested offset + * @param reqLen the requested length + * @param allocSize the allocated size. + * @throws MemoryBoundsException if the given arguments constitute a violation + * of the invariants equation expressed above. + */ + public static void checkBounds(final long reqOff, final long reqLen, final long allocSize) { + if ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) < 0) { + throw new MemoryBoundsException( + "reqOffset: " + reqOff + ", reqLength: " + reqLen + + ", (reqOff + reqLen): " + (reqOff + reqLen) + ", allocSize: " + allocSize); + } + } + + /** + * Checks the runtime Java Version string. Note that Java 17 and 21 is allowed only because some clients do not use the + * WritableMemory.allocateDirect(..) and related functions, which will not work with Java versions >= 14. + * The on-heap functions may work with 17 and 21, nonetheless, versions > Java 11 are not officially supported. + * Caveat emptor. + * @param jdkVer the <i>System.getProperty("java.version")</i> string of the form "p0.p1.X" + * @param p0 The first number group + * @param p1 The second number group + */ + static void checkJavaVersion(final String jdkVer, final int p0, final int p1 ) { + final boolean ok = ((p0 == 1) && (p1 == 8)) || (p0 == 8) || (p0 == 11) || (p0 == 17 || (p0 == 21) || (p0 == 25)); Review Comment: it's the real change - add 25 to the JDK version allow list -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
