AdaptiveRowStructure.java
/*
** Module : AdaptiveRowStructure.java
** Abstract : An adaptive row structure that can choose the right delegate live.
**
** Copyright (c) 2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------------------------------Description---------------------------------------
** 001 AL2 20231128 Initial version.
** AL2 20231211 Recieve the DMO meta directly in the constructor.
** AL2 20231212 Use dmoMeta directly, without getter.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.persist.orm;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import com.goldencode.p2j.persist.*;
/**
* Versatile row structure that can be used when there is no feedback on the order of the
* properties in a select query prior construction. This is an optimistic structure that
* presumes that the properties are in order (suitable to fast hydration). If the invariant
* is violated in the mean-time, this will invalidate into an un-ordered implementation.
*/
public class AdaptiveRowStructure
extends AbstractRowStructure
{
/** The collected fields for select query */
private RowStructure delegate;
/**
* Create a new instance.
*
* @param dmoMeta
* The DMO metadata of the underlying DMO for this structure.
*/
public AdaptiveRowStructure(DmoMeta dmoMeta)
{
super(dmoMeta);
delegate = new OrderedRowStructure(dmoMeta);
}
/**
* Append the recid to the row structure.
*
* @param recid
* The recid property to be appended to the structure.
*
* @return {@code true} if adding the recid to the row structure succeeded
* If there is some invariant on the structure, adding props may fail.
*/
@Override
public boolean addRecid(Property recid)
{
if (delegate.addRecid(recid))
{
return true;
}
invalidate();
return delegate.addRecid(recid);
}
/**
* Append the multiplex to the row structure.
*
* @param multiplex
* The multiplex property to be appended to the structure.
*
* @return {@code true} if adding the multiplex to the row structure succeeded
* If there is some invariant on the structure, adding props may fail.
*/
@Override
public boolean addMultiplex(Property multiplex)
{
if (delegate.addMultiplex(multiplex))
{
return true;
}
invalidate();
return delegate.addMultiplex(multiplex);
}
/**
* Append a property to the row structure. This doesn't check if the property actually belongs to the
* DMO class used for this structure.
*
* @param prop
* The property to be appended to the structure.
*
* @return {@code true} if adding the prop to the row structure succeeded
* If there is some invariant on the structure, adding props may fail.
*/
@Override
public boolean addProperty(Property prop)
{
if (delegate.addProperty(prop))
{
return true;
}
invalidate();
return delegate.addProperty(prop);
}
/**
* Retrieve the number of SQL fields that will be interpreted by this row structure. This is not the
* same as the number of properties (e.g. datetime-tz consumes 2 SQL columns)
*
* @return The number of SQL fields that are covered by this row structure.
*/
@Override
public int getCount()
{
return delegate.getCount();
}
/**
* Retrieve an iterator to all properties in this structure, including the recid and multiplex.
*
* @return An iterator to all properties in this structure, in order of their append.
*/
@Override
public Iterator<Property> getProps()
{
return delegate.getProps();
}
/**
* The core hydration logic based on the row structure. This class is specialized in traversing the
* result-set and populating the record with the data found there.
*
* @param session
* The underlying session that generated the result-set. It is used only to emit
* load SQLs for the non-expanded extents. If this is {@code null}, the normalized
* extents will be skipped.
* @param resultSet
* The result-set that provides the low-level data that should be hydrated into
* the record.
* @param rsOffset
* The offset where the DMO starts. This is the case for queries that had joins,
* so the current record may not start at index 1.
* @param r
* The record to be hydrated.
*
* @return The next rsOffset, from where the next DMO in the result-set will start (if any).
*
* @throws PersistenceException
* thrown when hydrating extents fails (as it requires extra SQL queries to be run).
* @throws SQLException
* throw when failing to hydrate from the result-set.
*/
@Override
public int hydrate(Session session, ResultSet resultSet, int rsOffset, BaseRecord r)
throws PersistenceException,
SQLException
{
return delegate.hydrate(session, resultSet, rsOffset, r);
}
/**
* Invalidation logic that will pick up everything that was appended until now and moved into
* an un-ordered implementation. This routine should be avoided as much as possible in the
* critical parts, as it is quite time consuming.
*/
private void invalidate()
{
// this is a slow process; so attempt to optimize cases where we need to invalidate
RowStructure unorderedDelegate = new UnorderedRowStructure(dmoMeta);
Iterator<Property> props = delegate.getProps();
boolean hasExtents = delegate.hasExtents();
while (props.hasNext())
{
Property prop = props.next();
switch (prop.id)
{
case ReservedProperty.ID_PRIMARY_KEY:
unorderedDelegate.addRecid(prop);
break;
case ReservedProperty.ID_MULTIPLEX:
unorderedDelegate.addRecid(prop);
break;
default:
unorderedDelegate.addProperty(prop);
}
}
this.delegate = unorderedDelegate;
if (hasExtents)
{
this.delegate.forceExtents();
}
}
}