Skip to content

Commit

Permalink
add CosIdAnnotationSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Aug 3, 2021
1 parent 7759d2b commit 39dd15f
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 48 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public interface OrderRepository {
> Kotlin DSL
``` kotlin
val cosidVersion = "1.3.8";
val cosidVersion = "1.3.9";
implementation("me.ahoo.cosid:cosid-spring-boot-starter:${cosidVersion}")
```

Expand All @@ -396,7 +396,7 @@ public interface OrderRepository {
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.3.8</cosid.version>
<cosid.version>1.3.9</cosid.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -486,7 +486,7 @@ cosid:
``` shell
gradle cosid-core:jmh
# or
java -jar cosid-core/build/libs/cosid-core-1.3.8-jmh.jar -bm thrpt -wi 1 -rf json -f 1
java -jar cosid-core/build/libs/cosid-core-1.3.9-jmh.jar -bm thrpt -wi 1 -rf json -f 1
```

```
Expand All @@ -507,7 +507,7 @@ SnowflakeIdBenchmark.secondSnowflakeId_generate thrpt 4206843.
``` shell
gradle cosid-redis:jmh
# or
java -jar cosid-redis/build/libs/cosid-redis-1.3.8-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
java -jar cosid-redis/build/libs/cosid-redis-1.3.9-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
```

```
Expand All @@ -525,7 +525,7 @@ RedisChainIdBenchmark.step_1000 thrpt 5 127439148.104 ± 1833743
![RedisChainIdBenchmark-Sample](./docs/jmh/RedisChainIdBenchmark-Sample.png)

```shell
java -jar cosid-redis/build/libs/cosid-redis-1.3.8-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-redis/build/libs/cosid-redis-1.3.9-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```

```
Expand All @@ -550,7 +550,7 @@ RedisChainIdBenchmark.step_1000:step_1000·p1.00 sample 37.440
``` shell
gradle cosid-jdbc:jmh
# or
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.8-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.9-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
```

```
Expand All @@ -566,7 +566,7 @@ MySqlChainIdBenchmark.step_1000 thrpt 5 123131804.260 ± 1488004.
![MySqlChainIdBenchmark-Sample](./docs/jmh/MySqlChainIdBenchmark-Sample.png)

```shell
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.8-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.9-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```
```
Benchmark Mode Cnt Score Error Units
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author ahoo wang
*/
public class DefaultIdGeneratorProvider implements IdGeneratorProvider {

public static final IdGeneratorProvider INSTANCE = new DefaultIdGeneratorProvider();
private IdGenerator shareIdGenerator;

private final ConcurrentHashMap<String, IdGenerator> nameMapIdGen;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (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.support;

import me.ahoo.cosid.annotation.CosId;
import me.ahoo.cosid.provider.IdGeneratorProvider;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* @author ahoo wang
*/
public class CosIdAnnotationSupport {

private final IdGeneratorProvider idGeneratorProvider;
private final ConcurrentHashMap<Class<?>, List<CosIdField>> classMapField;

public CosIdAnnotationSupport(IdGeneratorProvider idGeneratorProvider) {
this.classMapField = new ConcurrentHashMap<>();
this.idGeneratorProvider = idGeneratorProvider;
}

public void ensureId(Object entity) {
getCosIdFields(entity.getClass()).forEach(cosIdField -> cosIdField.ensureId(entity, idGeneratorProvider));
}

private List<CosIdField> getCosIdFields(Class<?> entityClass) {
return classMapField.computeIfAbsent(entityClass, (key) -> Arrays.stream(entityClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(CosId.class))
.map(field -> {
CosId cosId = field.getAnnotation(CosId.class);
return new CosIdField(cosId, field);
})
.collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* limitations under the License.
*/

package me.ahoo.cosid.mybatis;
package me.ahoo.cosid.support;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (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.support;

import me.ahoo.cosid.annotation.CosId;
import me.ahoo.cosid.jvm.JdkId;
import me.ahoo.cosid.provider.DefaultIdGeneratorProvider;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author ahoo wang
*/
public class CosIdAnnotationSupportTest {
private IdGeneratorProvider idGeneratorProvider = DefaultIdGeneratorProvider.INSTANCE;
private CosIdAnnotationSupport cosIdSupport = new CosIdAnnotationSupport(idGeneratorProvider);

@Test
void ensureId() {
idGeneratorProvider.setShare(JdkId.INSTANCE);
Entity entity=new Entity();
cosIdSupport.ensureId(entity);
Assertions.assertEquals(1,entity.getId());
Assertions.assertEquals("2",entity.getStringId());
}

@Test
void ensureIdNotFindIdGen() {
MissingIdGenEntity entity=new MissingIdGenEntity();
Assertions.assertThrows(IllegalArgumentException.class,()->{
cosIdSupport.ensureId(entity);
});
}


public static class MissingIdGenEntity{
@CosId("missing")
private long id;

}

}
45 changes: 45 additions & 0 deletions cosid-core/src/test/java/me/ahoo/cosid/support/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (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.support;

import me.ahoo.cosid.annotation.CosId;

/**
* @author ahoo wang
*/
public class Entity {

@CosId
private long id;

@CosId
private String stringId;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getStringId() {
return stringId;
}

public void setStringId(String stringId) {
this.stringId = stringId;
}

}
34 changes: 8 additions & 26 deletions cosid-mybatis/src/main/java/me/ahoo/cosid/mybatis/CosIdPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
package me.ahoo.cosid.mybatis;


import me.ahoo.cosid.annotation.CosId;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import me.ahoo.cosid.support.CosIdAnnotationSupport;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
Expand All @@ -24,9 +23,8 @@
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.Collection;
import java.util.Map;

/**
* @author ahoo wang
Expand All @@ -38,12 +36,10 @@
public class CosIdPlugin implements Interceptor {

public static final String DEFAULT_LIST_KEY = "list";
private final IdGeneratorProvider idGeneratorProvider;
private final ConcurrentHashMap<Class<?>, List<CosIdField>> classMapField;
private final CosIdAnnotationSupport cosIdSupport;

public CosIdPlugin(IdGeneratorProvider idGeneratorProvider) {
classMapField = new ConcurrentHashMap<>();
this.idGeneratorProvider = idGeneratorProvider;
public CosIdPlugin(CosIdAnnotationSupport cosIdSupport) {
this.cosIdSupport = cosIdSupport;
}

@SuppressWarnings("rawtypes")
Expand All @@ -58,29 +54,15 @@ public Object intercept(Invocation invocation) throws Throwable {

Object parameter = args[1];
if (!(parameter instanceof Map)) {
ensureId(parameter);
cosIdSupport.ensureId(parameter);
return invocation.proceed();
}

Collection entityList = (Collection) ((Map) parameter).get(DEFAULT_LIST_KEY);

for (Object entity : entityList) {
ensureId(entity);
cosIdSupport.ensureId(entity);
}
return invocation.proceed();
}

private void ensureId(Object entity) {
getCosIdFields(entity.getClass()).forEach(cosIdField -> cosIdField.ensureId(entity, idGeneratorProvider));
}

private List<CosIdField> getCosIdFields(Class<?> entityClass) {
return classMapField.computeIfAbsent(entityClass, (key) -> Arrays.stream(entityClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(CosId.class))
.map(field -> {
CosId cosId = field.getAnnotation(CosId.class);
return new CosIdField(cosId, field);
})
.collect(Collectors.toList()));
}
}
2 changes: 1 addition & 1 deletion cosid-rest-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cosid:
core-pool-size: 2
prefetch-period: 1s
distributor:
type: redis
type: jdbc
share:
offset: 0
step: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import me.ahoo.cosid.provider.DefaultIdGeneratorProvider;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import me.ahoo.cosid.support.CosIdAnnotationSupport;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -36,6 +37,12 @@ public CosIdAutoConfiguration(CosIdProperties cosIdProperties) {
@Bean
@ConditionalOnMissingBean
public IdGeneratorProvider idGeneratorProvider() {
return new DefaultIdGeneratorProvider();
return DefaultIdGeneratorProvider.INSTANCE;
}

@Bean
@ConditionalOnMissingBean
public CosIdAnnotationSupport cosIdAnnotationSupport(IdGeneratorProvider idGeneratorProvider) {
return new CosIdAnnotationSupport(idGeneratorProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package me.ahoo.cosid.spring.boot.starter.mybatis;

import me.ahoo.cosid.mybatis.CosIdPlugin;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import me.ahoo.cosid.spring.boot.starter.ConditionalOnCosIdEnabled;
import me.ahoo.cosid.support.CosIdAnnotationSupport;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
Expand All @@ -31,8 +31,8 @@ public class CosIdMybatisAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public CosIdPlugin cosIdPlugin(IdGeneratorProvider idGeneratorProvider) {
return new CosIdPlugin(idGeneratorProvider);
public CosIdPlugin cosIdPlugin(CosIdAnnotationSupport cosIdSupport) {
return new CosIdPlugin(cosIdSupport);
}

}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#

group=me.ahoo.cosid
version=1.3.8
version=1.3.9

description=Global distributed ID generator
website=https://github.com/Ahoo-Wang/CosId
Expand Down
Loading

0 comments on commit 39dd15f

Please sign in to comment.