H2MapToMap.java

/*
** Module   : H2MapToMap.java
** Abstract : Define a H2-backed map, with other maps as values.
**
** Copyright (c) 2020, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 CA  20200412 Created initial version, for incremental conversion support.
*/
/*
** 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.sql.*;
import java.util.*;
import java.util.function.*;

/**
 * Define a H2-backed map, with its values other db-backed maps.
 * <p>
 * After adding elements to this map, a {@link #get} operation needs to be performed, so that the
 * caller uses the db-backed collection.
 */
public class H2MapToMap<K>
extends HashMap<K, Map<?, ?>>
implements StoredCollection
{
   /** The helper for SQL usage. */
   private DBHelper helper;

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

   /** The child's map key class. */
   private Class<?> key2Class;

   /** The child's map value class. */
   private Class<?> valueClass;
   
   /** The map's table name. */
   private String tableName;

   /** The child map's table name. */
   private final String childTableName;
   
   /** The child map's work table name. */
   private final String childWorkTable;

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

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

   /** The child's map insert SQL. */
   private final String mapInsertSql;
   
   /** The child's map clear SQL. */
   private final String mapClearSql;

   /** The child's work table insert SQL. */
   private final String mapWorkInsertSql;
   
   /** The child's work table clear SQL. */
   private final String mapWorkClearSql;
   
   /**
    * Create a new db-backed map, with the specified name.
    * 
    * @param    keyClass
    *           The map's key class.
    * @param    key2Class
    *           The child's map key class.
    * @param    valueClass
    *           The map's value class.
    * @param    table
    *           The map's (and main table) name.
    * @param    helper
    *           The SQL helper.
    */
   public H2MapToMap(Class<?> keyClass, 
                     Class<?> key2Class,
                     Class<?> valueClass, 
                     String   table, 
                     DBHelper helper)
   {
      this.keyClass = keyClass;
      this.key2Class = key2Class;
      this.valueClass = valueClass;
      this.tableName = table;
      this.helper = helper;
   
      this.childTableName = tableName + "__map";
      this.childWorkTable = childTableName + "__work";

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

      String createSQL = "CREATE TABLE IF NOT EXISTS " + table + 
                         " (key " +  sqlKeyType + " PRIMARY KEY);";
      helper.executeSQL(createSQL);
      
      // load the main table
      helper.executeQuery("select key from " + tableName, (ResultSet rs) -> 
      {
         try
         {
            K key = (K) rs.getObject(1);
            super.put(key, new H2ChildMap(key));
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      });
      
      loadChildMaps();
      
      this.clearSql  = "delete from " + table;
      this.insertSql = "insert into " + table + " values (?)";

      this.mapInsertSql = "insert into " + childTableName + " values (?, ?, ?)";
      this.mapClearSql  = "delete from " + childTableName;
      
      // misc used work table sqls
      this.mapWorkInsertSql = "INSERT INTO " + childWorkTable + " VALUES (?, ?, ?, ?, ?)";
      this.mapWorkClearSql  = "DELETE FROM " + childWorkTable;
   }

   /**
    * Perform a put operation.
    * <p>
    * The value map will be converted to a {@link H2ChildMap}.  Use a {@link #get} operation
    * to ensure the caller is using the db-backed map.
    * <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 Map<?, ?> put(K key, Map<?, ?> value)
   {
      Map old = remove(key);
      H2ChildMap map = new H2ChildMap(key);
      map.clear();
      map.putAll(value);
      
      super.put(key, map);
      
      return 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((K) key, (Map<?, ?>) 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 map, or <code>null</code> if it does not exist.
    */
   @Override
   public Map<?, ?> remove(Object key)
   {
      H2ChildMap old = (H2ChildMap) super.remove(key);
      Map<?, ?> map = null;
      if (old != null)
      {
         map = new HashMap<>(old);
         old.clear();
      }

      return old == null ? null : map;
   }

   /**
    * 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()
   {
      for (Map<?, ?> map : values())
      {
         ((H2ChildMap) map).clear();
      }
      
      super.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
         {
            ps.setObject(1, key);
            ps.addBatch();
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      });

      // clear the child sets and save the data
      helper.executeSQL(mapClearSql);
      helper.persistCollection(keySet(), mapInsertSql, (PreparedStatement ps, Object fkId) -> 
      {
         H2ChildMap child = (H2ChildMap) get(fkId);
         
         try
         {
            for (Object key : child.keySet())
            {
               Object value = child.get(key);
               ps.setObject(1, fkId);
               ps.setObject(2, key);
               ps.setObject(3, value);
               ps.addBatch();
            }
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      });
      
      helper.executeSQL(mapWorkClearSql);
      helper.persistCollection(keySet(), mapWorkInsertSql, (PreparedStatement ps, Object fkId) -> 
      {
         H2ChildMap child = (H2ChildMap) get(fkId);
         
         try
         {
            child.persistChanges(ps);
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      });
   }
   
   /**
    * Load all the child maps from the database, in their corresponding collections in the map.
    */
   private void loadChildMaps()
   {
      Class<?> fkClass = this.keyClass;
      Class<?> childKeyClass = this.key2Class;
      Class<?> childValueClass = this.valueClass;
      String sqlFkType = helper.convertToSql(fkClass);
      String sqlKeyType = helper.convertToSql(childKeyClass);
      String sqlValueType = helper.convertToSql(childValueClass);
      String createSQL = "CREATE TABLE IF NOT EXISTS " + childTableName + " (" +
                         "fkId " + sqlFkType + " NOT NULL, " + 
                         "key " + sqlKeyType + " NOT NULL, " + 
                         "value " + sqlValueType + ");";
      helper.executeSQL(createSQL);
      helper.executeSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_" + childTableName + "__pk ON " +
                        childTableName + " (fkId, key);");

      // the work map table
      String createWorkSQL = "CREATE TABLE IF NOT EXISTS " + childWorkTable + " ( " +
                             "fileId BIGINT NOT NULL, " + 
                             "astId BIGINT NOT NULL, " + 
                             "fkId " + sqlFkType + " NOT NULL, " + 
                             "key " + sqlKeyType + " NOT NULL, " + 
                             "value " + sqlValueType + " NOT NULL);";
      helper.executeSQL(createWorkSQL);
      helper.executeSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_" + childWorkTable + "__pk ON " +
                        childWorkTable + " (fileId, astId, fkId, key);");

      /*
      // load the main table
      Consumer<ResultSet> main = (rs) -> 
      {
         try
         {
            Object key = rs.getObject(1);
            Object value = rs.getObject(2);
            Object fkId = rs.getObject(3);

            H2ChildMap child = (H2ChildMap) this.get(fkId);
            if (child.containsKey(key))
            {
               throw new IllegalStateException("Key already exists!");
            }

            child.putSuper(key, value);
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      };
      helper.executeQuery("select key, value, fkId from " + childTableName, main);
      */
      
      // load the work table
      Consumer<ResultSet> work = (rs) -> 
      {
         try
         {
            Object key = rs.getObject(3);
            Object value = rs.getObject(4);
            Object fkId = rs.getObject(5);

            H2ChildMap child = (H2ChildMap) this.get(fkId);
            /*
            if (child.containsKey(key))
            {
               throw new IllegalStateException("Key already exists!");
            }
            */
            child.putSuper(key, value);
            child.logChange(rs.getLong(1), rs.getLong(2), key, value);
         }
         catch (SQLException e)
         {
            throw new RuntimeException(e);
         }
      };

      helper.executeQuery("select fileId, astId, key, value, fkId from " + childWorkTable, work);
   }
}