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.impl.query;
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.graph.WriteGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.request.Read;
20 import org.simantics.db.request.ReadExt;
21 import org.simantics.db.request.RequestFlags;
23 final public class ReadEntry<T> extends CacheEntryBase {
25 protected Read<T> request;
27 public ReadEntry(Read<T> request) {
28 this.request = request;
33 return request.hashCode();
37 public Object getOriginalRequest() {
42 public void discard() {
47 final public void addOrSet(AsyncReadGraph graph, Object item) {
49 assert(assertPending());
51 // ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
63 // for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
64 // proc.first.execute(graph, (T)item);
71 final public Query getQuery() {
76 public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
78 QueryProcessor qp = (QueryProcessor)provider;
80 WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
82 ReadGraphImpl graph = write.newSync(entry);
87 T result = request.perform(graph);
88 addOrSet(graph, result);
90 } catch (Throwable t) {
99 public void removeEntry(QueryProcessor processor) {
100 processor.cache.readMap.remove(request);
105 if(request instanceof ReadExt) {
106 return ((ReadExt)request).getType();
108 return RequestFlags.INVALIDATE;
113 public String toString() {
114 if(request == null) return "DISCARDED";
115 else return request.toString() + statusOrException;
122 public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
124 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
129 proc.exception(graph, (Throwable)getResult());
130 } catch (Throwable t) {
135 proc.execute(graph, (T)getResult());
136 } catch (Throwable t) {
142 return (T)getResult();
147 public String toString() {
148 if(request == null) return "DISCARDED";
149 else return request.toString() + " - " + statusOrException;
152 public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
153 if(procedure != null) performFromCache(graph, processor, procedure);
159 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
160 if(request instanceof ReadExt) {
161 return ((ReadExt)request).isImmutable(graph);