Skip to content

Commit 9f91dbe

Browse files
committed
feat(core): add support for orderly snowflake friendly ID conversion
- Implement SnowflakeFriendlyIdConverterStat to provide detailed statistics - Enhance SnowflakeIdStateParser with new methods for padStart and character size - Update SnowflakeFriendlyIdConverter to support padStart configuration - Add new configuration example for orderly snowflake friendly ID - Modify existing tests to use new SnowflakeFriendlyIdConverterStat
1 parent 6ab7912 commit 9f91dbe

File tree

6 files changed

+127
-2
lines changed

6 files changed

+127
-2
lines changed

cosid-core/src/main/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import me.ahoo.cosid.IdConverter;
1717
import me.ahoo.cosid.snowflake.MillisecondSnowflakeIdStateParser;
1818
import me.ahoo.cosid.snowflake.SnowflakeIdStateParser;
19+
import me.ahoo.cosid.stat.Stat;
20+
import me.ahoo.cosid.stat.converter.SnowflakeFriendlyIdConverterStat;
1921

2022
import javax.annotation.Nonnull;
2123

@@ -49,4 +51,12 @@ public long asLong(@Nonnull String idString) {
4951
return snowflakeIdStateParser.parse(idString).getId();
5052
}
5153

54+
@Override
55+
public Stat stat() {
56+
return new SnowflakeFriendlyIdConverterStat(
57+
getClass().getSimpleName(),
58+
snowflakeIdStateParser.isPadStart(),
59+
snowflakeIdStateParser.getMachineCharSize(),
60+
snowflakeIdStateParser.getSequenceCharSize());
61+
}
5262
}

cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeIdStateParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public ZoneId getZoneId() {
8080
return zoneId;
8181
}
8282

83+
public boolean isPadStart() {
84+
return padStart;
85+
}
86+
87+
public int getMachineCharSize() {
88+
return machineCharSize;
89+
}
90+
91+
public int getSequenceCharSize() {
92+
return sequenceCharSize;
93+
}
94+
8395
protected abstract DateTimeFormatter getDateTimeFormatter();
8496

8597
protected abstract LocalDateTime getTimestamp(long diffTime);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package me.ahoo.cosid.stat.converter;
15+
16+
import me.ahoo.cosid.stat.Stat;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Data;
20+
21+
@AllArgsConstructor
22+
@Data
23+
public class SnowflakeFriendlyIdConverterStat implements Stat {
24+
private final String kind;
25+
private final boolean padStart;
26+
private final int machineCharSize;
27+
private final int sequenceCharSize;
28+
}

cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import me.ahoo.cosid.snowflake.SecondSnowflakeId;
2020
import me.ahoo.cosid.snowflake.SecondSnowflakeIdStateParser;
2121
import me.ahoo.cosid.snowflake.SnowflakeId;
22-
import me.ahoo.cosid.stat.SimpleStat;
2322

23+
import me.ahoo.cosid.stat.converter.SnowflakeFriendlyIdConverterStat;
2424
import org.junit.jupiter.api.Assertions;
2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.params.ParameterizedTest;
@@ -68,6 +68,6 @@ void getParser() {
6868

6969
@Test
7070
void stat() {
71-
assertThat(SnowflakeFriendlyIdConverter.INSTANCE.stat(), instanceOf(SimpleStat.class));
71+
assertThat(SnowflakeFriendlyIdConverter.INSTANCE.stat(), instanceOf(SnowflakeFriendlyIdConverterStat.class));
7272
}
7373
}

documentation/docs/guide/specific-id.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,76 @@ cosid:
6161
```
6262
:::
6363

64+
## snowflake_orderly_friendly
65+
66+
使用 _SnowflakeId_ 算法,要求输出的有序的ID字符串:
67+
68+
- 格式:`yyyyMMddHHmmssSSS-[0]<machineId>-[0]<sequence>`
69+
- 例如:`20250215122059820-0000-0001`
70+
71+
72+
::: code-group
73+
```yaml {9-14} [配置]
74+
cosid:
75+
machine:
76+
enabled: true
77+
distributor:
78+
type: redis
79+
snowflake:
80+
enabled: true
81+
provider:
82+
snowflake_orderly_friendly:
83+
converter:
84+
type: snowflake_friendly
85+
friendly:
86+
pad-start: true
87+
```
88+
```json [配置信息]
89+
{
90+
"snowflake_orderly_friendly": {
91+
"kind": "StringSnowflakeId",
92+
"actual": {
93+
"kind": "ClockSyncSnowflakeId",
94+
"actual": {
95+
"kind": "MillisecondSnowflakeId",
96+
"epoch": 1577203200000,
97+
"timestampBit": 41,
98+
"machineBit": 10,
99+
"sequenceBit": 12,
100+
"machineId": 0,
101+
"lastTimestamp": -1,
102+
"converter": {
103+
"kind": "Radix62IdConverter",
104+
"radix": 62,
105+
"charSize": 11,
106+
"padStart": true,
107+
"maxId": 9223372036854776000,
108+
"actual": null
109+
},
110+
"safeJavascript": false,
111+
"actual": null
112+
},
113+
"converter": {
114+
"kind": "Radix62IdConverter",
115+
"radix": 62,
116+
"charSize": 11,
117+
"padStart": true,
118+
"maxId": 9223372036854776000,
119+
"actual": null
120+
}
121+
},
122+
"converter": {
123+
"kind": "SnowflakeFriendlyIdConverter",
124+
"padStart": true,
125+
"machineCharSize": 4,
126+
"sequenceCharSize": 4,
127+
"actual": null
128+
}
129+
}
130+
}
131+
```
132+
:::
133+
64134
## snowflake_short_id
65135

66136
使用 _SnowflakeId_ 算法,要求输出的ID字符串:

examples/cosid-example-redis/src/main/resources/application.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ cosid:
3232
snowflake_friendly:
3333
converter:
3434
type: snowflake_friendly
35+
snowflake_orderly_friendly:
36+
converter:
37+
type: snowflake_friendly
38+
friendly:
39+
pad-start: true
3540
snowflake_short_id:
3641
converter:
3742
type: radix

0 commit comments

Comments
 (0)