# H2MapToSet.java

    /*
    ** Module   : H2MapToSet.java
    ** Abstract : Define a H2-backed map, with other sets 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.Consumer;

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

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

       /** The child's set key 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 set insert SQL. */
       private final String setInsertSql;

       /** The child's set clear SQL. */
       private final String setClearSql;

       /** The child's work table insert SQL. */
       private final String setWorkInsertSql;

       /** The child's work table clear SQL. */
       private final String setWorkClearSql;

       /**
        * 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 H2MapToSet(Class<?> keyClass, Class<?> valueClass, String table, DBHelper helper)
       {
          this.keyClass = keyClass;
          this.valueClass = valueClass;
          this.tableName = table;
          this.helper = helper;

          this.childTableName = tableName + "__set";
          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 H2ChildSet(key));
             }
             catch (SQLException e)
             {
                throw new RuntimeException(e);
             }
          });

          loadChildSets();

          this.clearSql  = "delete from " + table;
          this.insertSql = "insert into " + table + " values (?)";

          this.setInsertSql = "insert into " + childTableName + " values (?, ?)";
          this.setClearSql  = "delete from " + childTableName;

          // misc used work table sqls
          this.setWorkInsertSql = "INSERT INTO " + childWorkTable + " VALUES (?, ?, ?, ?)";
          this.setWorkClearSql  = "DELETE FROM " + childWorkTable;
       }

       /**
        * Perform a put operation.
        * <p>
        * The value set will be converted to a {@link H2ChildSet}.  Use a {@link #get} operation
        * to ensure the caller is using the db-backed set.
        * <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 Set<?> put(K key, Set<?> value)
       {
          Set old = remove(key);
          H2ChildSet set = new H2ChildSet(key);
          set.clear();
          set.addAll(value);

          super.put(key, set);

          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, (Set<?>) 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 set, or <code>null</code> if it does not exist.
        */
       @Override
       public Set<?> remove(Object key)
       {
          H2ChildSet old = (H2ChildSet) super.remove(key);
          Set<?> set = null;
          if (old != null)
          {
             set = new TreeSet<>(old);
             old.clear();
          }

          return old == null ? null : set;
       }

       /**
        * 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 (Collection<?> set : values())
          {
             ((H2ChildSet) set).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(setClearSql);
          helper.persistCollection(keySet(), setInsertSql, (PreparedStatement ps, Object fkId) ->
          {
             H2ChildSet child = (H2ChildSet) get(fkId);

             try
             {
                for (Object key : child)
                {
                   ps.setObject(1, fkId);
                   ps.setObject(2, key);
                   ps.addBatch();
                }
             }
             catch (SQLException e)
             {
                throw new RuntimeException(e);
             }
          });

          helper.executeSQL(setWorkClearSql);
          helper.persistCollection(keySet(), setWorkInsertSql, (PreparedStatement ps, Object fkId) ->
          {
             H2ChildSet child = (H2ChildSet) 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 loadChildSets()
       {
          Class<?> fkClass = this.keyClass;
          Class<?> childKeyClass = this.valueClass;
          String sqlFkType = helper.convertToSql(fkClass);
          String sqlKeyType = helper.convertToSql(childKeyClass);
          String createSQL = "CREATE TABLE IF NOT EXISTS " + childTableName + " (" +
                             "fkId " + sqlFkType + " NOT NULL, " +
                             "key " + sqlKeyType + " NOT NULL);";
          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);";
          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 fkId = rs.getObject(2);

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

                child.addSuper(key);
             }
             catch (SQLException e)
             {
                throw new RuntimeException(e);
             }
          };
          helper.executeQuery("select key, fkId from " + childTableName, main);
          */

          // load the work table
          Consumer<ResultSet> work = (rs) ->
          {
             try
             {
                Object key = rs.getObject(3);
                Object fkId = rs.getObject(4);

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

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