X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Fprocessor%2FMergingDelayedWriteProcessor.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Fprocessor%2FMergingDelayedWriteProcessor.java;h=2e5e416261685b12947aacacfab8c21235d3ca4a;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=65a7fc75b0d64b94c9759ec493761d0fb7bf48e8;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingDelayedWriteProcessor.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingDelayedWriteProcessor.java index 65a7fc75b..2e5e41626 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingDelayedWriteProcessor.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingDelayedWriteProcessor.java @@ -1,100 +1,100 @@ -/******************************************************************************* - * 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.WriteGraph; -import org.simantics.db.common.request.DelayedWriteRequest; -import org.simantics.db.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.DelayedWrite; - -final public class MergingDelayedWriteProcessor extends ProcessorBase { - - final long transactionKeepalivePeriod; - final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); - final private AsyncRequestProcessor processor; - - boolean hasAlreadyRequest = false; - - public MergingDelayedWriteProcessor(AsyncRequestProcessor processor, long transactionKeepalivePeriod) { - this.processor = processor; - this.transactionKeepalivePeriod = transactionKeepalivePeriod; - } - - class MergedWrite extends DelayedWriteRequest { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - -// System.out.println("MergedWrite begins"); - - while (true) { - - DelayedWrite next = queue.poll(); - if(next == null) { - synchronized (MergingDelayedWriteProcessor.this) { - if (transactionKeepalivePeriod > 0) { - try { - MergingDelayedWriteProcessor.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(DelayedWrite request) { - - queue.add(request); - - synchronized (this) { - - if (!hasAlreadyRequest) { - processor.asyncRequest(new MergedWrite()); - hasAlreadyRequest = true; - } else { - notify(); - } - - } - - } - - @Override - public String toString() { - return "MergingDelayedWriteProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")"; - } - -} +/******************************************************************************* + * 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.WriteGraph; +import org.simantics.db.common.request.DelayedWriteRequest; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.DelayedWrite; + +final public class MergingDelayedWriteProcessor extends ProcessorBase { + + final long transactionKeepalivePeriod; + final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); + final private AsyncRequestProcessor processor; + + boolean hasAlreadyRequest = false; + + public MergingDelayedWriteProcessor(AsyncRequestProcessor processor, long transactionKeepalivePeriod) { + this.processor = processor; + this.transactionKeepalivePeriod = transactionKeepalivePeriod; + } + + class MergedWrite extends DelayedWriteRequest { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + +// System.out.println("MergedWrite begins"); + + while (true) { + + DelayedWrite next = queue.poll(); + if(next == null) { + synchronized (MergingDelayedWriteProcessor.this) { + if (transactionKeepalivePeriod > 0) { + try { + MergingDelayedWriteProcessor.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(DelayedWrite request) { + + queue.add(request); + + synchronized (this) { + + if (!hasAlreadyRequest) { + processor.asyncRequest(new MergedWrite()); + hasAlreadyRequest = true; + } else { + notify(); + } + + } + + } + + @Override + public String toString() { + return "MergingDelayedWriteProcessor@" + System.identityHashCode(this) + " (based on " + processor + ")"; + } + +}