@@ -72,12 +78,18 @@ public IdSegmentChain getHead() {
* @param forwardChain forward IdSegmentChain
*/
private void forward(IdSegmentChain forwardChain) {
- if (forwardChain.compareTo(headChain) > 0) {
- if (log.isDebugEnabled()) {
- log.debug("Forward [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain);
- }
+ if (headChain.getVersion() >= forwardChain.getVersion()) {
+ return;
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Forward [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain);
+ }
+ if (maxIdDistributor.allowReset()) {
+ headChain = forwardChain;
+ } else if (forwardChain.compareTo(headChain) > 0) {
headChain = forwardChain;
}
+
}
private IdSegmentChain generateNext(IdSegmentChain previousChain, int segments) {
@@ -168,7 +180,7 @@ public void run() {
public void prefetch() {
- long wakeupTimeGap = Clock.CACHE.secondTime() - lastHungerTime;
+ long wakeupTimeGap = Clock.SYSTEM.secondTime() - lastHungerTime;
final boolean hunger = wakeupTimeGap < hungerThreshold;
final int prePrefetchDistance = this.prefetchDistance;
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentId.java
index 0944f8b0c5..67bb93e67e 100644
--- a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentId.java
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentId.java
@@ -14,6 +14,8 @@
package me.ahoo.cosid.segment;
import me.ahoo.cosid.IdGenerator;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
+import me.ahoo.cosid.stat.generator.SegmentIdStat;
/**
* Segment algorithm ID generator.
@@ -24,4 +26,22 @@
*/
public interface SegmentId extends IdGenerator {
int ONE_STEP = 1;
+
+ IdSegment current();
+
+ @Override
+ default IdGeneratorStat stat() {
+ return new SegmentIdStat(getClass().getSimpleName(),
+ current().getFetchTime(),
+ current().getMaxId(),
+ current().getOffset(),
+ current().getSequence(),
+ current().getStep(),
+ current().isExpired(),
+ current().isOverflow(),
+ current().isAvailable(),
+ current().group(),
+ idConverter().stat()
+ );
+ }
}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/StringSegmentId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/StringSegmentId.java
index 9f9ca1230e..175a7cef29 100644
--- a/cosid-core/src/main/java/me/ahoo/cosid/segment/StringSegmentId.java
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/StringSegmentId.java
@@ -13,8 +13,9 @@
package me.ahoo.cosid.segment;
-import me.ahoo.cosid.StringIdGeneratorDecorator;
import me.ahoo.cosid.IdConverter;
+import me.ahoo.cosid.StringIdGeneratorDecorator;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
/**
* String SegmentId.
@@ -22,8 +23,20 @@
* @author ahoo wang
*/
public class StringSegmentId extends StringIdGeneratorDecorator implements SegmentId {
-
+ private final SegmentId actualSegmentId;
+
public StringSegmentId(SegmentId actual, IdConverter idConverter) {
super(actual, idConverter);
+ this.actualSegmentId = actual;
+ }
+
+ @Override
+ public IdSegment current() {
+ return actualSegmentId.current();
+ }
+
+ @Override
+ public IdGeneratorStat stat() {
+ return super.stat();
}
}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java
new file mode 100644
index 0000000000..f0cf7d7625
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import me.ahoo.cosid.segment.IdSegment;
+import me.ahoo.cosid.segment.IdSegmentChain;
+import me.ahoo.cosid.segment.IdSegmentDistributor;
+import me.ahoo.cosid.segment.IdSegmentDistributorDefinition;
+import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+
+import javax.annotation.Nonnull;
+
+public class DefaultGroupedIdSegmentDistributor implements GroupedIdSegmentDistributor {
+ private final GroupBySupplier groupBySupplier;
+ private final IdSegmentDistributorDefinition idSegmentDistributorDefinition;
+ private final IdSegmentDistributorFactory idSegmentDistributorFactory;
+ private volatile GroupedBinding currentGroup;
+
+ public DefaultGroupedIdSegmentDistributor(GroupBySupplier groupBySupplier, IdSegmentDistributorDefinition idSegmentDistributorDefinition, IdSegmentDistributorFactory idSegmentDistributorFactory) {
+ this.groupBySupplier = groupBySupplier;
+ this.idSegmentDistributorDefinition = idSegmentDistributorDefinition;
+ this.idSegmentDistributorFactory = idSegmentDistributorFactory;
+ this.ensureGroupedBinding();
+ }
+
+ private GroupedBinding ensureGroupedBinding() {
+ GroupedKey groupedKey = groupBySupplier.get();
+ if (currentGroup != null && currentGroup.group().equals(groupedKey)) {
+ return currentGroup;
+ }
+ synchronized (this) {
+ if (currentGroup != null && currentGroup.group().equals(groupedKey)) {
+ return currentGroup;
+ }
+ String groupedName = idSegmentDistributorDefinition.getName() + "@" + groupedKey.getKey();
+ IdSegmentDistributorDefinition groupedDef = new IdSegmentDistributorDefinition(idSegmentDistributorDefinition.getNamespace(),
+ groupedName,
+ idSegmentDistributorDefinition.getOffset(),
+ idSegmentDistributorDefinition.getStep());
+ this.currentGroup = new GroupedBinding(groupedKey, idSegmentDistributorFactory.create(groupedDef));
+ }
+
+ return currentGroup;
+ }
+
+ public GroupBySupplier groupBySupplier() {
+ return groupBySupplier;
+ }
+
+ @Nonnull
+ @Override
+ public String getNamespace() {
+ return this.idSegmentDistributorDefinition.getNamespace();
+ }
+
+ @Nonnull
+ @Override
+ public String getName() {
+ return this.idSegmentDistributorDefinition.getName();
+ }
+
+ @Override
+ public long getStep() {
+ return this.idSegmentDistributorDefinition.getStep();
+ }
+
+ @Override
+ public GroupedKey group() {
+ return this.ensureGroupedBinding().group();
+ }
+
+ @Override
+ public long nextMaxId() {
+ return this.ensureGroupedBinding().nextMaxId();
+ }
+
+ @Override
+ public long nextMaxId(long step) {
+ return this.ensureGroupedBinding().nextMaxId(step);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegment nextIdSegment() {
+ return this.ensureGroupedBinding().nextIdSegment();
+ }
+
+ @Nonnull
+ @Override
+ public IdSegment nextIdSegment(long ttl) {
+ return this.ensureGroupedBinding().nextIdSegment(ttl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegment nextIdSegment(int segments, long ttl) {
+ return this.ensureGroupedBinding().nextIdSegment(segments, ttl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) {
+ return this.ensureGroupedBinding().nextIdSegmentChain(previousChain, segments, ttl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain) {
+ return this.ensureGroupedBinding().nextIdSegmentChain(previousChain);
+ }
+
+ public static class GroupedBinding implements GroupedIdSegmentDistributor {
+
+ private final GroupedKey group;
+ private final IdSegmentDistributor idSegmentDistributor;
+
+ public GroupedBinding(GroupedKey group, IdSegmentDistributor idSegmentDistributor) {
+ this.group = group;
+ this.idSegmentDistributor = idSegmentDistributor;
+ }
+
+ @Override
+ public GroupedKey group() {
+ return group;
+ }
+
+ @Nonnull
+ @Override
+ public String getNamespace() {
+ return idSegmentDistributor.getNamespace();
+ }
+
+ @Nonnull
+ @Override
+ public String getName() {
+ return idSegmentDistributor.getName();
+ }
+
+ @Override
+ public long getStep() {
+ return idSegmentDistributor.getStep();
+ }
+
+ @Override
+ public long nextMaxId(long step) {
+ return idSegmentDistributor.nextMaxId(step);
+ }
+
+ private long getMinTtl(long ttl) {
+ long groupedTtl = group.ttl();
+ return Math.min(groupedTtl, ttl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegment nextIdSegment(long ttl) {
+ long minTtl = getMinTtl(ttl);
+ return GroupedIdSegmentDistributor.super.nextIdSegment(minTtl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegment nextIdSegment(int segments, long ttl) {
+ long minTtl = getMinTtl(ttl);
+ return GroupedIdSegmentDistributor.super.nextIdSegment(segments, minTtl);
+ }
+
+ @Nonnull
+ @Override
+ public IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) {
+ long minTtl = getMinTtl(ttl);
+ return GroupedIdSegmentDistributor.super.nextIdSegmentChain(previousChain, segments, minTtl);
+ }
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java
new file mode 100644
index 0000000000..38e3fa7d2b
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import java.util.function.Supplier;
+
+public interface GroupBySupplier extends Supplier {
+}
+
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/Grouped.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/Grouped.java
new file mode 100644
index 0000000000..0ea853a283
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/Grouped.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+public interface Grouped {
+ default GroupedKey group() {
+ return GroupedKey.NEVER;
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedAccessor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedAccessor.java
new file mode 100644
index 0000000000..482ec62292
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedAccessor.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.ThreadSafe;
+import java.util.Objects;
+
+@ThreadSafe
+public final class GroupedAccessor {
+ private static final ThreadLocal CURRENT = new ThreadLocal<>();
+
+ public static void set(GroupedKey groupedKey) {
+ CURRENT.set(groupedKey);
+ }
+
+ public static void setIfNotNever(GroupedKey groupedKey) {
+ if (GroupedKey.NEVER.equals(groupedKey)) {
+ return;
+ }
+ set(groupedKey);
+ }
+
+ @Nullable
+ public static GroupedKey get() {
+ return CURRENT.get();
+ }
+
+ public static GroupedKey requiredGet() {
+ return Objects.requireNonNull(get(), "The current thread has not set the GroupedKey.");
+ }
+
+ public static void clear() {
+ CURRENT.remove();
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java
new file mode 100644
index 0000000000..6826dd86d1
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import me.ahoo.cosid.segment.IdSegmentDistributor;
+
+public interface GroupedIdSegmentDistributor extends IdSegmentDistributor {
+
+ @Override
+ default boolean allowReset() {
+ return true;
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java
new file mode 100644
index 0000000000..3985af61f8
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import me.ahoo.cosid.segment.IdSegmentDistributor;
+import me.ahoo.cosid.segment.IdSegmentDistributorDefinition;
+import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+
+import javax.annotation.Nonnull;
+
+public class GroupedIdSegmentDistributorFactory implements IdSegmentDistributorFactory {
+ private final GroupBySupplier groupBySupplier;
+ private final IdSegmentDistributorFactory actual;
+
+ public GroupedIdSegmentDistributorFactory(GroupBySupplier groupBySupplier, IdSegmentDistributorFactory actual) {
+ this.groupBySupplier = groupBySupplier;
+ this.actual = actual;
+ }
+
+ @Nonnull
+ @Override
+ public IdSegmentDistributor create(IdSegmentDistributorDefinition definition) {
+ return new DefaultGroupedIdSegmentDistributor(groupBySupplier, definition, actual);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedKey.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedKey.java
new file mode 100644
index 0000000000..269f7abc23
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedKey.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import me.ahoo.cosid.segment.IdSegment;
+import me.ahoo.cosid.util.Clock;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+public final class GroupedKey {
+ public static final GroupedKey NEVER = new GroupedKey("", IdSegment.TIME_TO_LIVE_FOREVER);
+ private final String key;
+ private final long ttlAt;
+
+ public GroupedKey(String key, long ttlAt) {
+ this.key = key;
+ this.ttlAt = ttlAt;
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ /**
+ * get ttlAt of group.
+ *
+ * @return ttlAt
+ * @see IdSegment#getTtl()
+ */
+ public long getTtlAt() {
+ return ttlAt;
+ }
+
+ public long ttl() {
+ return ttlAt - Clock.CACHE.secondTime();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ GroupedKey that = (GroupedKey) o;
+ return ttlAt == that.ttlAt && Objects.equals(key, that.key);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(key, ttlAt);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("key", key)
+ .add("ttlAt", ttlAt)
+ .toString();
+ }
+
+ public static GroupedKey forever(String key) {
+ return new GroupedKey(key, IdSegment.TIME_TO_LIVE_FOREVER);
+ }
+}
+
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/AbstractDateGroupBySupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/AbstractDateGroupBySupplier.java
new file mode 100644
index 0000000000..fa196f6a9a
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/AbstractDateGroupBySupplier.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import me.ahoo.cosid.segment.grouped.GroupBySupplier;
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAccessor;
+
+public abstract class AbstractDateGroupBySupplier implements GroupBySupplier {
+ protected final DateTimeFormatter formatter;
+
+ public AbstractDateGroupBySupplier(DateTimeFormatter formatter) {
+ this.formatter = formatter;
+ }
+
+ abstract D now();
+
+ abstract LocalDateTime lastTimestamp(D date);
+
+ @Override
+ public GroupedKey get() {
+ D nowDate = now();
+ String key = formatter.format(nowDate);
+
+ LocalDateTime lastTs = lastTimestamp(nowDate);
+ ZoneId currentZone = ZoneId.systemDefault();
+ long ttlAt = lastTs.atZone(currentZone).toInstant().toEpochMilli() / 1000;
+ return new GroupedKey(key, ttlAt);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplier.java
new file mode 100644
index 0000000000..6bf2110091
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplier.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Year;
+import java.time.format.DateTimeFormatter;
+
+public class YearGroupBySupplier extends AbstractDateGroupBySupplier {
+
+ public YearGroupBySupplier(DateTimeFormatter formatter) {
+ super(formatter);
+ }
+
+ public YearGroupBySupplier(String pattern) {
+ this(DateTimeFormatter.ofPattern(pattern));
+ }
+
+ @Override
+ Year now() {
+ return Year.now();
+ }
+
+ @Override
+ LocalDateTime lastTimestamp(Year date) {
+ return LocalDateTime.of(LocalDate.MAX.withYear(date.getValue()), LocalTime.MAX);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplier.java
new file mode 100644
index 0000000000..7f5307f180
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplier.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+
+public class YearMonthDayGroupBySupplier extends AbstractDateGroupBySupplier {
+
+ public YearMonthDayGroupBySupplier(DateTimeFormatter formatter) {
+ super(formatter);
+ }
+
+ public YearMonthDayGroupBySupplier(String pattern) {
+ this(DateTimeFormatter.ofPattern(pattern));
+ }
+
+ @Override
+ LocalDate now() {
+ return LocalDate.now();
+ }
+
+ @Override
+ LocalDateTime lastTimestamp(LocalDate date) {
+ return LocalDateTime.of(date, LocalTime.MAX);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplier.java
new file mode 100644
index 0000000000..261f1c07e8
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplier.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.YearMonth;
+import java.time.format.DateTimeFormatter;
+
+
+public class YearMonthGroupBySupplier extends AbstractDateGroupBySupplier {
+ public YearMonthGroupBySupplier(DateTimeFormatter formatter) {
+ super(formatter);
+ }
+
+ public YearMonthGroupBySupplier(String pattern) {
+ this(DateTimeFormatter.ofPattern(pattern));
+ }
+
+ @Override
+ YearMonth now() {
+ return YearMonth.now();
+ }
+
+ @Override
+ LocalDateTime lastTimestamp(YearMonth date) {
+ LocalDate lastDate = LocalDate.MAX.withYear(date.getYear()).withMonth(date.getMonthValue());
+ return LocalDateTime.of(lastDate, LocalTime.MAX);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java
index 43951df7b6..b028615bf7 100644
--- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java
+++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java
@@ -16,6 +16,7 @@
import me.ahoo.cosid.IdGeneratorDecorator;
import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.snowflake.exception.ClockBackwardsException;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
import lombok.extern.slf4j.Slf4j;
@@ -27,26 +28,26 @@
* @author ahoo wang
*/
@Slf4j
-public class ClockSyncSnowflakeId implements SnowflakeId, IdGeneratorDecorator {
-
+public class ClockSyncSnowflakeId implements IdGeneratorDecorator, SnowflakeId {
+
private final SnowflakeId actual;
private final ClockBackwardsSynchronizer clockBackwardsSynchronizer;
-
+
public ClockSyncSnowflakeId(SnowflakeId actual) {
this(actual, ClockBackwardsSynchronizer.DEFAULT);
}
-
+
public ClockSyncSnowflakeId(SnowflakeId actual, ClockBackwardsSynchronizer clockBackwardsSynchronizer) {
this.actual = actual;
this.clockBackwardsSynchronizer = clockBackwardsSynchronizer;
}
-
+
@Nonnull
@Override
public SnowflakeId getActual() {
return actual;
}
-
+
@Override
public long generate() {
try {
@@ -59,57 +60,60 @@ public long generate() {
return actual.generate();
}
}
-
-
+
+ @Override
+ public IdGeneratorStat stat() {
+ return IdGeneratorDecorator.super.stat();
+ }
+
@Override
public long getEpoch() {
return actual.getEpoch();
}
-
+
@Override
public int getTimestampBit() {
return actual.getTimestampBit();
}
-
+
@Override
public int getMachineBit() {
return actual.getMachineBit();
}
-
+
@Override
public int getSequenceBit() {
return actual.getSequenceBit();
}
-
+
@Override
public boolean isSafeJavascript() {
return actual.isSafeJavascript();
}
-
+
@Override
public long getMaxTimestamp() {
return actual.getMaxTimestamp();
}
-
+
@Override
public int getMaxMachine() {
return actual.getMaxMachine();
}
-
+
@Override
public long getMaxSequence() {
return actual.getMaxSequence();
}
-
+
@Override
public long getLastTimestamp() {
return actual.getLastTimestamp();
}
-
+
@Override
public int getMachineId() {
return actual.getMachineId();
}
-
-
+
}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java
index 5b65ff67e0..fa1e482c3a 100644
--- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java
+++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java
@@ -14,6 +14,8 @@
package me.ahoo.cosid.snowflake;
import me.ahoo.cosid.IdGenerator;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
+import me.ahoo.cosid.stat.generator.SnowflakeIdStat;
/**
* Snowflake algorithm ID generator.
@@ -24,15 +26,15 @@
*/
public interface SnowflakeId extends IdGenerator {
int TOTAL_BIT = 63;
-
+
long getEpoch();
-
+
int getTimestampBit();
-
+
int getMachineBit();
-
+
int getSequenceBit();
-
+
/**
* 是否是 Javascript 安全的 SnowflakeId.
* {@link SafeJavaScriptSnowflakeId#JAVA_SCRIPT_MAX_SAFE_NUMBER_BIT}.
@@ -42,19 +44,33 @@ public interface SnowflakeId extends IdGenerator {
default boolean isSafeJavascript() {
return (getTimestampBit() + getMachineBit() + getSequenceBit()) <= SafeJavaScriptSnowflakeId.JAVA_SCRIPT_MAX_SAFE_NUMBER_BIT;
}
-
+
long getMaxTimestamp();
-
+
int getMaxMachine();
-
+
long getMaxSequence();
-
+
long getLastTimestamp();
-
+
int getMachineId();
-
-
+
static long defaultSequenceResetThreshold(int sequenceBit) {
return ~(-1L << (sequenceBit - 1));
}
+
+ @Override
+ default IdGeneratorStat stat() {
+ return new SnowflakeIdStat(
+ getClass().getSimpleName(),
+ getEpoch(),
+ getTimestampBit(),
+ getMachineBit(),
+ getSequenceBit(),
+ isSafeJavascript(),
+ getMachineId(),
+ getLastTimestamp(),
+ idConverter().stat()
+ );
+ }
}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java
index c45128d0cf..78c276d56f 100644
--- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java
+++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java
@@ -15,6 +15,7 @@
import me.ahoo.cosid.IdConverter;
import me.ahoo.cosid.StringIdGeneratorDecorator;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
/**
* String SnowflakeId.
@@ -73,4 +74,10 @@ public long getLastTimestamp() {
public int getMachineId() {
return snowflakeId.getMachineId();
}
+
+
+ @Override
+ public IdGeneratorStat stat() {
+ return super.stat();
+ }
}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/SimpleStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/SimpleStat.java
new file mode 100644
index 0000000000..8982c12aec
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/SimpleStat.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public class SimpleStat implements Stat {
+ private final String kind;
+ private final Stat actual;
+}
\ No newline at end of file
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/Stat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/Stat.java
new file mode 100644
index 0000000000..14c72b3b4d
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/Stat.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat;
+
+import javax.annotation.Nullable;
+
+public interface Stat {
+
+ String getKind();
+
+ @Nullable
+ default Stat getActual() {
+ return null;
+ }
+
+ static Stat simple(String kind, @Nullable Stat actual) {
+ return new SimpleStat(kind, actual);
+ }
+
+ static Stat simple(String kind) {
+ return simple(kind, null);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/Statistical.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/Statistical.java
new file mode 100644
index 0000000000..abc45f0ac0
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/Statistical.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat;
+
+@FunctionalInterface
+public interface Statistical {
+ Stat stat();
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/DatePrefixConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/DatePrefixConverterStat.java
new file mode 100644
index 0000000000..6a42b66191
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/DatePrefixConverterStat.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class DatePrefixConverterStat implements Stat {
+ private final String kind;
+ private final String pattern;
+ private final Stat actual;
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/GroupedPrefixConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/GroupedPrefixConverterStat.java
new file mode 100644
index 0000000000..a48d307ee1
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/GroupedPrefixConverterStat.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class GroupedPrefixConverterStat implements Stat {
+ private final String kind;
+ private final String delimiter;
+ private final Stat actual;
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/PrefixConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/PrefixConverterStat.java
new file mode 100644
index 0000000000..28089cf4f5
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/PrefixConverterStat.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class PrefixConverterStat implements Stat {
+ private final String kind;
+ private final String prefix;
+ private final Stat actual;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/RadixConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/RadixConverterStat.java
new file mode 100644
index 0000000000..dc3f8e9e71
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/RadixConverterStat.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class RadixConverterStat implements Stat {
+ private final String kind;
+ private final int radix;
+ private final int charSize;
+ private final boolean padStart;
+ private final long maxId;
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/SuffixConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/SuffixConverterStat.java
new file mode 100644
index 0000000000..59e7f08461
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/SuffixConverterStat.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class SuffixConverterStat implements Stat {
+ private final String kind;
+ private final String suffix;
+ private final Stat actual;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/ToStringConverterStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/ToStringConverterStat.java
new file mode 100644
index 0000000000..ed9bdf22ff
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/converter/ToStringConverterStat.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.converter;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class ToStringConverterStat implements Stat {
+ private final String kind;
+ private final boolean padStart;
+ private final int charSize;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/CosIdGeneratorStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/CosIdGeneratorStat.java
new file mode 100644
index 0000000000..14a18519a9
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/CosIdGeneratorStat.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.generator;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class CosIdGeneratorStat implements IdGeneratorStat {
+ private final String kind;
+ private final int machineId;
+ private final long lastTimestamp;
+ private final Stat converter;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/IdGeneratorStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/IdGeneratorStat.java
new file mode 100644
index 0000000000..021380c8de
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/IdGeneratorStat.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.generator;
+
+import me.ahoo.cosid.stat.Stat;
+
+import javax.annotation.Nullable;
+
+public interface IdGeneratorStat extends Stat {
+ @Nullable
+ @Override
+ default IdGeneratorStat getActual() {
+ return null;
+ }
+
+ @Nullable
+ Stat getConverter();
+
+ static IdGeneratorStat simple(String kind, @Nullable IdGeneratorStat actual, Stat converter) {
+ return new SimpleIdGeneratorStat(kind, actual, converter);
+ }
+
+ static IdGeneratorStat simple(String kind, Stat converter) {
+ return simple(kind, null, converter);
+ }
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SegmentIdStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SegmentIdStat.java
new file mode 100644
index 0000000000..8d78fb92ac
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SegmentIdStat.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.generator;
+
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class SegmentIdStat implements IdGeneratorStat {
+ private final String kind;
+ private final long fetchTime;
+ private final long maxId;
+ private final long offset;
+ private final long sequence;
+ private final long step;
+ private final boolean isExpired;
+ private final boolean isOverflow;
+ private final boolean isAvailable;
+ private final GroupedKey groupedKey;
+ private final Stat converter;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SimpleIdGeneratorStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SimpleIdGeneratorStat.java
new file mode 100644
index 0000000000..517b765617
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SimpleIdGeneratorStat.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.generator;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class SimpleIdGeneratorStat implements IdGeneratorStat {
+ private final String kind;
+ private final IdGeneratorStat actual;
+ private final Stat converter;
+
+}
diff --git a/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SnowflakeIdStat.java b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SnowflakeIdStat.java
new file mode 100644
index 0000000000..6a92bfdd3f
--- /dev/null
+++ b/cosid-core/src/main/java/me/ahoo/cosid/stat/generator/SnowflakeIdStat.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat.generator;
+
+import me.ahoo.cosid.stat.Stat;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@AllArgsConstructor
+@Data
+public final class SnowflakeIdStat implements IdGeneratorStat {
+ private final String kind;
+ private final long epoch;
+ private final int timestampBit;
+ private final int machineBit;
+ private final int sequenceBit;
+ private final boolean isSafeJavascript;
+ private final int machineId;
+ private final long lastTimestamp;
+ private final Stat converter;
+
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/accessor/NotFoundCosIdAccessorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/accessor/NotFoundCosIdAccessorTest.java
new file mode 100644
index 0000000000..729dc22084
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/accessor/NotFoundCosIdAccessorTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.accessor;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import org.junit.jupiter.api.Test;
+
+class NotFoundCosIdAccessorTest {
+ @Test
+ public void getIdDefinition() {
+ assertThat(CosIdAccessor.NOT_FOUND.getIdDefinition(), nullValue());
+ }
+
+ @Test
+ public void getIdGenerator() {
+ assertThat(CosIdAccessor.NOT_FOUND.getIdGenerator(), nullValue());
+ }
+
+ @Test
+ public void getIdField() {
+ assertThat(CosIdAccessor.NOT_FOUND.getIdField(), nullValue());
+ }
+
+ @Test
+ public void getId() {
+ assertThat(CosIdAccessor.NOT_FOUND.getId(new Object()), nullValue());
+ }
+
+ @Test
+ public void setId() {
+ CosIdAccessor.NOT_FOUND.setId(new Object(), new Object());
+ }
+
+ @Test
+ public void ensureId() {
+ assertThat(CosIdAccessor.NOT_FOUND.ensureId(new Object()), equalTo(false));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java b/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java
new file mode 100644
index 0000000000..1ff3bfee10
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.accessor.parser;
+
+import com.google.common.collect.Lists;
+import me.ahoo.cosid.accessor.IdDefinition;
+import me.ahoo.cosid.annotation.AnnotationDefinitionParser;
+import me.ahoo.cosid.annotation.entity.IdNotFoundEntity;
+import me.ahoo.cosid.annotation.entity.LongIdEntity;
+import me.ahoo.cosid.provider.IdGeneratorProvider;
+
+import lombok.SneakyThrows;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+class CompositeFieldDefinitionParserTest {
+
+ @SneakyThrows
+ @Test
+ void parse() {
+
+ CompositeFieldDefinitionParser compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(Lists.newArrayList(AnnotationDefinitionParser.INSTANCE));
+ Field idField = LongIdEntity.class.getDeclaredField("id");
+ IdDefinition idDefinition = compositeFieldDefinitionParser.parse(Lists.newArrayList(LongIdEntity.class), idField);
+
+ Assertions.assertNotEquals(IdDefinition.NOT_FOUND, idDefinition);
+ Assertions.assertEquals(IdGeneratorProvider.SHARE, idDefinition.getGeneratorName());
+ }
+
+ @SneakyThrows
+ @Test
+ void parseIfNotFound() {
+ CompositeFieldDefinitionParser compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(Lists.newArrayList(AnnotationDefinitionParser.INSTANCE));
+ Field nameField = IdNotFoundEntity.class.getDeclaredField("name");
+ IdDefinition idDefinition = compositeFieldDefinitionParser.parse(Lists.newArrayList(IdNotFoundEntity.class), nameField);
+ Assertions.assertEquals(IdDefinition.NOT_FOUND, idDefinition);
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/accessor/registry/DefaultAccessorRegistryTest.java b/cosid-core/src/test/java/me/ahoo/cosid/accessor/registry/DefaultAccessorRegistryTest.java
index 5cd8fae2d0..6d82b798fc 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/accessor/registry/DefaultAccessorRegistryTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/accessor/registry/DefaultAccessorRegistryTest.java
@@ -13,8 +13,12 @@
package me.ahoo.cosid.accessor.registry;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.accessor.parser.DefaultAccessorParser;
import me.ahoo.cosid.annotation.AnnotationDefinitionParser;
+import me.ahoo.cosid.annotation.entity.IntIdEntity;
import me.ahoo.cosid.annotation.entity.LongIdEntity;
import me.ahoo.cosid.annotation.entity.MissingIdGenEntity;
import me.ahoo.cosid.annotation.entity.StringIdEntity;
@@ -30,9 +34,9 @@
* @author ahoo wang
*/
class DefaultAccessorRegistryTest {
-
+
CosIdAccessorRegistry accessorRegistry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));
-
+
@Test
void ensureIdExistsByAnnotation() {
DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
@@ -41,7 +45,7 @@ void ensureIdExistsByAnnotation() {
accessorRegistry.ensureId(entity);
Assertions.assertEquals(888, entity.getId());
}
-
+
@Test
void ensureIdNotFindByAnnotation() {
MissingIdGenEntity entity = new MissingIdGenEntity();
@@ -49,7 +53,7 @@ void ensureIdNotFindByAnnotation() {
accessorRegistry.ensureId(entity);
});
}
-
+
@Test
void ensureStringId() {
DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
@@ -57,5 +61,12 @@ void ensureStringId() {
accessorRegistry.ensureId(entity);
Assertions.assertFalse(Strings.isNullOrEmpty(entity.getId()));
}
-
+
+ @Test
+ void ensureIntId() {
+ DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
+ IntIdEntity entity = new IntIdEntity();
+ accessorRegistry.ensureId(entity);
+ assertThat(entity.getId(), not(0));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/accessor/scanner/DefaultCosIdScannerTest.java b/cosid-core/src/test/java/me/ahoo/cosid/accessor/scanner/DefaultCosIdScannerTest.java
index 23dcadd929..0b5ea4e36e 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/accessor/scanner/DefaultCosIdScannerTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/accessor/scanner/DefaultCosIdScannerTest.java
@@ -14,7 +14,6 @@
package me.ahoo.cosid.accessor.scanner;
import me.ahoo.cosid.accessor.CosIdAccessor;
-import me.ahoo.cosid.accessor.parser.CosIdAccessorParser;
import me.ahoo.cosid.accessor.parser.DefaultAccessorParser;
import me.ahoo.cosid.accessor.parser.NamedDefinitionParser;
import me.ahoo.cosid.accessor.registry.CosIdAccessorRegistry;
@@ -23,6 +22,7 @@
import me.ahoo.cosid.accessor.scanner.entity.OrderItemEntity;
import me.ahoo.cosid.annotation.AnnotationDefinitionParser;
import me.ahoo.cosid.annotation.entity.ChildEntity;
+import me.ahoo.cosid.annotation.entity.IntIdEntity;
import me.ahoo.cosid.annotation.entity.LongIdEntity;
import me.ahoo.cosid.annotation.entity.MissingIdGenEntity;
import me.ahoo.cosid.annotation.entity.PrimitiveLongIdEntity;
@@ -35,56 +35,62 @@
* @author ahoo wang
*/
class DefaultCosIdScannerTest {
-
+
@Test
void scanAnnotationDefinitionParser() {
CosIdAccessorRegistry registry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));
DefaultCosIdScanner scanner =
new DefaultCosIdScanner(new String[] {"me.ahoo.cosid.accessor.annotation.entity"}, AnnotationDefinitionParser.INSTANCE, registry);
scanner.scan();
-
+
CosIdAccessor cosIdAccessor = registry.get(LongIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(LongIdEntity.class, cosIdAccessor.getIdDeclaringClass());
-
+
cosIdAccessor = registry.get(MissingIdGenEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(MissingIdGenEntity.class, cosIdAccessor.getIdDeclaringClass());
-
+
cosIdAccessor = registry.get(PrimitiveLongIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(PrimitiveLongIdEntity.class, cosIdAccessor.getIdDeclaringClass());
-
+
+ cosIdAccessor = registry.get(IntIdEntity.class);
+ Assertions.assertNotNull(cosIdAccessor);
+ Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
+ Assertions.assertEquals(IntIdEntity.class, cosIdAccessor.getIdDeclaringClass());
+
cosIdAccessor = registry.get(StringIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(StringIdEntity.class, cosIdAccessor.getIdDeclaringClass());
-
+
cosIdAccessor = registry.get(ChildEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(LongIdEntity.class, cosIdAccessor.getIdDeclaringClass());
}
-
+
@Test
void scanNamedDefinitionParser() {
CosIdAccessorRegistry registry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));
DefaultCosIdScanner scanner =
new DefaultCosIdScanner(new String[] {"me.ahoo.cosid.accessor.scanner.entity"}, new NamedDefinitionParser("id"), registry);
scanner.scan();
-
+
CosIdAccessor cosIdAccessor = registry.get(OrderEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(OrderEntity.class, cosIdAccessor.getIdDeclaringClass());
-
+
cosIdAccessor = registry.get(OrderItemEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(OrderItemEntity.class, cosIdAccessor.getIdDeclaringClass());
+
}
-
+
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IdNotFoundEntity.java b/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IdNotFoundEntity.java
new file mode 100644
index 0000000000..1529b0d327
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IdNotFoundEntity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.annotation.entity;
+
+/**
+ * @author ahoo wang
+ */
+public class IdNotFoundEntity {
+ private String name;
+
+}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IntIdEntity.java b/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IntIdEntity.java
new file mode 100644
index 0000000000..ccb276182a
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/annotation/entity/IntIdEntity.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.annotation.entity;
+
+import me.ahoo.cosid.annotation.CosId;
+
+/**
+ * @author ahoo wang
+ */
+public class IntIdEntity {
+ @CosId
+ private int id;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/DatePrefixIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/DatePrefixIdConverterTest.java
new file mode 100644
index 0000000000..ed9c02b048
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/DatePrefixIdConverterTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.converter;
+
+import me.ahoo.cosid.stat.converter.DatePrefixConverterStat;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+import static me.ahoo.cosid.converter.GroupedPrefixIdConverter.DEFAULT_DELIMITER;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.instanceOf;
+
+class DatePrefixIdConverterTest {
+ private final DatePrefixIdConverter idConverter = new DatePrefixIdConverter("yyMMdd", DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);
+
+ @Test
+ void asString() {
+ String idString = idConverter.asString(1);
+ String expected = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + DEFAULT_DELIMITER + "1";
+ assertThat(idString, equalTo(expected));
+ }
+
+ @Test
+ void asLong() {
+ assertThat(idConverter.asLong("240618-1"), equalTo(1L));
+ }
+
+ @Test
+ void getActual() {
+ assertThat(idConverter.getActual(), equalTo(ToStringIdConverter.INSTANCE));
+ }
+
+ @Test
+ void stat() {
+ assertThat(idConverter.stat(), instanceOf(DatePrefixConverterStat.class));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/GroupedPrefixIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/GroupedPrefixIdConverterTest.java
new file mode 100644
index 0000000000..4c4ba34248
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/GroupedPrefixIdConverterTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.converter;
+
+import static me.ahoo.cosid.converter.GroupedPrefixIdConverter.DEFAULT_DELIMITER;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.segment.grouped.GroupedAccessor;
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+import me.ahoo.cosid.stat.converter.GroupedPrefixConverterStat;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class GroupedPrefixIdConverterTest {
+ GroupedPrefixIdConverter converter = new GroupedPrefixIdConverter(DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);
+
+ @Test
+ void asString() {
+ GroupedAccessor.set(GroupedKey.forever("2023"));
+ assertThat(converter.getDelimiter(), equalTo("-"));
+ assertThat(converter.asString(1), equalTo("2023-1"));
+ assertThat(converter.getActual(), equalTo(ToStringIdConverter.INSTANCE));
+ GroupedAccessor.clear();
+ }
+
+ @Test
+ void asStringIfEmptyDelimiter() {
+ GroupedPrefixIdConverter converter = new GroupedPrefixIdConverter("", ToStringIdConverter.INSTANCE);
+ GroupedAccessor.set(GroupedKey.forever("2023"));
+ assertThat(converter.asString(1), equalTo("20231"));
+ GroupedAccessor.clear();
+ }
+
+ @Test
+ void asLong() {
+ Assertions.assertThrows(UnsupportedOperationException.class, () -> converter.asLong("2023-1"));
+ }
+
+ @Test
+ void stat() {
+ assertThat(converter.stat(), instanceOf(GroupedPrefixConverterStat.class));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/PrefixIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/PrefixIdConverterTest.java
index e19100f9e7..af98e0d7d4 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/PrefixIdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/PrefixIdConverterTest.java
@@ -14,12 +14,12 @@
package me.ahoo.cosid.converter;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.stat.converter.PrefixConverterStat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
import java.util.concurrent.ThreadLocalRandom;
@@ -35,6 +35,11 @@ void getSuffix() {
assertThat(idConverter.getPrefix(), equalTo(PREFIX));
}
+ @Test
+ void getActual() {
+ assertThat(idConverter.getActual(), equalTo(ToStringIdConverter.INSTANCE));
+ }
+
@Test
void asString() {
long randomId = ThreadLocalRandom.current().nextLong();
@@ -65,4 +70,8 @@ void asLongWhenNumberFormat() {
});
}
+ @Test
+ void stat() {
+ assertThat(idConverter.stat(), instanceOf(PrefixConverterStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix36IdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix36IdConverterTest.java
index 56d4cae0e4..33199d0f89 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix36IdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix36IdConverterTest.java
@@ -13,8 +13,12 @@
package me.ahoo.cosid.converter;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.IdGenerator;
import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.stat.converter.RadixConverterStat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -25,7 +29,7 @@
* @author ahoo wang
*/
class Radix36IdConverterTest {
-
+
@ParameterizedTest
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE})
void asString(long argId) {
@@ -40,7 +44,7 @@ void asStringWhenIdNegative() {
Radix36IdConverter.INSTANCE.asString(-1L);
});
}
-
+
@ParameterizedTest
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE})
void asLong(long argId) {
@@ -48,12 +52,12 @@ void asLong(long argId) {
long actual = Radix36IdConverter.INSTANCE.asLong(idStr);
Assertions.assertEquals(argId, actual);
}
-
+
@Test
void asLongWhenNumberFormat() {
int charSize = 2;
Radix36IdConverter idConvert = Radix36IdConverter.of(false, charSize);
-
+
Assertions.assertThrows(NumberFormatException.class, () -> {
idConvert.asLong("-1");
});
@@ -64,7 +68,7 @@ void asLongWhenNumberFormat() {
idConvert.asLong("1_");
});
}
-
+
@ParameterizedTest
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE})
void asStringPad(long argId) {
@@ -72,7 +76,7 @@ void asStringPad(long argId) {
Assertions.assertNotNull(idStr);
Assertions.assertEquals(Radix36IdConverter.MAX_CHAR_SIZE, idStr.length());
}
-
+
@ParameterizedTest
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE})
void asLongPad(long argId) {
@@ -80,7 +84,7 @@ void asLongPad(long argId) {
long actual = Radix36IdConverter.PAD_START.asLong(idStr);
Assertions.assertEquals(argId, actual);
}
-
+
@Test
void asStringSnowflakeId() {
IdGenerator idGenerator = new MillisecondSnowflakeId(1);
@@ -89,7 +93,7 @@ void asStringSnowflakeId() {
Assertions.assertNotNull(idStr);
Assertions.assertEquals(Radix36IdConverter.MAX_CHAR_SIZE, idStr.length());
}
-
+
@Test
void asStringCharSize12() {
int charSize = 12;
@@ -101,11 +105,11 @@ void asStringCharSize12() {
Assertions.assertEquals(charSize, actualIdStr.length());
long actualId = idConvert.asLong(actualIdStr);
Assertions.assertEquals(id, actualId);
-
+
actualIdStr = idConvert.asString(1L);
Assertions.assertEquals(1, actualIdStr.length());
}
-
+
@Test
void asStringPadCharSize12() {
int charSize = 12;
@@ -113,5 +117,9 @@ void asStringPadCharSize12() {
String actualIdStr = idConvert.asString(1L);
Assertions.assertEquals(charSize, actualIdStr.length());
}
-
+
+ @Test
+ void stat() {
+ assertThat(Radix36IdConverter.INSTANCE.stat(), instanceOf(RadixConverterStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix62IdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix62IdConverterTest.java
index 9c63347c66..8bfbc09969 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix62IdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/Radix62IdConverterTest.java
@@ -13,8 +13,12 @@
package me.ahoo.cosid.converter;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.IdGenerator;
import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.stat.converter.RadixConverterStat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -115,4 +119,8 @@ void asStringPadCharSize10() {
Assertions.assertEquals(charSize, actualIdStr.length());
}
+ @Test
+ void stat() {
+ assertThat(Radix62IdConverter.INSTANCE.stat(), instanceOf(RadixConverterStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java
index 0d2f13d9bc..e9bb912ca9 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java
@@ -13,9 +13,13 @@
package me.ahoo.cosid.converter;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.snowflake.SecondSnowflakeId;
import me.ahoo.cosid.snowflake.SecondSnowflakeIdStateParser;
import me.ahoo.cosid.snowflake.SnowflakeId;
+import me.ahoo.cosid.stat.SimpleStat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -60,8 +64,10 @@ void getParser() {
SecondSnowflakeIdStateParser snowflakeIdStateParser = SecondSnowflakeIdStateParser.of(idGen);
SnowflakeFriendlyIdConverter snowflakeFriendlyIdConverter = new SnowflakeFriendlyIdConverter(snowflakeIdStateParser);
Assertions.assertNotNull(snowflakeFriendlyIdConverter.getParser());
-
}
-
+ @Test
+ void stat() {
+ assertThat(SnowflakeFriendlyIdConverter.INSTANCE.stat(), instanceOf(SimpleStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/SuffixIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/SuffixIdConverterTest.java
index f7ed2eb7e7..356c327e6e 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/SuffixIdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/SuffixIdConverterTest.java
@@ -14,7 +14,9 @@
package me.ahoo.cosid.converter;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.stat.converter.SuffixConverterStat;
import org.junit.jupiter.api.Test;
@@ -34,6 +36,11 @@ void getSuffix() {
assertThat(idConverter.getSuffix(), equalTo(SUFFIX));
}
+ @Test
+ void getActual() {
+ assertThat(idConverter.getActual(), equalTo(ToStringIdConverter.INSTANCE));
+ }
+
@Test
void asString() {
long randomId = ThreadLocalRandom.current().nextLong();
@@ -47,4 +54,9 @@ void asLong() {
long actual = idConverter.asLong(randomId + SUFFIX);
assertThat(actual, equalTo(randomId));
}
+
+ @Test
+ void stat() {
+ assertThat(idConverter.stat(), instanceOf(SuffixConverterStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/ToStringIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/ToStringIdConverterTest.java
index 7be26eaa4c..102912b270 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/converter/ToStringIdConverterTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/ToStringIdConverterTest.java
@@ -13,6 +13,11 @@
package me.ahoo.cosid.converter;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.stat.converter.ToStringConverterStat;
+
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -59,4 +64,9 @@ void asStringWithPadStart() {
Assertions.assertEquals("00001", idConvert.asString(1));
Assertions.assertEquals(1, idConvert.asLong("00001"));
}
+
+ @Test
+ void stat() {
+ assertThat(ToStringIdConverter.INSTANCE.stat(), instanceOf(ToStringConverterStat.class));
+ }
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/cosid/ClockSyncCosIdGeneratorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/cosid/ClockSyncCosIdGeneratorTest.java
index b7753f3cd7..02dccc3060 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/cosid/ClockSyncCosIdGeneratorTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/cosid/ClockSyncCosIdGeneratorTest.java
@@ -22,6 +22,16 @@ class ClockSyncCosIdGeneratorTest {
private final Radix62CosIdGenerator radix62CosIdGenerator = new Radix62CosIdGenerator(1);
private final ClockSyncCosIdGenerator clockSyncCosIdGenerator = new ClockSyncCosIdGenerator(radix62CosIdGenerator);
+ @Test
+ void getActual() {
+ assertThat(clockSyncCosIdGenerator.getActual(), sameInstance(radix62CosIdGenerator));
+ }
+
+ @Test
+ void getMachineId() {
+ assertThat(clockSyncCosIdGenerator.getMachineId(), equalTo(1));
+ }
+
@Test
void getLastTimestamp() {
assertThat(clockSyncCosIdGenerator.getLastTimestamp(), equalTo(radix62CosIdGenerator.getLastTimestamp()));
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/jvm/AtomicLongGeneratorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/jvm/AtomicLongGeneratorTest.java
index dc96f5fa15..5ed4e826e4 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/jvm/AtomicLongGeneratorTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/jvm/AtomicLongGeneratorTest.java
@@ -20,11 +20,12 @@
* @author ahoo wang
*/
class AtomicLongGeneratorTest {
-
+
@Test
void generate() {
long idFirst = AtomicLongGenerator.INSTANCE.generate();
long idSecond = AtomicLongGenerator.INSTANCE.generate();
Assertions.assertTrue(idSecond > idFirst);
}
+
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/machine/LocalHostAddressSupplierTest.java b/cosid-core/src/test/java/me/ahoo/cosid/machine/LocalHostAddressSupplierTest.java
new file mode 100644
index 0000000000..7d5ee1cdf7
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/machine/LocalHostAddressSupplierTest.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.machine;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import org.junit.jupiter.api.Test;
+
+class LocalHostAddressSupplierTest {
+
+ @Test
+ void getHostName() {
+ assertThat(LocalHostAddressSupplier.INSTANCE.getHostAddress(), notNullValue());
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/DefaultSegmentIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/DefaultSegmentIdTest.java
index 1f217bb925..debf2cfa93 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/segment/DefaultSegmentIdTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/DefaultSegmentIdTest.java
@@ -13,6 +13,9 @@
package me.ahoo.cosid.segment;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.test.ConcurrentGenerateSpec;
import me.ahoo.cosid.test.ConcurrentGenerateStingSpec;
@@ -30,6 +33,12 @@ void generate() {
Assertions.assertTrue(defaultSegmentId.generate() > 0);
}
+ @Test
+ void current() {
+ DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
+ assertThat(defaultSegmentId.current(), equalTo(DefaultIdSegment.OVERFLOW));
+ }
+
@Test
void generateWhenConcurrent() {
DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java
index 36f079ccef..7a31da1750 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java
@@ -14,10 +14,13 @@
package me.ahoo.cosid.segment;
import static me.ahoo.cosid.segment.IdSegment.TIME_TO_LIVE_FOREVER;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
import me.ahoo.cosid.segment.concurrent.PrefetchWorkerExecutorService;
import me.ahoo.cosid.test.ConcurrentGenerateSpec;
import me.ahoo.cosid.test.ConcurrentGenerateStingSpec;
+import me.ahoo.cosid.test.ModSpec;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -34,9 +37,9 @@ class SegmentChainIdTest {
@Test
void sort() {
IdSegmentDistributor idSegmentDistributor = new IdSegmentDistributor.Atomic();
- IdSegmentChain idSegmentChain1 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot());
- IdSegmentChain idSegmentChain2 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot());
- IdSegmentChain idSegmentChain3 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot());
+ IdSegmentChain idSegmentChain1 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false));
+ IdSegmentChain idSegmentChain2 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false));
+ IdSegmentChain idSegmentChain3 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false));
List chainList = Arrays.asList(idSegmentChain2, idSegmentChain1, idSegmentChain3);
chainList.sort(null);
Assertions.assertEquals(idSegmentChain1, chainList.get(0));
@@ -44,10 +47,16 @@ void sort() {
Assertions.assertEquals(idSegmentChain3, chainList.get(2));
}
+ @Test
+ void current() {
+ SegmentChainId segmentChainId = new SegmentChainId(TIME_TO_LIVE_FOREVER, 10, new IdSegmentDistributor.Atomic(2), PrefetchWorkerExecutorService.DEFAULT);
+ assertThat(segmentChainId.current().isAvailable(), equalTo(false));
+ }
+
@Test
void nextIdSegmentsChain() {
IdSegmentDistributor idSegmentDistributor = new IdSegmentDistributor.Atomic();
- IdSegmentChain rootChain = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(), 3, TIME_TO_LIVE_FOREVER);
+ IdSegmentChain rootChain = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(true), 3, TIME_TO_LIVE_FOREVER);
Assertions.assertEquals(0, rootChain.getVersion());
Assertions.assertEquals(0, rootChain.getIdSegment().getOffset());
Assertions.assertEquals(30, rootChain.getStep());
@@ -69,6 +78,12 @@ public void concurrent_generate() {
new ConcurrentGenerateSpec(segmentChainId).verify();
}
+ @Test
+ public void sequenceModUniformity() {
+ SegmentChainId segmentChainId = new SegmentChainId(new IdSegmentDistributor.Mock());
+ new ModSpec(99999, 4, 100, segmentChainId::generate, ModSpec.DEFAULT_WAIT).verify();
+ }
+
@Test
public void generateWhenConcurrentString() {
IdSegmentDistributor testMaxIdDistributor = new IdSegmentDistributor.Mock();
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/StringSegmentIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/StringSegmentIdTest.java
index 3dc5f454e4..0c0d384bd4 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/segment/StringSegmentIdTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/StringSegmentIdTest.java
@@ -17,5 +17,6 @@ void ctor() {
DefaultSegmentId delegate = new DefaultSegmentId(new IdSegmentDistributor.Mock());
StringSegmentId stringSegmentId = new StringSegmentId(delegate, Radix62IdConverter.PAD_START);
Assertions.assertNotNull(stringSegmentId);
+ Assertions.assertNotNull(stringSegmentId.current());
}
}
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java
new file mode 100644
index 0000000000..163d10df96
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.segment.IdSegmentDistributor;
+import me.ahoo.cosid.segment.IdSegmentDistributorDefinition;
+import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+
+import org.junit.jupiter.api.Test;
+
+class DefaultGroupedIdSegmentDistributorTest {
+
+ @Test
+ void ensureGrouped() {
+ MockGroupBySupplier groupedSupplier = new MockGroupBySupplier(GroupedKey.forever("group-1"));
+ IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition("ns", "n", 0, 1);
+ IdSegmentDistributorFactory actual = definition1 -> new IdSegmentDistributor.Mock();
+ DefaultGroupedIdSegmentDistributor distributor = new DefaultGroupedIdSegmentDistributor(groupedSupplier, definition, actual);
+ long maxId1 = distributor.nextMaxId(1);
+ assertThat(maxId1, equalTo(1L));
+ long maxId2 = distributor.nextMaxId(1);
+ assertThat(maxId2, equalTo(2L));
+ assertThat(distributor.groupBySupplier().get().getKey(), equalTo("group-1"));
+
+ groupedSupplier.setGroup(GroupedKey.forever("group-2"));
+ maxId1 = distributor.nextMaxId(1);
+ assertThat(maxId1, equalTo(1L));
+ maxId2 = distributor.nextMaxId(1);
+ assertThat(maxId2, equalTo(2L));
+ assertThat(distributor.groupBySupplier().get().getKey(), equalTo("group-2"));
+ }
+
+ public static class MockGroupBySupplier implements GroupBySupplier {
+ private GroupedKey group;
+
+ public MockGroupBySupplier(GroupedKey group) {
+ this.group = group;
+ }
+
+ public GroupedKey getGroup() {
+ return group;
+ }
+
+ public MockGroupBySupplier setGroup(GroupedKey group) {
+ this.group = group;
+ return this;
+ }
+
+ @Override
+ public GroupedKey get() {
+ return group;
+ }
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplierTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplierTest.java
new file mode 100644
index 0000000000..0a7d72d886
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearGroupBySupplierTest.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+import org.junit.jupiter.api.Test;
+
+import java.time.Year;
+import java.time.format.DateTimeFormatter;
+
+class YearGroupBySupplierTest {
+
+ @Test
+ void year() {
+ String pattern = "yyyy";
+ YearGroupBySupplier supplier = new YearGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertThat(groupedKey.getKey(), equalTo(Year.now().format(DateTimeFormatter.ofPattern(pattern))));
+ }
+
+ @Test
+ void yearTwoPattern() {
+ String pattern = "yy";
+ YearGroupBySupplier supplier = new YearGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertThat(groupedKey.getKey(), equalTo(Year.now().format(DateTimeFormatter.ofPattern(pattern))));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplierTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplierTest.java
new file mode 100644
index 0000000000..1b00277d4c
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplierTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class YearMonthDayGroupBySupplierTest {
+
+ @Test
+ void get() {
+ String pattern = "yyyyMMdd";
+ YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)));
+ }
+
+ @Test
+ void getYyMm() {
+ String pattern = "yyMMdd";
+ YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplierTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplierTest.java
new file mode 100644
index 0000000000..e1fe51262a
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplierTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.segment.grouped.date;
+
+import me.ahoo.cosid.segment.grouped.GroupedKey;
+import org.junit.jupiter.api.Test;
+
+import java.time.YearMonth;
+import java.time.format.DateTimeFormatter;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class YearMonthGroupBySupplierTest {
+
+ @Test
+ void get() {
+ String pattern = "yyyy-MM";
+ YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern)));
+ }
+
+ @Test
+ void getYyMm() {
+ String pattern = "yyMM";
+ YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern);
+ GroupedKey groupedKey = supplier.get();
+ assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern)));
+ }
+}
\ No newline at end of file
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/snowflake/MillisecondSnowflakeIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/snowflake/MillisecondSnowflakeIdTest.java
index dd24010c84..c6f15234b6 100644
--- a/cosid-core/src/test/java/me/ahoo/cosid/snowflake/MillisecondSnowflakeIdTest.java
+++ b/cosid-core/src/test/java/me/ahoo/cosid/snowflake/MillisecondSnowflakeIdTest.java
@@ -7,14 +7,13 @@
import me.ahoo.cosid.converter.Radix62IdConverter;
import me.ahoo.cosid.test.ConcurrentGenerateSpec;
import me.ahoo.cosid.test.ConcurrentGenerateStingSpec;
+import me.ahoo.cosid.test.ModSpec;
-import com.google.common.collect.Range;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
-import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.LockSupport;
/**
@@ -63,50 +62,20 @@ public void sequenceIncrement() {
long id2 = snowflakeId.generate();
SnowflakeIdState snowflakeIdState2 = snowflakeId.friendlyId(id2);
- assertThat(snowflakeIdState2.getTimestamp(), greaterThan(snowflakeIdState.getTimestamp()));
- assertThat(snowflakeIdState2.getSequence(), greaterThan(snowflakeIdState.getSequence()));
+ assertThat(
+ snowflakeIdState2.getTimestamp() + ">" + snowflakeIdState.getTimestamp(),
+ snowflakeIdState2.getTimestamp(), greaterThan(snowflakeIdState.getTimestamp())
+ );
+ assertThat(
+ snowflakeIdState2.getSequence() + ">" + snowflakeIdState.getSequence(),
+ snowflakeIdState2.getSequence(), greaterThan(snowflakeIdState.getSequence())
+ );
}
@Test
public void sequenceModUniformity() {
- int divisor = 4;
- int total = 99999;
- int avg = total / divisor;
- double diff = (avg * .001);
-
- int mod0Counter = 0;
- int mod1Counter = 0;
- int mod2Counter = 0;
- int mod3Counter = 0;
- for (int i = 0; i < total; i++) {
- long id = snowflakeId.generate();
- int mod = (int) (id % divisor);
- switch (mod) {
- case 0: {
- mod0Counter++;
- break;
- }
- case 1: {
- mod1Counter++;
- break;
- }
- case 2: {
- mod2Counter++;
- break;
- }
- case 3: {
- mod3Counter++;
- break;
- }
- }
- int wait = ThreadLocalRandom.current().nextInt(0, 1000);
- LockSupport.parkNanos(wait);
- }
- assertThat((double) mod0Counter, closeTo(avg, diff));
- assertThat((double) mod1Counter, closeTo(avg, diff));
- assertThat((double) mod2Counter, closeTo(avg, diff));
- assertThat((double) mod3Counter, closeTo(avg, diff));
+ new ModSpec(99999, 4, 100, snowflakeId::generate, ModSpec.DEFAULT_WAIT).verify();
}
@Test
diff --git a/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java b/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java
new file mode 100644
index 0000000000..3e09dfb2fa
--- /dev/null
+++ b/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.stat;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.IdGenerator;
+import me.ahoo.cosid.converter.Radix62IdConverter;
+import me.ahoo.cosid.cosid.Radix36CosIdGenerator;
+import me.ahoo.cosid.jvm.UuidGenerator;
+import me.ahoo.cosid.segment.DefaultSegmentId;
+import me.ahoo.cosid.segment.IdSegmentDistributor;
+import me.ahoo.cosid.segment.StringSegmentId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.StringSnowflakeId;
+import me.ahoo.cosid.stat.generator.CosIdGeneratorStat;
+import me.ahoo.cosid.stat.generator.IdGeneratorStat;
+import me.ahoo.cosid.stat.generator.SegmentIdStat;
+
+import me.ahoo.cosid.stat.generator.SimpleIdGeneratorStat;
+import me.ahoo.cosid.stat.generator.SnowflakeIdStat;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class StatisticalTest {
+
+ @Test
+ void statUuidGenerator() {
+ IdGeneratorStat stat = UuidGenerator.INSTANCE.stat();
+ Assertions.assertNotNull(stat);
+ }
+
+ @Test
+ void statSnowflakeId() {
+ IdGenerator snowflakeId = new MillisecondSnowflakeId(0);
+ IdGeneratorStat stat = snowflakeId.stat();
+ Assertions.assertNotNull(stat);
+ assertThat(stat, Matchers.instanceOf(SnowflakeIdStat.class));
+ SnowflakeIdStat snowflakeIdStat = (SnowflakeIdStat) stat;
+ assertThat(snowflakeIdStat.getMachineId(), equalTo(0));
+ }
+
+ @Test
+ void statStringSnowflakeId() {
+ IdGenerator snowflakeId = new StringSnowflakeId(new MillisecondSnowflakeId(0), Radix62IdConverter.INSTANCE);
+ IdGeneratorStat stat = snowflakeId.stat();
+ Assertions.assertNotNull(stat);
+ assertThat(stat, Matchers.instanceOf(SimpleIdGeneratorStat.class));
+ }
+
+ @Test
+ void statSegmentId() {
+ IdGenerator segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
+ IdGeneratorStat stat = segmentId.stat();
+ Assertions.assertNotNull(stat);
+ assertThat(stat, Matchers.instanceOf(SegmentIdStat.class));
+ }
+
+ @Test
+ void statStringSegmentId() {
+ IdGenerator segmentId = new StringSegmentId(new DefaultSegmentId(new IdSegmentDistributor.Mock()), Radix62IdConverter.INSTANCE);
+ IdGeneratorStat stat = segmentId.stat();
+ Assertions.assertNotNull(stat);
+ assertThat(stat, Matchers.instanceOf(SimpleIdGeneratorStat.class));
+ }
+
+ @Test
+ void statCosIdGenerator() {
+ IdGenerator cosIdGenerator = new Radix36CosIdGenerator(0);
+ IdGeneratorStat stat = cosIdGenerator.stat();
+ Assertions.assertNotNull(stat);
+ assertThat(stat, Matchers.instanceOf(CosIdGeneratorStat.class));
+ CosIdGeneratorStat cosIdGeneratorStat = (CosIdGeneratorStat) stat;
+ assertThat(cosIdGeneratorStat.getMachineId(), equalTo(0));
+ }
+}
\ No newline at end of file
diff --git a/cosid-dependencies/build.gradle.kts b/cosid-dependencies/build.gradle.kts
index cc61f438c6..4c40e188b4 100644
--- a/cosid-dependencies/build.gradle.kts
+++ b/cosid-dependencies/build.gradle.kts
@@ -1,4 +1,3 @@
-
/*
* Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,17 +12,18 @@
*/
dependencies {
- api(platform("org.springframework.boot:spring-boot-dependencies:2.7.12"))
- api(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.7"))
- api(platform("com.squareup.okhttp3:okhttp-bom:4.11.0"))
- api(platform("org.testcontainers:testcontainers-bom:1.18.3"))
+ api(platform(libs.springBootDependencies))
+ api(platform(libs.springCloudDependencies))
+ api(platform(libs.okhttpBom))
+ api(platform(libs.testcontainersBom))
constraints {
- api("org.projectlombok:lombok:1.18.28")
- api("org.mybatis:mybatis:3.5.13")
- api("com.google.guava:guava:30.0-jre")
- api("org.junit-pioneer:junit-pioneer:1.9.1")
- api("org.hamcrest:hamcrest:2.2")
- api("org.openjdk.jmh:jmh-core:1.36")
- api("org.openjdk.jmh:jmh-generator-annprocess:1.36")
+ api(libs.guava)
+ api(libs.mybatis)
+ api(libs.mybatisSpringBoot)
+ api(libs.springDocStarterWebfluxUi)
+ api(libs.junitPioneer)
+ api(libs.hamcrest)
+ api(libs.jmhCore)
+ api(libs.jmhGeneratorAnnprocess)
}
}
diff --git a/cosid-flowable/build.gradle.kts b/cosid-flowable/build.gradle.kts
new file mode 100644
index 0000000000..305065e4eb
--- /dev/null
+++ b/cosid-flowable/build.gradle.kts
@@ -0,0 +1,6 @@
+dependencies {
+ implementation(libs.flowableEngineCommon)
+ api(project(":cosid-core"))
+ testImplementation(project(":cosid-test"))
+
+}
diff --git a/cosid-flowable/src/main/java/me/ahoo/cosid/flowable/FlowableIdGenerator.java b/cosid-flowable/src/main/java/me/ahoo/cosid/flowable/FlowableIdGenerator.java
new file mode 100644
index 0000000000..a846215225
--- /dev/null
+++ b/cosid-flowable/src/main/java/me/ahoo/cosid/flowable/FlowableIdGenerator.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.flowable;
+
+import me.ahoo.cosid.provider.IdGeneratorProvider;
+import me.ahoo.cosid.provider.LazyIdGenerator;
+
+/**
+ * Flowable IdGenerator Based on CosId.
+ */
+public class FlowableIdGenerator implements org.flowable.common.engine.impl.cfg.IdGenerator {
+ /**
+ * The key of the system property that can be used to set the id generator name.
+ */
+ public static final String ID_KEY = "cosid.flowable";
+ private static final String ID_NAME;
+ private static final LazyIdGenerator ID_GENERATOR;
+
+ static {
+ ID_NAME = System.getProperty(ID_KEY, IdGeneratorProvider.SHARE);
+ ID_GENERATOR = new LazyIdGenerator(ID_NAME);
+ }
+
+ public String getNextId() {
+ return ID_GENERATOR.generateAsString();
+ }
+}
diff --git a/cosid-flowable/src/test/java/me/ahoo/cosid/flowable/FlowableIdGeneratorTest.java b/cosid-flowable/src/test/java/me/ahoo/cosid/flowable/FlowableIdGeneratorTest.java
new file mode 100644
index 0000000000..4a6f430bc8
--- /dev/null
+++ b/cosid-flowable/src/test/java/me/ahoo/cosid/flowable/FlowableIdGeneratorTest.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.flowable;
+
+import static me.ahoo.cosid.flowable.FlowableIdGenerator.ID_KEY;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+import me.ahoo.cosid.provider.DefaultIdGeneratorProvider;
+import me.ahoo.cosid.test.MockIdGenerator;
+
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.SetSystemProperty;
+
+class FlowableIdGeneratorTest {
+
+ @SetSystemProperty(key = ID_KEY, value = "flowable")
+ @Test
+ void getNextId() {
+ DefaultIdGeneratorProvider.INSTANCE.set("flowable", MockIdGenerator.usePrefix("flowable_"));
+ String id = new FlowableIdGenerator().getNextId();
+ assertThat(id, startsWith("flowable_"));
+ }
+}
\ No newline at end of file
diff --git a/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/GroupedJdbcIdSegmentDistributorTest.java b/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/GroupedJdbcIdSegmentDistributorTest.java
new file mode 100644
index 0000000000..6a69285786
--- /dev/null
+++ b/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/GroupedJdbcIdSegmentDistributorTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.jdbc;
+
+import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+import me.ahoo.cosid.test.segment.distributor.GroupedIdSegmentDistributorSpec;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import javax.sql.DataSource;
+
+/**
+ * @author ahoo wang
+ */
+class GroupedJdbcIdSegmentDistributorTest extends GroupedIdSegmentDistributorSpec {
+ DataSource dataSource;
+ JdbcIdSegmentDistributorFactory distributorFactory;
+ JdbcIdSegmentInitializer mySqlIdSegmentInitializer;
+
+ @BeforeEach
+ void setup() {
+ dataSource = DataSourceFactory.INSTANCE.createDataSource();
+ mySqlIdSegmentInitializer = new JdbcIdSegmentInitializer(dataSource);
+ distributorFactory =
+ new JdbcIdSegmentDistributorFactory(dataSource, true, mySqlIdSegmentInitializer, JdbcIdSegmentDistributor.INCREMENT_MAX_ID_SQL, JdbcIdSegmentDistributor.FETCH_MAX_ID_SQL);
+ }
+
+ @Override
+ protected IdSegmentDistributorFactory getFactory() {
+ return distributorFactory;
+ }
+}
diff --git a/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributorTest.java b/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributorTest.java
index 74f931bace..92bf6a4375 100644
--- a/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributorTest.java
+++ b/cosid-jdbc/src/test/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributorTest.java
@@ -55,7 +55,7 @@ protected void setMaxIdBack(T distributor, long
@Override
public void nextMaxIdWhenBack() {
- //TODO
+
}
@Test
diff --git a/cosid-mod-test/build.gradle.kts b/cosid-mod-test/build.gradle.kts
new file mode 100644
index 0000000000..f7bfe5a095
--- /dev/null
+++ b/cosid-mod-test/build.gradle.kts
@@ -0,0 +1,18 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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.
+ */
+dependencies {
+ implementation(project(":cosid-core"))
+ testImplementation(project(":cosid-test"))
+ testImplementation("com.netease.nim:camellia-id-gen-core:1.2.20")
+ testImplementation("org.apache.shardingsphere:shardingsphere-sharding-core:5.4.1")
+}
diff --git a/cosid-mod-test/src/jmh/java/me/ahoo/cosid/test/mod/SnowflakeIdBenchmark.java b/cosid-mod-test/src/jmh/java/me/ahoo/cosid/test/mod/SnowflakeIdBenchmark.java
new file mode 100644
index 0000000000..489d4f438a
--- /dev/null
+++ b/cosid-mod-test/src/jmh/java/me/ahoo/cosid/test/mod/SnowflakeIdBenchmark.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.test.mod;
+
+import me.ahoo.cosid.snowflake.ClockSyncSnowflakeId;
+import me.ahoo.cosid.snowflake.DefaultSnowflakeFriendlyId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.SnowflakeFriendlyId;
+
+import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeConfig;
+import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeIdGen;
+import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+
+import java.util.Properties;
+
+/**
+ * SnowflakeId Benchmark.
+ *
+ * @author ahoo wang
+ */
+@State(Scope.Benchmark)
+public class SnowflakeIdBenchmark {
+ public static final int TEST_MACHINE_ID = 1;
+ SnowflakeFriendlyId cosidSnowflakeId;
+ CamelliaSnowflakeIdGen camelliaSnowflakeId;
+ SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeId;
+ SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeIdVibration127;
+
+ /**
+ * Initialize IdGenerator.
+ */
+ @Setup
+ public void setup() {
+ MillisecondSnowflakeId idGen = new MillisecondSnowflakeId(TEST_MACHINE_ID);
+ cosidSnowflakeId = new DefaultSnowflakeFriendlyId(new ClockSyncSnowflakeId(idGen));
+ CamelliaSnowflakeConfig camelliaSnowflakeCfg = new CamelliaSnowflakeConfig();
+ camelliaSnowflakeCfg.setWorkerIdGen(maxWorkerId -> TEST_MACHINE_ID);
+ camelliaSnowflakeId = new CamelliaSnowflakeIdGen(camelliaSnowflakeCfg);
+
+ Properties defaultProperties = new Properties();
+ defaultProperties.setProperty("max-tolerate-time-difference-milliseconds", String.valueOf(200));
+ shardingsphereSnowflakeId = new SnowflakeKeyGenerateAlgorithm();
+ shardingsphereSnowflakeId.init(defaultProperties);
+
+ Properties vibration127Properties = new Properties(defaultProperties);
+ shardingsphereSnowflakeIdVibration127 = new SnowflakeKeyGenerateAlgorithm();
+ vibration127Properties.setProperty("max-vibration-offset", String.valueOf(127));
+ shardingsphereSnowflakeIdVibration127.init(vibration127Properties);
+ }
+
+ @Benchmark
+ public long cosid() {
+ return cosidSnowflakeId.generate();
+ }
+
+ @Benchmark
+ public long netease() {
+ return camelliaSnowflakeId.genId();
+ }
+
+ @Benchmark
+ public long shardingsphere() {
+ return shardingsphereSnowflakeId.generateKey();
+ }
+
+ @Benchmark
+ public long shardingsphereVibration127() {
+ return shardingsphereSnowflakeIdVibration127.generateKey();
+ }
+}
diff --git a/cosid-mod-test/src/test/java/me/ahoo/cosid/test/mod/ModTest.java b/cosid-mod-test/src/test/java/me/ahoo/cosid/test/mod/ModTest.java
new file mode 100644
index 0000000000..8a0ed03bff
--- /dev/null
+++ b/cosid-mod-test/src/test/java/me/ahoo/cosid/test/mod/ModTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.test.mod;
+
+import me.ahoo.cosid.snowflake.ClockSyncSnowflakeId;
+import me.ahoo.cosid.snowflake.DefaultSnowflakeFriendlyId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.SnowflakeFriendlyId;
+import me.ahoo.cosid.test.ModSpec;
+
+import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeConfig;
+import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeIdGen;
+import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.Properties;
+import java.util.concurrent.locks.LockSupport;
+
+public class ModTest {
+ public static final int TEST_MACHINE_ID = 1;
+ private static final int ITERATIONS = 1000;
+ private static final double ALLOWABLE_POP_STD = 10;
+ SnowflakeFriendlyId cosidSnowflakeId;
+ CamelliaSnowflakeIdGen camelliaSnowflakeId;
+ SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeId = new SnowflakeKeyGenerateAlgorithm();
+
+ @BeforeEach
+ void setup() {
+ MillisecondSnowflakeId idGen = new MillisecondSnowflakeId(TEST_MACHINE_ID);
+ cosidSnowflakeId = new DefaultSnowflakeFriendlyId(new ClockSyncSnowflakeId(idGen));
+ CamelliaSnowflakeConfig camelliaSnowflakeCfg = new CamelliaSnowflakeConfig();
+ camelliaSnowflakeCfg.setWorkerIdGen(maxWorkerId -> TEST_MACHINE_ID);
+ camelliaSnowflakeId = new CamelliaSnowflakeIdGen(camelliaSnowflakeCfg);
+ }
+
+ @Test
+ public void cosidMod4() {
+ ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, cosidSnowflakeId::generate, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void shardingsphereMod4Default() {
+ ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, shardingsphereSnowflakeId::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void shardingsphereMod4Vibration3() {
+ SnowflakeKeyGenerateAlgorithm idGen = new SnowflakeKeyGenerateAlgorithm();
+ Properties properties = new Properties();
+ properties.setProperty("max-vibration-offset", String.valueOf(3));
+ idGen.init(properties);
+ ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, idGen::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void neteaseMod4() {
+ ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, camelliaSnowflakeId::genId, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void cosidMod128() {
+ ModSpec spec = new ModSpec(ITERATIONS, 128, ALLOWABLE_POP_STD, cosidSnowflakeId::generate, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void neteaseMod128() {
+ ModSpec spec = new ModSpec(ITERATIONS, 128, ALLOWABLE_POP_STD, camelliaSnowflakeId::genId, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+
+ @Test
+ public void shardingsphereMod128Vibration127() {
+ SnowflakeKeyGenerateAlgorithm idGen = new SnowflakeKeyGenerateAlgorithm();
+ Properties properties = new Properties();
+ properties.setProperty("max-vibration-offset", String.valueOf(127));
+ idGen.init(properties);
+ ModSpec spec = new ModSpec(ITERATIONS, 128, ALLOWABLE_POP_STD, idGen::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
+ spec.run();
+ }
+}
diff --git a/cosid-mongo/build.gradle.kts b/cosid-mongo/build.gradle.kts
index f4caf435fa..b5fe999e73 100644
--- a/cosid-mongo/build.gradle.kts
+++ b/cosid-mongo/build.gradle.kts
@@ -18,6 +18,7 @@ dependencies {
compileOnly("org.mongodb:mongodb-driver-reactivestreams")
compileOnly("io.projectreactor:reactor-core")
testImplementation(project(":cosid-test"))
+ testImplementation("io.projectreactor:reactor-test")
testImplementation("org.mongodb:mongodb-driver-sync")
testImplementation("io.projectreactor:reactor-core")
testImplementation("org.mongodb:mongodb-driver-reactivestreams")
diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java
index 26ea67128a..03fe608200 100644
--- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java
+++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java
@@ -13,6 +13,8 @@
package me.ahoo.cosid.mongo;
+import me.ahoo.cosid.mongo.reactive.BlockingAdapter;
+
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
@@ -20,5 +22,6 @@ public interface Documents {
String ID_FIELD = "_id";
FindOneAndUpdateOptions UPDATE_AFTER_OPTIONS = new FindOneAndUpdateOptions()
- .returnDocument(ReturnDocument.AFTER);
+ .returnDocument(ReturnDocument.AFTER)
+ .maxTime(BlockingAdapter.DEFAULT_TIME_OUT.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS);
}
diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/BlockingAdapter.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/BlockingAdapter.java
index 6c273a6643..d31dff77df 100644
--- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/BlockingAdapter.java
+++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/BlockingAdapter.java
@@ -22,7 +22,7 @@
import java.util.concurrent.TimeoutException;
public final class BlockingAdapter {
- private static final Duration DEFAULT_TIME_OUT = Duration.ofSeconds(10);
+ public static final Duration DEFAULT_TIME_OUT = Duration.ofSeconds(10);
private BlockingAdapter() {
}
diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java
new file mode 100644
index 0000000000..2df95e9095
--- /dev/null
+++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)].
+ * Licensed 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 me.ahoo.cosid.mongo;
+
+import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentDistributorFactory;
+import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentInitializer;
+import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+import me.ahoo.cosid.test.container.MongoLauncher;
+import me.ahoo.cosid.test.segment.distributor.GroupedIdSegmentDistributorSpec;
+
+import com.mongodb.reactivestreams.client.MongoClients;
+import com.mongodb.reactivestreams.client.MongoDatabase;
+import org.junit.jupiter.api.BeforeEach;
+
+class GroupedMongoReactiveIdSegmentDistributorTest extends GroupedIdSegmentDistributorSpec {
+ MongoDatabase mongoDatabase;
+ IdSegmentDistributorFactory distributorFactory;
+ MongoReactiveIdSegmentInitializer idSegmentInitializer;
+
+ @BeforeEach
+ void setup() {
+ mongoDatabase = MongoClients.create(MongoLauncher.getConnectionString()).getDatabase("cosid_db");
+ idSegmentInitializer = new MongoReactiveIdSegmentInitializer(mongoDatabase);
+ idSegmentInitializer.ensureCosIdCollection();
+ distributorFactory =
+ new MongoReactiveIdSegmentDistributorFactory(mongoDatabase, true);
+ }
+
+ @Override
+ protected IdSegmentDistributorFactory getFactory() {
+ return distributorFactory;
+ }
+}
\ No newline at end of file
diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java
index b3bd53c9e9..e28a6ac977 100644
--- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java
+++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java
@@ -15,6 +15,7 @@
import me.ahoo.cosid.segment.IdSegmentDistributor;
import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+import me.ahoo.cosid.test.container.MongoLauncher;
import me.ahoo.cosid.test.segment.distributor.IdSegmentDistributorSpec;
import com.mongodb.client.MongoClients;
diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentInitializerTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentInitializerTest.java
index 2c584dc5d7..bfdd6df322 100644
--- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentInitializerTest.java
+++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentInitializerTest.java
@@ -16,6 +16,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import me.ahoo.cosid.test.MockIdGenerator;
+import me.ahoo.cosid.test.container.MongoLauncher;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoMachineIdDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoMachineIdDistributorTest.java
index 79b8b18a4a..ebc4709f0d 100644
--- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoMachineIdDistributorTest.java
+++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoMachineIdDistributorTest.java
@@ -16,14 +16,13 @@
import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.machine.MachineIdDistributor;
import me.ahoo.cosid.machine.MachineStateStorage;
+import me.ahoo.cosid.test.container.MongoLauncher;
import me.ahoo.cosid.test.machine.distributor.MachineIdDistributorSpec;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.junit.jupiter.api.BeforeEach;
-import java.time.Duration;
-
class MongoMachineIdDistributorTest extends MachineIdDistributorSpec {
MongoDatabase mongoDatabase;
MachineIdDistributor machineIdDistributor;
@@ -44,12 +43,5 @@ void setup() {
protected MachineIdDistributor getDistributor() {
return machineIdDistributor;
}
-
- @Override
- protected Duration getSafeGuardDuration() {
- if (System.getenv().containsKey("CI")) {
- return Duration.ofSeconds(10);
- }
- return super.getSafeGuardDuration();
- }
+
}
\ No newline at end of file
diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java
index 730f71c578..55275b0611 100644
--- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java
+++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java
@@ -13,15 +13,26 @@
package me.ahoo.cosid.mongo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentDistributorFactory;
import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentInitializer;
import me.ahoo.cosid.segment.IdSegmentDistributor;
+import me.ahoo.cosid.segment.IdSegmentDistributorDefinition;
import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
+import me.ahoo.cosid.segment.SegmentChainId;
+import me.ahoo.cosid.test.MockIdGenerator;
+import me.ahoo.cosid.test.container.MongoLauncher;
import me.ahoo.cosid.test.segment.distributor.IdSegmentDistributorSpec;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoDatabase;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
+import reactor.test.StepVerifier;
class MongoReactiveIdSegmentDistributorTest extends IdSegmentDistributorSpec {
MongoDatabase mongoDatabase;
@@ -32,6 +43,7 @@ class MongoReactiveIdSegmentDistributorTest extends IdSegmentDistributorSpec {
void setup() {
mongoDatabase = MongoClients.create(MongoLauncher.getConnectionString()).getDatabase("cosid_db");
idSegmentInitializer = new MongoReactiveIdSegmentInitializer(mongoDatabase);
+
idSegmentInitializer.ensureCosIdCollection();
distributorFactory =
new MongoReactiveIdSegmentDistributorFactory(mongoDatabase, true);
@@ -51,4 +63,36 @@ protected void setMaxIdBack(T distributor, long
public void nextMaxIdWhenBack() {
}
+
+ @Test
+ public void nextMaxIdInParallel() {
+ Mono