From c0ca4527cc1731aa16a5dfd13010344a4a7e816d Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 10 Dec 2020 16:52:30 +0100 Subject: [PATCH] [jOOQ/jOOQ#11099] Cache RecordDelegate instance in CursorImpl --- .../main/java/org/jooq/impl/CursorImpl.java | 13 +++--- .../java/org/jooq/impl/RecordDelegate.java | 40 ++++++++++++++----- jOOQ/src/main/java/org/jooq/impl/Tools.java | 20 +--------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java index 71210440f6..8ba9857bee 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java @@ -1551,7 +1551,7 @@ final class CursorImpl extends AbstractCursor implements Cu /** * The (potentially) pre-fetched next record */ - private R next; + private R next; /** * Whether the underlying {@link ResultSet} has a next record. This @@ -1562,12 +1562,15 @@ final class CursorImpl extends AbstractCursor implements Cu *
  • false: there aren't any next records
  • * */ - private Boolean hasNext; + private Boolean hasNext; /** * [#11099] Cache this instance for the entire cursor. */ - private final CursorRecordInitialiser initialiser = new CursorRecordInitialiser(fields, 0); + private final CursorRecordInitialiser initialiser = new CursorRecordInitialiser(fields, 0); + + @SuppressWarnings("unchecked") + private final RecordDelegate recordDelegate = Tools.newRecord(true, (F0) factory, ((DefaultExecuteContext) ctx).originalConfiguration()); @Override public final boolean hasNext() { @@ -1601,9 +1604,7 @@ final class CursorImpl extends AbstractCursor implements Cu try { if (!isClosed && rs.next()) { - record = Tools.newRecord(true, (F0) factory, ((DefaultExecuteContext) ctx).originalConfiguration()) - .operate(initialiser.reset()); - + record = recordDelegate.operate(initialiser.reset()); rows++; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java b/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java index 3aa9434cdc..edea245cd1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java +++ b/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java @@ -62,29 +62,47 @@ import org.jooq.exception.ControlFlowSignal; final class RecordDelegate { private final Configuration configuration; - private final R record; + private final F0 recordSupplier; + private final Boolean fetched; private final RecordLifecycleType type; - RecordDelegate(Configuration configuration, R record) { - this(configuration, record, LOAD); + RecordDelegate(Configuration configuration, F0 recordSupplier, Boolean fetched) { + this(configuration, recordSupplier, fetched, LOAD); } - RecordDelegate(Configuration configuration, R record, RecordLifecycleType type) { + RecordDelegate(Configuration configuration, F0 recordSupplier, Boolean fetched, RecordLifecycleType type) { this.configuration = configuration; - this.record = record; + this.recordSupplier = recordSupplier; + this.fetched = fetched; this.type = type; } - static final RecordDelegate delegate(Configuration configuration, R record) { - return new RecordDelegate<>(configuration, record); - } - - static final RecordDelegate delegate(Configuration configuration, R record, RecordLifecycleType type) { - return new RecordDelegate<>(configuration, record, type); + static final RecordDelegate delegate( + final Configuration configuration, + final R record, + final RecordLifecycleType type + ) { + return new RecordDelegate<>( + configuration, + new F0() { + @Override + public R apply() { + return record; + } + }, + null, + type + ); } @SuppressWarnings("unchecked") final R operate(RecordOperation operation) throws E { + R record = recordSupplier.apply(); + + // [#3300] Records that were fetched from the database + if (fetched != null && record instanceof AbstractRecord) + ((AbstractRecord) record).fetched = fetched; + RecordListenerProvider[] providers = null; RecordListener[] listeners = null; DefaultRecordContext ctx = null; diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index 92add49ac7..eb74ead3ca 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -881,13 +881,6 @@ final class Tools { return newRecord(fetched, type, fields, null); } - /** - * Create a new record - */ - static final RecordDelegate newRecord(boolean fetched, Table type) { - return newRecord(fetched, type, null); - } - /** * Create a new record */ @@ -921,18 +914,7 @@ final class Tools { * Create a new record. */ static final RecordDelegate newRecord(boolean fetched, F0 factory, Configuration configuration) { - try { - R record = factory.apply(); - - // [#3300] Records that were fetched from the database - if (record instanceof AbstractRecord) - ((AbstractRecord) record).fetched = fetched; - - return new RecordDelegate<>(configuration, record); - } - catch (Exception e) { - throw new IllegalStateException("Could not construct new record", e); - } + return new RecordDelegate<>(configuration, factory, fetched); } static final AbstractRow row0(FieldsImpl fields) {