]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingWriteOnlyProcessor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / processor / MergingWriteOnlyProcessor.java
index 6c6883dea955ddbf3c8fb50d11912d31cbe50a0c..b29765ba101ceb6a231e17d8a2751e911e7f286f 100644 (file)
-/*******************************************************************************\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 org.simantics.db.common.processor;\r
-\r
-import java.util.concurrent.ConcurrentLinkedQueue;\r
-\r
-import org.simantics.db.AsyncRequestProcessor;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteOnlyGraph;\r
-import org.simantics.db.common.request.WriteOnlyRequest;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.WriteOnly;\r
-\r
-final public class MergingWriteOnlyProcessor extends ProcessorBase {\r
-\r
-    final long transactionKeepalivePeriod;\r
-    final ConcurrentLinkedQueue<WriteOnly> queue = new ConcurrentLinkedQueue<WriteOnly>(); \r
-    final private AsyncRequestProcessor processor;\r
-    final private VirtualGraph vg;\r
-\r
-    boolean hasAlreadyRequest = false;\r
-\r
-    public MergingWriteOnlyProcessor(AsyncRequestProcessor processor, VirtualGraph vg, long transactionKeepalivePeriod) {\r
-        this.processor = processor;\r
-        this.vg = vg;\r
-        this.transactionKeepalivePeriod = transactionKeepalivePeriod;\r
-    }\r
-\r
-    class MergedWrite extends WriteOnlyRequest {\r
-\r
-       public MergedWrite(VirtualGraph vg) {\r
-               super(vg);\r
-               }\r
-       \r
-        @Override\r
-        public void perform(WriteOnlyGraph graph) throws DatabaseException {\r
-\r
-//            System.out.println("MergedWrite begins");\r
-\r
-            while (true) {\r
-                \r
-                WriteOnly next = queue.poll();\r
-                if(next == null) {\r
-                    synchronized (MergingWriteOnlyProcessor.this) {\r
-                        if (transactionKeepalivePeriod > 0) {\r
-                            try {\r
-                                MergingWriteOnlyProcessor.this.wait(transactionKeepalivePeriod);\r
-                            } catch (InterruptedException e) {\r
-                                       Logger.defaultLogError(e);\r
-                            }\r
-                            next = queue.poll();\r
-                        }\r
-                        if(next == null) {\r
-                            hasAlreadyRequest = false;\r
-//                            System.out.println("MergedWrite ends");\r
-                            return;\r
-                        }\r
-                    }\r
-                }\r
-\r
-//                System.out.println("MergedWrite executes " + next);\r
-                try {\r
-                       next.perform(graph);\r
-                } catch(Throwable t) {\r
-                       Logger.defaultLogError(t);\r
-                }\r
-\r
-            }\r
-\r
-        }\r
-\r
-    }\r
-\r
-    @Override\r
-    public void asyncRequest(WriteOnly request) {\r
-        \r
-        queue.add(request);\r
-\r
-        synchronized (this) {\r
-        \r
-          if (!hasAlreadyRequest) {\r
-              processor.asyncRequest(new MergedWrite(vg));\r
-              hasAlreadyRequest = true;\r
-          } else {\r
-              notify();\r
-          }\r
-          \r
-        }\r
-\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        return "MergingWriteOnlyProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")";\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.common.processor;
+
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import org.simantics.db.AsyncRequestProcessor;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteOnlyGraph;
+import org.simantics.db.common.request.WriteOnlyRequest;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.WriteOnly;
+
+final public class MergingWriteOnlyProcessor extends ProcessorBase {
+
+    final long transactionKeepalivePeriod;
+    final ConcurrentLinkedQueue<WriteOnly> queue = new ConcurrentLinkedQueue<WriteOnly>(); 
+    final private AsyncRequestProcessor processor;
+    final private VirtualGraph vg;
+
+    boolean hasAlreadyRequest = false;
+
+    public MergingWriteOnlyProcessor(AsyncRequestProcessor processor, VirtualGraph vg, long transactionKeepalivePeriod) {
+        this.processor = processor;
+        this.vg = vg;
+        this.transactionKeepalivePeriod = transactionKeepalivePeriod;
+    }
+
+    class MergedWrite extends WriteOnlyRequest {
+
+       public MergedWrite(VirtualGraph vg) {
+               super(vg);
+               }
+       
+        @Override
+        public void perform(WriteOnlyGraph graph) throws DatabaseException {
+
+//            System.out.println("MergedWrite begins");
+
+            while (true) {
+                
+                WriteOnly next = queue.poll();
+                if(next == null) {
+                    synchronized (MergingWriteOnlyProcessor.this) {
+                        if (transactionKeepalivePeriod > 0) {
+                            try {
+                                MergingWriteOnlyProcessor.this.wait(transactionKeepalivePeriod);
+                            } catch (InterruptedException e) {
+                                       Logger.defaultLogError(e);
+                            }
+                            next = queue.poll();
+                        }
+                        if(next == null) {
+                            hasAlreadyRequest = false;
+//                            System.out.println("MergedWrite ends");
+                            return;
+                        }
+                    }
+                }
+
+//                System.out.println("MergedWrite executes " + next);
+                try {
+                       next.perform(graph);
+                } catch(Throwable t) {
+                       Logger.defaultLogError(t);
+                }
+
+            }
+
+        }
+
+    }
+
+    @Override
+    public void asyncRequest(WriteOnly request) {
+        
+        queue.add(request);
+
+        synchronized (this) {
+        
+          if (!hasAlreadyRequest) {
+              processor.asyncRequest(new MergedWrite(vg));
+              hasAlreadyRequest = true;
+          } else {
+              notify();
+          }
+          
+        }
+
+    }
+
+    @Override
+    public String toString() {
+        return "MergingWriteOnlyProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")";
+    }
+
+}