Skip to content

Commit 4020039

Browse files
author
tigraboris
committed
Initial commit
0 parents  commit 4020039

32 files changed

+1765
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
/out/
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/build/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/

build.gradle

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
buildscript {
2+
ext {
3+
springBootVersion = '2.0.2.RELEASE'
4+
}
5+
repositories {
6+
mavenCentral()
7+
mavenLocal()
8+
jcenter()
9+
maven { url "https://nexus.4digi.ru/repository/maven-releases/"}
10+
}
11+
dependencies {
12+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
13+
}
14+
}
15+
16+
apply plugin: 'java'
17+
apply plugin: 'eclipse'
18+
apply plugin: 'org.springframework.boot'
19+
apply plugin: 'io.spring.dependency-management'
20+
21+
22+
23+
group = 'ru.gpb.utils.db.data.generator'
24+
version = '0.2'
25+
sourceCompatibility = 1.8
26+
27+
repositories {
28+
mavenCentral()
29+
mavenLocal()
30+
jcenter()
31+
maven {
32+
url 'https://dl.bintray.com/palantir/releases'
33+
}
34+
maven {
35+
url 'https://packages.confluent.io/maven/'
36+
}
37+
maven { url "https://repo.spring.io/snapshot" }
38+
maven { url "https://repo.spring.io/milestone" }
39+
maven { url "https://nexus.4digi.ru/repository/maven-releases/"}
40+
}
41+
42+
43+
44+
dependencies {
45+
compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
46+
compileOnly('org.projectlombok:lombok')
47+
testCompileOnly('org.projectlombok:lombok')
48+
testCompile('org.springframework.boot:spring-boot-starter-test:2.0.2.RELEASE')
49+
testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
50+
51+
}
52+
53+
dependencyManagement {
54+
imports {
55+
mavenBom "org.springframework.cloud.stream.app:app-starters-core-dependencies:2.0.0.RC1"
56+
}
57+
}

readme.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#### Description:
2+
Database data generator.It based on Spring Data Repository.
3+
4+
#### Work stages:
5+
...
6+
7+
8+
#### Properties:
9+
* set default jpa properties - @EnableJpaRepositories, @EntityScan and etc
10+
* should set **@EnableDatabaseDataGenerator**
11+
* for Tables with One To One relations should field optional=true to be exist for one of that tables
12+
* add gradle dependency
13+
```
14+
compile group: 'ru.gpb.als.source.generator', name: 'db-data-generator', version: '0.1'
15+
```
16+
* (optional) add property *generator.cache-entity-size* to your application file.
17+
* It manages generated size between many2many relations. By default it is 20.
18+
* For example, we have 2 tables A and B with m2m rel. Generator takes 20 entities from each table and generate 400 relations each other.
19+
20+
#### Usage examples:
21+
22+
##### Get the factory:
23+
```
24+
@Autowired
25+
private DatabaseDataGeneratorFactory factory;
26+
27+
```
28+
##### Generate all values
29+
30+
```
31+
String report =
32+
factory
33+
.generator()
34+
.generateAll()
35+
.report();
36+
37+
```
38+
39+
##### Generate by Class
40+
```
41+
String report =
42+
factory
43+
.generator()
44+
.generateByClass(Limit.class)
45+
.generateByClass(Customer.class)
46+
.generateByClass(Currency.class)
47+
.generateByClass(OrganizationUnit.class)
48+
.report();
49+
50+
```
51+
##### Generate by Table
52+
```
53+
String report =
54+
factory
55+
.generator()
56+
.generateByTable("schema","table")
57+
.report();
58+
59+
```
60+
61+
##### Generate by Table with exception
62+
```
63+
64+
try {
65+
InnerLog log =
66+
factory
67+
.generator()
68+
.generateByTable("schema", "table")
69+
.withException()
70+
.log;
71+
} catch (DataGenerationException e) {
72+
e.printStackTrace();
73+
}
74+
75+
```
76+
77+
##### Generate by Class and repeate it 10 times
78+
```
79+
InnerLog log =
80+
factory
81+
.generator().repeate(10)
82+
.generateByClass(Limit.class)
83+
.log;
84+
85+
```
86+
##### Generate by Class and do it till exprected log size
87+
```
88+
InnerLog s =
89+
factory
90+
.generator().metronome(1, TimeUnit.SECONDS)
91+
.predicate(countPredicate(10))
92+
.generateByClass(Customer.class)
93+
.log();
94+
95+
InnerLog s =
96+
factory
97+
.generator().metronome(1, TimeUnit.SECONDS)
98+
.predicate(ctx -> ctx.log.markerValue() < 10)
99+
.generateByClass(Customer.class)
100+
.log();
101+
```

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'db-data-generator'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.gpb.utils.db.data.generator;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = "ru.gpb.utils.db.data.generator")
8+
public class DatabaseDataGeneratorConfig { }
9+
10+
// TODO: 7/26/2018 Если у колонки есть ограничение по длине, то это нужно считывать и запоминать.
11+
// TODO: 7/25/2018 Корректно обработать связи одной таблицы друг с другом -> parent child ex - Limit
12+
// TODO: 7/26/2018 Если в соседней таблице нет записей, то связи, естественно не будет. Нужно предсумотреть.
13+
// TODO: 7/26/2018 Обработать ситуацию когда у связанной сущности нет репозитория.(создать бин?)
14+
// TODO: 7/26/2018 возможность задавать алгоритм генератора для отдельного типа на лету.
15+
// TODO: 7/27/2018 тесты для коллекций, для разных баз ...
16+
// TODO: 7/27/2018 добавить описание в readme
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.gpb.utils.db.data.generator;
2+
3+
4+
import org.springframework.context.annotation.Import;
5+
import ru.gpb.utils.db.data.generator.worker.DatabaseDataGeneratorFactory;
6+
7+
import java.lang.annotation.*;
8+
9+
/**
10+
*
11+
* Annotation to enable database data plainValueGenerator @see {@link DatabaseDataGeneratorFactory}
12+
* This annotation allows to scan all packages for this app and add all founded beans to parent spring app.
13+
*
14+
*
15+
* @author Boris Zhguchev
16+
* */
17+
18+
@Target(ElementType.TYPE)
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Documented @Inherited
21+
@Import(DatabaseDataGeneratorConfig.class)
22+
public @interface EnableDatabaseDataGenerator { }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.gpb.utils.db.data.generator.timer;
2+
// 2018.07.24
3+
4+
/**
5+
*
6+
* Class for getting current time in nanos. It needed for metronome.
7+
*
8+
*/
9+
public interface Clock {
10+
11+
default long currentTimeInNanos() {
12+
return currentTimeInMillis() * 1000000L;
13+
}
14+
15+
long currentTimeInMillis();
16+
17+
18+
/**default implementation from system time*/
19+
static Clock system() {
20+
return new Clock() {
21+
@Override
22+
public long currentTimeInMillis() {
23+
return System.currentTimeMillis();
24+
}
25+
26+
@Override
27+
public long currentTimeInNanos() {
28+
return System.nanoTime();
29+
}
30+
};
31+
}
32+
33+
34+
35+
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Debezium Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package ru.gpb.utils.db.data.generator.timer;
7+
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.locks.LockSupport;
10+
11+
/**
12+
*
13+
* Class for generation equal pauses by interrupted thread.
14+
*
15+
* */
16+
public interface Metronome {
17+
18+
void pause() throws InterruptedException;
19+
20+
/**
21+
* default implementation based on {@link LockSupport#parkNanos(long)}
22+
* */
23+
static Metronome parker(long period, TimeUnit unitMetric, Clock clock) {
24+
long periodInNanos = unitMetric.toNanos(period);
25+
return new Metronome() {
26+
27+
private long next = clock.currentTimeInNanos() + periodInNanos;
28+
29+
@Override
30+
public void pause() throws InterruptedException {
31+
while (next > clock.currentTimeInNanos()) {
32+
LockSupport.parkNanos(next - clock.currentTimeInNanos());
33+
if ( Thread.currentThread().isInterrupted() ) {
34+
throw new InterruptedException();
35+
}
36+
}
37+
next = next + periodInNanos;
38+
}
39+
40+
};
41+
}
42+
static Metronome systemParker(long period, TimeUnit unitMetric) {
43+
return parker(period,unitMetric,Clock.system());
44+
}
45+
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.gpb.utils.db.data.generator.worker;
2+
// 2018.07.24
3+
4+
import java.math.BigDecimal;
5+
import java.sql.Timestamp;
6+
import java.util.Date;
7+
import java.util.UUID;
8+
import java.util.function.Function;
9+
10+
/**
11+
*
12+
* Based abstract implementation for {@link PlainTypeGeneratorSupplier}.
13+
*
14+
* @author Boris Zhguchev
15+
*/
16+
public abstract class AbstractPlainTypeGeneratorSupplier implements PlainTypeGeneratorSupplier {
17+
@Override
18+
public Function<MetaData, UUID> uuid() {
19+
return p -> UUID.randomUUID();
20+
}
21+
22+
@Override
23+
public Function<MetaData, String> string() {
24+
return p -> "";
25+
}
26+
27+
@Override
28+
public Function<MetaData, BigDecimal> bigDecimal() {
29+
return p -> BigDecimal.ONE;
30+
}
31+
32+
@Override
33+
public Function<MetaData, Integer> integer() {
34+
return p -> 1;
35+
}
36+
37+
@Override
38+
public Function<MetaData, Double> doubleVal() {
39+
return p -> 1d;
40+
}
41+
42+
@Override
43+
public Function<MetaData, Date> date() {
44+
return p -> new Date(0);
45+
}
46+
47+
@Override
48+
public Function<MetaData, Timestamp> timestamp() {
49+
return p -> new Timestamp(0);
50+
}
51+
52+
@Override
53+
public Function<MetaData, Character> character() {
54+
return p -> 'D';
55+
}
56+
57+
@Override
58+
public Function<MetaData, byte[]> bytes() {
59+
return p -> "".getBytes();
60+
}
61+
62+
@Override
63+
public Function<MetaData, Boolean> booleanV() {
64+
return p -> true;
65+
}
66+
67+
68+
}

0 commit comments

Comments
 (0)