X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FNamespaceIndex.java;h=e55e99c6117bd1bf296c6f90a7b31f558ea7be51;hb=ad18bf76b19bec530bc68c6041ab5fb245b8ee4d;hp=521638e8fca24a9ade31f551a1c667e179bd3dd7;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java index 521638e8f..e55e99c61 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java @@ -1,320 +1,262 @@ -/******************************************************************************* - * 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.impl.query; - -import gnu.trove.map.hash.TObjectIntHashMap; - -import java.util.concurrent.Semaphore; - -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.util.URIStringUtils; -import org.simantics.db.common.WriteBindings; -import org.simantics.db.common.exception.DebugException; -import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; - -final public class NamespaceIndex extends StringQuery>> { - -// public ArrayList>> procs = null; - - private NamespaceIndex(final String id) { - super(id); - } - - final static void runner(ReadGraphImpl graph, final String id, final QueryProcessor provider, NamespaceIndex cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure> procedure) { - - NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id); - if(entry == null) { - - entry = new NamespaceIndex(id); - entry.setPending(); - entry.clearResult(provider.querySupport); - entry.putEntry(provider); - - provider.performForEach(graph, entry, parent, listener, procedure); - - } else { - - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList>>(); -// entry.procs.add(procedure); -// provider.registerDependencies(graph, entry, parent, listener, procedure, false); -// return; - } - } - } - provider.performForEach(graph, entry, parent, listener, procedure); - } - - } - - final public static void queryEach(ReadGraphImpl graph, final String id, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure> procedure) { - - final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id); - - if(parent == null && listener == null && entry != null && entry.isReady()) { - entry.performFromCache(graph, provider, procedure); - return; - } - - runner(graph, id, provider, entry, parent, listener, procedure); - - } - - @Override - public NamespaceIndex getEntry(QueryProcessor provider) { - return provider.namespaceIndexMap22.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.namespaceIndexMap22.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.namespaceIndexMap22.remove(id); - } - - final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure> procedure) { - - if(root == 0) { - add2(graph, null); - procedure.execute(graph, null); -// System.err.println("NamespaceIndex[" + id + "]->null"); - return; - } - - final int consistsOf = provider.getConsistsOf(); - final int hasName = provider.getHasName(); - - final TObjectIntHashMap result = new TObjectIntHashMap(); - - Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - - if(isPending()) { - add2(graph, result); - procedure.execute(graph, result); -// System.err.println("NamespaceIndex[" + id + "]->" + result.size()); - } else { - procedure.exception(graph, (Throwable)statusOrException); - } - - } - - @Override - public void finished(ReadGraphImpl graph) { - - dec(graph); - - } - - @Override - public void execute(ReadGraphImpl graph, final int obj) { - - //System.out.println(id + " => " + obj); - - inc(); - - Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, int i) { - - inc(); - - ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, byte[] value) { - - if(value != null) { - - try { - - Binding b = WriteBindings.STRING; - Serializer serializer = b.serializer(); - final String part = (String)serializer.deserialize(value); - - synchronized(result) { - Object previous = result.put(URIStringUtils.escape(part), obj); - // TODO: this is not the most elegant solution - if(previous != null) previous = ""; - } - - } catch (Throwable e) { - if(DebugException.DEBUG) new DebugException(e).printStackTrace(); - } - - } - - dec(graph); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - except(t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - except(t); - dec(graph); - } - - }); - - } - - }); - - } - - @Override - public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure> procedure) { - -// System.err.println("NamespaceIndex " + id); - - if("http://".equals(id) || "http:/".equals(id)) { - index(graph, processor, processor.getRootLibrary(), procedure); - } else { - final String[] parts = URIStringUtils.splitURI(id); - if(parts != null) { - NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure>() { - - @Override - public void execute(ReadGraphImpl graph, TObjectIntHashMap index) { - - if(index != null) { - index(graph, processor, index.get(parts[1]), procedure); - } else { - add2(graph, null); - procedure.execute(graph, null); -// System.err.println("NamespaceIndex[" + id + "]->null"); - } - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - except(t); - procedure.exception(graph, t); - } - - }); - } else { - add2(graph, null); - procedure.execute(graph, null); -// System.err.println("NamespaceIndex[" + id + "]->null"); - } - - } - - } - - @Override - public String toString() { - return "NamespaceIndex[" + id + "]"; - } - - synchronized private void add(TObjectIntHashMap result) { - - throw new Error("Not possible!"); - - } - - private void add2(ReadGraphImpl graph, TObjectIntHashMap result) { - - if(!isPending()) { - new Exception(""+hashCode()).printStackTrace(); - } - - assert(isPending()); - -// ArrayList>> p = null; - - synchronized(this) { - - setResult(result); - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) { -// -// for(InternalProcedure> proc : p) proc.execute(graph, result); -// -// } - - } - - @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure> procedure) { - - assert(isReady()); - - if(handleException(graph, procedure)) return; - - procedure.execute(graph, (TObjectIntHashMap)getResult()); - - } - - @Override - public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) { - - final Semaphore s = new Semaphore(0); - - computeForEach(graph, provider, new InternalProcedure>() { - - @Override - public void execute(ReadGraphImpl graph, TObjectIntHashMap result) { - s.release(); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - throw new Error("Error in recompute.", t); - } - - }); - - while(!s.tryAcquire()) { - provider.resume(graph); - } - - } - -} - +/******************************************************************************* + * 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.impl.query; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.util.URIStringUtils; +import org.simantics.db.common.WriteBindings; +import org.simantics.db.common.exception.DebugException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.InternalProcedure; + +import gnu.trove.map.hash.TObjectIntHashMap; + +final public class NamespaceIndex extends StringQuery>> { + + NamespaceIndex(final String id) { + super(id); + } + + @Override + final public void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } + + final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure> procedure) throws DatabaseException { + + if(root == 0) { + add2(graph, null); + procedure.execute(graph, null); +// System.err.println("NamespaceIndex[" + id + "]->null"); + return; + } + + final int consistsOf = provider.getConsistsOf(); + final int hasName = provider.getHasName(); + + final TObjectIntHashMap result = new TObjectIntHashMap(); + + QueryCache.runnerObjects(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + + if(isPending()) { + add2(graph, result); + procedure.execute(graph, result); +// System.err.println("NamespaceIndex[" + id + "]->" + result.size()); + } else { + procedure.exception(graph, (Throwable)statusOrException); + } + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException { + + //System.out.println(id + " => " + obj); + + inc(); + + QueryCache.runnerObjects(graph, obj, hasName, graph.parent, null, new IntProcedure() { + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + + inc(); + + QueryCache.runnerValueQuery(graph, i, NamespaceIndex.this, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException { + + if(value != null) { + + try { + + Binding b = WriteBindings.STRING; + Serializer serializer = b.serializer(); + final String part = (String)serializer.deserialize(value); + + synchronized(result) { + Object previous = result.put(URIStringUtils.escape(part), obj); + // TODO: this is not the most elegant solution + if(previous != null) previous = ""; + } + + } catch (Throwable e) { + if(DebugException.DEBUG) new DebugException(e).printStackTrace(); + } + + } + + dec(graph); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + except(t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + except(t); + dec(graph); + } + + }); + + } + + }); + + } + + @Override + public Object compute(ReadGraphImpl graph, final InternalProcedure> procedure) throws DatabaseException { + + QueryProcessor processor = graph.processor; + +// System.err.println("NamespaceIndex " + id); + + if("http://".equals(id) || "http:/".equals(id)) { + index(graph, processor, processor.getRootLibrary(), procedure); + } else { + final String[] parts = URIStringUtils.splitURI(id); + if(parts != null) { + QueryCache.runnerNamespaceIndex(graph, parts[0], this, null, new InternalProcedure>() { + + @Override + public void execute(ReadGraphImpl graph, TObjectIntHashMap index) throws DatabaseException { + + if(index != null) { + index(graph, processor, index.get(parts[1]), procedure); + } else { + add2(graph, null); + procedure.execute(graph, null); +// System.err.println("NamespaceIndex[" + id + "]->null"); + } + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + if(DebugException.DEBUG) new DebugException(t).printStackTrace(); + except(t); + procedure.exception(graph, t); + } + + }); + } else { + add2(graph, null); + procedure.execute(graph, null); +// System.err.println("NamespaceIndex[" + id + "]->null"); + } + + } + + return getResult(); + + } + + @Override + public String toString() { + return "NamespaceIndex[" + id + "]"; + } + + synchronized private void add(TObjectIntHashMap result) { + + throw new Error("Not possible!"); + + } + + private void add2(ReadGraphImpl graph, TObjectIntHashMap result) { + + if(!isPending()) { + new Exception(""+hashCode()).printStackTrace(); + } + + assert(isPending()); + +// ArrayList>> p = null; + + synchronized(this) { + + setResult(result); + setReady(); +// p = procs; +// procs = null; + + } + +// if(p != null) { +// +// for(InternalProcedure> proc : p) proc.execute(graph, result); +// +// } + + } + + @Override + public Object performFromCache(ReadGraphImpl graph, InternalProcedure> procedure) throws DatabaseException { + + assert(isReady()); + + if(handleException(graph, procedure)) return (Throwable)statusOrException; + + TObjectIntHashMap result = (TObjectIntHashMap)getResult(); + + procedure.execute(graph, result); + + return result; + + } + + @Override + public void recompute(ReadGraphImpl graph) throws DatabaseException { + + compute(graph, new InternalProcedure>() { + + @Override + public void execute(ReadGraphImpl graph, TObjectIntHashMap result) { + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + if(DebugException.DEBUG) new DebugException(t).printStackTrace(); + throw new Error("Error in recompute.", t); + } + + }); + + } + +} +