# H2Set.java

    /*
    ** Module   : H2Set.java
    ** Abstract : Define a H2-backed set.
    **
    ** 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.*;

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

    /**
     * A H2-backed set, which has any changes to it logged in a separate work table.
     */
    class H2Set
    extends TreeSet
    implements StoredCollection
    {
       /** 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 element's class. */
       private final Class<?> keyClass;

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

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

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

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

       /** The set's work table insert SQL. */
       private final String workInsertSql;

       /** The set's work table clear SQL. */
       private final String workClearSql;

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

          this.tableName = table;
          this.keyClass = keyClass;
          this.helper = helper;

          String sqlKeyType = helper.convertToSql(keyClass);
          String createSQL = "CREATE TABLE IF NOT EXISTS " + tableName +
                             " (key " + sqlKeyType + " PRIMARY KEY);";
          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," +
                                 " key " + sqlKeyType + " NOT NULL);";
          helper.executeSQL(createWorkSQL);
          helper.executeSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_" + workTable + "__pk ON " +
                            workTable + " (fileId, astId, key);");

          /*
          // load the main table
          Consumer<ResultSet> main = (rs) ->
          {
             try
             {
                Object key = rs.getObject(1);
                if (this.contains(key))
                {
                   throw new IllegalStateException("Key already exists!");
                }

                super.add(key);
             }
             catch (SQLException e)
             {
                throw new RuntimeException(e);
             }
          };
          helper.executeQuery("select key from " + tableName, main);
          */

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

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

                super.add(key);
                this.changes.log(rs.getLong(1), rs.getLong(2), key);
             }
             catch (SQLException e)
             {
                throw new RuntimeException(e);
             }
          };
          helper.executeQuery("select fileId, astId, key from " + workTable, work);

          // misc used sqls
          this.insertSql   = "insert into " + tableName + " values (?)";
          this.clearSql    = "delete from " + tableName;

          // misc used work table sqls
          this.workInsertSql    = "INSERT INTO " + workTable + " VALUES (?, ?, ?)";
          this.workClearSql     = "DELETE FROM " + workTable;
       }

       /**
        * Constructor used for sub-classes, which delegate the db-storage to another collection.
        */
       protected H2Set()
       {
          this.resolver = AstSymbolResolver.getResolver();
          this.manager = AstManager.get();

          helper = null;
          keyClass = null;
          tableName = null;
          workTable = null;
          insertSql = null;
          clearSql = null;
          workInsertSql = null;
          workClearSql = null;
       }

       /**
        * Check if the set contains the specified key.
        * <p>
        * If the key 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 set.
        */
       @Override
       public boolean contains(Object key)
       {
          boolean has = super.contains(key);

          if (has)
          {
             logChange(key);
          }

          return has;
       }

       /**
        * Perform an add 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.
        *
        * @return   <code>true</code> if the key already exists.
        */
       @Override
       public boolean add(Object key)
       {
          boolean has = super.add(key);

          logChange(key);

          return has;
       }

       /**
        * Merge the specified collection into this one.  This will rely on {@link #add} to log the
        * access to the set.
        *
        * @param    c
        *           The collection to merge.
        */
       @Override
       public boolean addAll(Collection c)
       {
          boolean changed = false;
          for (Object o : c)
          {
             changed = add(o) || changed;
          }

          return changed;
       }

       /**
        * Remove this key from the set.
        * <p>
        * If the key exists, then all changes related to it will be cleared.
        *
        * @param    key
        *           The key to remove.
        */
       @Override
       public boolean remove(Object key)
       {
          boolean has = super.remove(key);

          if (has)
          {
             changes.remove(key);
          }

          return has;
       }

       /**
        * Clear the set and all its changes.
        */
       @Override
       public void clear()
       {
          super.clear();

          changes.clear();
       }

       /**
        * Persist this set and all the changes to the database.
        */
       @Override
       public void persist()
       {
          // clear the table and save the data
          helper.executeSQL(clearSql);

          helper.persistCollection(this, insertSql, (PreparedStatement ps, Object key) ->
          {
             try
             {
                ps.setObject(1, key);
                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(this);

          changes.persist(workInsertSql,
                          helper,
                          (AstKey key, Object val) -> new Object[] { key.fileId, key.astId, key.key });
       }

       /**
        * Log this access in the set as a change.
        *
        * @param    key
        *           The accessed key.
        */
       private void logChange(Object key)
       {
          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);
       }
    }