This is an automated email from the ASF dual-hosted git repository. agura pushed a commit to branch ignite-14403 in repository https://gitbox.apache.org/repos/asf/ignite-3.git
commit a8919feb9c7abe1e86a0f943d84bae1a697c18e8 Author: Andrey Gura <[email protected]> AuthorDate: Thu Mar 25 18:16:13 2021 +0300 IGNITE-14403 Added ignite-core module and IgniteUuid class --- pom.xml => modules/core/pom.xml | 55 ++++++--- .../java/org/apache/ignite/lang/IgniteUuid.java | 135 +++++++++++++++++++++ .../apache/ignite/lang/IgniteUuidGenerator.java | 84 +++++++++++++ pom.xml | 1 + 4 files changed, 257 insertions(+), 18 deletions(-) diff --git a/pom.xml b/modules/core/pom.xml similarity index 52% copy from pom.xml copy to modules/core/pom.xml index 4a225d9..c84ded1 100644 --- a/pom.xml +++ b/modules/core/pom.xml @@ -17,8 +17,10 @@ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<!-- + POM file. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -26,23 +28,40 @@ <groupId>org.apache.ignite</groupId> <artifactId>ignite-parent</artifactId> <version>1</version> - <relativePath>parent</relativePath> + <relativePath>../../parent/pom.xml</relativePath> </parent> - <artifactId>apache-ignite</artifactId> + <artifactId>ignite-core</artifactId> <version>3.0.0-SNAPSHOT</version> - <packaging>pom</packaging> - - <modules> - <module>modules/api</module> - <module>modules/bytecode</module> - <module>modules/cli</module> - <module>modules/cli-common</module> - <module>modules/configuration</module> - <module>modules/configuration-annotation-processor</module> - <module>modules/network</module> - <module>modules/rest</module> - <module>modules/runner</module> - <module>modules/schema</module> - </modules> + + <dependencies> + <!-- Test dependencies. --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + </plugin> + </plugins> + </build> </project> diff --git a/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuid.java b/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuid.java new file mode 100644 index 0000000..d05ff50 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuid.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.lang; + +import java.io.Serializable; +import java.util.UUID; + +/** + * This is a faster performing version of {@link UUID}. On basic tests this version is at least + * 10x time faster for ID creation. It uses extra memory for 8-byte counter additionally to + * internal UUID. + */ +public final class IgniteUuid implements Comparable<IgniteUuid>, Cloneable, Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final UUID gid; + + /** */ + private final long locId; + + /** + * Constructs {@code IgniteUuid} from a global and local identifiers. + * + * @param gid UUID. + * @param locId Counter. + */ + public IgniteUuid(UUID gid, long locId) { + assert gid != null; + + this.gid = gid; + this.locId = locId; + } + + /** + * Converts string into {@code IgniteUuid}. The String must be in the format generated + * by {@link #toString() IgniteUuid.toString()} method. + * + * @param s String to convert to {@code IgniteUuid}. + * @return {@code IgniteUuid} instance representing given string. + */ + public static IgniteUuid fromString(String s) { + int firstDash = s.indexOf('-'); + + return new IgniteUuid( + UUID.fromString(s.substring(firstDash + 1)), + Long.valueOf(new StringBuilder(s.substring(0, firstDash)).reverse().toString(), 16) + ); + } + + /** + * Gets a short string version of this ID. Use it only for UI where full version is + * available to the application. + * + * @return Short string version of this ID. + */ + public String shortString() { + return new StringBuilder(Long.toHexString(locId)).reverse().toString(); + } + + /** + * Gets global ID portion of this {@code IgniteUuid}. + * + * @return Global ID portion of this {@code IgniteUuid}. + */ + public UUID globalId() { + return gid; + } + + /** + * Gets local ID portion of this {@code IgniteUuid}. + * + * @return Local ID portion of this {@code IgniteUuid}. + */ + public long localId() { + return locId; + } + + /** {@inheritDoc} */ + @Override public int compareTo(IgniteUuid o) { + if (o == this) + return 0; + + int res = Long.compare(locId, o.locId); + + if (res == 0) + res = gid.compareTo(o.globalId()); + + return res; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + if (obj == this) + return true; + + if (!(obj instanceof IgniteUuid)) + return false; + + IgniteUuid that = (IgniteUuid)obj; + + return that.locId == locId && that.gid.equals(gid); + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return 31 * gid.hashCode() + (int)(locId ^ (locId >>> 32)); + } + + /** {@inheritDoc} */ + @Override public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return shortString() + '-' + gid.toString(); + } +} diff --git a/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuidGenerator.java b/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuidGenerator.java new file mode 100644 index 0000000..0266b59 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/lang/IgniteUuidGenerator.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.lang; + +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Generator of {@link IgniteUuid}. + */ +public class IgniteUuidGenerator { + /** VM ID. */ + private final UUID globalId; + + /** */ + private final AtomicLong cntGen; + + /** + * Constructs instance of generator. + * + * @param globalId VM ID. + * @param genStart Start of generation sequence. + */ + public IgniteUuidGenerator(UUID globalId, long genStart) { + this.globalId = globalId; + + cntGen = new AtomicLong(genStart); + } + + /** + * Gets {@link UUID} associated with local VM. + * + * @return {@link UUID} associated with local VM. + */ + public UUID vmId() { + return globalId; + } + + /** + * Gets last generated local ID. + * + * @return Last generated local ID. + */ + public long lastLocalId() { + return cntGen.get(); + } + + /** + * Creates new pseudo-random ID. + * + * @return Newly created pseudo-random ID. + */ + public IgniteUuid randomUuid() { + return new IgniteUuid(globalId, cntGen.incrementAndGet()); + } + + /** + * Constructs new {@link IgniteUuid} based on global and local ID portions. + * + * @param id UUID instance. + * @return Newly created pseudo-random ID. + */ + public IgniteUuid fromUuid(UUID id) { + Objects.requireNonNull(id, "id must not be null"); + + return new IgniteUuid(id, cntGen.getAndIncrement()); + } +} diff --git a/pom.xml b/pom.xml index 4a225d9..1c0872b 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ <module>modules/cli-common</module> <module>modules/configuration</module> <module>modules/configuration-annotation-processor</module> + <module>modules/core</module> <module>modules/network</module> <module>modules/rest</module> <module>modules/runner</module>
