Skip to content

Commit

Permalink
feat: add workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Dec 20, 2023
1 parent 03afcab commit 3de9daa
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 36 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: build

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
check_format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Check format
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
&& sh ./tools/check_format.sh

test_ingester_common:
needs: check_format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Maven Test
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
&& (mvn --projects ingester-common test
|| mvn --projects ingester-common test
|| mvn --projects ingester-common test)

test_ingester_grpc:
needs: check_format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Maven Test
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
&& (mvn --projects ingester-grpc test
|| mvn --projects ingester-grpc test
|| mvn --projects ingester-grpc test)

test_ingester_protocol:
needs: check_format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Maven Test
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
&& (mvn --projects ingester-protocol test
|| mvn --projects ingester-protocol test
|| mvn --projects ingester-protocol test)
29 changes: 29 additions & 0 deletions .github/workflows/mvn_publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Maven Publish

on:
release:
types: [created]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout Git Repo
uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase

- name: Build with Maven
run: mvn clean deploy --batch-mode -DskipTests -P release -B -U -e
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
1 change: 0 additions & 1 deletion ingester-example/src/main/java/io/greptime/QuickStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.greptime.options.GreptimeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
Expand Down
3 changes: 1 addition & 2 deletions ingester-protocol/src/main/java/io/greptime/GreptimeDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -66,7 +65,7 @@ public class GreptimeDB implements Write, WritePOJO, Lifecycle<GreptimeOptions>,
private final int id;
private final AtomicBoolean started = new AtomicBoolean(false);

private final PojoMapper pojoMapper = new PojoMapper();
private final PojoMapper pojoMapper = new PojoMapper(65536);

private GreptimeOptions opts;
private RouterClient routerClient;
Expand Down
69 changes: 38 additions & 31 deletions ingester-protocol/src/main/java/io/greptime/PojoMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,26 @@
public class PojoMapper {

private final ConcurrentMap<String, Map<String, Field>> classFieldCache = new ConcurrentHashMap<>();
private final int maxCachedPOJOs;

public PojoMapper(int maxCachedPOJOs) {
this.maxCachedPOJOs = maxCachedPOJOs;
}

public <M> TableRows toTableRows(List<M> pojos) {
Ensures.ensureNonNull(pojos, "pojos");
Ensures.ensure(!pojos.isEmpty(), "pojos can not be empty");

M pojo = pojos.get(0);
Class<?> metricType = pojo.getClass();
cacheMetricClass(metricType);
M first = pojos.get(0);

for (M row : pojos) {
Class<?> rowType = row.getClass();
if (!rowType.equals(metricType)) {
throw new PojoException("All POJOs must be of the same type");
}
}
Class<?> metricType = first.getClass();

Map<String, Field> fieldMap = getAndCacheMetricClass(metricType);

String database = getDatabase(metricType);
String metricName = getMetricName(metricType);
Map<String, Field> fieldMap = this.classFieldCache.get(metricType.getName());

TableName tableName = TableName.with(database, metricName);
TableSchema.Builder schemaBuilder = TableSchema.newBuilder(tableName);

String[] columnNames = new String[fieldMap.size()];
DataType[] dataTypes = new DataType[fieldMap.size()];
Expand All @@ -87,17 +85,24 @@ public <M> TableRows toTableRows(List<M> pojos) {
i++;
}

TableSchema schema = schemaBuilder.columnNames(columnNames) //
TableSchema schema = TableSchema.newBuilder(tableName) //
.columnNames(columnNames) //
.semanticTypes(semanticTypes) //
.dataTypes(dataTypes).build();
.dataTypes(dataTypes) //
.build();

TableRows tableRows = TableRows.newBuilder(schema).build();
for (M row : pojos) {
for (M pojo : pojos) {
Class<?> type = pojo.getClass();
if (!type.equals(metricType)) {
throw new PojoException("All POJOs must be of the same type");
}

Object[] values = new Object[fieldMap.size()];
int j = 0;
for (Map.Entry<String, Field> entry : fieldMap.entrySet()) {
Field field = entry.getValue();
Object value = getObject(row, field);
Object value = getObject(pojo, field);
values[j] = value;

j++;
Expand Down Expand Up @@ -144,23 +149,25 @@ private <M> Object getObject(M metric, Field field) {
return value;
}

private void cacheMetricClass(Class<?>... metricTypes) {
for (Class<?> metricType : metricTypes) {
this.classFieldCache.computeIfAbsent(metricType.getName(), k -> {
Map<String, Field> fieldMap = new HashMap<>();
Class<?> currentType = metricType;
while (currentType != null) {
for (Field field : currentType.getDeclaredFields()) {
Column colAnnotation = field.getAnnotation(Column.class);
if (colAnnotation == null) {
continue;
}
fieldMap.put(colAnnotation.name(), field);
private Map<String, Field> getAndCacheMetricClass(Class<?> metricType) {
if (this.classFieldCache.size() >= this.maxCachedPOJOs) {
this.classFieldCache.clear();
}

return this.classFieldCache.computeIfAbsent(metricType.getName(), k -> {
Map<String, Field> fieldMap = new HashMap<>();
Class<?> currentType = metricType;
while (currentType != null) {
for (Field field : currentType.getDeclaredFields()) {
Column colAnnotation = field.getAnnotation(Column.class);
if (colAnnotation == null) {
continue;
}
currentType = currentType.getSuperclass();
fieldMap.put(colAnnotation.name(), field);
}
return fieldMap;
});
}
currentType = currentType.getSuperclass();
}
return fieldMap;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void testToTableRows() {
Pojo1Test pojo1 = createNewPojo1Test();
pojos1.add(pojo1);
}
TableRows tp1 = new PojoMapper().toTableRows(pojos1);
TableRows tp1 = new PojoMapper(65536).toTableRows(pojos1);
Assert.assertEquals("public", tp1.tableName().getDatabaseName());
Assert.assertEquals("pojo1", tp1.tableName().getTableName());
Assert.assertEquals(50, tp1.pointCount());
Expand All @@ -49,7 +49,7 @@ public void testToTableRows() {
Pojo2Test pojo2 = createNewPojo2Test();
pojos2.add(pojo2);
}
TableRows tp2 = new PojoMapper().toTableRows(pojos2);
TableRows tp2 = new PojoMapper(65536).toTableRows(pojos2);
Assert.assertEquals("public", tp2.tableName().getDatabaseName());
Assert.assertEquals("pojo2", tp2.tableName().getTableName());
Assert.assertEquals(30, tp2.pointCount());
Expand Down

0 comments on commit 3de9daa

Please sign in to comment.