1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.processor;
14 import java.util.concurrent.ConcurrentLinkedQueue;
16 import org.simantics.db.AsyncRequestProcessor;
17 import org.simantics.db.VirtualGraph;
18 import org.simantics.db.WriteOnlyGraph;
19 import org.simantics.db.common.request.WriteOnlyRequest;
20 import org.simantics.db.common.utils.Logger;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.request.WriteOnly;
24 final public class MergingWriteOnlyProcessor extends ProcessorBase {
26 final long transactionKeepalivePeriod;
27 final ConcurrentLinkedQueue<WriteOnly> queue = new ConcurrentLinkedQueue<WriteOnly>();
28 final private AsyncRequestProcessor processor;
29 final private VirtualGraph vg;
31 boolean hasAlreadyRequest = false;
33 public MergingWriteOnlyProcessor(AsyncRequestProcessor processor, VirtualGraph vg, long transactionKeepalivePeriod) {
34 this.processor = processor;
36 this.transactionKeepalivePeriod = transactionKeepalivePeriod;
39 class MergedWrite extends WriteOnlyRequest {
41 public MergedWrite(VirtualGraph vg) {
46 public void perform(WriteOnlyGraph graph) throws DatabaseException {
48 // System.out.println("MergedWrite begins");
52 WriteOnly next = queue.poll();
54 synchronized (MergingWriteOnlyProcessor.this) {
55 if (transactionKeepalivePeriod > 0) {
57 MergingWriteOnlyProcessor.this.wait(transactionKeepalivePeriod);
58 } catch (InterruptedException e) {
59 Logger.defaultLogError(e);
64 hasAlreadyRequest = false;
65 // System.out.println("MergedWrite ends");
71 // System.out.println("MergedWrite executes " + next);
74 } catch(Throwable t) {
75 Logger.defaultLogError(t);
85 public void asyncRequest(WriteOnly request) {
91 if (!hasAlreadyRequest) {
92 processor.asyncRequest(new MergedWrite(vg));
93 hasAlreadyRequest = true;
103 public String toString() {
104 return "MergingWriteOnlyProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")";