public void removeTablesForDatabase(String catalogName, String schemaName) { lock.write(() -> { tablesByTableId.entrySet().removeIf(tableIdTableEntry -> { TableId tableId = tableIdTableEntry.getKey(); boolean equalCatalog = Objects.equals(catalogName, tableId.catalog()); boolean equalSchema = Objects.equals(schemaName, tableId.schema()); return equalSchema && equalCatalog; }); }); }
@Override public String toString() { return lock.read(() -> { StringBuilder sb = new StringBuilder(); sb.append("Tables {"); if (!tablesByTableId.isEmpty()) { sb.append(System.lineSeparator()); tablesByTableId.forEach((tableId, table) -> { sb.append(" ").append(tableId).append(": {").append(System.lineSeparator()); table.toString(sb, " "); sb.append(" }").append(System.lineSeparator()); }); } sb.append("}"); return sb.toString(); }); }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param changer the function that accepts the current {@link Table} and returns either the same or an updated * {@link Table}; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table updateTable(TableId tableId, Function<Table, Table> changer) { return lock.write(() -> { TableImpl existing = tablesByTableId.get(tableId); Table updated = changer.apply(existing); if (updated != existing) { tablesByTableId.put(tableId, new TableImpl(tableId, updated.columns(), updated.primaryKeyColumnNames(), updated.defaultCharsetName())); } changes.add(tableId); return existing; }); }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param columnDefs the list of column definitions; may not be null or empty * @param primaryKeyColumnNames the list of the column names that make up the primary key; may be null or empty * @param defaultCharsetName the name of the character set that should be used by default * @return the previous table definition, or null if there was no prior table definition */ public Table overwriteTable(TableId tableId, List<Column> columnDefs, List<String> primaryKeyColumnNames, String defaultCharsetName) { return lock.write(() -> { TableImpl updated = new TableImpl(tableId, columnDefs, primaryKeyColumnNames, defaultCharsetName); TableImpl existing = tablesByTableId.get(tableId); if ( existing == null || !existing.equals(updated) ) { // Our understanding of the table has changed ... changes.add(tableId); tablesByTableId.put(tableId,updated); } return tablesByTableId.get(tableId); }); }
/** * Rename an existing table. * * @param existingTableId the identifier of the existing table to be renamed; may not be null * @param newTableId the new identifier for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table renameTable(TableId existingTableId, TableId newTableId) { return lock.write(() -> { Table existing = forTable(existingTableId); if (existing == null) return null; tablesByTableId.remove(existing.id()); TableImpl updated = new TableImpl(newTableId, existing.columns(), existing.primaryKeyColumnNames(), existing.defaultCharsetName()); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(existingTableId); changes.add(updated.id()); } }); }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param changer the function that accepts the current {@link Table} and returns either the same or an updated * {@link Table}; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table updateTable(TableId tableId, Function<Table, Table> changer) { return lock.write(() -> { TableImpl existing = tablesByTableId.get(tableId); Table updated = changer.apply(existing); if (updated != existing) { tablesByTableId.put(tableId, new TableImpl(tableId, updated.columns(), updated.primaryKeyColumnNames(), updated.defaultCharsetName())); } changes.add(tableId); return existing; }); }
/** * Add or update the definition for the identified table. * * @param table the definition for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table overwriteTable(Table table) { return lock.write(() -> { TableImpl updated = new TableImpl(table); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(updated.id()); } }); }
@Override public String toString() { return lock.read(() -> { StringBuilder sb = new StringBuilder(); sb.append("Tables {"); if (!tablesByTableId.isEmpty()) { sb.append(System.lineSeparator()); tablesByTableId.forEach((tableId, table) -> { sb.append(" ").append(tableId).append(": {").append(System.lineSeparator()); table.toString(sb, " "); sb.append(" }").append(System.lineSeparator()); }); } sb.append("}"); return sb.toString(); }); }
@Override public int hashCode() { return tablesByTableId.hashCode(); }
public Tables subset(TableFilter filter) { if (filter == null) return this; return lock.read(() -> { Tables result = new Tables(tableIdCaseInsensitive); tablesByTableId.forEach((tableId, table) -> { if (filter.isIncluded(tableId)) { result.overwriteTable(table); } }); return result; }); }
/** * Get the set of {@link TableId}s for which there is a {@link Schema}. * * @return the immutable set of table identifiers; never null */ public Set<TableId> tableIds() { return lock.read(() -> Collect.unmodifiableSet(tablesByTableId.ids())); }
public void putAll(TablesById tablesByTableId) { if(tableIdCaseInsensitive) { tablesByTableId.values.entrySet() .forEach(e -> put(e.getKey().toLowercase(), e.getValue())); } else { values.putAll(tablesByTableId.values); } }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Tables) { Tables that = (Tables) obj; return this.tablesByTableId.equals(that.tablesByTableId); } return false; }
public void removeTablesForDatabase(String catalogName, String schemaName) { lock.write(() -> { tablesByTableId.entrySet().removeIf(tableIdTableEntry -> { TableId tableId = tableIdTableEntry.getKey(); boolean equalCatalog = Objects.equals(catalogName, tableId.catalog()); boolean equalSchema = Objects.equals(schemaName, tableId.schema()); return equalSchema && equalCatalog; }); }); }
public void clear() { lock.write(() -> { tablesByTableId.clear(); changes.clear(); }); }
/** * Create an empty set of definitions. * * @param tableIdCaseInsensitive - true if lookup is case insensitive (typical for MySQL on Windows) */ public Tables(boolean tableIdCaseInsensitive) { this.tableIdCaseInsensitive = tableIdCaseInsensitive; this.tablesByTableId = new TablesById(tableIdCaseInsensitive); this.changes = new TableIds(tableIdCaseInsensitive); }
/** * Obtain the definition of the identified table. * * @param tableId the identifier of the table * @return the table definition, or null if there was no definition for the identified table */ public Table forTable(TableId tableId) { return lock.read(() -> tablesByTableId.get(tableId)); }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param columnDefs the list of column definitions; may not be null or empty * @param primaryKeyColumnNames the list of the column names that make up the primary key; may be null or empty * @param defaultCharsetName the name of the character set that should be used by default * @return the previous table definition, or null if there was no prior table definition */ public Table overwriteTable(TableId tableId, List<Column> columnDefs, List<String> primaryKeyColumnNames, String defaultCharsetName) { return lock.write(() -> { TableImpl updated = new TableImpl(tableId, columnDefs, primaryKeyColumnNames, defaultCharsetName); TableImpl existing = tablesByTableId.get(tableId); if ( existing == null || !existing.equals(updated) ) { // Our understanding of the table has changed ... changes.add(tableId); tablesByTableId.put(tableId,updated); } return tablesByTableId.get(tableId); }); }
/** * Rename an existing table. * * @param existingTableId the identifier of the existing table to be renamed; may not be null * @param newTableId the new identifier for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table renameTable(TableId existingTableId, TableId newTableId) { return lock.write(() -> { Table existing = forTable(existingTableId); if (existing == null) return null; tablesByTableId.remove(existing.id()); TableImpl updated = new TableImpl(newTableId, existing.columns(), existing.primaryKeyColumnNames(), existing.defaultCharsetName()); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(existingTableId); changes.add(updated.id()); } }); }
/** * Add or update the definition for the identified table. * * @param table the definition for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table overwriteTable(Table table) { return lock.write(() -> { TableImpl updated = new TableImpl(table); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(updated.id()); } }); }