package org.umlg.sqlg.structure;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * A transaction scoped cache.
 * Date: 2014/10/04
 * Time: 3:20 PM
 */
public class TransactionCache {

    private Connection connection;
    private Map<ElementPropertyRollback, Object> elementPropertyRollbackFunctions = new WeakHashMap<>();
    private BatchManager batchManager;
//    private Map<RecordId, SqlgVertex> vertexCache = new WeakHashMap<>();
    //true if a schema modification statement has been executed.
    //it is important to know this as schema modification creates exclusive locks.
    //In particular it locks querying the schema itself.
    private boolean schemaModification = false;

    static TransactionCache of(Connection connection, BatchManager batchManager) {
        return new TransactionCache(connection, batchManager);
    }

    private TransactionCache(
            Connection connection,
            BatchManager batchManager) {

        this.connection = connection;
        this.batchManager = batchManager;
    }

    Connection getConnection() {
        return this.connection;
    }

    Map<ElementPropertyRollback, Object> getElementPropertyRollback() {
        return this.elementPropertyRollbackFunctions;
    }

    BatchManager getBatchManager() {
        return this.batchManager;
    }

    void clear() {
        this.elementPropertyRollbackFunctions.clear();
        this.batchManager.clear();
//        this.vertexCache.clear();
        try {
            this.connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * The recordId is not referenced in the SqlgVertex.
     * It is important that the value of the WeakHashMap does not reference the key.
     *
     * @param sqlgGraph
     * @return
     */
    SqlgVertex putVertexIfAbsent(SqlgGraph sqlgGraph, String schema, String table, Long id) {
//        SqlgVertex sqlgVertex = this.vertexCache.get(recordId);
        SqlgVertex sqlgVertex = null;
        if (sqlgVertex == null) {
            sqlgVertex = new SqlgVertex(sqlgGraph, id, schema, table);
//            this.vertexCache.put(recordId, sqlgVertex);
            return sqlgVertex;
        } else {
            return sqlgVertex;
        }
    }

    SqlgVertex putVertexIfAbsent(SqlgVertex sqlgVertex) {
        RecordId vertexRecordId = (RecordId)sqlgVertex.id();
//        SqlgVertex sqlgVertexFromCache = this.vertexCache.get(vertexRecordId);
        SqlgVertex sqlgVertexFromCache = null;
        if (sqlgVertexFromCache == null) {
            //copy the RecordId so that the WeakHashMap value does not reference the key
            SchemaTable schemaTable = vertexRecordId.getSchemaTable();
            RecordId recordId = RecordId.from(SchemaTable.of(schemaTable.getSchema(), schemaTable.getTable()), vertexRecordId.getId());
//            this.vertexCache.put(recordId, sqlgVertex);
            return sqlgVertex;
        } else {
            return sqlgVertexFromCache;
        }
    }

    void add(SqlgVertex sqlgVertex) {
        RecordId vertexRecordId = (RecordId) sqlgVertex.id();
//        if (this.vertexCache.containsKey(vertexRecordId)) {
//            throw new IllegalStateException("The vertex cache should never already contain a new vertex!");
//        } else {
            SchemaTable schemaTable = vertexRecordId.getSchemaTable();
            RecordId recordId = RecordId.from(SchemaTable.of(schemaTable.getSchema(), schemaTable.getTable()), vertexRecordId.getId());
//            this.vertexCache.put(recordId, sqlgVertex);
//        }
    }

}

@@ -426,18 +426,16 @@
 anager;%0A
-//
     priv
@@ -495,24 +495,24 @@
 ashMap%3C%3E();%0A
+
     //true i
@@ -1488,26 +1488,24 @@
 er.clear();%0A
-//
         this
@@ -1518,32 +1518,32 @@
 xCache.clear();%0A
+
         try %7B%0A  
@@ -1966,26 +1966,102 @@
  Long id) %7B%0A
-//
+        RecordId recordId = RecordId.from(SchemaTable.of(schema, table), id);%0A
         Sqlg
@@ -2104,32 +2104,34 @@
 .get(recordId);%0A
+//
         SqlgVert
@@ -2257,18 +2257,16 @@
 table);%0A
-//
         
@@ -2305,32 +2305,32 @@
 d, sqlgVertex);%0A
+
             retu
@@ -2520,34 +2520,32 @@
 qlgVertex.id();%0A
-//
         SqlgVert
@@ -2603,24 +2603,26 @@
 xRecordId);%0A
+//
         Sqlg
@@ -2987,34 +2987,32 @@
 ordId.getId());%0A
-//
             this
@@ -3043,32 +3043,32 @@
 d, sqlgVertex);%0A
+
             retu
@@ -3256,18 +3256,16 @@
 x.id();%0A
-//
         
@@ -3316,18 +3316,16 @@
 dId)) %7B%0A
-//
         
@@ -3424,18 +3424,16 @@
 tex!%22);%0A
-//
         
@@ -3648,18 +3648,16 @@
 tId());%0A
-//
         
@@ -3704,18 +3704,16 @@
 ertex);%0A
-//
         

put vertex cache back in