]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingDelayedWriteProcessor.java
Export also dynamic issues to issue CSV export file
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / processor / MergingDelayedWriteProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.processor;
13
14 import java.util.concurrent.ConcurrentLinkedQueue;
15
16 import org.simantics.db.AsyncRequestProcessor;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.DelayedWriteRequest;
19 import org.simantics.db.common.utils.Logger;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.request.DelayedWrite;
22
23 final public class MergingDelayedWriteProcessor extends ProcessorBase {
24
25     final long transactionKeepalivePeriod;
26     final ConcurrentLinkedQueue<DelayedWrite> queue = new ConcurrentLinkedQueue<DelayedWrite>(); 
27     final private AsyncRequestProcessor processor;
28
29     boolean hasAlreadyRequest = false;
30
31     public MergingDelayedWriteProcessor(AsyncRequestProcessor processor, long transactionKeepalivePeriod) {
32         this.processor = processor;
33         this.transactionKeepalivePeriod = transactionKeepalivePeriod;
34     }
35
36     class MergedWrite extends DelayedWriteRequest {
37
38         @Override
39         public void perform(WriteGraph graph) throws DatabaseException {
40
41 //            System.out.println("MergedWrite begins");
42
43             while (true) {
44                 
45                 DelayedWrite next = queue.poll();
46                 if(next == null) {
47                     synchronized (MergingDelayedWriteProcessor.this) {
48                         if (transactionKeepalivePeriod > 0) {
49                             try {
50                                 MergingDelayedWriteProcessor.this.wait(transactionKeepalivePeriod);
51                             } catch (InterruptedException e) {
52                                         Logger.defaultLogError(e);
53                             }
54                             next = queue.poll();
55                         }
56                         if(next == null) {
57                             hasAlreadyRequest = false;
58 //                            System.out.println("MergedWrite ends");
59                             return;
60                         }
61                     }
62                 }
63
64 //                System.out.println("MergedWrite executes " + next);
65                 try {
66                     next.perform(graph);
67                 } catch(Throwable t) {
68                         Logger.defaultLogError(t);
69                 }
70
71             }
72
73         }
74
75     }
76
77     @Override
78     public void asyncRequest(DelayedWrite request) {
79         
80         queue.add(request);
81
82         synchronized (this) {
83         
84           if (!hasAlreadyRequest) {
85               processor.asyncRequest(new MergedWrite());
86               hasAlreadyRequest = true;
87           } else {
88               notify();
89           }
90           
91         }
92
93     }
94
95     @Override
96     public String toString() {
97         return "MergingDelayedWriteProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")";
98     }
99
100 }