]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/GraphSessionSocket.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / GraphSessionSocket.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/GraphSessionSocket.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/GraphSessionSocket.java
new file mode 100644 (file)
index 0000000..ed6a5ba
--- /dev/null
@@ -0,0 +1,91 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.vtt.simantics.procore.internal;\r
+\r
+import org.simantics.db.SessionReference;\r
+import org.simantics.db.Database.Session;\r
+import org.simantics.db.Database.Session.Information;\r
+import org.simantics.db.Database.Session.Transaction;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+import fi.vtt.simantics.procore.DebugPolicy;\r
+\r
+final class GraphSessionSocket extends GraphSession {\r
+    \r
+    public GraphSessionSocket(SessionImplSocket sessionImpl, SessionReference sessionReference, Session dbSession)\r
+    throws DatabaseException {\r
+        super(sessionImpl, sessionReference, dbSession);\r
+    }\r
+    @Override\r
+    protected ServerInformationImpl getServerInformation()\r
+    throws DatabaseException {\r
+        Information t = dbSession.getInformation();\r
+        return new ServerInformationImpl(t.getServerId(), t.getProtocolId(), t.getDatabaseId(), t.getFirstChangeSetId());\r
+    }\r
+    @Override\r
+    public long askReadTransaction(int thread)\r
+    throws DatabaseException {\r
+        Transaction t = dbSession.askReadTransaction();\r
+        updateLastChangeSetId(thread, t.getHeadChangeSetId(), true);\r
+        return t.getTransactionId();\r
+    }\r
+    @Override\r
+    public long askWriteTransaction(int thread, long transactionId)\r
+    throws DatabaseException {\r
+        Transaction t = dbSession.askWriteTransaction(transactionId);\r
+        updateLastChangeSetId(thread, t.getHeadChangeSetId(), true);\r
+        return t.getTransactionId();\r
+    }\r
+    @Override\r
+    public void endTransaction(long transactionId, boolean write)\r
+    throws DatabaseException {\r
+        long headChangeSetId = dbSession.endTransaction(transactionId);\r
+        updateLastChangeSetId(Integer.MIN_VALUE, headChangeSetId, false);\r
+    }\r
+    @Override\r
+    public void cancelCommit(long transactionId, long csid, byte[] metadata, SynchronizeContextI context)\r
+    throws DatabaseException {\r
+        try {\r
+            assert(null == synchronizeContext);\r
+            synchronizeContext = context;\r
+            long headChangeSetId = dbSession.cancelCommit(transactionId, csid, metadata, context);\r
+            if (csid+1 != headChangeSetId)\r
+                Logger.defaultLogError("Client and server out of synchronisation. Client cs="\r
+                    + (csid+1) + "server cs=" + headChangeSetId);\r
+            metadataCache.addNext(headChangeSetId, metadata);\r
+        } finally {\r
+            synchronizeContext = null;\r
+        }\r
+    }\r
+    @Override\r
+    public void acceptCommit(long transactionId, long csid, byte[] metadata)\r
+    throws DatabaseException {\r
+        dbSession.acceptCommit(transactionId, csid, metadata);\r
+        metadataCache.addNext(csid+1, metadata);\r
+    }\r
+    @Override\r
+    public void stop()\r
+    throws DatabaseException {\r
+        dbSession.close();\r
+    }\r
+    @Override\r
+    public long reserveIds(int count)\r
+    throws DatabaseException {\r
+        Logger.defaultLogTrace("Asking for ids " + count + ".");\r
+        long firstId = dbSession.reserveIds(count);\r
+        Logger.defaultLogTrace("First id is " + firstId + ".");\r
+        if (DebugPolicy.REPORT_CLUSTER_ID_ALLOCATION)\r
+            System.out.println("Client reserves new ids [" + firstId + " - " + (firstId+count-1) + "] from server.");\r
+        return firstId;\r
+    }\r
+}\r