Skip to content

Commit dee26c1

Browse files
committed
Move typedb-common java library into typedb-dependencies
1 parent 367a198 commit dee26c1

19 files changed

+1205
-2
lines changed

.factory/automation.yml

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ build:
1818
bazel test $(bazel query 'kind(checkstyle_test, //...)') --test_output=streamed || exit 1
1919
tool/ide/rust/sync.sh
2020
cargo generate-lockfile --quiet && echo SUCCESS
21+
deploy-maven-snapshot:
22+
filter:
23+
owner: typedb
24+
branch: [ master, development ]
25+
image: typedb-ubuntu-22.04
26+
dependencies: [ build ]
27+
command: |
28+
export DEPLOY_MAVEN_USERNAME=$REPO_TYPEDB_USERNAME
29+
export DEPLOY_MAVEN_PASSWORD=$REPO_TYPEDB_PASSWORD
30+
bazel run //tool/bazelinstall:remote_cache_setup.sh
31+
bazel run --define version=$(git rev-parse HEAD) //common/java:deploy-maven -- snapshot
2132
sync-dependencies:
2233
image: typedb-ubuntu-22.04
2334
filter:

WORKSPACE

+18-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ kotlin_repositories()
3737
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
3838
kt_register_toolchains()
3939

40+
# Load //common
41+
load("//common/java:artifacts.bzl", typedb_common_java_maven_artifacts = "maven_artifacts")
42+
4043
load("//library/ortools/cc:deps.bzl", "google_or_tools_mac", "google_or_tools_linux", "google_or_tools_windows")
4144
google_or_tools_mac()
4245
google_or_tools_linux()
4346
google_or_tools_windows()
4447

4548
# Load //tool/common
46-
load("//tool/common:deps.bzl", "typedb_dependencies_ci_pip", typedb_dependencies_tool_maven_artifacts = "maven_artifacts")
49+
load("//tool/common:deps.bzl", "typedb_dependencies_ci_pip", common_tool_maven_artifacts = "maven_artifacts")
4750
typedb_dependencies_ci_pip()
4851
load("@typedb_dependencies_ci_pip//:requirements.bzl", "install_deps")
4952
install_deps()
@@ -64,6 +67,10 @@ sonarcloud_dependencies()
6467
load("//tool/swig:deps.bzl", swig_deps = "deps")
6568
swig_deps()
6669

70+
###################################
71+
# Load @typedb_bazel_distribution #
72+
###################################
73+
6774
# Load @typedb_bazel_distribution
6875
load("//distribution:deps.bzl", "typedb_bazel_distribution")
6976
typedb_bazel_distribution()
@@ -74,8 +81,17 @@ rules_pkg()
7481
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
7582
rules_pkg_dependencies()
7683

84+
# Load @typedb_bazel_distribution_cloudsmith
85+
load("@typedb_bazel_distribution//common/uploader:deps.bzl", uploader_dpes = "deps")
86+
uploader_dpes()
87+
load("@typedb_bazel_distribution_uploader//:requirements.bzl", install_uploader_deps = "install_deps")
88+
install_uploader_deps()
89+
7790
# Load Maven artifacts
78-
maven(typedb_dependencies_tool_maven_artifacts)
91+
maven(
92+
typedb_common_java_maven_artifacts +
93+
common_tool_maven_artifacts
94+
)
7995

8096
# Load Rust Crate dependencies
8197
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

common/BUILD

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
6+
load("//tool/checkstyle:rules.bzl", "checkstyle_test")
7+
8+
checkstyle_test(
9+
name = "checkstyle",
10+
include = ["BUILD"],
11+
license_type = "mpl-header",
12+
)
13+
14+

common/java/BUILD

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
6+
exports_files(["VERSION"], visibility = ["//visibility:public"])
7+
load("@typedb_bazel_distribution//maven:rules.bzl", "assemble_maven", "deploy_maven")
8+
load("//distribution/maven:version.bzl", "version")
9+
load("//library/maven:artifacts.bzl", "artifacts")
10+
load("//tool/checkstyle:rules.bzl", "checkstyle_test")
11+
load("//distribution:deployment.bzl", "deployment")
12+
13+
java_library(
14+
name = "typedb-common",
15+
srcs = glob([
16+
"collection/*.java",
17+
"concurrent/*.java",
18+
"concurrent/actor/*.java",
19+
"concurrent/actor/eventloop/*.java",
20+
"exception/*.java",
21+
"util/*.java",
22+
"yaml/*.java",
23+
]),
24+
deps = [
25+
"@maven//:org_yaml_snakeyaml",
26+
],
27+
visibility = ["//visibility:public"],
28+
tags = [
29+
"maven_coordinates=com.typedb:typedb-common:{pom_version}",
30+
],
31+
)
32+
33+
assemble_maven(
34+
name = "assemble-maven",
35+
target = ":typedb-common",
36+
workspace_refs = "@typedb_dependencies_workspace_refs//:refs.json",
37+
version_overrides = version(artifacts_org = artifacts, artifacts_repo={}),
38+
project_name = "TypeDB Common",
39+
project_description = "TypeDB Common classes and tools",
40+
project_url = "https://github.com/typedb/typeql",
41+
scm_url = "https://github.com/typedb/typeql",
42+
)
43+
44+
deploy_maven(
45+
name = "deploy-maven",
46+
target = ":assemble-maven",
47+
snapshot = deployment['maven']['snapshot']['upload'],
48+
release = deployment['maven']['release']['upload']
49+
)
50+
51+
checkstyle_test(
52+
name = "checkstyle",
53+
include = glob([
54+
"BUILD",
55+
"collection/*",
56+
"concurrent/*",
57+
"concurrent/actor/*.java",
58+
"concurrent/actor/eventloop/*.java",
59+
"exception/*",
60+
"util/*",
61+
"yaml/*.java"
62+
]),
63+
license_type = "mpl-header",
64+
)

common/java/artifacts.bzl

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
maven_artifacts = [
6+
"org.yaml:snakeyaml",
7+
]

common/java/collection/Bytes.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
package com.typedb.common.collection;
8+
9+
import java.util.Map;
10+
11+
import static com.typedb.common.collection.Collections.map;
12+
import static com.typedb.common.collection.Collections.pair;
13+
14+
public class Bytes {
15+
16+
private static final String PREFIX = "0x";
17+
// TODO: convert HEX_ARRAY to byte[] once upgraded to Java 9+
18+
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
19+
private static final Map<Character, Integer> HEX_MAP = map(pair('0', 0), pair('1', 1), pair('2', 2), pair('3', 3),
20+
pair('4', 4), pair('5', 5), pair('6', 6), pair('7', 7),
21+
pair('8', 8), pair('9', 9), pair('a', 10), pair('b', 11),
22+
pair('c', 12), pair('d', 13), pair('e', 14), pair('f', 15));
23+
24+
public static byte[] hexStringToBytes(String hexString) {
25+
assert hexString.length() % 2 == 0;
26+
assert hexString.startsWith(PREFIX);
27+
28+
hexString = hexString.replace(PREFIX, "");
29+
30+
final int len = hexString.length();
31+
final byte[] bytes = new byte[len / 2];
32+
for (int i = 0; i < bytes.length; i++) {
33+
bytes[i] = (byte) ((HEX_MAP.get(hexString.charAt(i * 2)) << 4) + HEX_MAP.get(hexString.charAt((i * 2) + 1)));
34+
}
35+
return bytes;
36+
}
37+
38+
public static String bytesToHexString(byte[] bytes) {
39+
final char[] hexChars = new char[bytes.length * 2];
40+
for (int j = 0; j < bytes.length; j++) {
41+
final int v = bytes[j] & 0xFF;
42+
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
43+
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
44+
}
45+
// TODO when hexChars is a byte[]: return new String(hexChars, StandardCharsets.UTF_8);
46+
return PREFIX + new String(hexChars);
47+
}
48+
}
+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
package com.typedb.common.collection;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.Collection;
12+
import java.util.HashMap;
13+
import java.util.HashSet;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.NavigableSet;
17+
import java.util.Objects;
18+
import java.util.Set;
19+
20+
public class Collections {
21+
22+
@SafeVarargs
23+
public static <K, V> Map<K, V> map(Pair<K, V>... pairs) {
24+
Map<K, V> map = new HashMap<>();
25+
for (Pair<K, V> tuple : pairs) {
26+
map.put(tuple.first(), tuple.second());
27+
}
28+
return java.util.Collections.unmodifiableMap(map);
29+
}
30+
31+
public static <K, V> Map<K, V> map(Map<K, V> map) {
32+
return java.util.Collections.unmodifiableMap(map);
33+
}
34+
35+
@SafeVarargs
36+
public static <T> Set<T> set(T... items) {
37+
return set(Arrays.asList(items));
38+
}
39+
40+
public static <T> Set<T> set(Collection<T> collection) {
41+
Set<T> set = new HashSet<>(collection);
42+
return java.util.Collections.unmodifiableSet(set);
43+
}
44+
45+
@SafeVarargs
46+
public static <T> Set<T> set(Collection<T> collection, T item, T... items) {
47+
Set<T> combined = new HashSet<>(collection);
48+
combined.add(item);
49+
combined.addAll(Arrays.asList(items));
50+
return java.util.Collections.unmodifiableSet(combined);
51+
}
52+
53+
@SafeVarargs
54+
public static <T> Set<T> concatToSet(Collection<? extends T> collection, Collection<? extends T>... collections) {
55+
Set<T> combined = new HashSet<>(collection);
56+
for (Collection<? extends T> c : collections) combined.addAll(c);
57+
return java.util.Collections.unmodifiableSet(combined);
58+
}
59+
60+
@SafeVarargs
61+
public static <T> List<T> list(T... items) {
62+
return java.util.Collections.unmodifiableList(Arrays.asList(items));
63+
}
64+
65+
public static <T> List<T> list(Collection<T> collection) {
66+
List<T> list = new ArrayList<>(collection);
67+
return java.util.Collections.unmodifiableList(list);
68+
}
69+
70+
@SafeVarargs
71+
public static <T> List<T> list(Collection<T> collection, T item, T... array) {
72+
List<T> combined = new ArrayList<>(collection);
73+
combined.add(item);
74+
combined.addAll(Arrays.asList(array));
75+
return java.util.Collections.unmodifiableList(combined);
76+
}
77+
78+
@SafeVarargs
79+
public static <T> List<T> concatToList(Collection<? extends T> collection, Collection<? extends T>... collections) {
80+
List<T> combined = new ArrayList<>(collection);
81+
for (Collection<? extends T> c : collections) combined.addAll(c);
82+
return java.util.Collections.unmodifiableList(combined);
83+
}
84+
85+
public static <A, B> Pair<A, B> pair(A first, B second) {
86+
return new Pair<>(first, second);
87+
}
88+
89+
public static <A, B, C> Triple<A, B, C> triple(A first, B second, C third) {
90+
return new Triple<>(first, second, third);
91+
}
92+
93+
@SafeVarargs
94+
public static <T> boolean containsAll(Collection<T> collection, T... values) {
95+
for (T value : values) {
96+
if (!collection.contains(value)) return false;
97+
}
98+
return true;
99+
}
100+
101+
public static <T> boolean arrayContains(T[] values, T value) {
102+
for (final T v : values) {
103+
if (Objects.equals(value, v)) return true;
104+
}
105+
return false;
106+
}
107+
108+
public static <T> Set<T> intersection(Set<T> set1, Set<T> set2) {
109+
Set<T> minSet;
110+
Set<T> maxSet;
111+
if (set1.size() < set2.size()) {
112+
minSet = set1;
113+
maxSet = set2;
114+
} else {
115+
minSet = set2;
116+
maxSet = set1;
117+
}
118+
Set<T> intersection = new HashSet<>();
119+
for (T elem : minSet) {
120+
if (maxSet.contains(elem)) intersection.add(elem);
121+
}
122+
return intersection;
123+
}
124+
125+
public static <T> boolean hasIntersection(Set<T> set1, Set<T> set2) {
126+
Set<T> minSet;
127+
Set<T> maxSet;
128+
if (set1.size() < set2.size()) {
129+
minSet = set1;
130+
maxSet = set2;
131+
} else {
132+
minSet = set2;
133+
maxSet = set1;
134+
}
135+
for (T elem : minSet) {
136+
if (maxSet.contains(elem)) return true;
137+
}
138+
return false;
139+
}
140+
141+
/**
142+
* Optimised set intersection detection when using sorted sets
143+
*/
144+
public static <T extends Comparable<T>> boolean hasIntersection(NavigableSet<T> set1, NavigableSet<T> set2) {
145+
NavigableSet<T> active = set1;
146+
NavigableSet<T> other = set2;
147+
if (active.isEmpty()) return false;
148+
T currentKey = active.first();
149+
while (currentKey != null) {
150+
T otherKey = other.ceiling(currentKey);
151+
if (otherKey != null && otherKey.equals(currentKey)) return true;
152+
currentKey = otherKey;
153+
NavigableSet<T> tmp = other;
154+
other = active;
155+
active = tmp;
156+
}
157+
return false;
158+
}
159+
}

0 commit comments

Comments
 (0)