X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Fprocessor%2FMergingWriteOnlyProcessor.java;h=b29765ba101ceb6a231e17d8a2751e911e7f286f;hb=25e3207c8a1aad3cbe72202750f67f0483ccb1a2;hp=6c6883dea955ddbf3c8fb50d11912d31cbe50a0c;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingWriteOnlyProcessor.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingWriteOnlyProcessor.java index 6c6883dea..b29765ba1 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingWriteOnlyProcessor.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingWriteOnlyProcessor.java @@ -1,107 +1,107 @@ -/******************************************************************************* - * 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 queue = new ConcurrentLinkedQueue(); - 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 + ")"; - } - -} +/******************************************************************************* + * 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 queue = new ConcurrentLinkedQueue(); + 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 + ")"; + } + +}