Added additional argument to Table class to initialize string lookup table

This commit is contained in:
Collin Smith 2021-01-04 14:36:13 -08:00
parent b4d54d687c
commit cc672557cd
2 changed files with 20 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import javax.lang.model.element.Modifier;
@ -37,11 +38,19 @@ class TableCodeGenerator extends CodeGenerator {
MethodSpec constructor(SchemaElement schemaElement) {
Schema config = schemaElement.annotation;
final boolean stringLookup;
if (config.indexed()) {
stringLookup = false;
} else {
TypeName primaryKeyType = ClassName.get(schemaElement.primaryKeyFieldElement.element());
stringLookup = Constants.STRING.equals(primaryKeyType);
}
return MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addStatement("super($T.class, $L, $Lf)", // does not append "f" automatically for float literals
schemaElement.element, config.initialCapacity(), config.loadFactor())
.addStatement("super($T.class, $L, $Lf, $L)", // does not append "f" automatically for float literals
schemaElement.element, config.initialCapacity(), config.loadFactor(), stringLookup)
.build();
}

View File

@ -32,10 +32,14 @@ public abstract class Table<R> implements Iterable<R> {
}
protected Table(Class<R> recordClass, int initialCapacity, float loadFactor) {
this(recordClass, initialCapacity, loadFactor, false);
}
protected Table(Class<R> recordClass, int initialCapacity, float loadFactor, boolean stringLookup) {
this.recordClass = recordClass;
records = new IntMap<>(initialCapacity, loadFactor);
ordered = new Array<>(true, (int) (initialCapacity * loadFactor), recordClass);
lookup = null;
lookup = new ObjectIntMap<>(stringLookup ? initialCapacity : 0, loadFactor);
}
protected abstract R newRecord();
@ -132,6 +136,10 @@ public abstract class Table<R> implements Iterable<R> {
return lookup == null ? null : get(lookup.get(id, -1));
}
private void resolve(String id) {
if (lookup == null) lookup = new ObjectIntMap<>();
}
public int size() {
return records.size;
}