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.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;
23 final public class MergingDelayedWriteProcessor extends ProcessorBase {
25 final long transactionKeepalivePeriod;
26 final ConcurrentLinkedQueue<DelayedWrite> queue = new ConcurrentLinkedQueue<DelayedWrite>();
27 final private AsyncRequestProcessor processor;
29 boolean hasAlreadyRequest = false;
31 public MergingDelayedWriteProcessor(AsyncRequestProcessor processor, long transactionKeepalivePeriod) {
32 this.processor = processor;
33 this.transactionKeepalivePeriod = transactionKeepalivePeriod;
36 class MergedWrite extends DelayedWriteRequest {
39 public void perform(WriteGraph graph) throws DatabaseException {
41 // System.out.println("MergedWrite begins");
45 DelayedWrite next = queue.poll();
47 synchronized (MergingDelayedWriteProcessor.this) {
48 if (transactionKeepalivePeriod > 0) {
50 MergingDelayedWriteProcessor.this.wait(transactionKeepalivePeriod);
51 } catch (InterruptedException e) {
52 Logger.defaultLogError(e);
57 hasAlreadyRequest = false;
58 // System.out.println("MergedWrite ends");
64 // System.out.println("MergedWrite executes " + next);
67 } catch(Throwable t) {
68 Logger.defaultLogError(t);
78 public void asyncRequest(DelayedWrite request) {
84 if (!hasAlreadyRequest) {
85 processor.asyncRequest(new MergedWrite());
86 hasAlreadyRequest = true;
96 public String toString() {
97 return "MergingDelayedWriteProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")";