Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
9591d7d
#882 jdp-2025-06: Schema Support
mrotteveel Jun 18, 2025
9865b6b
#882 Fix tests failing due to Firebird 6 schemas
mrotteveel Jun 18, 2025
a3c5979
#882 Implement to DatabaseMetaData.getSchemas return schemas
mrotteveel Jun 19, 2025
7deb6ed
#882 Implement DatabaseMetaData informational methods to report schem…
mrotteveel Jun 19, 2025
740ce4d
Use isNullOrEmpty
mrotteveel Jun 19, 2025
286eea0
#882 Schema support for DatabaseMetaData.getProcedures
mrotteveel Jun 19, 2025
44cccc2
#882 Use SYSTEM unquoted due to dialect 1 support
mrotteveel Jun 19, 2025
d34af61
#882 Schema support for getBestRowIdentifier
mrotteveel Jun 21, 2025
05edd2d
#882 Schema support for getColumnPrivileges
mrotteveel Jun 21, 2025
7699234
#882 Schema support for getColumns
mrotteveel Jun 21, 2025
1d30a6d
#882 Schema support for getImportedKeys/getExportedKeys/getCrossRefer…
mrotteveel Jun 21, 2025
69effeb
#882 Schema support for getFunctionColumns
mrotteveel Jun 22, 2025
a5d8434
#882 Schema support for getFunctions
mrotteveel Jun 22, 2025
c08df8b
#882 Schema support for getIndexInfo
mrotteveel Jun 22, 2025
a1655eb
#882 Schema support for getPrimaryKeys
mrotteveel Jun 22, 2025
6b55727
#882 Schema support for getProcedureColumns
mrotteveel Jun 22, 2025
584e2cc
#882 Schema support for getPseudoColumns
mrotteveel Jun 22, 2025
6bdb4bc
#882 Schema support for getTablePrivileges
mrotteveel Jun 23, 2025
73ebef5
#882 Schema support for getTables
mrotteveel Jun 23, 2025
90ce855
#882 Schema support for getCatalogs
mrotteveel Jun 23, 2025
11349a7
Mark metadata classes sealed or final
mrotteveel Jun 23, 2025
ea0a273
#882 Schema support for getXXXSourceCode
mrotteveel Jun 23, 2025
b60c7d6
Add ifSchemaElse also to FirebirdSupportInfo
mrotteveel Jun 23, 2025
a154551
#882 Client props, xid detection
mrotteveel Jun 24, 2025
18a5228
#882 Retrieve schema name in column information
mrotteveel Jun 24, 2025
bed0828
#882 Incomplete change to selectable procedure detection
mrotteveel Jun 24, 2025
ce3abcc
#882 Misc wording of jdp-2025-06
mrotteveel Jun 24, 2025
d365d92
#882 Define connection property searchPath
mrotteveel Jun 24, 2025
276d8bc
Add supportInfoFor(FirebirdConnection) to avoid wrapper check
mrotteveel Jun 25, 2025
501eb78
Tighten up StoredProcedureMetaData, use support info
mrotteveel Jun 25, 2025
ca102d2
#882 Implement Connection.get/setSchema
mrotteveel Jul 9, 2025
6a7b308
#882 Additional schema tests for FBResultSetMetaData
mrotteveel Jul 12, 2025
aa3d585
#882 Modify FBRowUpdater to search for schema
mrotteveel Jul 13, 2025
1f839f0
Deduplicate strings in StatementInfoProcessor
mrotteveel Jul 13, 2025
fbff0a6
Add some todo for later
mrotteveel Jul 14, 2025
10ed654
#882 Improve schema test coverage
mrotteveel Jul 17, 2025
3662432
#882 Improve schema test coverage (getFunctions)
mrotteveel Jul 17, 2025
b83f275
Replace QualifiedName with ObjectReference
mrotteveel Jul 18, 2025
2cb017e
Add schema support assumption to test
mrotteveel Jul 18, 2025
b8769ad
#882 Improve schema test coverage (getProcedures/getProcedureColumns)
mrotteveel Jul 22, 2025
099a4b2
#882 Improve schema test coverage (getIndexInfo/getPrimaryKeys)
mrotteveel Jul 22, 2025
6a9febc
#882 Improve schema test coverage (getPseudoColumns/getTables)
mrotteveel Jul 22, 2025
48f4b7c
#882 Improve schema test coverage (getTablePrivileges)
mrotteveel Jul 22, 2025
24478cf
#882 schema support for generated keys
mrotteveel Jul 29, 2025
d251d84
#882 Add schema support for FBCallableStatement
mrotteveel Oct 9, 2025
82aa541
Fix test failure on Firebird 5 and older
mrotteveel Oct 9, 2025
a7542c0
#882 Document decision not to backport schema support to Jaybird 6
mrotteveel Oct 11, 2025
04523d7
Misc. copy editing and fixing typos
mrotteveel Oct 11, 2025
381b792
#882 Add JB_PROCEDURE_SOURCE column to getProcedures
mrotteveel Oct 13, 2025
102a856
#882 Reject idea to add column with schemas to getCatalogs
mrotteveel Oct 13, 2025
567cdea
Remove unnecessary caching from BasicVersion
mrotteveel Oct 17, 2025
e2e4e87
#882 Schema support for StatisticsManager.getTableStatistics
mrotteveel Oct 17, 2025
2353b24
#882 Schema support for FBTableStatisticsManager
mrotteveel Oct 18, 2025
c2320ed
Fixed wrong name
mrotteveel Oct 19, 2025
180999c
#882 Added setters to FirebirdConnection to set search path
mrotteveel Oct 19, 2025
d934271
Document compatibility issue with schema(Pattern) in dbmd
mrotteveel Oct 25, 2025
a35a3a0
Correct documentation of getXXXSourceCode methods
mrotteveel Oct 30, 2025
48514d5
Misc review changes
mrotteveel Oct 30, 2025
13e38c3
Use Collection instead of List, use common code path
mrotteveel Nov 3, 2025
4a83dec
Simplify version string creation in GDSServerVersion
mrotteveel Nov 3, 2025
ec1056e
SPECIFIC_NAME should not include the schema
mrotteveel Nov 3, 2025
0c9efec
Update error codes and messages from 6.0.0.1338
mrotteveel Nov 4, 2025
e37afa1
Ignore drop schema failure, refactor to single code path
mrotteveel Nov 4, 2025
588a017
Finalize jdp-2025-06
mrotteveel Nov 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Deduplicate strings in StatementInfoProcessor
  • Loading branch information
mrotteveel committed Oct 25, 2025
commit 1f839f0c43851a99e1a9806a1f7a388744d556bd
11 changes: 7 additions & 4 deletions src/main/org/firebirdsql/gds/ng/StatementInfoProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.firebirdsql.gds.ISCConstants;
import org.firebirdsql.gds.ng.fields.RowDescriptor;
import org.firebirdsql.gds.ng.fields.RowDescriptorBuilder;
import org.firebirdsql.jaybird.util.StringDeduplicator;

import java.sql.SQLException;
import java.util.List;

import static org.firebirdsql.gds.VaxEncoding.iscVaxInteger;
import static org.firebirdsql.gds.VaxEncoding.iscVaxInteger2;
Expand All @@ -21,9 +23,11 @@
public final class StatementInfoProcessor implements InfoProcessor<InfoProcessor.StatementInfo> {

private static final System.Logger log = System.getLogger(StatementInfoProcessor.class.getName());
private static final List<String> DEDUPLICATOR_PRESET = List.of("SYSTEM", "PUBLIC", "SYSDBA");

private final AbstractFbStatement statement;
private final FbDatabase database;
private final StringDeduplicator stringDeduplicator = StringDeduplicator.of(DEDUPLICATOR_PRESET);

/**
* Creates an instance of this class.
Expand Down Expand Up @@ -85,10 +89,8 @@ private void handleTruncatedInfo(final StatementInfo info) throws SQLException {
newInfoItems[newIndex++] = 2; // size of short
newInfoItems[newIndex++] = (byte) (descriptorIndex & 0xFF);
newInfoItems[newIndex++] = (byte) (descriptorIndex >> 8);
newInfoItems[newIndex++] = infoItem;
} else {
newInfoItems[newIndex++] = infoItem;
}
newInfoItems[newIndex++] = infoItem;
}
assert newIndex == newInfoItems.length : "newInfoItems size too long";
// Doubling request buffer up to the maximum
Expand Down Expand Up @@ -212,10 +214,11 @@ private int readIntValue(StatementInfo info) {
private String readStringValue(StatementInfo info) {
int len = iscVaxInteger2(info.buffer, info.currentIndex);
info.currentIndex += 2;
if (len == 0) return "";
// TODO Is it correct to use the connection encoding here, or should we use UTF-8 always?
String value = database.getEncoding().decodeFromCharset(info.buffer, info.currentIndex, len);
info.currentIndex += len;
return value;
return stringDeduplicator.get(value);
}

/**
Expand Down
129 changes: 129 additions & 0 deletions src/main/org/firebirdsql/jaybird/util/StringDeduplicator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: Copyright 2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jaybird.util;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* Best-effort string deduplicator.
* <p>
* Given this class uses an LRU-evicted map with a maximum capacity internally, 100% deduplication of strings is
* <em>not</em> guaranteed.
* </p>
* <p>
* This class is <em>not</em> thread-safe.
* </p>
*
* @author Mark Rotteveel
* @since 7
*/
public final class StringDeduplicator {

private static final int DEFAULT_MAX_CAPACITY = 50;

private final Map<String, String> cache;

private StringDeduplicator(int maxCapacity, Collection<String> preset) {
cache = new StringCache(maxCapacity);
// NOTE: if preset.size() is greater than maxCapacity, prefix will be immediately evicted
preset.forEach(v -> cache.put(v, v));
}

/**
* Deduplicates this value if already cached, otherwise caches and returns {@code value}.
*
* @param value
* value to deduplicate (can be {@code null})
* @return previous cached value equal to {@code value}, or {@code value} itself
*/
public @Nullable String get(@Nullable String value) {
if (value == null) return null;
if (value.isEmpty()) return "";
return cache.computeIfAbsent(value, Function.identity());
}

/**
* @return a string deduplicator with a default max capacity
*/
public static StringDeduplicator of() {
return of(DEFAULT_MAX_CAPACITY, List.of());
}

/**
* @param preset
* values to initially add to the cache
* @return a string deduplicator with a default max capacity, initialized with {@code preset}
*/
public static StringDeduplicator of(String... preset) {
return of(Arrays.asList(preset));
}

/**
* @param preset
* values to initially add to the cache
* @return a string deduplicator with a default max capacity, initialized with {@code preset}
*/
public static StringDeduplicator of(Collection<String> preset) {
return of(DEFAULT_MAX_CAPACITY, preset);
}

/**
* @param maxCapacity
* maximum capacity
* @param preset
* values to initially add to the cache
* @return a string deduplicator with max capacity {@code maxCapacity}, initialized with {@code preset}
*/
public static StringDeduplicator of(int maxCapacity, String... preset) {
return of(maxCapacity, Arrays.asList(preset));
}

/**
* @param maxCapacity
* maximum capacity
* @param preset
* values to initially add to the cache
* @return a string deduplicator with max capacity {@code maxCapacity}, initialized with {@code preset}
*/
public static StringDeduplicator of(int maxCapacity, Collection<String> preset) {
return new StringDeduplicator(maxCapacity, preset);
}

/**
* String cache with LRU (Least-Recently Used) eviction order.
*/
private static final class StringCache extends LinkedHashMap<String, String> {

@Serial
private static final long serialVersionUID = -201579959301651333L;

private final int maxCapacity;

private StringCache(int maxCapacity) {
super(initialCapacity(maxCapacity), 0.75f, true);
if (maxCapacity <= 0) {
throw new IllegalArgumentException("maxCapacity must be greater than 0, was: " + maxCapacity);
}
this.maxCapacity = maxCapacity;
}

private static int initialCapacity(int maxCapacity) {
return Math.max(8, maxCapacity / 2);
}

@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
return size() > maxCapacity;
}

}

}
113 changes: 113 additions & 0 deletions src/test/org/firebirdsql/jaybird/util/StringDeduplicatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// SPDX-FileCopyrightText: Copyright 2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jaybird.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;
import java.util.stream.IntStream;

import static org.firebirdsql.common.matchers.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests for {@link StringDeduplicator}.
*/
class StringDeduplicatorTest {

@Test
void deduplicate() {
var deduplicator = StringDeduplicator.of();

final String value1 = "value1";
final String value1Copy = copyOf(value1);

// Check our assumptions (indirectly test copyOf(...))
assumeEqualNotSameInstance(value1, value1Copy, "value1Copy should be a distinct instance");

assertSame(value1, deduplicator.get(value1));
assertSame(value1, deduplicator.get(value1Copy));
}

@Test
void deduplicateToPreset() {
var deduplicator = StringDeduplicator.of("PRESET_1", "PRESET_2");

assertSame("PRESET_1", deduplicator.get(copyOf("PRESET_1")));
assertSame("PRESET_2", deduplicator.get(copyOf("PRESET_2")));
}

@Test
void eviction() {
final int maxCapacity = 5;
var deduplicator = StringDeduplicator.of(maxCapacity, List.of());

final String value1 = "value1";
final String value1Copy = copyOf(value1);

assertSame(value1, deduplicator.get(value1), value1);
// Deduplicate other values to evict value1
IntStream.rangeClosed(2, maxCapacity + 1).forEach(i -> {
String value = "value" + i;
assertSame(value, deduplicator.get(value), value);
});

assertSame(value1Copy, deduplicator.get(value1Copy), "expected value1Copy as value1 has been evicted");
}

@Test
void deduplicateNull() {
var deduplicator = StringDeduplicator.of();

assertNull(deduplicator.get(null));
}

@Test
void deduplicateEmptyString_toLiteralEmpty() {
var deduplicator = StringDeduplicator.of();

final String emptyCopy = copyOf("");
assumeEqualNotSameInstance("", emptyCopy, "emptyCopy should be a distinct instance");

assertSame("", deduplicator.get(emptyCopy), "should deduplicate to empty literal instance, not copy");
}

@ParameterizedTest
@ValueSource(ints = { -2, -1, 0 })
void cannotCreateWithMaxCapacityZeroOrSmaller(int maxCapacity) {
assertThrows(IllegalArgumentException.class, () -> StringDeduplicator.of(maxCapacity, List.of()));
}

@ParameterizedTest
@ValueSource(ints = { 1, 2, 10 })
void canCreateWithMaxCapacityOneOrGreater(int maxCapacity) {
assertDoesNotThrow(() -> StringDeduplicator.of(maxCapacity, List.of()));
}

/**
* Creates a distinct copy of {@code value}.
*
* @param value
* value to copy
* @return distinct copy of {@code value} (i.e. {@code new String(value)})
*/
@SuppressWarnings("StringOperationCanBeSimplified")
private static String copyOf(String value) {
return new String(value);
}

private void assumeEqualNotSameInstance(String v1, String v2, String message) {
assumeThat(message, v2,
allOf(equalTo(v1), not(sameInstance(v1))));
}

}