Package com.clickhouse.data
Class ClickHouseUtils
java.lang.Object
com.clickhouse.data.ClickHouseUtils
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringapplyVariables(String template, UnaryOperator<String> applyFunc) static StringapplyVariables(String template, Map<String, String> variables) static FileCreates a temporary file.static FilecreateTempFile(String prefix, String suffix) Creates a temporary file with given prefix and suffix.static FilecreateTempFile(String prefix, String suffix, boolean deleteOnExit) Creates a temporary file with the given prefix and suffix.static StringDecode given string usingURLDecoderandStandardCharsets.UTF_8.static StringEncode given string usingURLEncoderandStandardCharsets.UTF_8.static StringEscape quotes in given string.extractParameters(String query, Map<String, String> params) Extracts parameters, usually key-value pairs, from the given query string.Finds files according to the given pattern and path.static StringWrapper ofString.format(Locale.ROOT, ...).static chargetCloseBracket(char openBracket) static PathGets absolute and normalized path to the given file.static InputStreamgetFileInputStream(String file) Search file in current directory, home directory, and then classpath, Get input stream to read the given file.static OutputStreamgetFileOutputStream(String file) Get output stream for writing a file.static StringgetLeadingComment(String sql) static StringgetProperty(String key, String defaultValue, Properties... props) static StringgetProperty(String key, Properties... props) static <T> TgetService(Class<? extends T> serviceInterface) static <T> TgetService(Class<? extends T> serviceInterface, Supplier<T> supplier) static <T> TgetService(Class<? extends T> serviceInterface, T defaultService) Load service according to given interface usingServiceLoader, fallback to given default service or supplier function if not found.static intindexOf(byte[] bytes, byte[] search) static booleanisCloseBracket(char ch) static booleanisOpenBracket(char ch) static booleanisQuote(char ch) static booleanisSeparator(char ch) static ExecutorServicenewThreadPool(Object owner, int maxThreads, int maxRequests) static ExecutorServicenewThreadPool(Object owner, int coreThreads, int maxThreads, int maxRequests, long keepAliveTimeoutMs, boolean allowCoreThreadTimeout) static StringnormalizeDirectory(String dir) Normalizes given directory by appending back slash if it does exist.static ObjectSimple and un-protected JSON parser.static intreadEnumValues(String args, int startIndex, int len, Map<String, Integer> values) static intreadNameOrQuotedString(String args, int startIndex, int len, StringBuilder builder) static intreadParameters(String args, int startIndex, int len, List<String> params) readValueArray(String args, int startIndex, int len) static intreadValueArray(String args, int startIndex, int len, Consumer<String> func) static intskipBrackets(String args, int startIndex, int len, char bracket) Skip brackets and content inside with consideration of nested brackets, quoted string and comments.static intskipContentsUntil(String args, int startIndex, int len, char... endChars) Skip quoted string, comments, and brackets until seeing one ofendCharsor reaching end of the given string.static intskipContentsUntil(String args, int startIndex, int len, String[] keywords, boolean caseSensitive) Skip quoted string, comments, and brackets until seeing all the givenkeywords(with only whitespaces or comments in between) or reaching end of the given string.static intskipContentsUntil(String args, int startIndex, int len, String keyword, boolean caseSensitive) Skip quoted string, comments, and brackets until seeing thekeywordor reaching end of the given string.static intskipMultiLineComment(String args, int startIndex, int len) Skip nested multi-line comment.static intskipQuotedString(String args, int startIndex, int len, char quote) Skip quoted string.static intskipSingleLineComment(String args, int startIndex, int len) Skip single line comment.static StringtoJavaByteArrayExpression(byte[] bytes) static StringUnescape quoted string.static booleanwaitFor(AtomicBoolean flag, long timeout, TimeUnit unit) Waits until the flag turns totrueor timed out.
-
Field Details
-
DEFAULT_CHARSET
Default charset. -
MIN_CORE_THREADS
public static final int MIN_CORE_THREADS- See Also:
-
NULL_FUTURE
-
NULL_SUPPLIER
-
VARIABLE_PREFIX
- See Also:
-
VARIABLE_SUFFIX
- See Also:
-
-
Method Details
-
applyVariables
-
applyVariables
-
createTempFile
Creates a temporary file. Same ascreateTempFile(null, null, true).- Returns:
- non-null temporary file
- Throws:
IOException- when failed to create the temporary file
-
createTempFile
Creates a temporary file with given prefix and suffix. Same ascreateTempFile(prefix, suffix, true).- Parameters:
prefix- prefix, could be nullsuffix- suffix, could be null- Returns:
- non-null temporary file
- Throws:
IOException- when failed to create the temporary file
-
createTempFile
public static File createTempFile(String prefix, String suffix, boolean deleteOnExit) throws IOException Creates a temporary file with the given prefix and suffix. The file has only read and write access granted to the owner.- Parameters:
prefix- prefix, null or empty string is taken as"ch"suffix- suffix, null or empty string is taken as".data"deleteOnExit- whether the file be deleted on exit- Returns:
- non-null temporary file
- Throws:
IOException- when failed to create the temporary file
-
decode
Decode given string usingURLDecoderandStandardCharsets.UTF_8.- Parameters:
encodedString- encoded string- Returns:
- non-null decoded string
-
encode
Encode given string usingURLEncoderandStandardCharsets.UTF_8.- Parameters:
str- string to encode- Returns:
- non-null encoded string
-
extractParameters
Extracts parameters, usually key-value pairs, from the given query string.- Parameters:
query- non-empty query stringparams- mutable map for extracted parameters, a newHashMapwill be created when it's null- Returns:
- map with extracted parameters, usually same as
params
-
getFile
Gets absolute and normalized path to the given file.- Parameters:
file- non-empty file- Returns:
- non-null absolute and normalized path to the file
-
findFiles
Finds files according to the given pattern and path.- Parameters:
pattern- non-empty pattern may or may have syntax prefix as inFileSystem.getPathMatcher(String), defaults toglobsyntaxpaths- path to search, defaults to current work directory- Returns:
- non-null list of normalized absolute paths matching the pattern
- Throws:
IOException- when failed to find files
-
toJavaByteArrayExpression
-
indexOf
public static int indexOf(byte[] bytes, byte[] search) -
newThreadPool
-
newThreadPool
public static ExecutorService newThreadPool(Object owner, int coreThreads, int maxThreads, int maxRequests, long keepAliveTimeoutMs, boolean allowCoreThreadTimeout) -
isCloseBracket
public static boolean isCloseBracket(char ch) -
isOpenBracket
public static boolean isOpenBracket(char ch) -
isQuote
public static boolean isQuote(char ch) -
isSeparator
public static boolean isSeparator(char ch) -
escape
Escape quotes in given string.- Parameters:
str- stringquote- quote to escape- Returns:
- escaped string
-
unescape
Unescape quoted string.- Parameters:
str- quoted string- Returns:
- unescaped string
-
format
Wrapper ofString.format(Locale.ROOT, ...).- Parameters:
template- string to formatargs- arguments used in substitution- Returns:
- formatted string
-
normalizeDirectory
Normalizes given directory by appending back slash if it does exist.- Parameters:
dir- original directory- Returns:
- normalized directory
-
parseJson
Simple and un-protected JSON parser.- Parameters:
json- non-empty JSON string- Returns:
- object array, Boolean, Number, null, String, or Map
- Throws:
IllegalArgumentException- when JSON string is null or empty
-
getCloseBracket
public static char getCloseBracket(char openBracket) -
getService
-
getService
Load service according to given interface usingServiceLoader, fallback to given default service or supplier function if not found.- Type Parameters:
T- type of service- Parameters:
serviceInterface- non-null service interfacedefaultService- optionally default service- Returns:
- non-null service
-
getService
-
getFileInputStream
Search file in current directory, home directory, and then classpath, Get input stream to read the given file.- Parameters:
file- path to the file- Returns:
- input stream
- Throws:
FileNotFoundException- when the file does not exists
-
getFileOutputStream
Get output stream for writing a file. Directories and file will be created if they do not exist.- Parameters:
file- path to the file- Returns:
- output stream
- Throws:
IOException- when failed to create directories and/or file
-
getLeadingComment
-
getProperty
-
getProperty
-
skipBrackets
Skip brackets and content inside with consideration of nested brackets, quoted string and comments.- Parameters:
args- non-null string to scanstartIndex- start index, optionally index of the opening bracketlen- end index, usually length of the given stringbracket- opening bracket supported byisOpenBracket(char)- Returns:
- index next to matched closing bracket
- Throws:
IllegalArgumentException- when missing closing bracket(s)
-
skipQuotedString
Skip quoted string.- Parameters:
args- non-null string to scanstartIndex- start index, optionally start of the quoted stringlen- end index, usually length of the given stringquote- quote supported byisQuote(char)- Returns:
- index next to the other matched quote
- Throws:
IllegalArgumentException- when missing quote
-
skipSingleLineComment
Skip single line comment.- Parameters:
args- non-null string to scanstartIndex- start index, optionally start of the single line commentlen- end index, usually length of the given string- Returns:
- index of start of next line, right after
\n
-
skipMultiLineComment
Skip nested multi-line comment.- Parameters:
args- non-null string to scanstartIndex- start index, optionally start of the multi-line commentlen- end index, usually length of the given string- Returns:
- index next to end of the outter most multi-line comment
- Throws:
IllegalArgumentException- when multi-line comment is unclosed
-
skipContentsUntil
Skip quoted string, comments, and brackets until seeing one ofendCharsor reaching end of the given string.- Parameters:
args- non-null string to scanstartIndex- start indexlen- end index, usually length of the given stringendChars- skip characters until seeing one of the specified characters or reaching end of the string; '\0' is used when it's null or empty- Returns:
- index of
endCharorlen
-
skipContentsUntil
public static int skipContentsUntil(String args, int startIndex, int len, String keyword, boolean caseSensitive) Skip quoted string, comments, and brackets until seeing thekeywordor reaching end of the given string.- Parameters:
args- non-null string to scanstartIndex- start indexlen- end index, usually length of the given stringkeyword- keyword, null or empty string means any charactercaseSensitive- whether keyword is case sensitive or not- Returns:
- index of
endCharorlen
-
skipContentsUntil
public static int skipContentsUntil(String args, int startIndex, int len, String[] keywords, boolean caseSensitive) Skip quoted string, comments, and brackets until seeing all the givenkeywords(with only whitespaces or comments in between) or reaching end of the given string.- Parameters:
args- non-null string to scanstartIndex- start indexlen- end index, usually length of the given stringkeywords- keywords, null or empty one means any charactercaseSensitive- whether keyword is case sensitive or not- Returns:
- index of
endCharorlen
-
readNameOrQuotedString
public static int readNameOrQuotedString(String args, int startIndex, int len, StringBuilder builder) -
readEnumValues
-
readValueArray
-
readValueArray
-
readParameters
-
waitFor
public static boolean waitFor(AtomicBoolean flag, long timeout, TimeUnit unit) throws InterruptedException Waits until the flag turns totrueor timed out.- Parameters:
flag- non-null boolean flag to checktimeout- timeout, negative or zero means foreverunit- non-null time unit- Returns:
- true if the flag turns to true within given timeout; false otherwise
- Throws:
InterruptedException- when thread was interrupted
-