Skip to content

Commit

Permalink
Merge pull request #1568 from Okapist/read-data-by-column-name-optimi…
Browse files Browse the repository at this point in the history
…zation

Optimize read data by column name
  • Loading branch information
chernser authored Mar 28, 2024
2 parents 63d711a + 187fa3a commit ae75fc8
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.clickhouse.data;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Default implementation of {@link com.clickhouse.data.ClickHouseRecord},
Expand All @@ -13,6 +15,7 @@ public class ClickHouseSimpleRecord implements ClickHouseRecord {

private final List<ClickHouseColumn> columns;
private ClickHouseValue[] values;
private Map<String, Integer> columnsIndexes = null;

/**
* Creates a record object to wrap given values.
Expand Down Expand Up @@ -83,19 +86,25 @@ public ClickHouseValue getValue(int index) {

@Override
public ClickHouseValue getValue(String name) {
if(columnsIndexes == null)
columnsIndexes = new HashMap<>(columns.size());

return getValue(columnsIndexes.computeIfAbsent(name, this::computeColumnIndex));
}

@Override
public int size() {
return values.length;
}

private int computeColumnIndex(String name) {
int index = 0;
for (ClickHouseColumn c : columns) {
if (c.getColumnName().equalsIgnoreCase(name)) {
return getValue(index);
return index;
}
index++;
}

throw new IllegalArgumentException(ClickHouseUtils.format("Unable to find column [%s]", name));
}

@Override
public int size() {
return values.length;
}
}

0 comments on commit ae75fc8

Please sign in to comment.