Skip to content

Commit

Permalink
Prevent duplicate creation of the same table (#465)
Browse files Browse the repository at this point in the history
* Prevent duplicate creation of the same table

* One thread is responsible for creating all the tables.

* Collect all table names.

* spotless:apply

* Each thread creates the table that it is responsible for.

* Each thread creates the table that it is responsible for.
  • Loading branch information
YangYumings authored Nov 28, 2024
1 parent bb4035a commit f2826ee
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ public void registerDatabases(Session metaSession, List<TimeseriesSchema> schema
private void registerTable(Session metaSession, List<TimeseriesSchema> timeseriesSchemas)
throws TsdbException {
try {
// get all tables
Set<String> tableNames = getAllTables(timeseriesSchemas);
// register tables
DeviceSchema deviceSchema = timeseriesSchemas.get(0).getDeviceSchema();
HashMap<String, List<String>> tables = new HashMap<>();
for (TimeseriesSchema timeseriesSchema : timeseriesSchemas) {
DeviceSchema deviceSchema = timeseriesSchema.getDeviceSchema();
for (String tableName : tableNames) {
StringBuilder builder = new StringBuilder();
builder.append("create table if not exists ").append(deviceSchema.getTable()).append("(");
builder.append("create table if not exists ").append(tableName).append("(");
for (int i = 0; i < deviceSchema.getSensors().size(); i++) {
if (i != 0) builder.append(", ");
builder
Expand Down Expand Up @@ -143,6 +146,15 @@ private void registerTable(Session metaSession, List<TimeseriesSchema> timeserie
}
}

public Set<String> getAllTables(List<TimeseriesSchema> schemaList) {
Set<String> tableNames = new HashSet<>();
for (TimeseriesSchema timeseriesSchema : schemaList) {
DeviceSchema schema = timeseriesSchema.getDeviceSchema();
tableNames.add(schema.getTable());
}
return tableNames;
}

// region select

@Override
Expand Down

0 comments on commit f2826ee

Please sign in to comment.