public class Utils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static Predicate<java.lang.String> |
COMMITROOT_OR_REVISIONS
A predicate for _commitRoot and _revisions names.
|
static int |
NODE_NAME_LIMIT
The maximum size a node name, in bytes.
|
static int |
PATH_LONG
The maximum length of the parent path, in bytes.
|
static int |
PATH_SHORT
The length of path (in characters), whose UTF-8 representation can not
possibly be too large to be used for the primary key for the document
store.
|
static Predicate<java.lang.String> |
PROPERTY_OR_DELETED
A predicate for property and _deleted names.
|
static Predicate<java.lang.String> |
PROPERTY_OR_DELETED_OR_COMMITROOT_OR_REVISIONS
A predicate for property, _deleted, _commitRoot or _revisions names.
|
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static <T> CloseableIterable<T> |
abortingIterable(java.lang.Iterable<T> iterable,
Predicate<T> p)
Wraps the given iterable and aborts iteration over elements when the
predicate on an element evaluates to
false . |
static void |
alignWithExternalRevisions(@NotNull NodeDocument rootDoc,
@NotNull Clock clock,
int clusterId,
long warnThresholdMillis)
Makes sure the current time is after the most recent external revision
timestamp in the _lastRev map of the given root document.
|
static java.lang.String |
asISO8601(long ms)
Formats the epoch time in milliseconds as ISO-8601 in UTC.
|
static java.lang.Long |
asLong(@Nullable java.lang.Number n)
Returns the given number instance as a
Long . |
static java.lang.Iterable<StringValue> |
asStringValueIterable(@NotNull java.lang.Iterable<java.lang.String> values)
|
static void |
checkRevisionAge(DocumentStore store,
ClusterNodeInfo info,
Clock clock)
Check the revision age on the root document for the given cluster node
info.
|
static void |
closeIfCloseable(java.lang.Object obj)
Closes the obj its of type
Closeable . |
static <K> void |
deepCopyMap(java.util.Map<K,java.lang.Object> source,
java.util.Map<K,java.lang.Object> target)
Deep copy of a map that may contain map values.
|
static java.lang.StringBuilder |
encodeHexString(byte[] data,
java.lang.StringBuilder sb)
Encodes the given data as hexadecimal string representation and appends
it to the
StringBuilder . |
static java.lang.String |
escapePropertyName(java.lang.String propertyName) |
static int |
estimateMemoryUsage(java.util.Map<?,java.lang.Object> map) |
static java.lang.Iterable<NodeDocument> |
getAllDocuments(DocumentStore store)
Returns an
Iterable over all NodeDocument s in the given
store. |
static int |
getDepthFromId(java.lang.String id) |
static int |
getIdDepth(Path path)
Calculates the depth prefix of the id for the given
path . |
static java.lang.String |
getIdFromPath(@NotNull Path path) |
static java.lang.String |
getIdFromPath(@NotNull java.lang.String path) |
static java.lang.String |
getKeyLowerLimit(Path path)
Returns the lower key limit to retrieve the children of the given
path . |
static java.lang.String |
getKeyUpperLimit(Path path)
Returns the upper key limit to retrieve the children of the given
path . |
static long |
getMaxExternalTimestamp(java.lang.Iterable<Revision> revisions,
int localClusterId)
Returns the highest timestamp of all the passed external revisions.
|
static long |
getMinTimestampForDiff(@NotNull RevisionVector fromRev,
@NotNull RevisionVector toRev,
@NotNull RevisionVector minRevisions)
Returns the minimum timestamp to use for a query for child documents that
have been modified between
fromRev and toRev . |
static java.lang.String |
getModuleVersion()
Returns the version of the module that contains the DocumentNodeStore.
|
static @Nullable java.lang.String |
getParentId(java.lang.String id)
Returns the parent id for given id if possible
|
static @Nullable java.lang.String |
getParentIdFromLowerLimit(java.lang.String fromKey)
Returns parentId extracted from the fromKey.
|
static java.lang.String |
getPathFromId(java.lang.String id) |
static java.lang.String |
getPreviousIdFor(Path path,
Revision r,
int height) |
static Path |
getPreviousPathFor(Path path,
Revision r,
int height) |
static @NotNull NodeDocument |
getRootDocument(@NotNull DocumentStore store)
Returns the root node document of the given document store.
|
static java.lang.Iterable<NodeDocument> |
getSelectedDocuments(DocumentStore store,
java.lang.String indexedProperty,
long startValue)
Like
getSelectedDocuments(DocumentStore, String, long, int) with
a default batchSize . |
static java.lang.Iterable<NodeDocument> |
getSelectedDocuments(DocumentStore store,
java.lang.String indexedProperty,
long startValue,
int batchSize)
Returns an
Iterable over all NodeDocument s in the given
store matching a condition on an indexed property. |
static @NotNull RevisionVector |
getStartRevisions(@NotNull java.lang.Iterable<ClusterNodeInfoDocument> clusterNodes)
Returns a revision vector that contains a revision for each of the passed
cluster nodes with a revision timestamp that corresponds to the last
known time when the cluster node was started.
|
static boolean |
isCommitted(@Nullable java.lang.String tag)
Returns
true if a revision tagged with the given revision
should be considered committed, false otherwise. |
static boolean |
isGreaterOrEquals(@NotNull RevisionVector a,
@NotNull RevisionVector b)
Returns true if all the revisions in the
a greater or equals
to their counterparts in b . |
static boolean |
isHiddenPath(@NotNull java.lang.String path) |
static boolean |
isIdFromLongPath(java.lang.String id) |
static boolean |
isLeafPreviousDocId(java.lang.String id)
Determines if the passed id belongs to a leaf level previous doc
|
static boolean |
isLocalChange(@NotNull RevisionVector from,
@NotNull RevisionVector to,
int clusterId)
Returns
true if changes identified by the from and
to RevisionVector are considered local changes. |
static boolean |
isLongPath(Path path) |
static boolean |
isNodeNameLong(Path path,
int sizeLimit)
Checks whether Node name is too long or not based on underlining document store
|
static boolean |
isPreviousDocId(java.lang.String id)
Determines if the passed id belongs to a previous doc
|
static boolean |
isPropertyName(java.lang.String key) |
static void |
joinQuietly(java.lang.Thread... threads)
Calls
Thread.join() on each of the passed threads and catches
any potentially thrown InterruptedException . |
static @Nullable Revision |
max(@Nullable Revision a,
@Nullable Revision b)
Returns the revision with the newer timestamp or
null if both
revisions are null . |
static @Nullable Revision |
max(@Nullable Revision a,
@Nullable Revision b,
@NotNull java.util.Comparator<Revision> c)
Returns the revision which is considered more recent or
null if
both revisions are null . |
static @Nullable Revision |
min(@Nullable Revision a,
@Nullable Revision b)
Returns the revision with the older timestamp or
null if both
revisions are null . |
static @Nullable Revision |
min(@Nullable Revision a,
@Nullable Revision b,
@NotNull java.util.Comparator<Revision> c)
Returns the revision which is considered older or
null if
both revisions are null . |
static int |
pathDepth(java.lang.String path) |
static java.lang.Iterable<java.lang.String> |
pathToId(@NotNull java.lang.Iterable<java.lang.String> paths)
Transforms the given paths into ids using
getIdFromPath(String) . |
static @NotNull Revision |
resolveCommitRevision(@NotNull Revision rev,
@NotNull java.lang.String tag)
Resolve the commit revision for the given revision
rev and
the associated commit tag. |
static long |
sum(long... addends)
Calculates the sum of the given long values.
|
static java.lang.String |
timestampToString(long timestamp)
Provides a readable string for given timestamp
|
static java.lang.String |
unescapePropertyName(java.lang.String key) |
public static final int PATH_SHORT
public static final int PATH_LONG
public static final int NODE_NAME_LIMIT
public static final Predicate<java.lang.String> PROPERTY_OR_DELETED
public static final Predicate<java.lang.String> PROPERTY_OR_DELETED_OR_COMMITROOT_OR_REVISIONS
public static final Predicate<java.lang.String> COMMITROOT_OR_REVISIONS
public static int pathDepth(java.lang.String path)
public static int getIdDepth(Path path)
path
. The is
the same as pathDepth(String)
, but takes a Path
argument.path
- a path.path
.public static int estimateMemoryUsage(java.util.Map<?,java.lang.Object> map)
public static java.lang.String escapePropertyName(java.lang.String propertyName)
public static java.lang.String unescapePropertyName(java.lang.String key)
public static boolean isPropertyName(java.lang.String key)
public static java.lang.String getIdFromPath(@NotNull @NotNull java.lang.String path)
public static java.lang.String getIdFromPath(@NotNull @NotNull Path path)
public static java.lang.StringBuilder encodeHexString(byte[] data, java.lang.StringBuilder sb)
StringBuilder
. The hex digits are in lower case.data
- the bytes to encode.sb
- the hexadecimal string representation is appended to this
StringBuilder
.StringBuilder
passed to this method.@Nullable public static @Nullable java.lang.String getParentId(java.lang.String id)
It would return null in following cases
id
- id for which parent id needs to be determinedpublic static boolean isNodeNameLong(Path path, int sizeLimit)
path
- node pathsizeLimit
- sizeLimit for node namepublic static boolean isLongPath(Path path)
public static boolean isIdFromLongPath(java.lang.String id)
public static java.lang.String getPathFromId(java.lang.String id)
public static int getDepthFromId(java.lang.String id) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public static boolean isPreviousDocId(java.lang.String id)
id
- id to checkpublic static boolean isLeafPreviousDocId(java.lang.String id)
id
- id to checkpublic static <K> void deepCopyMap(java.util.Map<K,java.lang.Object> source, java.util.Map<K,java.lang.Object> target)
K
- the type of the map keysource
- the source maptarget
- the target mappublic static java.lang.String getKeyLowerLimit(Path path)
path
.path
- a path.public static java.lang.String getKeyUpperLimit(Path path)
path
.path
- a path.@Nullable public static @Nullable java.lang.String getParentIdFromLowerLimit(java.lang.String fromKey)
fromKey
- key used as start key in queriespublic static boolean isCommitted(@Nullable @Nullable java.lang.String tag)
true
if a revision tagged with the given revision
should be considered committed, false
otherwise. Committed
revisions have a tag, which equals 'c' or starts with 'c-'.tag
- the tag (may be null
).true
if committed; false
otherwise.@NotNull public static @NotNull Revision resolveCommitRevision(@NotNull @NotNull Revision rev, @NotNull @NotNull java.lang.String tag)
rev
and
the associated commit tag.rev
- a revision.tag
- the associated commit tag.rev
.public static void closeIfCloseable(java.lang.Object obj)
Closeable
. It is mostly
used to close Iterator/Iterables which are backed by say DBCursorobj
- object to closepublic static java.lang.String timestampToString(long timestamp)
@Nullable public static @Nullable Revision max(@Nullable @Nullable Revision a, @Nullable @Nullable Revision b)
null
if both
revisions are null
. The implementation will return the first
revision if both have the same timestamp.a
- the first revision (or null
).b
- the second revision (or null
).@Nullable public static @Nullable Revision max(@Nullable @Nullable Revision a, @Nullable @Nullable Revision b, @NotNull @NotNull java.util.Comparator<Revision> c)
null
if
both revisions are null
. The implementation will return the first
revision if both are considered equal. The comparison is done using the
provided comparator.a
- the first revision (or null
).b
- the second revision (or null
).c
- the comparator.@Nullable public static @Nullable Revision min(@Nullable @Nullable Revision a, @Nullable @Nullable Revision b)
null
if both
revisions are null
. The implementation will return the first
revision if both have the same timestamp.a
- the first revision (or null
).b
- the second revision (or null
).@Nullable public static @Nullable Revision min(@Nullable @Nullable Revision a, @Nullable @Nullable Revision b, @NotNull @NotNull java.util.Comparator<Revision> c)
null
if
both revisions are null
. The implementation will return the first
revision if both are considered equal. The comparison is done using the
provided comparator.a
- the first revision (or null
).b
- the second revision (or null
).c
- the comparator.public static java.lang.Iterable<NodeDocument> getAllDocuments(DocumentStore store)
Iterable
over all NodeDocument
s in the given
store. The returned Iterable does not guarantee a consistent
view on the store. it may return documents that have been added to the
store after this method had been called.store
- a DocumentStore
.Iterable
over all documents in the store.@NotNull public static @NotNull NodeDocument getRootDocument(@NotNull @NotNull DocumentStore store)
DocumentStore.find(Collection, String)
, which means the
implementation is allowed to return a cached version of the document.
The document is therefore not guaranteed to be up-to-date.store
- a document store.java.lang.IllegalStateException
- if there is no root document.public static java.lang.Iterable<NodeDocument> getSelectedDocuments(DocumentStore store, java.lang.String indexedProperty, long startValue, int batchSize)
Iterable
over all NodeDocument
s in the given
store matching a condition on an indexed property. The returned
Iterable
does not guarantee a consistent view on the store.
it may return documents that have been added to the store after this
method had been called.store
- a DocumentStore
.indexedProperty
- the name of the indexed property.startValue
- the lower bound value for the indexed property
(inclusive).batchSize
- number of documents to fetch at onceIterable
over all documents in the store matching the
conditionpublic static java.lang.Iterable<NodeDocument> getSelectedDocuments(DocumentStore store, java.lang.String indexedProperty, long startValue)
getSelectedDocuments(DocumentStore, String, long, int)
with
a default batchSize
.public static boolean isHiddenPath(@NotNull @NotNull java.lang.String path)
path
represent oak's internal path. That is, a path
element start with a colon.public static java.lang.Iterable<StringValue> asStringValueIterable(@NotNull @NotNull java.lang.Iterable<java.lang.String> values)
Iterable
from String
to
StringValue
elements. The Iterable
must no have
null
values.public static java.lang.Iterable<java.lang.String> pathToId(@NotNull @NotNull java.lang.Iterable<java.lang.String> paths)
getIdFromPath(String)
.public static long getMaxExternalTimestamp(java.lang.Iterable<Revision> revisions, int localClusterId)
localClusterId
.revisions
- the revisions to consider.localClusterId
- the id of the local cluster node.Long.MIN_VALUE
if none of the
revisions is external.public static java.lang.Long asLong(@Nullable @Nullable java.lang.Number n)
Long
.n
- a number or null
.Long
or null
if n
is null
.@NotNull public static @NotNull RevisionVector getStartRevisions(@NotNull @NotNull java.lang.Iterable<ClusterNodeInfoDocument> clusterNodes)
clusterNodes
- the cluster node information.public static long getMinTimestampForDiff(@NotNull @NotNull RevisionVector fromRev, @NotNull @NotNull RevisionVector toRev, @NotNull @NotNull RevisionVector minRevisions)
fromRev
and toRev
.fromRev
- the from revision.toRev
- the to revision.minRevisions
- the minimum revisions of foreign cluster nodes. These
are derived from the startTime of a cluster node.public static boolean isGreaterOrEquals(@NotNull @NotNull RevisionVector a, @NotNull @NotNull RevisionVector b)
a
greater or equals
to their counterparts in b
. If b
contains revisions
for cluster nodes that are not present in a
, return false.a
- b
- a
are at least
as recent as their counterparts in the b
public static boolean isLocalChange(@NotNull @NotNull RevisionVector from, @NotNull @NotNull RevisionVector to, int clusterId)
true
if changes identified by the from
and
to
RevisionVector
are considered local changes. That is
the only difference between the two revision vectors are for the given
(local) clusterId
.from
- the from revision vector.to
- the to revision vector.clusterId
- the local clusterId.public static <T> CloseableIterable<T> abortingIterable(java.lang.Iterable<T> iterable, Predicate<T> p)
false
. Calling
close()
on the returned iterable will close the passed iterable
if it is Closeable
.iterable
- the iterable to wrap.p
- the predicate.public static void alignWithExternalRevisions(@NotNull @NotNull NodeDocument rootDoc, @NotNull @NotNull Clock clock, int clusterId, long warnThresholdMillis) throws java.lang.InterruptedException
clock
is after the external
revision timestamp.rootDoc
- the root document.clock
- the clock.clusterId
- the local clusterId.warnThresholdMillis
- log a warning when an external change in
the future is detected with more than this time difference.java.lang.InterruptedException
- if the current thread is interrupted while
waiting. The interrupted status on the current thread is cleared
when this exception is thrown.public static void joinQuietly(java.lang.Thread... threads)
Thread.join()
on each of the passed threads and catches
any potentially thrown InterruptedException
.threads
- the threads to join.public static java.lang.String getModuleVersion()
public static void checkRevisionAge(DocumentStore store, ClusterNodeInfo info, Clock clock) throws DocumentStoreException
DocumentStoreException
if the
_lastRev
timestamp for the cluster node is newer then the current
clock
time. The check will not fail if the root document does
not exist or does not have a _lastRev
entry for the cluster node.store
- the document store from where to read the root document.info
- the cluster node info with the clusterId.clock
- the clock to get the current time.DocumentStoreException
- if the check fails.public static long sum(long... addends)
Long#MAX_VALUE
when the result
would actually be bigger than that. Similarly, Long#MIN_VALUE
is
returned when the result would actually be smaller than that.addends
- the values.public static java.lang.String asISO8601(long ms)
ms
- the time in milliseconds.Copyright © 2012–2022 The Apache Software Foundation. All rights reserved.