H2Map.java

/*
** Module   : H2Map.java
** Abstract : Define a H2-backed map.
**
** Copyright (c) 2020-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 CA  20200412 Created initial version, for incremental conversion support.
** 002 CA  20200416 Reduce the memory footprint for incremental conversion.
** 003 TJD 20220504 Java 11 compatibility minor changes
** 004 CA  20230127 Performance improvement for incremental conversion.
*/
/*
** 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.convert.db;

import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.util.function.*;

import com.goldencode.ast.*;
import com.goldencode.p2j.convert.db.LoggedCollection.AstKey;
import com.goldencode.p2j.pattern.*;

/**
 * A H2-backed map, which has any changes to it logged in a separate work table.
 */
public class H2Map
extends HashMap
implements StoredCollection
{
   /** A map defining custom serialization for certain values. */
   private static final Map<Class<?>, Class<? extends CustomExternalizable>> 
   externalizables = new HashMap<>();
   
   /** The change logger for this collection. */
   protected final LoggedCollection changes = new LoggedCollection();

   /** The AST symbol resolver. */
   private final AstSymbolResolver resolver;
   
   /** The AST manager. */
   private final AstManager manager;
   
   /** The helper for SQL usage. */
   private final DBHelper helper;

   /** The map key class. */
   private final Class<?> keyClass;
   
   /** The map value class. */
   private final Class<?> valueClass;

   /** The map's table name. */
   private final String tableName;

   /** The map's work table name. */
   private final String workTable;

   /** The map's insert SQL. */
   private final String insertSql;

   /** The map's clear SQL. */
   private final String clearSql;

   /** The map's work table insert SQL. */
   private final String workInsertSql;
   
   /** The map's work table clear SQL. */
   private final String workClearSql;
   
   /** A custom c'tor for serializing the value. */
   private final Constructor<? extends CustomExternalizable> valueCtor;
   
   static
   {
      externalizables.put(Aast.class, AstExternalizble.class);
   }

   /**
    * Create a new db-backed map, with the specified name.
    * 
    * @param    keyClass
    *           The map's key class.
    * @param    valueClass
    *           The map's value class.
    * @param    table
    *           The map's (and main table) name.
    * @param    helper
    *           The SQL helper.
    */
   public H2Map(Class<?> keyClass, Class<?> valueClass, String table, DBHelper helper)
   {
      this.resolver = AstSymbolResolver.getResolver();
      this.manager = AstManager.get();

      this.tableName = table;
      this.keyClass = keyClass;
      this.valueClass = valueClass;
      this.valueCtor = getCustomExternalizable(valueClass);
      this.helper = helper;

      // the main map table
      String sqlKeyType = helper.convertToSql(keyClass);
      String sqlValueType = helper.convertToSql(valueClass);

      String createSQL = "CREATE TABLE IF NOT EXISTS " + tableName + 
                         " (key " +  sqlKeyType + " PRIMARY KEY, value " + sqlValueType + ");";
      helper.executeSQL(createSQL);
      
      // the work map table
      workTable = tableName + "__work";
      String createWorkSQL = "CREATE TABLE IF NOT EXISTS " + workTable + "(" + 
                             " fileId BIGINT NOT NULL," +
                             " astId BIGINT NOT NULL," +
                             " pos BIGINT AUTO_INCREMENT," + 
                             " key " + sqlKeyType + "," +
                             " value " + sqlValueType + ");";
      helper.executeSQL(createWorkSQL);
      helper.executeSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_" + workTable + "__pk ON " + 
                        workTable + " (fileId, astId, key);");
      helper.executeSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_" + workTable + "__pos ON " + 
                        workTable + " (pos);");
      
      /*
      // load the main table
      Consumer<ResultSet> main = (rs) ->
      {
         try
         {
            Object key = rs.getObject(1);
            Object value = rs.getObject(2);

            if (this.containsKey(key))
            {
               throw new IllegalStateException("Key already exists!");
            }

            super.put(key, value);
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      };
      helper.executeQuery("select key, value from " + tableName, main);
      */
      
      // tmpTabNodes needs special processing - the value needs to be the 'authoritative' AST,
      // if it exists
      boolean specialProcessing = "tmpTabNodes".equalsIgnoreCase(table);
      Map loaded = specialProcessing ? new HashMap() : null;
      
      // load the work table
      Consumer<ResultSet> work = (rs) ->
      {
         try
         {
            Object key = rs.getObject(3);
            Object value = rs.getObject(4);
            
            /*
            if (this.containsKey(key))
            {
               throw new IllegalStateException("Key already exists!");
            }
            */
            
            if (specialProcessing)
            {
               AstExternalizble v = (AstExternalizble) value;
               if (v.isSpecial())
               {
                  loaded.put(key, value);
               }
            }
            
            super.put(key, value);
            
            this.changes.log(rs.getLong(1), rs.getLong(2), key, value);
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      };
      helper.executeQuery("select fileId, astId, key, value from " + workTable + " order by pos", 
                          work);
      
      if (specialProcessing)
      {
         // replace any 'override' values
         super.putAll(loaded);
      }
      
      this.insertSql = "insert into " + tableName + " values (?, ?)";
      this.clearSql = "delete from " + tableName;

      this.workInsertSql = "INSERT INTO " + workTable + " (fileId, astId, key, value) " + 
                           "VALUES (?, ?, ?, ?)";
      this.workClearSql = "delete from " + workTable;
   }
   
   /**
    * Constructor used for sub-classes, which delegate the db-storage to another collection.
    */
   protected H2Map()
   {
      this.resolver = AstSymbolResolver.getResolver();
      this.manager = AstManager.get();

      helper = null;
      keyClass = null;
      valueClass = null;
      valueCtor = null;
      tableName = null;
      workTable = null;
      insertSql = null;
      clearSql = null;
      workInsertSql = null;
      workClearSql = null;
   }
   
   /**
    * Get the value with the specified key.
    * <p>
    * If the value exists, the access will be logged.  This assumes the source AST requires for
    * the key to have this specific value. 

    * @param    key
    *           The key to get the value.
    * 
    * @return   The found value. 
    */
   @Override
   public Object get(Object key)
   {
      Object val = super.get(key);
      
      if (val != null)
      {
         logChange(key, val);
      }
      
      return unwrapValue(val);
   }
   
   /**
    * Check if the map contains the specified key.
    * <p>
    * If the value exists, the access will be logged.  This assumes the source AST requires for
    * the key to have this specific value.
    * 
    * @param    key
    *           The key to check.
    * 
    * @return   <code>true</code> if the key exists in the map.
    */
   @Override
   public boolean containsKey(Object key)
   {
      boolean has = super.containsKey(key);
      
      if (has)
      {
         logChange(key, super.get(key));
      }
      
      return has;
   }
   
   /**
    * Perform a put operation.
    * <p>
    * The access will be logged.  This assumes the source AST requires for the key to have this 
    * specific value.
    * 
    * @param    key
    *           The key.
    * @param    value
    *           The value.
    * 
    * @return   old
    *           The old value.
    */
   @Override
   public Object put(Object key, Object value)
   {
      value = wrapValue(value);
      
      Object old = super.put(key, value);
      
      logChange(key, value);
      
      return unwrapValue(old);
   }
   
   /**
    * Merge the specified map into this one.  This will rely on {@link #put} to log the access
    * to the map.
    * 
    * @param    m
    *           The map to merge.
    */
   @Override
   public void putAll(Map m)
   {
      for (Object key : m.keySet())
      {
         put(key, m.get(key));
      }
   }
   
   /**
    * Remove this key from the map.
    * <p>
    * If the key exists, then all changes related to it will be cleared.
    * 
    * @param    key
    *           The key to remove.
    * 
    * @return   The old value.
    */
   @Override
   public Object remove(Object key)
   {
      Object old = super.remove(key);

      if (old != null)
      {
         changes.remove(key);
      }
      
      return old;
   }

   /**
    * This API is not currently supported.
    */
   @Override
   public boolean remove(Object key, Object value)
   {
      throw new UnsupportedOperationException();
   }
   
   /**
    * Clear the map and all its changes.
    */
   @Override
   public void clear()
   {
      super.clear();
      
      changes.clear();
   }
   
   /**
    * Persist this map and all the changes to the database.
    */
   @Override
   public void persist()
   {
      // clear the table and save the data
      helper.executeSQL(clearSql);
      
      helper.persistCollection(keySet(), insertSql, (PreparedStatement ps, Object key) ->
      {
         try
         {
            Object value = super.get(key);
            ps.setObject(1, key);
            ps.setObject(2, value);
            ps.addBatch();
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      });
      
      // clear the work table and save the data
      helper.executeSQL(workClearSql);
      
      // ensure the changes are in sync with the main collection
      changes.retainAll(keySet());
      
      changes.persist(workInsertSql, helper, (AstKey key, Object val) -> 
      {
         return new Object[] { key.fileId, key.astId, key.key, val };
      });
   }
   
   /**
    * Force the map to have logged a specific value for this key, in the context of the current
    * source AST.
    * 
    * @param    key
    *           The map key.
    * @param    value
    *           The value to be logged as 'forced'.
    */
   public void forceChange(Object key, Object value)
   {
      // pin this change as 'mandatory'
      Aast src = resolver.getSourceAst();
      long astId = -1;
      long fileId = -1;
      if (src != null)
      {
         astId = src.getId();
         fileId = manager.getTreeId(astId);
      }
      changes.force(fileId, astId, key, wrapValue(value));
   }
   
   /**
    * Compact the elements of this collection, to reduce the memory footprint.
    * 
    * @param    fileId
    *           The AST file ID which has finished processing.
    */
   public void compact(long fileId)
   {
      if (valueCtor == null)
      {
         return;
      }
      
      for (Object key : keySet())
      {
         Object value = super.get(key);
         
         ((CustomExternalizable) value).compact(fileId);
      }
      
      changes.compact(fileId, (value) -> ((CustomExternalizable) value).compact(fileId));
   }
   
   /**
    * Wrap the value in a {@link CustomExternalizable}.
    * 
    * @param     value
    *            The value to wrap.
    *            
    * @return    See above.
    */
   @Override
   public Object wrapValue(Object value)
   {
      if (valueCtor != null)
      {
         try
         {
            value = valueCtor.newInstance(value);
         }
         catch (ReflectiveOperationException  e)
         {
            throw new RuntimeException(e);
         }
      }
      
      return value;
   }
   
   /**
    * Unwrap a value from a {@link CustomExternalizable}.
    * 
    * @param     value
    *            The value to unwrap.
    *            
    * @return    The real value (to be used by the actual user of the collection), as returned by 
    *            {@link CustomExternalizable#getValue()}.
    */
   @Override
   public Object unwrapValue(Object value)
   {
      if (value == null)
      {
         return null;
      }
      
      if (valueCtor != null)
      {
         value = ((CustomExternalizable) value).getValue();
      }
      
      return value;
   }

   /**
    * Log this access in the map as a change.
    * 
    * @param    key
    *           The map key.
    * @param    value
    *           The map value.
    */
   private void logChange(Object key, Object value)
   {
      if (inPersist())
      {
         return;
      }
      
      // add to work table
      Aast src = resolver.getSourceAst();
      long astId = -1;
      long fileId = -1;
      if (src != null)
      {
         astId = src.getId();
         fileId = manager.getTreeId(astId);
      }

      // log the change
      changes.log(fileId, astId, key, value);
   }
   
   /**
    * Find the constructor to create a new custom externalizable, for (de)serialization purposes.
    * 
    * @param    clazz
    *           The value class.
    *           
    * @return   The found constructor, which must have only an {@link Object} argument.
    */
   private Constructor<? extends CustomExternalizable> getCustomExternalizable(Class<?> clazz)
   {
      Class<? extends CustomExternalizable> extern = externalizables.get(clazz);
      try
      {
         Constructor<? extends CustomExternalizable> ctor = extern == null 
                                                              ? null 
                                                              : extern.getConstructor(Object.class);
         return ctor;
      }
      catch (NoSuchMethodException | 
             SecurityException e)
      {
         throw new RuntimeException(e);
      }
   }
}