[jOOQ/jOOQ#9009] Consistent caching in AbstractMeta
`AbstractMeta` now implements caching for all methods defined by the `Meta` interface.
This commit is contained in:
parent
116cd25994
commit
7da5918be7
@ -54,6 +54,8 @@ import org.jooq.Named;
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.Sequence;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.UniqueKey;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
@ -62,17 +64,18 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 910484713008245977L;
|
||||
|
||||
private final Map<Name, Catalog> cachedCatalogs;
|
||||
private final Map<Name, Schema> cachedQualifiedSchemas;
|
||||
private final Map<Name, Table<?>> cachedQualifiedTables;
|
||||
private final Map<Name, Sequence<?>> cachedQualifiedSequences;
|
||||
private final Map<Name, List<Schema>> cachedUnqualifiedSchemas;
|
||||
private final Map<Name, List<Table<?>>> cachedUnqualifiedTables;
|
||||
private final Map<Name, List<Sequence<?>>> cachedUnqualifiedSequences;
|
||||
private Map<Name, Catalog> cachedCatalogs;
|
||||
private Map<Name, Schema> cachedQualifiedSchemas;
|
||||
private Map<Name, Table<?>> cachedQualifiedTables;
|
||||
private Map<Name, Sequence<?>> cachedQualifiedSequences;
|
||||
private Map<Name, List<Schema>> cachedUnqualifiedSchemas;
|
||||
private Map<Name, List<Table<?>>> cachedUnqualifiedTables;
|
||||
private Map<Name, List<Sequence<?>>> cachedUnqualifiedSequences;
|
||||
private List<UniqueKey<?>> cachedPrimaryKeys;
|
||||
|
||||
AbstractMeta() {
|
||||
|
||||
// [#7165] TODO: Allow for opting out of this cache
|
||||
// [#9010] TODO: Allow for opting out of this cache
|
||||
this.cachedCatalogs = new LinkedHashMap<>();
|
||||
this.cachedQualifiedSchemas = new LinkedHashMap<>();
|
||||
this.cachedQualifiedTables = new LinkedHashMap<>();
|
||||
@ -80,6 +83,7 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
this.cachedUnqualifiedSchemas = new LinkedHashMap<>();
|
||||
this.cachedUnqualifiedTables = new LinkedHashMap<>();
|
||||
this.cachedUnqualifiedSequences = new LinkedHashMap<>();
|
||||
this.cachedPrimaryKeys = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -89,13 +93,26 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
|
||||
@Override
|
||||
public final Catalog getCatalog(Name name) {
|
||||
if (cachedCatalogs.isEmpty())
|
||||
for (Catalog catalog : getCatalogs())
|
||||
cachedCatalogs.put(catalog.getQualifiedName(), catalog);
|
||||
|
||||
initCatalogs();
|
||||
return cachedCatalogs.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() throws DataAccessException {
|
||||
initCatalogs();
|
||||
return Collections.unmodifiableList(new ArrayList<>(cachedCatalogs.values()));
|
||||
}
|
||||
|
||||
private final void initCatalogs() {
|
||||
if (cachedCatalogs == null) {
|
||||
cachedCatalogs = new LinkedHashMap<>();
|
||||
for (Catalog catalog : getCatalogs0())
|
||||
cachedCatalogs.put(catalog.getQualifiedName(), catalog);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<Catalog> getCatalogs0() throws DataAccessException;
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas(String name) {
|
||||
return getSchemas(name(name));
|
||||
@ -103,14 +120,36 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas(Name name) {
|
||||
initSchemas();
|
||||
return get(name, new Iterable<Schema>() {
|
||||
@Override
|
||||
public Iterator<Schema> iterator() {
|
||||
return getSchemas().iterator();
|
||||
return getSchemas0().iterator();
|
||||
}
|
||||
}, cachedQualifiedSchemas, cachedUnqualifiedSchemas);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() throws DataAccessException {
|
||||
initSchemas();
|
||||
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedSchemas.values()));
|
||||
}
|
||||
|
||||
private final void initSchemas() {
|
||||
if (cachedQualifiedSchemas == null) {
|
||||
cachedQualifiedSchemas = new LinkedHashMap<>();
|
||||
cachedUnqualifiedSchemas = new LinkedHashMap<>();
|
||||
get(name(""), new Iterable<Schema>() {
|
||||
@Override
|
||||
public Iterator<Schema> iterator() {
|
||||
return getSchemas0().iterator();
|
||||
}
|
||||
}, cachedQualifiedSchemas, cachedUnqualifiedSchemas);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<Schema> getSchemas0() throws DataAccessException;
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables(String name) {
|
||||
return getTables(name(name));
|
||||
@ -118,6 +157,7 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables(Name name) {
|
||||
initTables();
|
||||
return get(name, new Iterable<Table<?>>() {
|
||||
@Override
|
||||
public Iterator<Table<?>> iterator() {
|
||||
@ -126,6 +166,27 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
}, cachedQualifiedTables, cachedUnqualifiedTables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() throws DataAccessException {
|
||||
initTables();
|
||||
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedTables.values()));
|
||||
}
|
||||
|
||||
private final void initTables() {
|
||||
if (cachedQualifiedTables == null) {
|
||||
cachedQualifiedTables = new LinkedHashMap<>();
|
||||
cachedUnqualifiedTables = new LinkedHashMap<>();
|
||||
get(name(""), new Iterable<Table<?>>() {
|
||||
@Override
|
||||
public Iterator<Table<?>> iterator() {
|
||||
return getTables0().iterator();
|
||||
}
|
||||
}, cachedQualifiedTables, cachedUnqualifiedTables);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<Table<?>> getTables0() throws DataAccessException;
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences(String name) {
|
||||
return getSequences(name(name));
|
||||
@ -133,6 +194,7 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences(Name name) {
|
||||
initSequences();
|
||||
return get(name, new Iterable<Sequence<?>>() {
|
||||
@Override
|
||||
public Iterator<Sequence<?>> iterator() {
|
||||
@ -141,6 +203,40 @@ abstract class AbstractMeta implements Meta, Serializable {
|
||||
}, cachedQualifiedSequences, cachedUnqualifiedSequences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() throws DataAccessException {
|
||||
initSequences();
|
||||
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedSequences.values()));
|
||||
}
|
||||
|
||||
private final void initSequences() {
|
||||
if (cachedQualifiedSequences == null) {
|
||||
cachedQualifiedSequences = new LinkedHashMap<>();
|
||||
cachedUnqualifiedSequences = new LinkedHashMap<>();
|
||||
get(name(""), new Iterable<Sequence<?>>() {
|
||||
@Override
|
||||
public Iterator<Sequence<?>> iterator() {
|
||||
return getSequences0().iterator();
|
||||
}
|
||||
}, cachedQualifiedSequences, cachedUnqualifiedSequences);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<Sequence<?>> getSequences0() throws DataAccessException;
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() throws DataAccessException {
|
||||
initPrimaryKeys();
|
||||
return Collections.unmodifiableList(cachedPrimaryKeys);
|
||||
}
|
||||
|
||||
private final void initPrimaryKeys() {
|
||||
if (cachedPrimaryKeys == null)
|
||||
cachedPrimaryKeys = new ArrayList<>(getPrimaryKeys0());
|
||||
}
|
||||
|
||||
protected abstract List<UniqueKey<?>> getPrimaryKeys0() throws DataAccessException;
|
||||
|
||||
private final <T extends Named> List<T> get(Name name, Iterable<T> i, Map<Name, T> qualified, Map<Name, List<T>> unqualified) {
|
||||
if (qualified.isEmpty()) {
|
||||
for (T object : i) {
|
||||
|
||||
@ -39,7 +39,6 @@ package org.jooq.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.Catalog;
|
||||
@ -66,12 +65,12 @@ final class CatalogMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() {
|
||||
return Collections.unmodifiableList(Arrays.asList(catalogs));
|
||||
protected final List<Catalog> getCatalogs0() {
|
||||
return Arrays.asList(catalogs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
protected final List<Schema> getSchemas0() {
|
||||
List<Schema> result = new ArrayList<>();
|
||||
|
||||
for (Catalog catalog : catalogs)
|
||||
@ -81,7 +80,7 @@ final class CatalogMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
protected final List<Table<?>> getTables0() {
|
||||
List<Table<?>> result = new ArrayList<>();
|
||||
|
||||
for (Catalog catalog : catalogs)
|
||||
@ -92,7 +91,7 @@ final class CatalogMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() {
|
||||
protected final List<Sequence<?>> getSequences0() {
|
||||
List<Sequence<?>> result = new ArrayList<>();
|
||||
|
||||
for (Catalog catalog : catalogs)
|
||||
@ -103,7 +102,7 @@ final class CatalogMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() {
|
||||
protected final List<UniqueKey<?>> getPrimaryKeys0() {
|
||||
List<UniqueKey<?>> result = new ArrayList<>();
|
||||
|
||||
for (Catalog catalog : catalogs)
|
||||
|
||||
@ -494,28 +494,30 @@ final class InformationSchemaMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() {
|
||||
return Collections.<Catalog>unmodifiableList(catalogs);
|
||||
protected final List<Catalog> getCatalogs0() {
|
||||
return catalogs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
return Collections.<Schema>unmodifiableList(schemas);
|
||||
protected final List<Schema> getSchemas0() {
|
||||
return schemas;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
protected final List<Table<?>> getTables0() {
|
||||
return (List) tables;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
return Collections.<Table<?>>unmodifiableList(tables);
|
||||
protected final List<Sequence<?>> getSequences0() {
|
||||
return sequences;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() {
|
||||
return Collections.<Sequence<?>>unmodifiableList(sequences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() {
|
||||
return Collections.<UniqueKey<?>>unmodifiableList(primaryKeys);
|
||||
protected final List<UniqueKey<?>> getPrimaryKeys0() {
|
||||
return (List) primaryKeys;
|
||||
}
|
||||
|
||||
private final class InformationSchemaCatalog extends CatalogImpl {
|
||||
|
||||
@ -185,7 +185,7 @@ final class MetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() {
|
||||
protected final List<Catalog> getCatalogs0() {
|
||||
List<Catalog> result = new ArrayList<>();
|
||||
|
||||
|
||||
@ -219,7 +219,7 @@ final class MetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
protected final List<Schema> getSchemas0() {
|
||||
List<Schema> result = new ArrayList<>();
|
||||
|
||||
for (Catalog catalog : getCatalogs())
|
||||
@ -229,7 +229,7 @@ final class MetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
protected final List<Table<?>> getTables0() {
|
||||
List<Table<?>> result = new ArrayList<>();
|
||||
|
||||
for (Schema schema : getSchemas())
|
||||
@ -239,7 +239,7 @@ final class MetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() {
|
||||
protected final List<Sequence<?>> getSequences0() {
|
||||
List<Sequence<?>> result = new ArrayList<>();
|
||||
|
||||
for (Schema schema : getSchemas())
|
||||
@ -249,7 +249,7 @@ final class MetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() {
|
||||
protected final List<UniqueKey<?>> getPrimaryKeys0() {
|
||||
List<UniqueKey<?>> result = new ArrayList<>();
|
||||
|
||||
for (Table<?> table : getTables()) {
|
||||
|
||||
@ -68,7 +68,7 @@ final class SchemaMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() {
|
||||
protected final List<Catalog> getCatalogs0() {
|
||||
Set<Catalog> result = new LinkedHashSet<>();
|
||||
|
||||
for (Schema schema : schemas)
|
||||
@ -79,12 +79,12 @@ final class SchemaMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
protected final List<Schema> getSchemas0() {
|
||||
return Collections.unmodifiableList(Arrays.asList(schemas));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
protected final List<Table<?>> getTables0() {
|
||||
List<Table<?>> result = new ArrayList<>();
|
||||
|
||||
for (Schema schema : schemas)
|
||||
@ -94,7 +94,7 @@ final class SchemaMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() {
|
||||
protected final List<Sequence<?>> getSequences0() {
|
||||
List<Sequence<?>> result = new ArrayList<>();
|
||||
|
||||
for (Schema schema : schemas)
|
||||
@ -104,7 +104,7 @@ final class SchemaMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() {
|
||||
protected final List<UniqueKey<?>> getPrimaryKeys0() {
|
||||
List<UniqueKey<?>> result = new ArrayList<>();
|
||||
|
||||
for (Schema schema : schemas)
|
||||
|
||||
@ -68,7 +68,7 @@ final class TableMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Catalog> getCatalogs() {
|
||||
protected final List<Catalog> getCatalogs0() {
|
||||
Set<Catalog> result = new LinkedHashSet<>();
|
||||
|
||||
for (Table<?> table : tables)
|
||||
@ -80,7 +80,7 @@ final class TableMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
protected final List<Schema> getSchemas0() {
|
||||
Set<Schema> result = new LinkedHashSet<>();
|
||||
|
||||
for (Table<?> table : tables)
|
||||
@ -91,17 +91,17 @@ final class TableMetaImpl extends AbstractMeta {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
protected final List<Table<?>> getTables0() {
|
||||
return Collections.unmodifiableList(Arrays.asList(tables));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Sequence<?>> getSequences() {
|
||||
protected final List<Sequence<?>> getSequences0() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKey<?>> getPrimaryKeys() {
|
||||
protected final List<UniqueKey<?>> getPrimaryKeys0() {
|
||||
List<UniqueKey<?>> result = new ArrayList<>();
|
||||
|
||||
for (Table<?> table : tables)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user