DmoSignature.java

/*
** Module   : DmoSignature.java
** Abstract : A way to structurally identify a DMO through string representations.
**
** Copyright (c) 2020-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description---------------------------------------
** 001 AIL 20201214 First revision.
**     CA  20210508 Added a signature for the DMO schema, used by shared buffer/temp-table match.
** 002 SR  20230704 Added sqlSignature used for copy-temp-table.
** 003 DDF 20250210 Added simpleSchemaSignature.
*/

/*
** 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.util.*;

/**
 * This structure keeps together representative strings for one DMO. This is useful when we need to check
 * fast if two DMOs are compatible (if they signatures are compatible - eventually equal).  *
 */
public class DmoSignature
{
   /** Delimiter between the original property id and its position in the explicit signature */
   private static final String KEY_VALUE_DELIMITER = "->";

   /** Delimiter between multiple pairs of mappings between property id and signature position */
   private static final String ENTRY_DELIMITER = ",";

   /** A string which has information only about property order, types and constraints */
   private final String quickSignature;
   
   /** A comprehensive string which has information regarding property names, types and constraints */
   private final String explicitSignature;
   
   /** 
    * A mapping useful only when an explicit signature exists. 
    * This is used to store a mapping between the original property id (order) 
    * and their new positions in the explicit signature.
    */
   private final Map<Integer, Integer> explicitIdToPos;
   
   /** A signature for the {@code explicitIdToPos} mapping, which can fasten the signature matching*/
   private final String idToPosString;

   /** The schema signature to match shared buffers or temp-tables with the found master buffer/temp-table. */
   private final String schemaSignature;
   
   /** 
    * The simple schema signature to match shared buffers or temp-tables with the found master 
    * buffer/temp-table. This signature does not use any options (e.g. no-undo). 
    */
   private final String simpleSchemaSignature;
   
   /** A signature that has the SQL fields names besides explicitSignature info. */
   private final String sqlSignature;

   /**
    * Basic constructor.
    * 
    * @param   schemaSignature
    *          The schema signature.
    * @param   simpleSchemaSignature
    *          The simple schema signature.
    * @param   quickSignature
    *          A quick signature for the target DMO.
    * @param   explicitSignature
    *          An explicit signature for the target DMO.
    * @param   explicitIdToPos
    *          A mapping between the original property ids and their positions in the explicit signature
    * @param   sqlSignature
    *          An explicit signature that also contains the field`s sql name.
    */
   public DmoSignature(String schemaSignature,
                       String simpleSchemaSignature,
                       String quickSignature, 
                       String explicitSignature,
                       Map<Integer, Integer> explicitIdToPos,
                       String sqlSignature)
   {
      this.schemaSignature = schemaSignature;
      this.simpleSchemaSignature = simpleSchemaSignature;
      this.quickSignature = quickSignature;
      this.explicitSignature = explicitSignature;
      this.explicitIdToPos = explicitIdToPos;
      this.sqlSignature = sqlSignature;
      
      if (explicitIdToPos != null)
      {
         List<String> signatures = new ArrayList<>();
         for (Map.Entry<Integer, Integer> entry : explicitIdToPos.entrySet())
         {
            signatures.add(entry.getKey() + KEY_VALUE_DELIMITER + entry.getValue());
         }
         idToPosString = String.join(ENTRY_DELIMITER, signatures);
      }
      else
      {
         idToPosString = null;
      }
   }

   /**
    * Get the {@link #schemaSignature}.
    * 
    * @return   The schema signature.
    */
   public String getSchemaSignature()
   {
      return schemaSignature;
   }
   
   /**
    * Get the {@link #simpleSchemaSignature}.
    * 
    * @return   The simple schema signature which does not contain any options.
    */
   public String getSimpleSchemaSignature()
   {
      return simpleSchemaSignature;
   }
   
   /**
    * Getter for the quick signature.
    * 
    * @return   The quick signature.
    */
   public String getQuickSignature()
   {
      return quickSignature;
   }
   
   /**
    * Getter for the explicit signature.
    * 
    * @return   The explicit signature.
    */
   public String getExplicitSignature()
   {
      return explicitSignature;
   }
   
   /**
    * Getter for the id to position mapping signature.
    * 
    * @return   The signature of the if to position mapping.
    */
   public String getIdToPosAsString()
   {
      return idToPosString;
   }
   
   /**
    * Getter for the SQL signature.
    * 
    * @return   The SQL signature.
    */
   public String getSqlSignature()
   {
      return sqlSignature;
   }

}