code
stringlengths
67
466k
docstring
stringlengths
1
13.2k
public long sendRpc(ByteBuffer message, RpcResponseCallback callback) { if (logger.isTraceEnabled()) { logger.trace("Sending RPC to {}", getRemoteAddress(channel)); } long requestId = requestId(); handler.addRpcRequest(requestId, callback); RpcChannelListener listener = new RpcChannelListener(requestId, callback); channel.writeAndFlush(new RpcRequest(requestId, new NioManagedBuffer(message))) .addListener(listener); return requestId; }
Sends an opaque message to the RpcHandler on the server-side. The callback will be invoked with the server's response or upon any failure. @param message The message to send. @param callback Callback to handle the RPC's reply. @return The RPC's id.
public long uploadStream( ManagedBuffer meta, ManagedBuffer data, RpcResponseCallback callback) { if (logger.isTraceEnabled()) { logger.trace("Sending RPC to {}", getRemoteAddress(channel)); } long requestId = requestId(); handler.addRpcRequest(requestId, callback); RpcChannelListener listener = new RpcChannelListener(requestId, callback); channel.writeAndFlush(new UploadStream(requestId, meta, data)).addListener(listener); return requestId; }
Send data to the remote end as a stream. This differs from stream() in that this is a request to *send* data to the remote end, not to receive it from the remote. @param meta meta data associated with the stream, which will be read completely on the receiving end before the stream itself. @param data this will be streamed to the remote end to allow for transferring large amounts of data without reading into memory. @param callback handles the reply -- onSuccess will only be called when both message and data are received successfully.
public ByteBuffer sendRpcSync(ByteBuffer message, long timeoutMs) { final SettableFuture<ByteBuffer> result = SettableFuture.create(); sendRpc(message, new RpcResponseCallback() { @Override public void onSuccess(ByteBuffer response) { ByteBuffer copy = ByteBuffer.allocate(response.remaining()); copy.put(response); // flip "copy" to make it readable copy.flip(); result.set(copy); } @Override public void onFailure(Throwable e) { result.setException(e); } }); try { return result.get(timeoutMs, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } catch (Exception e) { throw Throwables.propagate(e); } }
Synchronously sends an opaque message to the RpcHandler on the server-side, waiting for up to a specified timeout for a response.
private boolean refill() throws IOException { if (!byteBuffer.hasRemaining()) { byteBuffer.clear(); int nRead = 0; while (nRead == 0) { nRead = fileChannel.read(byteBuffer); } if (nRead < 0) { return false; } byteBuffer.flip(); } return true; }
Checks weather data is left to be read from the input stream. @return true if data is left, false otherwise @throws IOException
public String signCookie(String str) { if (str == null || str.isEmpty()) { throw new IllegalArgumentException("NULL or empty string to sign"); } String signature = getSignature(str); if (LOG.isDebugEnabled()) { LOG.debug("Signature generated for " + str + " is " + signature); } return str + SIGNATURE + signature; }
Sign the cookie given the string token as input. @param str Input token @return Signed token that can be used to create a cookie
public String verifyAndExtract(String signedStr) { int index = signedStr.lastIndexOf(SIGNATURE); if (index == -1) { throw new IllegalArgumentException("Invalid input sign: " + signedStr); } String originalSignature = signedStr.substring(index + SIGNATURE.length()); String rawValue = signedStr.substring(0, index); String currentSignature = getSignature(rawValue); if (LOG.isDebugEnabled()) { LOG.debug("Signature generated for " + rawValue + " inside verify is " + currentSignature); } if (!originalSignature.equals(currentSignature)) { throw new IllegalArgumentException("Invalid sign, original = " + originalSignature + " current = " + currentSignature); } return rawValue; }
Verify a signed string and extracts the original string. @param signedStr The already signed string @return Raw Value of the string without the signature
private String getSignature(String str) { try { MessageDigest md = MessageDigest.getInstance(SHA_STRING); md.update(str.getBytes()); md.update(secretBytes); byte[] digest = md.digest(); return new Base64(0).encodeToString(digest); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException("Invalid SHA digest String: " + SHA_STRING + " " + ex.getMessage(), ex); } }
Get the signature of the input string based on SHA digest algorithm. @param str Input token @return Signed String
void closeIterator(LevelDBIterator<?> it) throws IOException { synchronized (this._db) { DB _db = this._db.get(); if (_db != null) { it.close(); } } }
Closes the given iterator if the DB is still open. Trying to close a JNI LevelDB handle with a closed DB can cause JVM crashes, so this ensures that situation does not happen.
LevelDBTypeInfo getTypeInfo(Class<?> type) throws Exception { LevelDBTypeInfo ti = types.get(type); if (ti == null) { LevelDBTypeInfo tmp = new LevelDBTypeInfo(this, type, getTypeAlias(type)); ti = types.putIfAbsent(type, tmp); if (ti == null) { ti = tmp; } } return ti; }
Returns metadata about indices for the given type.
DB db() { DB _db = this._db.get(); if (_db == null) { throw new IllegalStateException("DB is closed."); } return _db; }
Try to avoid use-after close since that has the tendency of crashing the JVM. This doesn't prevent methods that retrieved the instance from using it after close, but hopefully will catch most cases; otherwise, we'll need some kind of locking.
public void registerApp(String appId, String shuffleSecret) { // Always put the new secret information to make sure it's the most up to date. // Otherwise we have to specifically look at the application attempt in addition // to the applicationId since the secrets change between application attempts on yarn. shuffleSecretMap.put(appId, shuffleSecret); logger.info("Registered shuffle secret for application {}", appId); }
Register an application with its secret. Executors need to first authenticate themselves with the same secret before fetching shuffle files written by other executors in this application.
public void registerApp(String appId, ByteBuffer shuffleSecret) { registerApp(appId, JavaUtils.bytesToString(shuffleSecret)); }
Register an application with its secret specified as a byte buffer.
public static void set(Object baseObject, long baseOffset, int index) { assert index >= 0 : "index (" + index + ") should >= 0"; final long mask = 1L << (index & 0x3f); // mod 64 and shift final long wordOffset = baseOffset + (index >> 6) * WORD_SIZE; final long word = Platform.getLong(baseObject, wordOffset); Platform.putLong(baseObject, wordOffset, word | mask); }
Sets the bit at the specified index to {@code true}.
public static boolean isSet(Object baseObject, long baseOffset, int index) { assert index >= 0 : "index (" + index + ") should >= 0"; final long mask = 1L << (index & 0x3f); // mod 64 and shift final long wordOffset = baseOffset + (index >> 6) * WORD_SIZE; final long word = Platform.getLong(baseObject, wordOffset); return (word & mask) != 0; }
Returns {@code true} if the bit is set at the specified index.
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInWords) { long addr = baseOffset; for (int i = 0; i < bitSetWidthInWords; i++, addr += WORD_SIZE) { if (Platform.getLong(baseObject, addr) != 0) { return true; } } return false; }
Returns {@code true} if any bit is set.
public static int nextSetBit( Object baseObject, long baseOffset, int fromIndex, int bitsetSizeInWords) { int wi = fromIndex >> 6; if (wi >= bitsetSizeInWords) { return -1; } // Try to find the next set bit in the current word final int subIndex = fromIndex & 0x3f; long word = Platform.getLong(baseObject, baseOffset + wi * WORD_SIZE) >> subIndex; if (word != 0) { return (wi << 6) + subIndex + java.lang.Long.numberOfTrailingZeros(word); } // Find the next set bit in the rest of the words wi += 1; while (wi < bitsetSizeInWords) { word = Platform.getLong(baseObject, baseOffset + wi * WORD_SIZE); if (word != 0) { return (wi << 6) + java.lang.Long.numberOfTrailingZeros(word); } wi += 1; } return -1; }
Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then {@code -1} is returned. <p> To iterate over the true bits in a BitSet, use the following loop: <pre> <code> for (long i = bs.nextSetBit(0, sizeInWords); i &gt;= 0; i = bs.nextSetBit(i + 1, sizeInWords)) { // operate on index i here } </code> </pre> @param fromIndex the index to start checking from (inclusive) @param bitsetSizeInWords the size of the bitset, measured in 8-byte words @return the index of the next set bit, or -1 if there is no such bit
@Override public SessionHandle openSession(String username, String password, Map<String, String> configuration) throws HiveSQLException { return cliService.openSession(username, password, configuration); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#openSession(java.lang.String, java.lang.String, java.util.Map)
@Override public GetInfoValue getInfo(SessionHandle sessionHandle, GetInfoType getInfoType) throws HiveSQLException { return cliService.getInfo(sessionHandle, getInfoType); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#getInfo(org.apache.hive.service.cli.SessionHandle, java.util.List)
@Override public OperationHandle executeStatement(SessionHandle sessionHandle, String statement, Map<String, String> confOverlay) throws HiveSQLException { return cliService.executeStatement(sessionHandle, statement, confOverlay); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#executeStatement(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.util.Map)
@Override public OperationHandle getSchemas(SessionHandle sessionHandle, String catalogName, String schemaName) throws HiveSQLException { return cliService.getSchemas(sessionHandle, catalogName, schemaName); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#getSchemas(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String)
@Override public OperationHandle getTables(SessionHandle sessionHandle, String catalogName, String schemaName, String tableName, List<String> tableTypes) throws HiveSQLException { return cliService.getTables(sessionHandle, catalogName, schemaName, tableName, tableTypes); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#getTables(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String, java.lang.String, java.util.List)
@Override public OperationHandle getColumns(SessionHandle sessionHandle, String catalogName, String schemaName, String tableName, String columnName) throws HiveSQLException { return cliService.getColumns(sessionHandle, catalogName, schemaName, tableName, columnName); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#getColumns(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
@Override public OperationHandle getFunctions(SessionHandle sessionHandle, String catalogName, String schemaName, String functionName) throws HiveSQLException { return cliService.getFunctions(sessionHandle, catalogName, schemaName, functionName); }
/* (non-Javadoc) @see org.apache.hive.service.cli.CLIServiceClient#getFunctions(org.apache.hive.service.cli.SessionHandle, java.lang.String)
public void free() { if (consumer != null) { if (array != null) { consumer.freeArray(array); } array = null; } }
Free the memory used by pointer array.
public void insertRecord(long recordPointer, long keyPrefix, boolean prefixIsNull) { if (!hasSpaceForAnotherRecord()) { throw new IllegalStateException("There is no space for new record"); } if (prefixIsNull && radixSortSupport != null) { // Swap forward a non-null record to make room for this one at the beginning of the array. array.set(pos, array.get(nullBoundaryPos)); pos++; array.set(pos, array.get(nullBoundaryPos + 1)); pos++; // Place this record in the vacated position. array.set(nullBoundaryPos, recordPointer); nullBoundaryPos++; array.set(nullBoundaryPos, keyPrefix); nullBoundaryPos++; } else { array.set(pos, recordPointer); pos++; array.set(pos, keyPrefix); pos++; } }
Inserts a record to be sorted. Assumes that the record pointer points to a record length stored as a 4-byte integer, followed by the record's bytes. @param recordPointer pointer to a record in a data page, encoded by {@link TaskMemoryManager}. @param keyPrefix a user-defined key prefix
public UnsafeSorterIterator getSortedIterator() { int offset = 0; long start = System.nanoTime(); if (sortComparator != null) { if (this.radixSortSupport != null) { offset = RadixSort.sortKeyPrefixArray( array, nullBoundaryPos, (pos - nullBoundaryPos) / 2L, 0, 7, radixSortSupport.sortDescending(), radixSortSupport.sortSigned()); } else { MemoryBlock unused = new MemoryBlock( array.getBaseObject(), array.getBaseOffset() + pos * 8L, (array.size() - pos) * 8L); LongArray buffer = new LongArray(unused); Sorter<RecordPointerAndKeyPrefix, LongArray> sorter = new Sorter<>(new UnsafeSortDataFormat(buffer)); sorter.sort(array, 0, pos / 2, sortComparator); } } totalSortTimeNanos += System.nanoTime() - start; if (nullBoundaryPos > 0) { assert radixSortSupport != null : "Nulls are only stored separately with radix sort"; LinkedList<UnsafeSorterIterator> queue = new LinkedList<>(); // The null order is either LAST or FIRST, regardless of sorting direction (ASC|DESC) if (radixSortSupport.nullsFirst()) { queue.add(new SortedIterator(nullBoundaryPos / 2, 0)); queue.add(new SortedIterator((pos - nullBoundaryPos) / 2, offset)); } else { queue.add(new SortedIterator((pos - nullBoundaryPos) / 2, offset)); queue.add(new SortedIterator(nullBoundaryPos / 2, 0)); } return new UnsafeExternalSorter.ChainedIterator(queue); } else { return new SortedIterator(pos / 2, offset); } }
Return an iterator over record pointers in sorted order. For efficiency, all calls to {@code next()} will return the same mutable object.
public static long nextPowerOf2(long num) { final long highBit = Long.highestOneBit(num); return (highBit == num) ? num : highBit << 1; }
Returns the next number greater or equal num that is power of 2.
public static boolean arrayEquals( Object leftBase, long leftOffset, Object rightBase, long rightOffset, final long length) { int i = 0; // check if stars align and we can get both offsets to be aligned if ((leftOffset % 8) == (rightOffset % 8)) { while ((leftOffset + i) % 8 != 0 && i < length) { if (Platform.getByte(leftBase, leftOffset + i) != Platform.getByte(rightBase, rightOffset + i)) { return false; } i += 1; } } // for architectures that support unaligned accesses, chew it up 8 bytes at a time if (unaligned || (((leftOffset + i) % 8 == 0) && ((rightOffset + i) % 8 == 0))) { while (i <= length - 8) { if (Platform.getLong(leftBase, leftOffset + i) != Platform.getLong(rightBase, rightOffset + i)) { return false; } i += 8; } } // this will finish off the unaligned comparisons, or do the entire aligned // comparison whichever is needed. while (i < length) { if (Platform.getByte(leftBase, leftOffset + i) != Platform.getByte(rightBase, rightOffset + i)) { return false; } i += 1; } return true; }
Optimized byte array equality check for byte arrays. @return true if the arrays are equal, false otherwise
public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { case STATUS_CODE: return isSetStatusCode(); case INFO_MESSAGES: return isSetInfoMessages(); case SQL_STATE: return isSetSqlState(); case ERROR_CODE: return isSetErrorCode(); case ERROR_MESSAGE: return isSetErrorMessage(); } throw new IllegalStateException(); }
Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise
public ByteBuffer toByteBuffer() { // Allow room for encoded message, plus the type byte ByteBuf buf = Unpooled.buffer(encodedLength() + 1); buf.writeByte(type().id); encode(buf); assert buf.writableBytes() == 0 : "Writable bytes remain: " + buf.writableBytes(); return buf.nioBuffer(); }
Serializes the 'type' byte followed by the message itself.
public SparkLauncher setSparkHome(String sparkHome) { checkNotNull(sparkHome, "sparkHome"); builder.childEnv.put(ENV_SPARK_HOME, sparkHome); return this; }
Set a custom Spark installation location for the application. @param sparkHome Path to the Spark installation to use. @return This launcher.
public Process launch() throws IOException { ProcessBuilder pb = createBuilder(); boolean outputToLog = outputStream == null; boolean errorToLog = !redirectErrorStream && errorStream == null; String loggerName = getLoggerName(); if (loggerName != null && outputToLog && errorToLog) { pb.redirectErrorStream(true); } Process childProc = pb.start(); if (loggerName != null) { InputStream logStream = outputToLog ? childProc.getInputStream() : childProc.getErrorStream(); new OutputRedirector(logStream, loggerName, REDIRECTOR_FACTORY); } return childProc; }
Launches a sub-process that will start the configured Spark application. <p> The {@link #startApplication(SparkAppHandle.Listener...)} method is preferred when launching Spark, since it provides better control of the child application. @return A process handle for the Spark app.
@Override public SparkAppHandle startApplication(SparkAppHandle.Listener... listeners) throws IOException { LauncherServer server = LauncherServer.getOrCreateServer(); ChildProcAppHandle handle = new ChildProcAppHandle(server); for (SparkAppHandle.Listener l : listeners) { handle.addListener(l); } String secret = server.registerHandle(handle); String loggerName = getLoggerName(); ProcessBuilder pb = createBuilder(); boolean outputToLog = outputStream == null; boolean errorToLog = !redirectErrorStream && errorStream == null; // Only setup stderr + stdout to logger redirection if user has not otherwise configured output // redirection. if (loggerName == null && (outputToLog || errorToLog)) { String appName; if (builder.appName != null) { appName = builder.appName; } else if (builder.mainClass != null) { int dot = builder.mainClass.lastIndexOf("."); if (dot >= 0 && dot < builder.mainClass.length() - 1) { appName = builder.mainClass.substring(dot + 1, builder.mainClass.length()); } else { appName = builder.mainClass; } } else if (builder.appResource != null) { appName = new File(builder.appResource).getName(); } else { appName = String.valueOf(COUNTER.incrementAndGet()); } String loggerPrefix = getClass().getPackage().getName(); loggerName = String.format("%s.app.%s", loggerPrefix, appName); } if (outputToLog && errorToLog) { pb.redirectErrorStream(true); } pb.environment().put(LauncherProtocol.ENV_LAUNCHER_PORT, String.valueOf(server.getPort())); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_SECRET, secret); try { Process child = pb.start(); InputStream logStream = null; if (loggerName != null) { logStream = outputToLog ? child.getInputStream() : child.getErrorStream(); } handle.setChildProc(child, loggerName, logStream); } catch (IOException ioe) { handle.kill(); throw ioe; } return handle; }
Starts a Spark application. <p> Applications launched by this launcher run as child processes. The child's stdout and stderr are merged and written to a logger (see <code>java.util.logging</code>) only if redirection has not otherwise been configured on this <code>SparkLauncher</code>. The logger's name can be defined by setting {@link #CHILD_PROCESS_LOGGER_NAME} in the app's configuration. If that option is not set, the code will try to derive a name from the application's name or main class / script file. If those cannot be determined, an internal, unique name will be used. In all cases, the logger name will start with "org.apache.spark.launcher.app", to fit more easily into the configuration of commonly-used logging systems. @since 1.6.0 @see AbstractLauncher#startApplication(SparkAppHandle.Listener...) @param listeners Listeners to add to the handle before the app is launched. @return A handle for the launched application.
String findSparkSubmit() { String script = isWindows() ? "spark-submit.cmd" : "spark-submit"; return join(File.separator, builder.getSparkHome(), "bin", script); }
Visible for testing.
public void addSpillIfNotEmpty(UnsafeSorterIterator spillReader) throws IOException { if (spillReader.hasNext()) { // We only add the spillReader to the priorityQueue if it is not empty. We do this to // make sure the hasNext method of UnsafeSorterIterator returned by getSortedIterator // does not return wrong result because hasNext will return true // at least priorityQueue.size() times. If we allow n spillReaders in the // priorityQueue, we will have n extra empty records in the result of UnsafeSorterIterator. spillReader.loadNext(); priorityQueue.add(spillReader); numRecords += spillReader.getNumRecords(); } }
Add an UnsafeSorterIterator to this merger
public static void main(String[] args) { SparkSession spark = SparkSession .builder() .appName("Java Spark SQL user-defined DataFrames aggregation example") .getOrCreate(); // $example on:untyped_custom_aggregation$ // Register the function to access it spark.udf().register("myAverage", new MyAverage()); Dataset<Row> df = spark.read().json("examples/src/main/resources/employees.json"); df.createOrReplaceTempView("employees"); df.show(); // +-------+------+ // | name|salary| // +-------+------+ // |Michael| 3000| // | Andy| 4500| // | Justin| 3500| // | Berta| 4000| // +-------+------+ Dataset<Row> result = spark.sql("SELECT myAverage(salary) as average_salary FROM employees"); result.show(); // +--------------+ // |average_salary| // +--------------+ // | 3750.0| // +--------------+ // $example off:untyped_custom_aggregation$ spark.stop(); }
$example off:untyped_custom_aggregation$
public void start() { if (blockIds.length == 0) { throw new IllegalArgumentException("Zero-sized blockIds array"); } client.sendRpc(openMessage.toByteBuffer(), new RpcResponseCallback() { @Override public void onSuccess(ByteBuffer response) { try { streamHandle = (StreamHandle) BlockTransferMessage.Decoder.fromByteBuffer(response); logger.trace("Successfully opened blocks {}, preparing to fetch chunks.", streamHandle); // Immediately request all chunks -- we expect that the total size of the request is // reasonable due to higher level chunking in [[ShuffleBlockFetcherIterator]]. for (int i = 0; i < streamHandle.numChunks; i++) { if (downloadFileManager != null) { client.stream(OneForOneStreamManager.genStreamChunkId(streamHandle.streamId, i), new DownloadCallback(i)); } else { client.fetchChunk(streamHandle.streamId, i, chunkCallback); } } } catch (Exception e) { logger.error("Failed while starting block fetches after success", e); failRemainingBlocks(blockIds, e); } } @Override public void onFailure(Throwable e) { logger.error("Failed while starting block fetches", e); failRemainingBlocks(blockIds, e); } }); }
Begins the fetching process, calling the listener with every block fetched. The given message will be serialized with the Java serializer, and the RPC must return a {@link StreamHandle}. We will send all fetch requests immediately, without throttling.
private void failRemainingBlocks(String[] failedBlockIds, Throwable e) { for (String blockId : failedBlockIds) { try { listener.onBlockFetchFailure(blockId, e); } catch (Exception e2) { logger.error("Error in block fetch failure callback", e2); } } }
Invokes the "onBlockFetchFailure" callback for every listed block id.
public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { case SESSION_HANDLE: return isSetSessionHandle(); case OWNER: return isSetOwner(); case RENEWER: return isSetRenewer(); } throw new IllegalStateException(); }
Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise
private String getClientNameFromCookie(Cookie[] cookies) { // Current Cookie Name, Current Cookie Value String currName, currValue; // Following is the main loop which iterates through all the cookies send by the client. // The HS2 generated cookies are of the format hive.server2.auth=<value> // A cookie which is identified as a hiveserver2 generated cookie is validated // by calling signer.verifyAndExtract(). If the validation passes, send the // username for which the cookie is validated to the caller. If no client side // cookie passes the validation, return null to the caller. for (Cookie currCookie : cookies) { // Get the cookie name currName = currCookie.getName(); if (!currName.equals(AUTH_COOKIE)) { // Not a HS2 generated cookie, continue. continue; } // If we reached here, we have match for HS2 generated cookie currValue = currCookie.getValue(); // Validate the value. currValue = signer.verifyAndExtract(currValue); // Retrieve the user name, do the final validation step. if (currValue != null) { String userName = HttpAuthUtils.getUserNameFromCookieToken(currValue); if (userName == null) { LOG.warn("Invalid cookie token " + currValue); continue; } //We have found a valid cookie in the client request. if (LOG.isDebugEnabled()) { LOG.debug("Validated the cookie for user " + userName); } return userName; } } // No valid HS2 generated cookies found, return null return null; }
Retrieves the client name from cookieString. If the cookie does not correspond to a valid client, the function returns null. @param cookies HTTP Request cookies. @return Client Username if cookieString has a HS2 Generated cookie that is currently valid. Else, returns null.
private String toCookieStr(Cookie[] cookies) { String cookieStr = ""; for (Cookie c : cookies) { cookieStr += c.getName() + "=" + c.getValue() + " ;\n"; } return cookieStr; }
Convert cookie array to human readable cookie string @param cookies Cookie Array @return String containing all the cookies separated by a newline character. Each cookie is of the format [key]=[value]
private String validateCookie(HttpServletRequest request) throws UnsupportedEncodingException { // Find all the valid cookies associated with the request. Cookie[] cookies = request.getCookies(); if (cookies == null) { if (LOG.isDebugEnabled()) { LOG.debug("No valid cookies associated with the request " + request); } return null; } if (LOG.isDebugEnabled()) { LOG.debug("Received cookies: " + toCookieStr(cookies)); } return getClientNameFromCookie(cookies); }
Validate the request cookie. This function iterates over the request cookie headers and finds a cookie that represents a valid client/server session. If it finds one, it returns the client name associated with the session. Else, it returns null. @param request The HTTP Servlet Request send by the client @return Client Username if the request has valid HS2 cookie, else returns null @throws UnsupportedEncodingException
private Cookie createCookie(String str) throws UnsupportedEncodingException { if (LOG.isDebugEnabled()) { LOG.debug("Cookie name = " + AUTH_COOKIE + " value = " + str); } Cookie cookie = new Cookie(AUTH_COOKIE, str); cookie.setMaxAge(cookieMaxAge); if (cookieDomain != null) { cookie.setDomain(cookieDomain); } if (cookiePath != null) { cookie.setPath(cookiePath); } cookie.setSecure(isCookieSecure); return cookie; }
Generate a server side cookie given the cookie value as the input. @param str Input string token. @return The generated cookie. @throws UnsupportedEncodingException
private static String getHttpOnlyCookieHeader(Cookie cookie) { NewCookie newCookie = new NewCookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getVersion(), cookie.getComment(), cookie.getMaxAge(), cookie.getSecure()); return newCookie + "; HttpOnly"; }
Generate httponly cookie from HS2 cookie @param cookie HS2 generated cookie @return The httponly cookie
private String doPasswdAuth(HttpServletRequest request, String authType) throws HttpAuthenticationException { String userName = getUsername(request, authType); // No-op when authType is NOSASL if (!authType.equalsIgnoreCase(HiveAuthFactory.AuthTypes.NOSASL.toString())) { try { AuthMethods authMethod = AuthMethods.getValidAuthMethod(authType); PasswdAuthenticationProvider provider = AuthenticationProviderFactory.getAuthenticationProvider(authMethod); provider.Authenticate(userName, getPassword(request, authType)); } catch (Exception e) { throw new HttpAuthenticationException(e); } } return userName; }
Do the LDAP/PAM authentication @param request @param authType @throws HttpAuthenticationException
private String doKerberosAuth(HttpServletRequest request) throws HttpAuthenticationException { // Try authenticating with the http/_HOST principal if (httpUGI != null) { try { return httpUGI.doAs(new HttpKerberosServerAction(request, httpUGI)); } catch (Exception e) { LOG.info("Failed to authenticate with http/_HOST kerberos principal, " + "trying with hive/_HOST kerberos principal"); } } // Now try with hive/_HOST principal try { return serviceUGI.doAs(new HttpKerberosServerAction(request, serviceUGI)); } catch (Exception e) { LOG.error("Failed to authenticate with hive/_HOST kerberos principal"); throw new HttpAuthenticationException(e); } }
Do the GSS-API kerberos authentication. We already have a logged in subject in the form of serviceUGI, which GSS-API will extract information from. In case of a SPNego request we use the httpUGI, for the authenticating service tickets. @param request @return @throws HttpAuthenticationException
private String getAuthHeader(HttpServletRequest request, String authType) throws HttpAuthenticationException { String authHeader = request.getHeader(HttpAuthUtils.AUTHORIZATION); // Each http request must have an Authorization header if (authHeader == null || authHeader.isEmpty()) { throw new HttpAuthenticationException("Authorization header received " + "from the client is empty."); } String authHeaderBase64String; int beginIndex; if (isKerberosAuthMode(authType)) { beginIndex = (HttpAuthUtils.NEGOTIATE + " ").length(); } else { beginIndex = (HttpAuthUtils.BASIC + " ").length(); } authHeaderBase64String = authHeader.substring(beginIndex); // Authorization header must have a payload if (authHeaderBase64String == null || authHeaderBase64String.isEmpty()) { throw new HttpAuthenticationException("Authorization header received " + "from the client does not contain any data."); } return authHeaderBase64String; }
Returns the base64 encoded auth header payload @param request @param authType @return @throws HttpAuthenticationException
static String join(String sep, String... elements) { StringBuilder sb = new StringBuilder(); for (String e : elements) { if (e != null) { if (sb.length() > 0) { sb.append(sep); } sb.append(e); } } return sb.toString(); }
Joins a list of strings using the given separator.
static String firstNonEmptyValue(String key, Map<?, ?>... maps) { for (Map<?, ?> map : maps) { String value = (String) map.get(key); if (!isEmpty(value)) { return value; } } return null; }
Returns the first non-empty value mapped to the given key in the given maps, or null otherwise.
static String firstNonEmpty(String... candidates) { for (String s : candidates) { if (!isEmpty(s)) { return s; } } return null; }
Returns the first non-empty, non-null string in the given list, or null otherwise.
static void mergeEnvPathList(Map<String, String> userEnv, String envKey, String pathList) { if (!isEmpty(pathList)) { String current = firstNonEmpty(userEnv.get(envKey), System.getenv(envKey)); userEnv.put(envKey, join(File.pathSeparator, current, pathList)); } }
Updates the user environment, appending the given pathList to the existing value of the given environment variable (or setting it if it hasn't yet been set).
static List<String> parseOptionString(String s) { List<String> opts = new ArrayList<>(); StringBuilder opt = new StringBuilder(); boolean inOpt = false; boolean inSingleQuote = false; boolean inDoubleQuote = false; boolean escapeNext = false; // This is needed to detect when a quoted empty string is used as an argument ("" or ''). boolean hasData = false; for (int i = 0; i < s.length(); i++) { int c = s.codePointAt(i); if (escapeNext) { opt.appendCodePoint(c); escapeNext = false; } else if (inOpt) { switch (c) { case '\\': if (inSingleQuote) { opt.appendCodePoint(c); } else { escapeNext = true; } break; case '\'': if (inDoubleQuote) { opt.appendCodePoint(c); } else { inSingleQuote = !inSingleQuote; } break; case '"': if (inSingleQuote) { opt.appendCodePoint(c); } else { inDoubleQuote = !inDoubleQuote; } break; default: if (!Character.isWhitespace(c) || inSingleQuote || inDoubleQuote) { opt.appendCodePoint(c); } else { opts.add(opt.toString()); opt.setLength(0); inOpt = false; hasData = false; } } } else { switch (c) { case '\'': inSingleQuote = true; inOpt = true; hasData = true; break; case '"': inDoubleQuote = true; inOpt = true; hasData = true; break; case '\\': escapeNext = true; inOpt = true; hasData = true; break; default: if (!Character.isWhitespace(c)) { inOpt = true; hasData = true; opt.appendCodePoint(c); } } } } checkArgument(!inSingleQuote && !inDoubleQuote && !escapeNext, "Invalid option string: %s", s); if (hasData) { opts.add(opt.toString()); } return opts; }
Parse a string as if it were a list of arguments, following bash semantics. For example: Input: "\"ab cd\" efgh 'i \" j'" Output: [ "ab cd", "efgh", "i \" j" ]
static void checkNotNull(Object o, String arg) { if (o == null) { throw new IllegalArgumentException(String.format("'%s' must not be null.", arg)); } }
Throws IllegalArgumentException if the given object is null.
static void checkArgument(boolean check, String msg, Object... args) { if (!check) { throw new IllegalArgumentException(String.format(msg, args)); } }
Throws IllegalArgumentException with the given message if the check is false.
static void checkState(boolean check, String msg, Object... args) { if (!check) { throw new IllegalStateException(String.format(msg, args)); } }
Throws IllegalStateException with the given message if the check is false.
static String quoteForBatchScript(String arg) { boolean needsQuotes = false; for (int i = 0; i < arg.length(); i++) { int c = arg.codePointAt(i); if (Character.isWhitespace(c) || c == '"' || c == '=' || c == ',' || c == ';') { needsQuotes = true; break; } } if (!needsQuotes) { return arg; } StringBuilder quoted = new StringBuilder(); quoted.append("\""); for (int i = 0; i < arg.length(); i++) { int cp = arg.codePointAt(i); switch (cp) { case '"': quoted.append('"'); break; default: break; } quoted.appendCodePoint(cp); } if (arg.codePointAt(arg.length() - 1) == '\\') { quoted.append("\\"); } quoted.append("\""); return quoted.toString(); }
Quote a command argument for a command to be run by a Windows batch script, if the argument needs quoting. Arguments only seem to need quotes in batch scripts if they have certain special characters, some of which need extra (and different) escaping. For example: original single argument: ab="cde fgh" quoted: "ab^=""cde fgh"""
static String quoteForCommandString(String s) { StringBuilder quoted = new StringBuilder().append('"'); for (int i = 0; i < s.length(); i++) { int cp = s.codePointAt(i); if (cp == '"' || cp == '\\') { quoted.appendCodePoint('\\'); } quoted.appendCodePoint(cp); } return quoted.append('"').toString(); }
Quotes a string so that it can be used in a command string. Basically, just add simple escapes. E.g.: original single argument : ab "cd" ef after: "ab \"cd\" ef" This can be parsed back into a single argument by python's "shlex.split()" function.
static int javaMajorVersion(String javaVersion) { String[] version = javaVersion.split("[+.\\-]+"); int major = Integer.parseInt(version[0]); // if major > 1, we're using the JEP-223 version string, e.g., 9-ea, 9+120 // otherwise the second number is the major version if (major > 1) { return major; } else { return Integer.parseInt(version[1]); } }
Get the major version of the java version string supplied. This method accepts any JEP-223-compliant strings (9-ea, 9+100), as well as legacy version strings such as 1.7.0_79
static String findJarsDir(String sparkHome, String scalaVersion, boolean failIfNotFound) { // TODO: change to the correct directory once the assembly build is changed. File libdir = new File(sparkHome, "jars"); if (!libdir.isDirectory()) { libdir = new File(sparkHome, String.format("assembly/target/scala-%s/jars", scalaVersion)); if (!libdir.isDirectory()) { checkState(!failIfNotFound, "Library directory '%s' does not exist; make sure Spark is built.", libdir.getAbsolutePath()); return null; } } return libdir.getAbsolutePath(); }
Find the location of the Spark jars dir, depending on whether we're looking at a build or a distribution directory.
public void pointTo(Object baseObject, long baseOffset, int sizeInBytes) { // Read the number of elements from the first 8 bytes. final long numElements = Platform.getLong(baseObject, baseOffset); assert numElements >= 0 : "numElements (" + numElements + ") should >= 0"; assert numElements <= Integer.MAX_VALUE : "numElements (" + numElements + ") should <= Integer.MAX_VALUE"; this.numElements = (int)numElements; this.baseObject = baseObject; this.baseOffset = baseOffset; this.sizeInBytes = sizeInBytes; this.elementOffset = baseOffset + calculateHeaderPortionInBytes(this.numElements); }
Update this UnsafeArrayData to point to different backing data. @param baseObject the base object @param baseOffset the offset within the base object @param sizeInBytes the size of this array's backing data, in bytes
public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { case KEY_TYPE_PTR: return isSetKeyTypePtr(); case VALUE_TYPE_PTR: return isSetValueTypePtr(); } throw new IllegalStateException(); }
Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise
public void prepare(HiveConf sqlOperationConf) throws HiveSQLException { setState(OperationState.RUNNING); try { driver = new Driver(sqlOperationConf, getParentSession().getUserName()); // set the operation handle information in Driver, so that thrift API users // can use the operation handle they receive, to lookup query information in // Yarn ATS String guid64 = Base64.encodeBase64URLSafeString(getHandle().getHandleIdentifier() .toTHandleIdentifier().getGuid()).trim(); driver.setOperationId(guid64); // In Hive server mode, we are not able to retry in the FetchTask // case, when calling fetch queries since execute() has returned. // For now, we disable the test attempts. driver.setTryCount(Integer.MAX_VALUE); String subStatement = new VariableSubstitution().substitute(sqlOperationConf, statement); response = driver.compileAndRespond(subStatement); if (0 != response.getResponseCode()) { throw toSQLException("Error while compiling statement", response); } mResultSchema = driver.getSchema(); // hasResultSet should be true only if the query has a FetchTask // "explain" is an exception for now if(driver.getPlan().getFetchTask() != null) { //Schema has to be set if (mResultSchema == null || !mResultSchema.isSetFieldSchemas()) { throw new HiveSQLException("Error compiling query: Schema and FieldSchema " + "should be set when query plan has a FetchTask"); } resultSchema = new TableSchema(mResultSchema); setHasResultSet(true); } else { setHasResultSet(false); } // Set hasResultSet true if the plan has ExplainTask // TODO explain should use a FetchTask for reading for (Task<? extends Serializable> task: driver.getPlan().getRootTasks()) { if (task.getClass() == ExplainTask.class) { resultSchema = new TableSchema(mResultSchema); setHasResultSet(true); break; } } } catch (HiveSQLException e) { setState(OperationState.ERROR); throw e; } catch (Exception e) { setState(OperationState.ERROR); throw new HiveSQLException("Error running query: " + e.toString(), e); } }
* Compile the query and extract metadata @param sqlOperationConf @throws HiveSQLException
private UserGroupInformation getCurrentUGI(HiveConf opConfig) throws HiveSQLException { try { return Utils.getUGI(); } catch (Exception e) { throw new HiveSQLException("Unable to get current user", e); } }
Returns the current UGI on the stack @param opConfig @return UserGroupInformation @throws HiveSQLException
private RowSet prepareFromRow(List<Object> rows, RowSet rowSet) throws Exception { for (Object row : rows) { rowSet.addRow((Object[]) row); } return rowSet; }
already encoded to thrift-able object in ThriftFormatter
private HiveConf getConfigForOperation() throws HiveSQLException { HiveConf sqlOperationConf = getParentSession().getHiveConf(); if (!getConfOverlay().isEmpty() || shouldRunAsync()) { // clone the parent session config for this query sqlOperationConf = new HiveConf(sqlOperationConf); // apply overlay query specific settings, if any for (Map.Entry<String, String> confEntry : getConfOverlay().entrySet()) { try { sqlOperationConf.verifyAndSet(confEntry.getKey(), confEntry.getValue()); } catch (IllegalArgumentException e) { throw new HiveSQLException("Error applying statement specific settings", e); } } } return sqlOperationConf; }
If there are query specific settings to overlay, then create a copy of config There are two cases we need to clone the session config that's being passed to hive driver 1. Async query - If the client changes a config setting, that shouldn't reflect in the execution already underway 2. confOverlay - The query specific settings should only be applied to the query config and not session @return new configuration @throws HiveSQLException
public static BloomFilter create(long expectedNumItems, double fpp) { if (fpp <= 0D || fpp >= 1D) { throw new IllegalArgumentException( "False positive probability must be within range (0.0, 1.0)" ); } return create(expectedNumItems, optimalNumOfBits(expectedNumItems, fpp)); }
Creates a {@link BloomFilter} with the expected number of insertions and expected false positive probability. Note that overflowing a {@code BloomFilter} with significantly more elements than specified, will result in its saturation, and a sharp deterioration of its false positive probability.
public static BloomFilter create(long expectedNumItems, long numBits) { if (expectedNumItems <= 0) { throw new IllegalArgumentException("Expected insertions must be positive"); } if (numBits <= 0) { throw new IllegalArgumentException("Number of bits must be positive"); } return new BloomFilterImpl(optimalNumOfHashFunctions(expectedNumItems, numBits), numBits); }
Creates a {@link BloomFilter} with given {@code expectedNumItems} and {@code numBits}, it will pick an optimal {@code numHashFunctions} which can minimize {@code fpp} for the bloom filter.
private void fetchAllOutstanding() { // Start by retrieving our shared state within a synchronized block. String[] blockIdsToFetch; int numRetries; RetryingBlockFetchListener myListener; synchronized (this) { blockIdsToFetch = outstandingBlocksIds.toArray(new String[outstandingBlocksIds.size()]); numRetries = retryCount; myListener = currentListener; } // Now initiate the fetch on all outstanding blocks, possibly initiating a retry if that fails. try { fetchStarter.createAndStart(blockIdsToFetch, myListener); } catch (Exception e) { logger.error(String.format("Exception while beginning fetch of %s outstanding blocks %s", blockIdsToFetch.length, numRetries > 0 ? "(after " + numRetries + " retries)" : ""), e); if (shouldRetry(e)) { initiateRetry(); } else { for (String bid : blockIdsToFetch) { listener.onBlockFetchFailure(bid, e); } } } }
Fires off a request to fetch all blocks that have not been fetched successfully or permanently failed (i.e., by a non-IOException).
private synchronized void initiateRetry() { retryCount += 1; currentListener = new RetryingBlockFetchListener(); logger.info("Retrying fetch ({}/{}) for {} outstanding blocks after {} ms", retryCount, maxRetries, outstandingBlocksIds.size(), retryWaitTime); executorService.submit(() -> { Uninterruptibles.sleepUninterruptibly(retryWaitTime, TimeUnit.MILLISECONDS); fetchAllOutstanding(); }); }
Lightweight method which initiates a retry in a different thread. The retry will involve calling fetchAllOutstanding() after a configured wait time.
private synchronized boolean shouldRetry(Throwable e) { boolean isIOException = e instanceof IOException || (e.getCause() != null && e.getCause() instanceof IOException); boolean hasRemainingRetries = retryCount < maxRetries; return isIOException && hasRemainingRetries; }
Returns true if we should retry due a block fetch failure. We will retry if and only if the exception was an IOException and we haven't retried 'maxRetries' times already.
@Override public UnsafeRow appendRow(Object kbase, long koff, int klen, Object vbase, long voff, int vlen) { // if run out of max supported rows or page size, return null if (numRows >= capacity || page == null || page.size() - pageCursor < recordLength) { return null; } long offset = page.getBaseOffset() + pageCursor; final long recordOffset = offset; Platform.copyMemory(kbase, koff, base, offset, klen); offset += klen; Platform.copyMemory(vbase, voff, base, offset, vlen); offset += vlen; Platform.putLong(base, offset, 0); pageCursor += recordLength; keyRowId = numRows; keyRow.pointTo(base, recordOffset, klen); valueRow.pointTo(base, recordOffset + klen, vlen); numRows++; return valueRow; }
Append a key value pair. It copies data into the backing MemoryBlock. Returns an UnsafeRow pointing to the value if succeeds, otherwise returns null.
@Override public UnsafeRow getKeyRow(int rowId) { assert(rowId >= 0); assert(rowId < numRows); if (keyRowId != rowId) { // if keyRowId == rowId, desired keyRow is already cached long offset = getKeyOffsetForFixedLengthRecords(rowId); keyRow.pointTo(base, offset, klen); // set keyRowId so we can check if desired row is cached keyRowId = rowId; } return keyRow; }
Returns the key row in this batch at `rowId`. Returned key row is reused across calls.
@Override protected UnsafeRow getValueFromKey(int rowId) { if (keyRowId != rowId) { getKeyRow(rowId); } assert(rowId >= 0); valueRow.pointTo(base, keyRow.getBaseOffset() + klen, vlen); return valueRow; }
Returns the value row by two steps: 1) looking up the key row with the same id (skipped if the key row is cached) 2) retrieve the value row by reusing the metadata from step 1) In most times, 1) is skipped because `getKeyRow(id)` is often called before `getValueRow(id)`.
@Override public org.apache.spark.unsafe.KVIterator<UnsafeRow, UnsafeRow> rowIterator() { return new org.apache.spark.unsafe.KVIterator<UnsafeRow, UnsafeRow>() { private final UnsafeRow key = new UnsafeRow(keySchema.length()); private final UnsafeRow value = new UnsafeRow(valueSchema.length()); private long offsetInPage = 0; private int recordsInPage = 0; private boolean initialized = false; private void init() { if (page != null) { offsetInPage = page.getBaseOffset(); recordsInPage = numRows; } initialized = true; } @Override public boolean next() { if (!initialized) init(); //searching for the next non empty page is records is now zero if (recordsInPage == 0) { freeCurrentPage(); return false; } key.pointTo(base, offsetInPage, klen); value.pointTo(base, offsetInPage + klen, vlen); offsetInPage += recordLength; recordsInPage -= 1; return true; } @Override public UnsafeRow getKey() { return key; } @Override public UnsafeRow getValue() { return value; } @Override public void close() { // do nothing } private void freeCurrentPage() { if (page != null) { freePage(page); page = null; } } }; }
Returns an iterator to go through all rows
@Override protected void reserveInternal(int newCapacity) { if (isArray() || type instanceof MapType) { int[] newLengths = new int[newCapacity]; int[] newOffsets = new int[newCapacity]; if (this.arrayLengths != null) { System.arraycopy(this.arrayLengths, 0, newLengths, 0, capacity); System.arraycopy(this.arrayOffsets, 0, newOffsets, 0, capacity); } arrayLengths = newLengths; arrayOffsets = newOffsets; } else if (type instanceof BooleanType) { if (byteData == null || byteData.length < newCapacity) { byte[] newData = new byte[newCapacity]; if (byteData != null) System.arraycopy(byteData, 0, newData, 0, capacity); byteData = newData; } } else if (type instanceof ByteType) { if (byteData == null || byteData.length < newCapacity) { byte[] newData = new byte[newCapacity]; if (byteData != null) System.arraycopy(byteData, 0, newData, 0, capacity); byteData = newData; } } else if (type instanceof ShortType) { if (shortData == null || shortData.length < newCapacity) { short[] newData = new short[newCapacity]; if (shortData != null) System.arraycopy(shortData, 0, newData, 0, capacity); shortData = newData; } } else if (type instanceof IntegerType || type instanceof DateType || DecimalType.is32BitDecimalType(type)) { if (intData == null || intData.length < newCapacity) { int[] newData = new int[newCapacity]; if (intData != null) System.arraycopy(intData, 0, newData, 0, capacity); intData = newData; } } else if (type instanceof LongType || type instanceof TimestampType || DecimalType.is64BitDecimalType(type)) { if (longData == null || longData.length < newCapacity) { long[] newData = new long[newCapacity]; if (longData != null) System.arraycopy(longData, 0, newData, 0, capacity); longData = newData; } } else if (type instanceof FloatType) { if (floatData == null || floatData.length < newCapacity) { float[] newData = new float[newCapacity]; if (floatData != null) System.arraycopy(floatData, 0, newData, 0, capacity); floatData = newData; } } else if (type instanceof DoubleType) { if (doubleData == null || doubleData.length < newCapacity) { double[] newData = new double[newCapacity]; if (doubleData != null) System.arraycopy(doubleData, 0, newData, 0, capacity); doubleData = newData; } } else if (childColumns != null) { // Nothing to store. } else { throw new RuntimeException("Unhandled " + type); } byte[] newNulls = new byte[newCapacity]; if (nulls != null) System.arraycopy(nulls, 0, newNulls, 0, capacity); nulls = newNulls; capacity = newCapacity; }
Spilt this function out since it is the slow path.
public static UTF8String fromBytes(byte[] bytes) { if (bytes != null) { return new UTF8String(bytes, BYTE_ARRAY_OFFSET, bytes.length); } else { return null; } }
Creates an UTF8String from byte array, which should be encoded in UTF-8. Note: `bytes` will be hold by returned UTF8String.
public static UTF8String fromBytes(byte[] bytes, int offset, int numBytes) { if (bytes != null) { return new UTF8String(bytes, BYTE_ARRAY_OFFSET + offset, numBytes); } else { return null; } }
Creates an UTF8String from byte array, which should be encoded in UTF-8. Note: `bytes` will be hold by returned UTF8String.
public static UTF8String fromAddress(Object base, long offset, int numBytes) { return new UTF8String(base, offset, numBytes); }
Creates an UTF8String from given address (base and offset) and length.
public static UTF8String fromString(String str) { return str == null ? null : fromBytes(str.getBytes(StandardCharsets.UTF_8)); }
Creates an UTF8String from String.
public static UTF8String blankString(int length) { byte[] spaces = new byte[length]; Arrays.fill(spaces, (byte) ' '); return fromBytes(spaces); }
Creates an UTF8String that contains `length` spaces.
public void writeToMemory(Object target, long targetOffset) { Platform.copyMemory(base, offset, target, targetOffset, numBytes); }
Writes the content of this string into a memory address, identified by an object and an offset. The target memory address must already been allocated, and have enough space to hold all the bytes in this string.
@Nonnull public ByteBuffer getByteBuffer() { if (base instanceof byte[] && offset >= BYTE_ARRAY_OFFSET) { final byte[] bytes = (byte[]) base; // the offset includes an object header... this is only needed for unsafe copies final long arrayOffset = offset - BYTE_ARRAY_OFFSET; // verify that the offset and length points somewhere inside the byte array // and that the offset can safely be truncated to a 32-bit integer if ((long) bytes.length < arrayOffset + numBytes) { throw new ArrayIndexOutOfBoundsException(); } return ByteBuffer.wrap(bytes, (int) arrayOffset, numBytes); } else { return ByteBuffer.wrap(getBytes()); } }
Returns a {@link ByteBuffer} wrapping the base object if it is a byte array or a copy of the data if the base object is not a byte array. Unlike getBytes this will not create a copy the array if this is a slice.
public int numChars() { int len = 0; for (int i = 0; i < numBytes; i += numBytesForFirstByte(getByte(i))) { len += 1; } return len; }
Returns the number of code points in it.
public long getPrefix() { // Since JVMs are either 4-byte aligned or 8-byte aligned, we check the size of the string. // If size is 0, just return 0. // If size is between 0 and 4 (inclusive), assume data is 4-byte aligned under the hood and // use a getInt to fetch the prefix. // If size is greater than 4, assume we have at least 8 bytes of data to fetch. // After getting the data, we use a mask to mask out data that is not part of the string. long p; long mask = 0; if (IS_LITTLE_ENDIAN) { if (numBytes >= 8) { p = Platform.getLong(base, offset); } else if (numBytes > 4) { p = Platform.getLong(base, offset); mask = (1L << (8 - numBytes) * 8) - 1; } else if (numBytes > 0) { p = (long) Platform.getInt(base, offset); mask = (1L << (8 - numBytes) * 8) - 1; } else { p = 0; } p = java.lang.Long.reverseBytes(p); } else { // byteOrder == ByteOrder.BIG_ENDIAN if (numBytes >= 8) { p = Platform.getLong(base, offset); } else if (numBytes > 4) { p = Platform.getLong(base, offset); mask = (1L << (8 - numBytes) * 8) - 1; } else if (numBytes > 0) { p = ((long) Platform.getInt(base, offset)) << 32; mask = (1L << (8 - numBytes) * 8) - 1; } else { p = 0; } } p &= ~mask; return p; }
Returns a 64-bit integer that can be used as the prefix used in sorting.
public byte[] getBytes() { // avoid copy if `base` is `byte[]` if (offset == BYTE_ARRAY_OFFSET && base instanceof byte[] && ((byte[]) base).length == numBytes) { return (byte[]) base; } else { byte[] bytes = new byte[numBytes]; copyMemory(base, offset, bytes, BYTE_ARRAY_OFFSET, numBytes); return bytes; } }
Returns the underline bytes, will be a copy of it if it's part of another array.
public UTF8String substring(final int start, final int until) { if (until <= start || start >= numBytes) { return EMPTY_UTF8; } int i = 0; int c = 0; while (i < numBytes && c < start) { i += numBytesForFirstByte(getByte(i)); c += 1; } int j = i; while (i < numBytes && c < until) { i += numBytesForFirstByte(getByte(i)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; copyMemory(base, offset + j, bytes, BYTE_ARRAY_OFFSET, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } }
Returns a substring of this. @param start the position of first code point @param until the position after last code point, exclusive.
public boolean contains(final UTF8String substring) { if (substring.numBytes == 0) { return true; } byte first = substring.getByte(0); for (int i = 0; i <= numBytes - substring.numBytes; i++) { if (getByte(i) == first && matchAt(substring, i)) { return true; } } return false; }
Returns whether this contains `substring` or not.
public UTF8String toUpperCase() { if (numBytes == 0) { return EMPTY_UTF8; } byte[] bytes = new byte[numBytes]; bytes[0] = (byte) Character.toTitleCase(getByte(0)); for (int i = 0; i < numBytes; i++) { byte b = getByte(i); if (numBytesForFirstByte(b) != 1) { // fallback return toUpperCaseSlow(); } int upper = Character.toUpperCase((int) b); if (upper > 127) { // fallback return toUpperCaseSlow(); } bytes[i] = (byte) upper; } return fromBytes(bytes); }
Returns the upper case of this string
public UTF8String toLowerCase() { if (numBytes == 0) { return EMPTY_UTF8; } byte[] bytes = new byte[numBytes]; bytes[0] = (byte) Character.toTitleCase(getByte(0)); for (int i = 0; i < numBytes; i++) { byte b = getByte(i); if (numBytesForFirstByte(b) != 1) { // fallback return toLowerCaseSlow(); } int lower = Character.toLowerCase((int) b); if (lower > 127) { // fallback return toLowerCaseSlow(); } bytes[i] = (byte) lower; } return fromBytes(bytes); }
Returns the lower case of this string
public UTF8String toTitleCase() { if (numBytes == 0) { return EMPTY_UTF8; } byte[] bytes = new byte[numBytes]; for (int i = 0; i < numBytes; i++) { byte b = getByte(i); if (i == 0 || getByte(i - 1) == ' ') { if (numBytesForFirstByte(b) != 1) { // fallback return toTitleCaseSlow(); } int upper = Character.toTitleCase(b); if (upper > 127) { // fallback return toTitleCaseSlow(); } bytes[i] = (byte) upper; } else { bytes[i] = b; } } return fromBytes(bytes); }
Returns the title case of this string, that could be used as title.
public int findInSet(UTF8String match) { if (match.contains(COMMA_UTF8)) { return 0; } int n = 1, lastComma = -1; for (int i = 0; i < numBytes; i++) { if (getByte(i) == (byte) ',') { if (i - (lastComma + 1) == match.numBytes && ByteArrayMethods.arrayEquals(base, offset + (lastComma + 1), match.base, match.offset, match.numBytes)) { return n; } lastComma = i; n++; } } if (numBytes - (lastComma + 1) == match.numBytes && ByteArrayMethods.arrayEquals(base, offset + (lastComma + 1), match.base, match.offset, match.numBytes)) { return n; } return 0; }
/* Returns the index of the string `match` in this String. This string has to be a comma separated list. If `match` contains a comma 0 will be returned. If the `match` isn't part of this String, 0 will be returned, else the index of match (1-based index)
private UTF8String copyUTF8String(int start, int end) { int len = end - start + 1; byte[] newBytes = new byte[len]; copyMemory(base, offset + start, newBytes, BYTE_ARRAY_OFFSET, len); return UTF8String.fromBytes(newBytes); }
Copy the bytes from the current UTF8String, and make a new UTF8String. @param start the start position of the current UTF8String in bytes. @param end the end position of the current UTF8String in bytes. @return a new UTF8String in the position of [start, end] of current UTF8String bytes.
public UTF8String trim(UTF8String trimString) { if (trimString != null) { return trimLeft(trimString).trimRight(trimString); } else { return null; } }
Based on the given trim string, trim this string starting from both ends This method searches for each character in the source string, removes the character if it is found in the trim string, stops at the first not found. It calls the trimLeft first, then trimRight. It returns a new string in which both ends trim characters have been removed. @param trimString the trim character string
public UTF8String trimLeft(UTF8String trimString) { if (trimString == null) return null; // the searching byte position in the source string int srchIdx = 0; // the first beginning byte position of a non-matching character int trimIdx = 0; while (srchIdx < numBytes) { UTF8String searchChar = copyUTF8String( srchIdx, srchIdx + numBytesForFirstByte(this.getByte(srchIdx)) - 1); int searchCharBytes = searchChar.numBytes; // try to find the matching for the searchChar in the trimString set if (trimString.find(searchChar, 0) >= 0) { trimIdx += searchCharBytes; } else { // no matching, exit the search break; } srchIdx += searchCharBytes; } if (trimIdx >= numBytes) { // empty string return EMPTY_UTF8; } else { return copyUTF8String(trimIdx, numBytes - 1); } }
Based on the given trim string, trim this string starting from left end This method searches each character in the source string starting from the left end, removes the character if it is in the trim string, stops at the first character which is not in the trim string, returns the new string. @param trimString the trim character string
public UTF8String trimRight(UTF8String trimString) { if (trimString == null) return null; int charIdx = 0; // number of characters from the source string int numChars = 0; // array of character length for the source string int[] stringCharLen = new int[numBytes]; // array of the first byte position for each character in the source string int[] stringCharPos = new int[numBytes]; // build the position and length array while (charIdx < numBytes) { stringCharPos[numChars] = charIdx; stringCharLen[numChars] = numBytesForFirstByte(getByte(charIdx)); charIdx += stringCharLen[numChars]; numChars ++; } // index trimEnd points to the first no matching byte position from the right side of // the source string. int trimEnd = numBytes - 1; while (numChars > 0) { UTF8String searchChar = copyUTF8String( stringCharPos[numChars - 1], stringCharPos[numChars - 1] + stringCharLen[numChars - 1] - 1); if (trimString.find(searchChar, 0) >= 0) { trimEnd -= stringCharLen[numChars - 1]; } else { break; } numChars --; } if (trimEnd < 0) { // empty string return EMPTY_UTF8; } else { return copyUTF8String(0, trimEnd); } }
Based on the given trim string, trim this string starting from right end This method searches each character in the source string starting from the right end, removes the character if it is in the trim string, stops at the first character which is not in the trim string, returns the new string. @param trimString the trim character string
public int indexOf(UTF8String v, int start) { if (v.numBytes() == 0) { return 0; } // locate to the start position. int i = 0; // position in byte int c = 0; // position in character while (i < numBytes && c < start) { i += numBytesForFirstByte(getByte(i)); c += 1; } do { if (i + v.numBytes > numBytes) { return -1; } if (ByteArrayMethods.arrayEquals(base, offset + i, v.base, v.offset, v.numBytes)) { return c; } i += numBytesForFirstByte(getByte(i)); c += 1; } while (i < numBytes); return -1; }
Returns the position of the first occurrence of substr in current string from the specified position (0-based index). @param v the string to be searched @param start the start position of the current string for searching @return the position of the first occurrence of substr, if not found, -1 returned.
private int find(UTF8String str, int start) { assert (str.numBytes > 0); while (start <= numBytes - str.numBytes) { if (ByteArrayMethods.arrayEquals(base, offset + start, str.base, str.offset, str.numBytes)) { return start; } start += 1; } return -1; }
Find the `str` from left to right.
public UTF8String subStringIndex(UTF8String delim, int count) { if (delim.numBytes == 0 || count == 0) { return EMPTY_UTF8; } if (count > 0) { int idx = -1; while (count > 0) { idx = find(delim, idx + 1); if (idx >= 0) { count --; } else { // can not find enough delim return this; } } if (idx == 0) { return EMPTY_UTF8; } byte[] bytes = new byte[idx]; copyMemory(base, offset, bytes, BYTE_ARRAY_OFFSET, idx); return fromBytes(bytes); } else { int idx = numBytes - delim.numBytes + 1; count = -count; while (count > 0) { idx = rfind(delim, idx - 1); if (idx >= 0) { count --; } else { // can not find enough delim return this; } } if (idx + delim.numBytes == numBytes) { return EMPTY_UTF8; } int size = numBytes - delim.numBytes - idx; byte[] bytes = new byte[size]; copyMemory(base, offset + idx + delim.numBytes, bytes, BYTE_ARRAY_OFFSET, size); return fromBytes(bytes); } }
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything the left of the final delimiter (counting from left) is returned. If count is negative, every to the right of the final delimiter (counting from the right) is returned. subStringIndex performs a case-sensitive match when searching for delim.
public UTF8String rpad(int len, UTF8String pad) { int spaces = len - this.numChars(); // number of char need to pad if (spaces <= 0 || pad.numBytes() == 0) { // no padding at all, return the substring of the current string return substring(0, len); } else { int padChars = pad.numChars(); int count = spaces / padChars; // how many padding string needed // the partial string of the padding UTF8String remain = pad.substring(0, spaces - padChars * count); byte[] data = new byte[this.numBytes + pad.numBytes * count + remain.numBytes]; copyMemory(this.base, this.offset, data, BYTE_ARRAY_OFFSET, this.numBytes); int offset = this.numBytes; int idx = 0; while (idx < count) { copyMemory(pad.base, pad.offset, data, BYTE_ARRAY_OFFSET + offset, pad.numBytes); ++ idx; offset += pad.numBytes; } copyMemory(remain.base, remain.offset, data, BYTE_ARRAY_OFFSET + offset, remain.numBytes); return UTF8String.fromBytes(data); } }
Returns str, right-padded with pad to a length of len For example: ('hi', 5, '??') =&gt; 'hi???' ('hi', 1, '??') =&gt; 'h'
public static UTF8String concat(UTF8String... inputs) { // Compute the total length of the result. long totalLength = 0; for (int i = 0; i < inputs.length; i++) { if (inputs[i] != null) { totalLength += (long)inputs[i].numBytes; } else { return null; } } // Allocate a new byte array, and copy the inputs one by one into it. final byte[] result = new byte[Ints.checkedCast(totalLength)]; int offset = 0; for (int i = 0; i < inputs.length; i++) { int len = inputs[i].numBytes; copyMemory( inputs[i].base, inputs[i].offset, result, BYTE_ARRAY_OFFSET + offset, len); offset += len; } return fromBytes(result); }
Concatenates input strings together into a single string. Returns null if any input is null.