-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support
GroupedIdSegmentDistributor
. (#362)
- Loading branch information
Showing
17 changed files
with
698 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.Year; | ||
import java.time.ZoneId; | ||
|
||
public enum DateGroupedSupplier implements GroupedSupplier { | ||
YEAR { | ||
@Override | ||
public GroupedKey get() { | ||
Year nowYear = Year.now(); | ||
String key = nowYear.toString(); | ||
|
||
LocalDateTime lastTs = LocalDateTime.of(LocalDate.MAX.withYear(nowYear.getValue()), LocalTime.MAX); | ||
ZoneId currentZone = ZoneId.systemDefault(); | ||
long ttlAt = lastTs.atZone(currentZone).toInstant().toEpochMilli() / 1000; | ||
return new GroupedKey(key, ttlAt); | ||
} | ||
} | ||
|
||
|
||
} |
147 changes: 147 additions & 0 deletions
147
...-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 GroupedSupplier groupedSupplier; | ||
private final IdSegmentDistributorDefinition idSegmentDistributorDefinition; | ||
private final IdSegmentDistributorFactory idSegmentDistributorFactory; | ||
private volatile GroupedBinding currentGroup; | ||
|
||
public DefaultGroupedIdSegmentDistributor(GroupedSupplier groupedSupplier, IdSegmentDistributorDefinition idSegmentDistributorDefinition, IdSegmentDistributorFactory idSegmentDistributorFactory) { | ||
this.groupedSupplier = groupedSupplier; | ||
this.idSegmentDistributorDefinition = idSegmentDistributorDefinition; | ||
this.idSegmentDistributorFactory = idSegmentDistributorFactory; | ||
this.ensureGrouped(); | ||
} | ||
|
||
private GroupedBinding ensureGrouped() { | ||
GroupedKey groupedKey = groupedSupplier.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; | ||
IdSegmentDistributorDefinition groupedDef = new IdSegmentDistributorDefinition(idSegmentDistributorDefinition.getNamespace(), | ||
groupedName, | ||
idSegmentDistributorDefinition.getOffset(), | ||
idSegmentDistributorDefinition.getStep()); | ||
this.currentGroup = new GroupedBinding(groupedKey, idSegmentDistributorFactory.create(groupedDef)); | ||
} | ||
|
||
return currentGroup; | ||
} | ||
|
||
@Override | ||
public GroupedSupplier groupedSupplier() { | ||
return groupedSupplier; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getNamespace() { | ||
return idSegmentDistributorDefinition.getNamespace(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getName() { | ||
return idSegmentDistributorDefinition.getName(); | ||
} | ||
|
||
@Override | ||
public long getStep() { | ||
return idSegmentDistributorDefinition.getStep(); | ||
} | ||
|
||
@Override | ||
public long nextMaxId(long step) { | ||
return this.ensureGrouped().nextMaxId(step); | ||
} | ||
|
||
private long getMinTtl(long ttl) { | ||
long groupedTtl = currentGroup.group().ttl(); | ||
return Math.min(groupedTtl, ttl); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public IdSegment nextIdSegment(long ttl) { | ||
long minTtl = getMinTtl(ttl); | ||
return this.ensureGrouped().nextIdSegment(minTtl); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public IdSegment nextIdSegment(int segments, long ttl) { | ||
long minTtl = getMinTtl(ttl); | ||
return this.ensureGrouped().nextIdSegment(segments, minTtl); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) { | ||
long minTtl = getMinTtl(ttl); | ||
return this.ensureGrouped().nextIdSegmentChain(previousChain, segments, minTtl); | ||
} | ||
|
||
public static class GroupedBinding implements IdSegmentDistributor { | ||
|
||
private final GroupedKey group; | ||
private final IdSegmentDistributor idSegmentDistributor; | ||
|
||
public GroupedBinding(GroupedKey group, IdSegmentDistributor idSegmentDistributor) { | ||
this.group = group; | ||
this.idSegmentDistributor = idSegmentDistributor; | ||
} | ||
|
||
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); | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 { | ||
GroupedSupplier groupedSupplier(); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 GroupedSupplier groupedSupplier; | ||
private final IdSegmentDistributorFactory actual; | ||
|
||
public GroupedIdSegmentDistributorFactory(GroupedSupplier groupedSupplier, IdSegmentDistributorFactory actual) { | ||
this.groupedSupplier = groupedSupplier; | ||
this.actual = actual; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public IdSegmentDistributor create(IdSegmentDistributorDefinition definition) { | ||
return new DefaultGroupedIdSegmentDistributor(groupedSupplier, definition, actual); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 { | ||
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); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 GroupedSupplier extends Supplier<GroupedKey> { | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (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 org.junit.jupiter.api.Test; | ||
|
||
class DateGroupedSupplierTest { | ||
|
||
@Test | ||
void year() { | ||
assertThat(DateGroupedSupplier.YEAR.get(), notNullValue()); | ||
} | ||
|
||
} |
Oops, something went wrong.