1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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 fi.vtt.simantics.procore.internal;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.nio.charset.Charset;
18 import java.nio.file.FileVisitResult;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21 import java.nio.file.SimpleFileVisitor;
22 import java.nio.file.attribute.BasicFileAttributes;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.TreeMap;
27 import java.util.concurrent.CopyOnWriteArrayList;
28 import java.util.concurrent.Semaphore;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import java.util.function.Consumer;
32 import org.eclipse.core.runtime.Platform;
33 import org.simantics.databoard.Bindings;
34 import org.simantics.db.AsyncReadGraph;
35 import org.simantics.db.ChangeSet;
36 import org.simantics.db.DevelopmentKeys;
37 import org.simantics.db.Disposable;
38 import org.simantics.db.ExternalValueSupport;
39 import org.simantics.db.Metadata;
40 import org.simantics.db.MonitorContext;
41 import org.simantics.db.MonitorHandler;
42 import org.simantics.db.ReadGraph;
43 import org.simantics.db.Resource;
44 import org.simantics.db.ResourceSerializer;
45 import org.simantics.db.Session;
46 import org.simantics.db.SessionManager;
47 import org.simantics.db.SessionVariables;
48 import org.simantics.db.VirtualGraph;
49 import org.simantics.db.WriteGraph;
50 import org.simantics.db.authentication.UserAuthenticationAgent;
51 import org.simantics.db.authentication.UserAuthenticator;
52 import org.simantics.db.common.Indexing;
53 import org.simantics.db.common.TransactionPolicyRelease;
54 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
55 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
56 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
57 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
58 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
59 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
60 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
61 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
62 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
63 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
64 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
65 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
66 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
67 import org.simantics.db.common.service.ServiceActivityMonitorImpl;
68 import org.simantics.db.common.service.ServiceActivityUpdaterForWriteTransactions;
69 import org.simantics.db.common.utils.Logger;
70 import org.simantics.db.event.ChangeEvent;
71 import org.simantics.db.event.ChangeListener;
72 import org.simantics.db.event.SessionEventListener;
73 import org.simantics.db.exception.CancelTransactionException;
74 import org.simantics.db.exception.ClusterSetExistException;
75 import org.simantics.db.exception.DatabaseException;
76 import org.simantics.db.exception.ImmutableException;
77 import org.simantics.db.exception.InvalidResourceReferenceException;
78 import org.simantics.db.exception.ResourceNotFoundException;
79 import org.simantics.db.exception.RuntimeDatabaseException;
80 import org.simantics.db.exception.ServiceException;
81 import org.simantics.db.exception.ServiceNotFoundException;
82 import org.simantics.db.impl.BlockingAsyncProcedure;
83 import org.simantics.db.impl.ClusterBase;
84 import org.simantics.db.impl.ClusterI;
85 import org.simantics.db.impl.ClusterTraitsBase;
86 import org.simantics.db.impl.ClusterTranslator;
87 import org.simantics.db.impl.ResourceImpl;
88 import org.simantics.db.impl.TransientGraph;
89 import org.simantics.db.impl.VirtualGraphImpl;
90 import org.simantics.db.impl.graph.DelayedWriteGraph;
91 import org.simantics.db.impl.graph.ReadGraphImpl;
92 import org.simantics.db.impl.graph.WriteGraphImpl;
93 import org.simantics.db.impl.graph.WriteSupport;
94 import org.simantics.db.impl.internal.RandomAccessValueSupport;
95 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
96 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
97 import org.simantics.db.impl.query.QueryCache;
98 import org.simantics.db.impl.query.QueryCacheBase;
99 import org.simantics.db.impl.query.QueryProcessor;
100 import org.simantics.db.impl.query.QueryProcessor.SessionRead;
101 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
102 import org.simantics.db.impl.service.QueryDebug;
103 import org.simantics.db.impl.support.VirtualGraphServerSupport;
104 import org.simantics.db.impl.support.WriteRequestScheduleSupport;
105 import org.simantics.db.procedure.AsyncListener;
106 import org.simantics.db.procedure.AsyncMultiListener;
107 import org.simantics.db.procedure.AsyncMultiProcedure;
108 import org.simantics.db.procedure.AsyncProcedure;
109 import org.simantics.db.procedure.Listener;
110 import org.simantics.db.procedure.ListenerBase;
111 import org.simantics.db.procedure.MultiListener;
112 import org.simantics.db.procedure.MultiProcedure;
113 import org.simantics.db.procedure.Procedure;
114 import org.simantics.db.procedure.SyncListener;
115 import org.simantics.db.procedure.SyncMultiListener;
116 import org.simantics.db.procedure.SyncMultiProcedure;
117 import org.simantics.db.procedure.SyncProcedure;
118 import org.simantics.db.procore.cluster.ClusterImpl;
119 import org.simantics.db.procore.cluster.ClusterTraits;
120 import org.simantics.db.procore.protocol.Constants;
121 import org.simantics.db.procore.protocol.DebugPolicy;
122 import org.simantics.db.request.AsyncMultiRead;
123 import org.simantics.db.request.AsyncRead;
124 import org.simantics.db.request.DelayedWrite;
125 import org.simantics.db.request.DelayedWriteResult;
126 import org.simantics.db.request.ExternalRead;
127 import org.simantics.db.request.MultiRead;
128 import org.simantics.db.request.Read;
129 import org.simantics.db.request.ReadInterface;
130 import org.simantics.db.request.Write;
131 import org.simantics.db.request.WriteInterface;
132 import org.simantics.db.request.WriteOnly;
133 import org.simantics.db.request.WriteOnlyResult;
134 import org.simantics.db.request.WriteResult;
135 import org.simantics.db.request.WriteTraits;
136 import org.simantics.db.service.ByteReader;
137 import org.simantics.db.service.ClusterBuilder;
138 import org.simantics.db.service.ClusterBuilderFactory;
139 import org.simantics.db.service.ClusterControl;
140 import org.simantics.db.service.ClusterSetsSupport;
141 import org.simantics.db.service.ClusterUID;
142 import org.simantics.db.service.ClusteringSupport;
143 import org.simantics.db.service.CollectionSupport;
144 import org.simantics.db.service.DebugSupport;
145 import org.simantics.db.service.DirectQuerySupport;
146 import org.simantics.db.service.EventSupport;
147 import org.simantics.db.service.GraphChangeListenerSupport;
148 import org.simantics.db.service.InitSupport;
149 import org.simantics.db.service.LifecycleSupport;
150 import org.simantics.db.service.ManagementSupport;
151 import org.simantics.db.service.QueryControl;
152 import org.simantics.db.service.SerialisationSupport;
153 import org.simantics.db.service.ServerInformation;
154 import org.simantics.db.service.ServiceActivityMonitor;
155 import org.simantics.db.service.SessionEventSupport;
156 import org.simantics.db.service.SessionMonitorSupport;
157 import org.simantics.db.service.SessionUserSupport;
158 import org.simantics.db.service.StatementSupport;
159 import org.simantics.db.service.TransactionPolicySupport;
160 import org.simantics.db.service.TransactionSupport;
161 import org.simantics.db.service.TransferableGraphSupport;
162 import org.simantics.db.service.UndoRedoSupport;
163 import org.simantics.db.service.VirtualGraphSupport;
164 import org.simantics.db.service.XSupport;
165 import org.simantics.layer0.Layer0;
166 import org.simantics.utils.DataContainer;
167 import org.simantics.utils.Development;
168 import org.simantics.utils.threads.logger.ITask;
169 import org.simantics.utils.threads.logger.ThreadLogger;
170 import org.slf4j.LoggerFactory;
172 import gnu.trove.procedure.TLongProcedure;
173 import gnu.trove.set.hash.TLongHashSet;
176 public abstract class SessionImplSocket implements Session, WriteRequestScheduleSupport {
178 private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SessionImplSocket.class);
180 protected static final boolean DEBUG = false;
182 private static final boolean DIAGNOSTICS = false;
184 private TransactionPolicySupport transactionPolicy;
186 final private ClusterControl clusterControl;
188 final protected BuiltinSupportImpl builtinSupport;
189 final protected VirtualGraphServerSupportImpl virtualGraphServerSupport;
190 final protected ClusterSetsSupport clusterSetsSupport;
191 final protected LifecycleSupportImpl lifecycleSupport;
193 protected QuerySupportImpl querySupport;
194 protected ResourceSupportImpl resourceSupport;
195 protected WriteSupport writeSupport;
196 public ClusterTranslator clusterTranslator;
198 boolean dirtyPrimitives = false;
200 public static final int SERVICE_MODE_CREATE = 2;
201 public static final int SERVICE_MODE_ALLOW = 1;
202 public int serviceMode = 0;
203 public boolean createdImmutableClusters = false;
204 public TLongHashSet createdClusters = new TLongHashSet();
209 * The service locator maintained by the workbench. These services are
210 * initialized during workbench during the <code>init</code> method.
212 final protected ServiceLocatorImpl serviceLocator = new ServiceLocatorImpl();
214 final public ResourceSerializerImpl resourceSerializer = new ResourceSerializerImpl();
216 final CopyOnWriteArrayList<ChangeListener> changeListeners2 = new CopyOnWriteArrayList<ChangeListener>();
218 final CopyOnWriteArrayList<ChangeListener> metadataListeners = new CopyOnWriteArrayList<ChangeListener>();
220 final CopyOnWriteArrayList<SessionEventListener> eventListeners = new CopyOnWriteArrayList<SessionEventListener>();
222 final HashSet<Thread> sessionThreads = new HashSet<Thread>();
224 final BijectionMap<MonitorContext, MonitorHandler> monitorContexts = new BijectionMap<MonitorContext, MonitorHandler>();
226 final protected State state = new State();
228 protected GraphSession graphSession = null;
230 protected SessionManager sessionManagerImpl = null;
232 protected UserAuthenticationAgent authAgent = null;
234 protected UserAuthenticator authenticator = null;
236 protected Resource user = null;
238 protected ClusterStream clusterStream = null;
240 protected SessionRequestManager requestManager = null;
242 public ClusterTable clusterTable = null;
244 public QueryProcessor queryProvider2 = null;
246 ClientChangesImpl clientChanges = null;
248 MonitorHandler[] monitorHandlers = new MonitorHandler[0];
250 // protected long newClusterId = Constants.NullClusterId;
252 protected int flushCounter = 0;
254 protected boolean writeOnly = false;
256 WriteState<?> writeState = null;
257 WriteStateBase<?> delayedWriteState = null;
258 protected Resource defaultClusterSet = null; // If not null then used for newResource().
260 public SessionImplSocket(SessionManager sessionManagerImpl, UserAuthenticationAgent authAgent) {
262 // if (authAgent == null)
263 // throw new IllegalArgumentException("null authentication agent");
265 File t = StaticSessionProperties.virtualGraphStoragePath;
268 this.clusterTable = new ClusterTable(this, t);
269 this.builtinSupport = new BuiltinSupportImpl(this);
270 this.sessionManagerImpl = sessionManagerImpl;
272 // this.authAgent = authAgent;
274 serviceLocator.registerService(Session.class, this);
275 serviceLocator.registerService(InitSupport.class, new InitSupportImpl(this));
276 serviceLocator.registerService(ManagementSupport.class, new ManagementSupportImpl(this));
277 serviceLocator.registerService(QueryControl.class, new QueryControlImpl(this));
278 serviceLocator.registerService(SessionUserSupport.class, new SessionUserSupportImpl(this));
279 serviceLocator.registerService(GraphChangeListenerSupport.class, new GraphChangeListenerSupportImpl(this));
280 serviceLocator.registerService(SessionEventSupport.class, new SessionEventSupportImpl(this));
281 serviceLocator.registerService(SerialisationSupport.class, new SerialisationSupportImpl(this));
282 serviceLocator.registerService(UndoRedoSupport.class, new UndoRedoSupportImpl(this));
283 serviceLocator.registerService(ClusteringSupport.class, new ClusteringSupportImpl(this));
284 serviceLocator.registerService(TransactionSupport.class, new TransactionSupportImpl(this));
285 serviceLocator.registerService(SessionMonitorSupport.class, new SessionMonitorSupportImpl(this));
286 serviceLocator.registerService(TransferableGraphSupport.class, new TransferableGraphSupportImpl(this));
287 serviceLocator.registerService(QueryDebug.class, new QueryDebugImpl(this));
288 serviceLocator.registerService(CollectionSupport.class, new CollectionSupportImpl(this));
289 serviceLocator.registerService(StatementSupport.class, new StatementSupportImpl(this));
290 serviceLocator.registerService(DirectQuerySupport.class, new DirectQuerySupportImpl(this));
291 serviceLocator.registerService(XSupport.class, new XSupportImpl(this));
292 serviceLocator.registerService(DebugSupport.class, new DebugSupportImpl());
293 serviceLocator.registerService(ExternalValueSupport.class, new ExternalValueSupportImpl(this));
294 serviceLocator.registerService(RandomAccessValueSupport.class, new RandomAccessValueSupportImpl());
295 serviceLocator.registerService(ServiceActivityMonitor.class, new ServiceActivityMonitorImpl());
296 serviceLocator.registerService(EventSupport.class, new EventSupportImpl());
297 ServiceActivityUpdaterForWriteTransactions.register(this);
299 this.virtualGraphServerSupport = new VirtualGraphServerSupportImpl(this, t);
300 serviceLocator.registerService(VirtualGraphSupport.class, virtualGraphServerSupport);
301 serviceLocator.registerService(VirtualGraphServerSupport.class, virtualGraphServerSupport);
302 this.lifecycleSupport = new LifecycleSupportImpl(this);
303 serviceLocator.registerService(LifecycleSupport.class, lifecycleSupport);
304 this.transactionPolicy = new TransactionPolicyRelease();
305 serviceLocator.registerService(TransactionPolicySupport.class, transactionPolicy);
306 this.clusterControl = new ClusterControlImpl(this);
307 serviceLocator.registerService(ClusterControl.class, clusterControl);
308 this.clusterSetsSupport = new ClusterSetsSupportImpl2(); // Using same path as virtual graphs.
309 this.clusterSetsSupport.setReadDirectory(t.toPath());
310 this.clusterSetsSupport.updateWriteDirectory(t.toPath());
311 serviceLocator.registerService(ClusterSetsSupport.class, clusterSetsSupport);
321 public Resource getRootLibrary() {
322 return queryProvider2.getRootLibraryResource();
325 void refresh(int thread, ClusterUID[] clusterUID, long csid) throws DatabaseException {
326 if (!graphSession.dbSession.refreshEnabled())
329 getClusterTable().refresh(csid, this, clusterUID);
330 } catch (Throwable t) {
331 Logger.defaultLogError("Refesh failed.", t);
335 static final class TaskHelper {
336 private final String name;
337 private Object result;
338 final Semaphore sema = new Semaphore(0);
339 private Throwable throwable = null;
340 final Consumer<DatabaseException> callback = e -> {
341 synchronized (TaskHelper.this) {
345 final Procedure<Object> proc = new Procedure<Object>() {
347 public void execute(Object result) {
348 callback.accept(null);
351 public void exception(Throwable t) {
352 if (t instanceof DatabaseException)
353 callback.accept((DatabaseException)t);
355 callback.accept(new DatabaseException("" + name + "operation failed.", t));
358 final WriteTraits writeTraits = new WriteTraits() {};
359 TaskHelper(String name) {
362 @SuppressWarnings("unchecked")
366 void setResult(Object result) {
367 this.result = result;
369 // Throwable throwableGet() {
372 synchronized void throwableSet(Throwable t) {
375 // void throwableSet(String t) {
376 // throwable = new InternalException("" + name + " operation failed. " + t);
378 synchronized void throwableCheck()
379 throws DatabaseException {
380 if (null != throwable)
381 if (throwable instanceof DatabaseException)
382 throw (DatabaseException)throwable;
384 throw new DatabaseException("Undo operation failed.", throwable);
386 void throw_(String message)
387 throws DatabaseException {
388 throw new DatabaseException("" + name + " operation failed. " + message);
394 private ListenerBase getListenerBase(Object procedure) {
395 if (procedure instanceof ListenerBase)
396 return (ListenerBase) procedure;
401 public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify) {
402 scheduleRequest(request, callback, notify, null);
406 * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.Write, org.simantics.utils.datastructures.Callback, java.util.concurrent.Semaphore, java.lang.Boolean)
409 public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
411 assert (request != null);
413 if(Development.DEVELOPMENT) {
415 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
416 System.err.println("schedule write '" + request + "'");
417 } catch (Throwable t) {
418 Logger.defaultLogError(t);
422 requestManager.scheduleWrite(new SessionTask(null) {
425 public void run0(int thread) {
427 if(Development.DEVELOPMENT) {
429 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
430 System.err.println("perform write '" + request + "'");
431 } catch (Throwable t) {
432 Logger.defaultLogError(t);
436 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
438 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
443 Disposable.safeDispose(clientChanges);
444 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
446 VirtualGraph vg = getProvider(request.getProvider());
448 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
449 writeState = new WriteState<Object>(writer, request, notify, new Procedure<Object>() {
452 public void execute(Object result) {
453 if(callback != null) callback.accept(null);
457 public void exception(Throwable t) {
458 if(callback != null) callback.accept((DatabaseException)t);
463 assert (null != writer);
464 // writer.state.barrier.inc();
467 request.perform(writer);
468 assert (null != writer);
469 } catch (Throwable t) {
470 if (!(t instanceof CancelTransactionException))
471 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", t);
472 writeState.except(t);
474 // writer.state.barrier.dec();
475 // writer.waitAsync(request);
479 assert(!queryProvider2.cache.dirty);
481 } catch (Throwable e) {
483 // Log it first, just to be safe that the error is always logged.
484 if (!(e instanceof CancelTransactionException))
485 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
487 // writeState.getGraph().state.barrier.dec();
488 // writeState.getGraph().waitAsync(request);
490 writeState.except(e);
493 // // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
494 // // All we can do here is to log those, can't really pass them anywhere.
495 // if (callback != null) {
496 // if(e instanceof DatabaseException) callback.run((DatabaseException)e);
497 // else callback.run(new DatabaseException(e));
499 // } catch (Throwable e2) {
500 // Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", e2);
502 // boolean empty = clusterStream.reallyFlush();
503 // int callerThread = -1;
504 // ClientChangesImpl cs = new ClientChangesImpl(SessionImplSocket.this);
505 // SynchronizeContext context = new SynchronizeContext(callerThread, SessionImplSocket.this, cs, 1);
507 // // Send all local changes to server so it can calculate correct reverse change set.
508 // // This will call clusterStream.accept().
509 // state.cancelCommit(context, clusterStream);
511 // if (!context.isOk()) // this is a blocking operation
512 // throw new InternalException("Cancel failed. This should never happen.");
513 // getQueryProvider2().performDirtyUpdates(writeState.getGraph());
515 // state.cancelCommit2(context, clusterStream);
516 // } catch (DatabaseException e3) {
517 // Logger.defaultLogError("Write request cancel caused an unexpected error.", e3);
519 // clusterStream.setOff(false);
520 // clientChanges = new ClientChangesImpl(SessionImplSocket.this);
525 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
531 if(Development.DEVELOPMENT) {
533 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
534 System.err.println("finish write '" + request + "'");
535 } catch (Throwable t) {
536 Logger.defaultLogError(t);
546 public <T> void scheduleRequest(final WriteResult<T> request, final Procedure<T> procedure, final Semaphore notify) {
547 scheduleRequest(request, procedure, notify, null);
551 * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.WriteResult, org.simantics.db.procedure.Procedure, java.util.concurrent.Semaphore, java.lang.Boolean)
554 public <T> void scheduleRequest(final WriteResult<T> request, final Procedure<T> procedure, final Semaphore notify, Boolean combine) {
556 assert (request != null);
558 requestManager.scheduleWrite(new SessionTask(null) {
561 public void run0(int thread) {
563 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
565 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
568 Disposable.safeDispose(clientChanges);
569 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
571 VirtualGraph vg = getProvider(request.getProvider());
572 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
575 WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, procedure);
576 writeState = writeStateT;
578 assert (null != writer);
579 // writer.state.barrier.inc();
580 writeStateT.setResult(request.perform(writer));
581 assert (null != writer);
583 // writer.state.barrier.dec();
584 // writer.waitAsync(null);
586 } catch (Throwable e) {
588 // writer.state.barrier.dec();
589 // writer.waitAsync(null);
591 writeState.except(e);
593 // state.stopWriteTransaction(clusterStream);
595 // } catch (Throwable e) {
596 // // Log it first, just to be safe that the error is always logged.
597 // Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
600 // // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
601 // // All we can do here is to log those, can't really pass them anywhere.
602 // if (procedure != null) {
603 // if(e instanceof DatabaseException) procedure.exception((DatabaseException)e);
604 // else procedure.exception(new DatabaseException(e));
606 // } catch (Throwable e2) {
607 // Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", e2);
610 // clientChanges = new ClientChangesImpl(SessionImplSocket.this);
612 // state.stopWriteTransaction(clusterStream);
615 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
618 // if(notify != null) notify.release();
628 public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify) {
629 scheduleRequest(request, callback, notify, null);
633 * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.DelayedWrite, org.simantics.utils.datastructures.Callback, java.util.concurrent.Semaphore, java.lang.Boolean)
636 public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
638 final ITask total = ThreadLogger.task("ScheduleDelayedWrite");
640 assert (request != null);
642 requestManager.scheduleWrite(new SessionTask(null) {
645 public void run0(int thread) {
646 fireSessionVariableChange(SessionVariables.QUEUED_READS);
648 Procedure<Object> stateProcedure = new Procedure<Object>() {
650 public void execute(Object result) {
651 if (callback != null)
652 callback.accept(null);
655 public void exception(Throwable t) {
656 if (callback != null) {
657 if (t instanceof DatabaseException) callback.accept((DatabaseException) t);
658 else callback.accept(new DatabaseException(t));
660 Logger.defaultLogError("Unhandled exception", t);
664 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
665 delayedWriteState = new WriteStateBase<Object>(request, notify, stateProcedure);
666 DelayedWriteGraph dwg = null;
667 // newGraph.state.barrier.inc();
670 dwg = new DelayedWriteGraph(newGraph);
671 request.perform(dwg);
672 } catch (Throwable e) {
673 delayedWriteState.except(e);
678 // newGraph.state.barrier.dec();
679 // newGraph.waitAsync(request);
680 fireSessionVariableChange(SessionVariables.QUEUED_READS);
683 delayedWriteState = null;
685 ITask task2 = ThreadLogger.task("DelayedWriteCommit");
686 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
689 Disposable.safeDispose(clientChanges);
690 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
694 VirtualGraph vg = getProvider(request.getProvider());
695 WriteSupport writeSupport = vg != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
697 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
699 writeState = new WriteState<Object>(writer, request, notify, stateProcedure);
701 assert (null != writer);
702 // writer.state.barrier.inc();
706 dwg.commit(writer, request);
708 if(defaultClusterSet != null) {
709 XSupport xs = getService(XSupport.class);
710 defaultClusterSet = xs.convertDelayedResourceToResource(defaultClusterSet);
711 ClusteringSupport cs = getService(ClusteringSupport.class);
712 clusterSetsSupport.put(defaultClusterSet.getResourceId(), cs.getCluster(defaultClusterSet));
715 // This makes clusters available from server
716 clusterStream.reallyFlush();
718 releaseWriteOnly(writer);
719 handleUpdatesAndMetadata(writer);
721 } catch (ServiceException e) {
722 // writer.state.barrier.dec();
723 // writer.waitAsync(null);
725 // These shall be requested from server
726 clusterTable.removeWriteOnlyClusters();
727 // This makes clusters available from server
728 clusterStream.reallyFlush();
730 releaseWriteOnly(writer);
731 writeState.except(e);
733 // Debugging & Profiling
734 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
744 public <T> void scheduleRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure, final Semaphore notify) {
745 scheduleRequest(request, procedure, notify, null);
749 * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.DelayedWriteResult, org.simantics.db.procedure.Procedure, java.util.concurrent.Semaphore, java.lang.Boolean)
752 public <T> void scheduleRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure, final Semaphore notify, Boolean combine) {
753 throw new Error("Not implemented");
756 protected ClusterImpl getNewResourceCluster() throws DatabaseException {
757 ClusterImpl cluster = clusterTable.getNewResourceCluster(clusterTranslator, graphSession, writeOnly);
758 if((serviceMode & SERVICE_MODE_CREATE) > 0) {
759 createdClusters.add(cluster.clusterId);
764 class WriteOnlySupport implements WriteSupport {
766 ClusterStream stream;
767 ClusterImpl currentCluster;
769 public WriteOnlySupport() {
770 this.stream = clusterStream;
774 public void claim(VirtualGraph provider, Resource subject, Resource predicate, Resource object) throws ServiceException {
775 claim(provider, ((ResourceImpl)subject).id, ((ResourceImpl)predicate).id, ((ResourceImpl)object).id );
779 public void claim(VirtualGraph provider, int s, int p, int o) throws ServiceException {
781 ClusterImpl cluster = clusterTable.getClusterByResourceKey(s);
783 if (cluster.getImmutable() && (serviceMode & SessionImplSocket.SERVICE_MODE_ALLOW) == 0)
784 if(s != queryProvider2.getRootLibrary())
785 throw new ImmutableException("Trying to modify immutable resource key=" + s);
788 maintainCluster(cluster, cluster.addRelation(s, p, o, clusterTranslator));
789 } catch (DatabaseException e) {
790 Logger.defaultLogError(e);
794 clientChanges.invalidate(s);
796 if (cluster.isWriteOnly())
798 queryProvider2.updateStatements(s, p);
803 public void claimValue(VirtualGraph provider, Resource resource, byte[] value) throws DatabaseException {
804 claimValue(provider, ((ResourceImpl)resource).id, value, value.length);
808 public void claimValue(VirtualGraph provider, int rid, byte[] value, int length) {
810 ClusterImpl cluster = clusterTable.getClusterByResourceKey(rid);
812 maintainCluster(cluster, cluster.setValue(rid, value, length, clusterTranslator));
813 } catch (DatabaseException e) {
814 Logger.defaultLogError(e);
817 clientChanges.invalidate(rid);
819 if (cluster.isWriteOnly())
821 queryProvider2.updateValue(rid);
826 public void claimValue(VirtualGraph provider, Resource resource, ByteReader reader, int amount) throws DatabaseException {
829 claimValue(provider,resource, reader.readBytes(null, amount));
833 byte[] bytes = new byte[65536];
835 int rid = ((ResourceImpl)resource).id;
836 ClusterImpl cluster = clusterTable.getClusterByResourceKey(rid);
840 int block = Math.min(left, 65536);
841 reader.readBytes(bytes, block);
842 maintainCluster(cluster, cluster.modiValueEx(rid, amount-left, block, bytes, 0, clusterTranslator));
845 } catch (DatabaseException e) {
846 Logger.defaultLogError(e);
849 clientChanges.invalidate(rid);
851 if (cluster.isWriteOnly())
853 queryProvider2.updateValue(rid);
857 private void maintainCluster(ClusterImpl before, ClusterI after_) {
858 if(after_ != null && after_ != before) {
859 ClusterImpl after = (ClusterImpl)after_;
860 if(currentCluster == before) {
861 currentCluster = after;
863 clusterTable.replaceCluster(after);
867 public int createResourceKey(int foreignCounter) throws DatabaseException {
868 if(currentCluster == null) {
869 currentCluster = getNewResourceCluster();
871 if(currentCluster.getNumberOfResources(clusterTranslator) == ClusterTable.CLUSTER_FILL_SIZE) {
872 ClusterWriteOnly newCluster = (ClusterWriteOnly)getNewResourceCluster();
873 newCluster.foreignLookup = new byte[foreignCounter];
874 currentCluster = newCluster;
876 System.err.println("foreignLookup " + currentCluster + " " + foreignCounter);
878 return currentCluster.createResource(clusterTranslator);
882 public Resource createResource(VirtualGraph provider) throws DatabaseException {
883 if(currentCluster == null) {
884 if (null != defaultClusterSet) {
885 ResourceImpl result = getNewResource(defaultClusterSet);
886 currentCluster = clusterTable.getClusterByResourceKey(result.id);
889 currentCluster = getNewResourceCluster();
892 if(currentCluster.getNumberOfResources(clusterTranslator) >= ClusterTable.CLUSTER_FILL_SIZE) {
893 if (null != defaultClusterSet) {
894 ResourceImpl result = getNewResource(defaultClusterSet);
895 currentCluster = clusterTable.getClusterByResourceKey(result.id);
898 currentCluster = getNewResourceCluster();
901 return new ResourceImpl(resourceSupport, currentCluster.createResource(clusterTranslator));
905 public Resource createResource(VirtualGraph provider, long clusterId)
906 throws DatabaseException {
907 return getNewResource(clusterId);
911 public Resource createResource(VirtualGraph provider, Resource clusterSet)
912 throws DatabaseException {
913 return getNewResource(clusterSet);
917 public void createClusterSet(VirtualGraph provider, Resource clusterSet)
918 throws DatabaseException {
919 getNewClusterSet(clusterSet);
923 public boolean hasClusterSet(VirtualGraph dummy, Resource clusterSet)
924 throws ServiceException {
925 return containsClusterSet(clusterSet);
928 public void selectCluster(long cluster) {
929 currentCluster = clusterTable.getClusterByClusterId(cluster);
930 long setResourceId = clusterSetsSupport.getSet(cluster);
931 clusterSetsSupport.put(setResourceId, cluster);
935 public Resource setDefaultClusterSet(Resource clusterSet)
936 throws ServiceException {
937 Resource result = setDefaultClusterSet4NewResource(clusterSet);
938 if(clusterSet != null) {
939 long id = clusterSetsSupport.get(clusterSet.getResourceId());
940 currentCluster = clusterTable.getClusterByClusterId(id);
943 currentCluster = null;
949 public void denyValue(VirtualGraph provider, Resource resource) throws ServiceException {
951 provider = getProvider(provider);
952 if (null == provider) {
953 int key = ((ResourceImpl)resource).id;
957 ClusterI cluster = clusterTable.getClusterProxyByResourceKey(key);
958 // if (cluster.getImmutable() && (serviceMode & SessionImplSocket.SERVICE_MODE_ALLOW) == 0)
959 // if(key != queryProvider2.getRootLibrary())
960 // throw new ImmutableException("Trying to modify immutable resource key=" + key);
963 // cluster.removeValue(key, clusterTranslator);
964 // } catch (DatabaseException e) {
965 // Logger.defaultLogError(e);
969 clusterTable.writeOnlyInvalidate(cluster);
972 int sResourceKey = ClusterTraits.getResourceIndexFromResourceKey(key);
973 clusterTranslator.addStatementIndex(cluster, sResourceKey, cluster.getClusterUID(), ClusterChange.DELETE_OPERATION);
974 clusterTranslator.removeValue(cluster);
975 } catch (DatabaseException e) {
976 Logger.defaultLogError(e);
979 queryProvider2.invalidateResource(key);
980 clientChanges.invalidate(key);
983 ((VirtualGraphImpl)provider).denyValue(((ResourceImpl) resource).id);
984 queryProvider2.updateValue(querySupport.getId(resource));
985 clientChanges.claimValue(resource);
992 public void flush(boolean intermediate) {
993 throw new UnsupportedOperationException();
997 public void flushCluster() {
998 clusterTable.flushCluster(graphSession);
999 if(defaultClusterSet != null) {
1000 clusterSetsSupport.put(defaultClusterSet.getResourceId(), Constants.NewClusterId);
1002 currentCluster = null;
1006 public void flushCluster(Resource r) {
1007 throw new UnsupportedOperationException("flushCluster resource " + r);
1015 public boolean removeStatement(VirtualGraph provider, Resource subject, Resource predicate,
1018 int s = ((ResourceImpl)subject).id;
1019 int p = ((ResourceImpl)predicate).id;
1020 int o = ((ResourceImpl)object).id;
1022 provider = getProvider(provider);
1023 if (null == provider) {
1025 ClusterI cluster = clusterTable.getClusterProxyByResourceKey(s);
1026 clusterTable.writeOnlyInvalidate(cluster);
1030 int sResourceKey = ClusterTraits.getResourceIndexFromResourceKey(s);
1032 ClusterI pc = clusterTable.getClusterProxyByResourceKey(p);
1033 ClusterI oc = clusterTable.getClusterProxyByResourceKey(o);
1035 clusterTranslator.addStatementIndex(cluster, sResourceKey, cluster.getClusterUID(), ClusterChange.REMOVE_OPERATION);
1036 clusterTranslator.addStatementIndex(cluster, p, pc.getClusterUID(), ClusterStream.NULL_OPERATION);
1037 clusterTranslator.addStatementIndex(cluster, o, oc.getClusterUID(), ClusterStream.NULL_OPERATION);
1038 clusterTranslator.removeStatement(cluster);
1040 queryProvider2.invalidateResource(s);
1041 clientChanges.invalidate(s);
1043 } catch (DatabaseException e) {
1045 Logger.defaultLogError(e);
1053 ((VirtualGraphImpl)provider).deny(s, p, o);
1054 queryProvider2.invalidateResource(s);
1055 clientChanges.invalidate(s);
1064 public void setValue(VirtualGraph provider, Resource resource, byte[] value) {
1065 throw new UnsupportedOperationException();
1069 public boolean writeOnly() {
1074 public void performWriteRequest(WriteGraph graph, Write request) throws DatabaseException {
1075 writeSupport.performWriteRequest(graph, request);
1079 public <T> T performWriteRequest(WriteGraph graph, WriteResult<T> request) throws DatabaseException {
1080 throw new UnsupportedOperationException();
1084 public void performWriteRequest(WriteGraph graph, WriteOnly request) throws DatabaseException {
1085 throw new UnsupportedOperationException();
1089 public <T> void addMetadata(Metadata data) throws ServiceException {
1090 writeSupport.addMetadata(data);
1094 public <T extends Metadata> T getMetadata(Class<T> clazz) throws ServiceException {
1095 return writeSupport.getMetadata(clazz);
1099 public TreeMap<String, byte[]> getMetadata() {
1100 return writeSupport.getMetadata();
1104 public void commitDone(WriteTraits writeTraits, long csid) {
1105 writeSupport.commitDone(writeTraits, csid);
1109 public void clearUndoList(WriteTraits writeTraits) {
1110 writeSupport.clearUndoList(writeTraits);
1113 public int clearMetadata() {
1114 return writeSupport.clearMetadata();
1118 public void startUndo() {
1119 writeSupport.startUndo();
1123 class VirtualWriteOnlySupport implements WriteSupport {
1126 // public void addStatement(VirtualGraph provider, Resource subject, Resource predicate,
1127 // Resource object) {
1128 // throw new UnsupportedOperationException();
1132 public void claim(VirtualGraph provider, Resource subject, Resource predicate, Resource object) {
1134 TransientGraph impl = (TransientGraph)provider;
1135 impl.claim(querySupport.getId(subject), querySupport.getId(predicate), querySupport.getId(object));
1136 getQueryProvider2().updateStatements(querySupport.getId(subject), querySupport.getId(predicate));
1137 clientChanges.claim(subject, predicate, object);
1142 public void claim(VirtualGraph provider, int subject, int predicate, int object) {
1144 TransientGraph impl = (TransientGraph)provider;
1145 impl.claim(subject, predicate, object);
1146 getQueryProvider2().updateStatements(subject, predicate);
1147 clientChanges.claim(subject, predicate, object);
1152 public void claimValue(VirtualGraph provider, Resource resource, byte[] value) throws DatabaseException {
1153 claimValue(provider, ((ResourceImpl)resource).id, value, value.length);
1157 public void claimValue(VirtualGraph provider, int resource, byte[] value, int length) {
1158 ((VirtualGraphImpl)provider).claimValue(resource, value, length);
1159 getQueryProvider2().updateValue(resource);
1160 clientChanges.claimValue(resource);
1164 public void claimValue(VirtualGraph provider, Resource resource, ByteReader reader, int amount) throws DatabaseException {
1165 byte[] value = reader.readBytes(null, amount);
1166 claimValue(provider, resource, value);
1170 public Resource createResource(VirtualGraph provider) {
1171 TransientGraph impl = (TransientGraph)provider;
1172 return impl.getResource(impl.newResource(false));
1176 public Resource createResource(VirtualGraph provider, long clusterId) {
1177 throw new UnsupportedOperationException();
1181 public Resource createResource(VirtualGraph provider, Resource clusterSet)
1182 throws DatabaseException {
1183 throw new UnsupportedOperationException();
1187 public void createClusterSet(VirtualGraph provider, Resource clusterSet)
1188 throws DatabaseException {
1189 throw new UnsupportedOperationException();
1193 public boolean hasClusterSet(VirtualGraph provider, Resource clusterSet)
1194 throws ServiceException {
1195 throw new UnsupportedOperationException();
1199 public Resource setDefaultClusterSet(Resource clusterSet)
1200 throws ServiceException {
1205 public void denyValue(VirtualGraph provider, Resource resource) {
1206 ((VirtualGraphImpl)provider).denyValue(((ResourceImpl) resource).id);
1207 getQueryProvider2().updateValue(querySupport.getId(resource));
1208 // NOTE: this only keeps track of value changes by-resource.
1209 clientChanges.claimValue(resource);
1213 public void flush(boolean intermediate) {
1214 throw new UnsupportedOperationException();
1218 public void flushCluster() {
1219 throw new UnsupportedOperationException();
1223 public void flushCluster(Resource r) {
1224 throw new UnsupportedOperationException("Resource " + r);
1232 public boolean removeStatement(VirtualGraph provider, Resource subject, Resource predicate, Resource object) {
1233 TransientGraph impl = (TransientGraph) provider;
1234 impl.deny(querySupport.getId(subject), querySupport.getId(predicate), querySupport.getId(object));
1235 getQueryProvider2().updateStatements(querySupport.getId(subject), querySupport.getId(predicate));
1236 clientChanges.deny(subject, predicate, object);
1241 public void setValue(VirtualGraph provider, Resource resource, byte[] value) {
1242 throw new UnsupportedOperationException();
1246 public boolean writeOnly() {
1251 public void performWriteRequest(WriteGraph graph, Write request) throws DatabaseException {
1252 throw new UnsupportedOperationException();
1256 public <T> T performWriteRequest(WriteGraph graph, WriteResult<T> request) throws DatabaseException {
1257 throw new UnsupportedOperationException();
1261 public void performWriteRequest(WriteGraph graph, WriteOnly request) throws DatabaseException {
1262 throw new UnsupportedOperationException();
1266 public <T> void addMetadata(Metadata data) throws ServiceException {
1267 throw new UnsupportedOperationException();
1271 public <T extends Metadata> T getMetadata(Class<T> clazz) throws ServiceException {
1272 throw new UnsupportedOperationException();
1276 public TreeMap<String, byte[]> getMetadata() {
1277 throw new UnsupportedOperationException();
1281 public void commitDone(WriteTraits writeTraits, long csid) {
1285 public void clearUndoList(WriteTraits writeTraits) {
1289 public int clearMetadata() {
1294 public void startUndo() {
1298 private <T> void performWriteOnly(WriteOnlyResult<T> request, Semaphore notify, Procedure<T> callback) {
1302 //int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1304 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1307 Disposable.safeDispose(clientChanges);
1308 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
1312 WriteSupport writeSupport = request.getProvider() != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
1314 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, request.getProvider());
1316 WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, callback);
1317 writeState = writeStateT;
1319 assert (null != writer);
1320 // writer.state.barrier.inc();
1321 long start = System.nanoTime();
1322 T result = request.perform(writer);
1323 long duration = System.nanoTime() - start;
1325 System.err.println("################");
1326 System.err.println("WriteOnly duration " + 1e-9*duration);
1328 writeStateT.setResult(result);
1330 // This makes clusters available from server
1331 clusterStream.reallyFlush();
1333 // This will trigger query updates
1334 releaseWriteOnly(writer);
1335 assert (null != writer);
1337 handleUpdatesAndMetadata(writer);
1339 } catch (CancelTransactionException e) {
1341 releaseWriteOnly(writeState.getGraph());
1343 clusterTable.removeWriteOnlyClusters();
1344 state.stopWriteTransaction(clusterStream);
1346 } catch (Throwable e) {
1348 e.printStackTrace();
1350 releaseWriteOnly(writeState.getGraph());
1352 clusterTable.removeWriteOnlyClusters();
1354 if (callback != null)
1355 callback.exception(new DatabaseException(e));
1357 state.stopWriteTransaction(clusterStream);
1358 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
1362 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1368 public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify) {
1369 scheduleRequest(request, callback, notify, null);
1373 public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
1377 assert (request != null);
1379 requestManager.scheduleWrite(new SessionTask(null) {
1382 public void run0(int thread) {
1384 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
1388 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1391 Disposable.safeDispose(clientChanges);
1392 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
1396 VirtualGraph vg = getProvider(request.getProvider());
1397 WriteSupport writeSupport = vg != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
1399 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
1401 writeState = new WriteState<Object>(writer, request, notify, new Procedure<Object>() {
1404 public void execute(Object result) {
1405 if(callback != null) callback.accept(null);
1409 public void exception(Throwable t) {
1410 if(callback != null) callback.accept((DatabaseException)t);
1415 assert (null != writer);
1416 // writer.state.barrier.inc();
1420 request.perform(writer);
1422 } catch (Throwable e) {
1424 // writer.state.barrier.dec();
1425 // writer.waitAsync(null);
1427 releaseWriteOnly(writer);
1429 clusterTable.removeWriteOnlyClusters();
1431 if(!(e instanceof CancelTransactionException)) {
1432 if (callback != null)
1433 callback.accept(new DatabaseException(e));
1436 writeState.except(e);
1442 // This makes clusters available from server
1443 boolean empty = clusterStream.reallyFlush();
1444 // This was needed to make WO requests call metadata listeners.
1445 // NOTE: the calling event does not contain clientChanges information.
1446 if (!empty && clientChanges.isEmpty())
1447 clientChanges.setNotEmpty(true);
1448 releaseWriteOnly(writer);
1449 assert (null != writer);
1452 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1465 public <T> void scheduleRequest(final WriteOnlyResult<T> request, final Procedure<T> callback, final Semaphore notify) {
1466 scheduleRequest(request, callback, notify, null);
1470 * @see org.simantics.db.impl.support.WriteRequestScheduleSupport#scheduleRequest(org.simantics.db.request.WriteOnlyResult, org.simantics.db.procedure.Procedure, java.util.concurrent.Semaphore, java.lang.Boolean)
1473 public <T> void scheduleRequest(final WriteOnlyResult<T> request, final Procedure<T> callback, final Semaphore notify, Boolean combine) {
1475 assert (request != null);
1477 requestManager.scheduleWrite(new SessionTask(null) {
1480 public void run0(int thread) {
1482 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
1484 performWriteOnly(request, notify, callback);
1494 public <T> void scheduleRequest(final Read<T> request, final AsyncProcedure<T> procedure, final Semaphore notify, final DataContainer<Throwable> throwable, final DataContainer<T> result) {
1496 assert (request != null);
1497 assert (procedure != null);
1499 requestManager.scheduleRead(new SessionRead(throwable, notify) {
1502 public void run0(int thread) {
1504 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1506 ITask task = ThreadLogger.task(request);
1508 ListenerBase listener = getListenerBase(procedure);
1510 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1512 // This is never synced but increase to prevent it from visiting 0
1513 newGraph.asyncBarrier.inc();
1517 if (listener != null) {
1521 AsyncProcedure ap = new AsyncProcedure<T>() {
1524 public void exception(AsyncReadGraph graph, Throwable t) {
1525 procedure.exception(graph, t);
1526 if(throwable != null) {
1529 // ErrorLogger.defaultLogError("Unhandled exception", t);
1534 public void execute(AsyncReadGraph graph, T t) {
1535 if(result != null) result.set(t);
1536 procedure.execute(graph, t);
1541 QueryCache.runnerReadEntry(newGraph, request, null, listener, ap, true);
1543 } catch (Throwable t) {
1544 // This is handled by the AsyncProcedure
1545 //Logger.defaultLogError("Internal error", t);
1552 T t = request.perform(newGraph);
1556 if(result != null) result.set(t);
1557 procedure.execute(newGraph, t);
1559 } catch (Throwable th) {
1561 if(throwable != null) {
1564 Logger.defaultLogError("Unhandled exception", th);
1569 } catch (Throwable t) {
1572 t.printStackTrace();
1574 if(throwable != null) {
1577 Logger.defaultLogError("Unhandled exception", t);
1582 procedure.exception(newGraph, t);
1584 } catch (Throwable t2) {
1586 if(throwable != null) {
1589 Logger.defaultLogError("Unhandled exception", t2);
1602 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1612 public <T> void scheduleRequest(final AsyncRead<T> request, final AsyncProcedure<T> procedure, final ListenerBase listener, final Semaphore notify) {
1614 assert (request != null);
1615 assert (procedure != null);
1617 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1619 requestManager.scheduleRead(new SessionRead(null, notify) {
1622 public void run0(int thread) {
1624 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1626 ITask task = ThreadLogger.task(request);
1628 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1632 if (listener != null) {
1635 QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
1636 } catch (DatabaseException e) {
1637 Logger.defaultLogError(e);
1642 BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph.asyncBarrier, newGraph, procedure, request) {
1644 public void execute(AsyncReadGraph graph_, T result) {
1646 super.execute(graph_, result);
1649 public void exception(AsyncReadGraph graph_, Throwable t) {
1651 super.exception(graph_, t);
1658 request.perform(newGraph, wrap);
1661 } catch (DatabaseException e) {
1663 Logger.defaultLogError(e);
1671 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1681 public <T> void scheduleRequest(final MultiRead<T> request, final SyncMultiProcedure<T> procedure, final Semaphore notify) {
1683 assert (request != null);
1684 assert (procedure != null);
1686 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1688 int sync = notify != null ? thread : -1;
1690 requestManager.scheduleRead(new SessionRead(null, notify) {
1693 public void run0(int thread) {
1695 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1697 ListenerBase listener = getListenerBase(procedure);
1699 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1703 if (listener != null) {
1705 newGraph.processor.query(newGraph, request, null, procedure, listener);
1707 // newGraph.waitAsync(request);
1711 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
1715 request.perform(newGraph, wrapper);
1717 } catch (Throwable t) {
1719 t.printStackTrace();
1727 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1737 public <T> void scheduleRequest(final AsyncMultiRead<T> request, final AsyncMultiProcedure<T> procedure, final Semaphore notify) {
1739 assert (request != null);
1740 assert (procedure != null);
1742 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1744 int sync = notify != null ? thread : -1;
1746 requestManager.scheduleRead(new SessionRead(null, notify) {
1749 public void run0(int thread) {
1751 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1753 ListenerBase listener = getListenerBase(procedure);
1755 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1759 if (listener != null) {
1761 newGraph.processor.query(newGraph, request, null, procedure, listener);
1763 // newGraph.waitAsync(request);
1767 final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
1771 request.perform(newGraph, wrapper);
1773 } catch (Throwable t) {
1775 t.printStackTrace();
1783 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1793 public <T> void scheduleRequest(final ExternalRead<T> request, final Procedure<T> procedure, final Semaphore notify, final DataContainer<Throwable> throwable, final DataContainer<T> result) {
1795 assert (request != null);
1796 assert (procedure != null);
1798 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1800 requestManager.scheduleRead(new SessionRead(throwable, notify) {
1803 public void run0(int thread) {
1805 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1807 ListenerBase listener = getListenerBase(procedure);
1809 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1813 if (listener != null) {
1816 QueryCacheBase.resultExternalReadEntry(newGraph, request, null, listener, procedure);
1817 } catch (DatabaseException e) {
1818 Logger.defaultLogError(e);
1823 // newGraph.state.barrier.inc();
1825 request.register(newGraph, new Listener<T>() {
1828 public void exception(Throwable t) {
1829 if(throwable != null) throwable.set(t);
1830 procedure.exception(t);
1831 // newGraph.state.barrier.dec();
1835 public void execute(T t) {
1836 if(result != null) result.set(t);
1837 procedure.execute(t);
1838 // newGraph.state.barrier.dec();
1842 public boolean isDisposed() {
1848 // newGraph.waitAsync(request);
1854 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1866 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
1868 scheduleRequest(request, procedure, null, null, null);
1873 public <T> T syncRequest(Read<T> request, AsyncProcedure<T> procedure) throws DatabaseException {
1875 Semaphore notify = new Semaphore(0);
1876 DataContainer<Throwable> container = new DataContainer<Throwable>();
1877 DataContainer<T> result = new DataContainer<T>();
1878 scheduleRequest(request, procedure, notify, container, result);
1879 acquire(notify, request);
1880 Throwable throwable = container.get();
1881 if(throwable != null) {
1882 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1883 else throw new DatabaseException("Unexpected exception", throwable);
1885 return result.get();
1889 public <T> T syncRequest(AsyncRead<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1891 Semaphore notify = new Semaphore(0);
1892 final DataContainer<Throwable> exceptionContainer = new DataContainer<Throwable>();
1893 final DataContainer<T> resultContainer = new DataContainer<T>();
1894 scheduleRequest(request, new AsyncProcedure<T>() {
1896 public void exception(AsyncReadGraph graph, Throwable throwable) {
1897 exceptionContainer.set(throwable);
1898 procedure.exception(graph, throwable);
1901 public void execute(AsyncReadGraph graph, T result) {
1902 resultContainer.set(result);
1903 procedure.execute(graph, result);
1905 }, getListenerBase(procedure), notify);
1906 acquire(notify, request, procedure);
1907 Throwable throwable = exceptionContainer.get();
1908 if (throwable != null) {
1909 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1910 else throw new DatabaseException("Unexpected exception", throwable);
1912 return resultContainer.get();
1917 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, final AsyncMultiProcedure<T> procedure) throws DatabaseException {
1919 Semaphore notify = new Semaphore(0);
1920 final DataContainer<Throwable> exceptionContainer = new DataContainer<Throwable>();
1921 scheduleRequest(request, new AsyncMultiProcedure<T>() {
1923 public void exception(AsyncReadGraph graph, Throwable throwable) {
1924 exceptionContainer.set(throwable);
1925 procedure.exception(graph, throwable);
1928 public void execute(AsyncReadGraph graph, T result) {
1929 procedure.execute(graph, result);
1932 public void finished(AsyncReadGraph graph) {
1933 procedure.finished(graph);
1936 acquire(notify, request, procedure);
1937 Throwable throwable = exceptionContainer.get();
1938 if (throwable != null) {
1939 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1940 else throw new DatabaseException("Unexpected exception", throwable);
1942 // TODO: implement return value
1943 System.err.println("TODO: implement return value for syncRequest(AsyncMultiRead, AsyncMultiProcedure)");
1948 public <T> T syncRequest(final ExternalRead<T> request) throws DatabaseException {
1949 return syncRequest(request, new ProcedureAdapter<T>());
1952 // assert(request != null);
1954 // final DataContainer<T> result = new DataContainer<T>();
1955 // final DataContainer<Throwable> exception = new DataContainer<Throwable>();
1957 // syncRequest(request, new Procedure<T>() {
1960 // public void execute(T t) {
1965 // public void exception(Throwable t) {
1966 // exception.set(t);
1971 // Throwable t = exception.get();
1973 // if(t instanceof DatabaseException) throw (DatabaseException)t;
1974 // else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(PrimitiveRead)", t);
1977 // return result.get();
1982 public <T> T syncRequest(ExternalRead<T> request, final Listener<T> procedure) throws DatabaseException {
1983 return syncRequest(request, (Procedure<T>)procedure);
1988 // public <T> T syncRequest(Read<T> request, AsyncProcedure<T> procedure) throws DatabaseException {
1989 // assertNotSession();
1990 // Semaphore notify = new Semaphore(0);
1991 // DataContainer<Throwable> container = new DataContainer<Throwable>();
1992 // DataContainer<T> result = new DataContainer<T>();
1993 // scheduleRequest(request, procedure, notify, container, result);
1994 // acquire(notify, request);
1995 // Throwable throwable = container.get();
1996 // if(throwable != null) {
1997 // if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1998 // else throw new DatabaseException("Unexpected exception", throwable);
2000 // return result.get();
2005 public <T> T syncRequest(ExternalRead<T> request, final Procedure<T> procedure) throws DatabaseException {
2007 Semaphore notify = new Semaphore(0);
2008 final DataContainer<Throwable> container = new DataContainer<Throwable>();
2009 final DataContainer<T> result = new DataContainer<T>();
2010 scheduleRequest(request, procedure, notify, container, result);
2011 acquire(notify, request);
2012 Throwable throwable = container.get();
2013 if (throwable != null) {
2014 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
2015 else throw new DatabaseException("Unexpected exception", throwable);
2017 return result.get();
2021 public void syncRequest(Write request) throws DatabaseException {
2024 Semaphore notify = new Semaphore(0);
2025 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2026 scheduleRequest(request, e -> exception.set(e), notify);
2027 acquire(notify, request);
2028 if(exception.get() != null) throw exception.get();
2032 public <T> T syncRequest(WriteResult<T> request) throws DatabaseException {
2034 Semaphore notify = new Semaphore(0);
2035 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2036 final DataContainer<T> result = new DataContainer<T>();
2037 scheduleRequest(request, new Procedure<T>() {
2040 public void exception(Throwable t) {
2045 public void execute(T t) {
2050 acquire(notify, request);
2051 if(exception.get() != null) {
2052 Throwable t = exception.get();
2053 if(t instanceof DatabaseException) throw (DatabaseException)t;
2054 else throw new DatabaseException(t);
2056 return result.get();
2060 public <T> T syncRequest(WriteOnlyResult<T> request) throws DatabaseException {
2062 Semaphore notify = new Semaphore(0);
2063 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2064 final DataContainer<T> result = new DataContainer<T>();
2065 scheduleRequest(request, new Procedure<T>() {
2068 public void exception(Throwable t) {
2073 public void execute(T t) {
2078 acquire(notify, request);
2079 if(exception.get() != null) {
2080 Throwable t = exception.get();
2081 if(t instanceof DatabaseException) throw (DatabaseException)t;
2082 else throw new DatabaseException(t);
2084 return result.get();
2088 public <T> T syncRequest(DelayedWriteResult<T> request) throws DatabaseException {
2090 Semaphore notify = new Semaphore(0);
2091 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2092 final DataContainer<T> result = new DataContainer<T>();
2093 scheduleRequest(request, new Procedure<T>() {
2096 public void exception(Throwable t) {
2101 public void execute(T t) {
2106 acquire(notify, request);
2107 if(exception.get() != null) {
2108 Throwable t = exception.get();
2109 if(t instanceof DatabaseException) throw (DatabaseException)t;
2110 else throw new DatabaseException(t);
2112 return result.get();
2116 public void syncRequest(DelayedWrite request) throws DatabaseException {
2118 Semaphore notify = new Semaphore(0);
2119 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2120 scheduleRequest(request, e -> exception.set(e), notify);
2121 acquire(notify, request);
2122 if(exception.get() != null) throw exception.get();
2126 public void syncRequest(WriteOnly request) throws DatabaseException {
2129 Semaphore notify = new Semaphore(0);
2130 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2131 scheduleRequest(request, e -> exception.set(e), notify);
2132 acquire(notify, request);
2133 if(exception.get() != null) throw exception.get();
2138 * GraphRequestProcessor interface
2142 public <T> void asyncRequest(final AsyncRead<T> request, final AsyncProcedure<T> procedure) {
2144 scheduleRequest(request, procedure, null, null);
2149 public <T> void asyncRequest(final AsyncMultiRead<T> request, final AsyncMultiProcedure<T> procedure) {
2151 scheduleRequest(request, procedure, null);
2156 public <T> void asyncRequest(final ExternalRead<T> request, final Procedure<T> procedure) {
2158 scheduleRequest(request, procedure, null, null, null);
2163 public void asyncRequest(final Write request, final Consumer<DatabaseException> callback) {
2165 scheduleRequest(request, callback, null);
2170 public <T> void asyncRequest(final WriteResult<T> request, final Procedure<T> procedure) {
2172 scheduleRequest(request, procedure, null);
2177 public <T> void asyncRequest(final WriteOnlyResult<T> request, final Procedure<T> procedure) {
2179 scheduleRequest(request, procedure, null);
2184 public <T> void asyncRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure) {
2186 scheduleRequest(request, procedure, null);
2191 public void asyncRequest(final DelayedWrite request, final Consumer<DatabaseException> callback) {
2193 scheduleRequest(request, callback, null);
2198 public void asyncRequest(final Write r) {
2199 asyncRequest(r, null);
2203 public void asyncRequest(final DelayedWrite r) {
2204 asyncRequest(r, null);
2208 public void asyncRequest(final WriteOnly request, final Consumer<DatabaseException> callback) {
2210 scheduleRequest(request, callback, null);
2215 public void asyncRequest(final WriteOnly request) {
2217 asyncRequest(request, null);
2222 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2223 r.request(this, procedure);
2227 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2228 r.request(this, procedure);
2232 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2233 r.request(this, procedure);
2237 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2238 r.request(this, procedure);
2242 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2243 r.request(this, procedure);
2247 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2248 r.request(this, procedure);
2252 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2253 return r.request(this);
2257 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2258 return r.request(this);
2262 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2263 r.request(this, procedure);
2267 public <T> void async(WriteInterface<T> r) {
2268 r.request(this, new ProcedureAdapter<T>());
2272 public void incAsync() {
2277 public void decAsync() {
2281 public long getCluster(ResourceImpl resource) {
2282 ClusterI cluster = clusterTable.getClusterByResourceKey(resource.id);
2283 return cluster.getClusterId();
2286 public long getCluster(int id) {
2287 if (clusterTable == null)
2288 System.out.println("SessionImplSocket.getCluster() clusterTable == null !!!!! how come");
2289 return clusterTable.getClusterIdByResourceKeyNoThrow(id);
2292 public ResourceImpl getResource(int id) {
2293 return new ResourceImpl(resourceSupport, id);
2296 public ResourceImpl getResource(int resourceIndex, long clusterId) {
2297 assert (!ClusterTraitsBase.isIllegalResourceIndex(resourceIndex));
2298 ClusterI proxy = clusterTable.getClusterByClusterId(clusterId);
2299 int key = proxy.getClusterKey();
2300 int resourceKey = ClusterTraitsBase.createResourceKeyNoThrow(key, resourceIndex);
2301 return new ResourceImpl(resourceSupport, resourceKey);
2304 public ResourceImpl getResource2(int id) {
2306 return new ResourceImpl(resourceSupport, id);
2309 final public int getId(ResourceImpl impl) {
2313 public static final Charset UTF8 = Charset.forName("utf-8");
2318 static boolean areVirtualStatementsLoaded(final VirtualGraphServerSupportImpl support, int subject) {
2319 for(TransientGraph g : support.providers) {
2320 if(g.isPending(subject)) return false;
2325 static boolean areVirtualStatementsLoaded(final VirtualGraphServerSupportImpl support, int subject, int predicate) {
2326 for(TransientGraph g : support.providers) {
2327 if(g.isPending(subject, predicate)) return false;
2332 static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, final Consumer<ReadGraphImpl> runnable) {
2334 Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
2336 AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
2339 public void accept(ReadGraphImpl graph) {
2340 if(ready.decrementAndGet() == 0) {
2341 runnable.accept(graph);
2347 for(TransientGraph g : support.providers) {
2348 if(g.isPending(subject)) {
2350 g.load(graph, subject, composite);
2351 } catch (DatabaseException e) {
2352 e.printStackTrace();
2355 composite.accept(graph);
2359 composite.accept(graph);
2363 static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, int predicate, final Consumer<ReadGraphImpl> runnable) {
2365 Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
2367 AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
2370 public void accept(ReadGraphImpl graph) {
2371 if(ready.decrementAndGet() == 0) {
2372 runnable.accept(graph);
2378 for(TransientGraph g : support.providers) {
2379 if(g.isPending(subject, predicate)) {
2381 g.load(graph, subject, predicate, composite);
2382 } catch (DatabaseException e) {
2383 e.printStackTrace();
2386 composite.accept(graph);
2390 composite.accept(graph);
2394 // void dumpHeap() {
2397 // ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
2398 // "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class).dumpHeap(
2399 // "d:/heap" + flushCounter + ".txt", true);
2400 // } catch (IOException e) {
2401 // e.printStackTrace();
2406 void fireReactionsToSynchronize(ChangeSet cs) {
2408 // Do not fire empty events
2412 ReadGraphImpl g = ReadGraphImpl.create(getQueryProvider2());
2413 // g.state.barrier.inc();
2417 if (!cs.isEmpty()) {
2418 ChangeEvent e2 = new ChangeEvent(g.getSession(), g, null, cs);
2419 for (ChangeListener l : changeListeners2) {
2422 } catch (Exception ex) {
2423 ex.printStackTrace();
2430 // g.state.barrier.dec();
2431 // g.waitAsync(null);
2437 void fireReactionsToCommit(ReadGraphImpl graph, ChangeSet cs2) {
2440 // Do not fire empty events
2445 // graph.state.barrier.inc();
2449 if (!cs2.isEmpty()) {
2451 ChangeEvent e2 = new ChangeEvent(graph.getSession(), graph, null, cs2);
2452 for (ChangeListener l : changeListeners2) {
2455 // System.out.println("changelistener " + l);
2456 } catch (Exception ex) {
2457 ex.printStackTrace();
2465 // graph.state.barrier.dec();
2466 // graph.waitAsync(null);
2469 } catch (Throwable t) {
2470 t.printStackTrace();
2474 void fireMetadataListeners(WriteGraphImpl graph, ChangeSet cs2) {
2478 // Do not fire empty events
2482 // Do not fire on virtual requests
2483 if(graph.getProvider() != null)
2486 WriteGraphImpl reactionGraph = WriteGraphImpl.create(graph.processor, writeSupport, null);
2490 ChangeEvent e2 = new ChangeEvent(graph.getSession(), reactionGraph, graph, cs2);
2491 for (ChangeListener l : metadataListeners) {
2494 } catch (Throwable ex) {
2495 LOGGER.error("Could not invoke listener {} with event {}", l, e2, ex);
2503 } catch (Throwable t) {
2504 LOGGER.error("Could not fire metadata listeners {} {}", graph, cs2, t);
2513 * @see org.simantics.db.ServiceLocator#getService(java.lang.Class)
2516 public <T> T getService(Class<T> api) {
2517 T t = peekService(api);
2519 if (state.isClosed())
2520 throw new ServiceNotFoundException(this, api, "Session has been shut down");
2521 throw new ServiceNotFoundException(this, api);
2529 protected abstract ServerInformation getCachedServerInformation();
2531 private Class<?> serviceKey1 = null;
2532 private Class<?> serviceKey2 = null;
2533 private Object service1 = null;
2534 private Object service2 = null;
2540 * fi.vtt.simantics.db.connection.ServiceLocator#peekService(java.lang.Class)
2542 @SuppressWarnings("unchecked")
2544 public synchronized <T> T peekService(Class<T> api) {
2546 if(serviceKey1 == api) {
2548 } else if (serviceKey2 == api) {
2550 Object result = service2;
2551 service2 = service1;
2552 serviceKey2 = serviceKey1;
2558 if (Layer0.class == api)
2560 if (ServerInformation.class == api)
2561 return (T) getCachedServerInformation();
2562 else if (WriteGraphImpl.class == api)
2563 return (T) writeState.getGraph();
2564 else if (ClusterBuilder.class == api)
2565 return (T)new ClusterBuilderImpl(this, (WriteOnlySupport)writeState.getGraph().writeSupport);
2566 else if (ClusterBuilderFactory.class == api)
2567 return (T)new ClusterBuilderFactoryImpl(this);
2569 service2 = service1;
2570 serviceKey2 = serviceKey1;
2572 service1 = serviceLocator.peekService(api);
2583 * fi.vtt.simantics.db.connection.ServiceLocator#hasService(java.lang.Class)
2586 public boolean hasService(Class<?> api) {
2587 return serviceLocator.hasService(api);
2591 * @param api the api that must be implemented by the specified service
2592 * @param service the service implementation
2595 public <T> void registerService(Class<T> api, T service) {
2596 if(Layer0.class == api) {
2597 L0 = (Layer0)service;
2600 serviceLocator.registerService(api, service);
2601 if (TransactionPolicySupport.class == api) {
2602 transactionPolicy = (TransactionPolicySupport)service;
2603 state.resetTransactionPolicy();
2605 if (api == serviceKey1)
2607 else if (api == serviceKey2)
2615 void fireSessionVariableChange(String variable) {
2616 for (MonitorHandler h : monitorHandlers) {
2617 MonitorContext ctx = monitorContexts.getLeft(h);
2619 // SafeRunner functionality repeated here to avoid dependency.
2621 h.valuesChanged(ctx);
2622 } catch (Exception e) {
2623 Logger.defaultLogError("monitor handler notification produced the following exception", e);
2624 } catch (LinkageError e) {
2625 Logger.defaultLogError("monitor handler notification produced a linkage error", e);
2631 class ResourceSerializerImpl implements ResourceSerializer {
2633 public long createRandomAccessId(int id) throws DatabaseException {
2636 int index = ClusterTraitsBase.getResourceIndexFromResourceKey(id);
2637 long cluster = getCluster(id);
2639 return 0; // Better to return 0 then invalid id.
2640 long result = ClusterTraitsBase.createResourceId(cluster, index);
2645 public long getRandomAccessId(Resource resource) throws DatabaseException {
2647 ResourceImpl resourceImpl = (ResourceImpl) resource;
2648 return createRandomAccessId(resourceImpl.id);
2653 public int getTransientId(Resource resource) throws DatabaseException {
2655 ResourceImpl resourceImpl = (ResourceImpl) resource;
2656 return resourceImpl.id;
2661 public String createRandomAccessId(Resource resource)
2662 throws InvalidResourceReferenceException {
2664 if(resource == null) throw new IllegalArgumentException();
2666 ResourceImpl resourceImpl = (ResourceImpl) resource;
2668 if (resourceImpl.id < 0) return String.valueOf(resourceImpl.id) + "_0";
2672 r = ClusterTraits.getResourceIndexFromResourceKey(resourceImpl.id);
2673 } catch (DatabaseException e1) {
2674 throw new InvalidResourceReferenceException(e1);
2677 // Serialize as '<resource index>_<cluster id>'
2678 return "" + r + "_" + getCluster(resourceImpl);
2679 } catch (Throwable e) {
2680 e.printStackTrace();
2681 throw new InvalidResourceReferenceException(e);
2686 public int getTransientId(long serialized) throws DatabaseException {
2687 if (serialized <= 0)
2688 return (int)serialized;
2689 int index = ClusterTraitsBase.getResourceIndexFromResourceId(serialized);
2690 long c = ClusterTraitsBase.getClusterIdFromResourceId(serialized);
2691 ClusterI cluster = clusterTranslator.getClusterByClusterId(c);
2692 if (cluster == null)
2693 throw new DatabaseException("Couldn't load cluster for id " + c + " for resource id " + serialized);
2694 int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), index);
2699 public Resource getResource(long randomAccessId) throws DatabaseException {
2700 return getResourceByKey(getTransientId(randomAccessId));
2704 public Resource getResource(int transientId) throws DatabaseException {
2705 return getResourceByKey(transientId);
2709 public Resource getResource(String randomAccessId)
2710 throws InvalidResourceReferenceException {
2712 int i = randomAccessId.indexOf('_');
2714 throw new InvalidResourceReferenceException("Could not parse resource id + cluster id from '"
2715 + randomAccessId + "'");
2716 int r = Integer.parseInt(randomAccessId.substring(0, i));
2717 if(r < 0) return getResourceByKey(r);
2718 long c = Long.parseLong(randomAccessId.substring(i + 1));
2719 ClusterI cluster = clusterTranslator.getClusterByClusterId(c);
2720 int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), r);
2721 if (cluster.hasResource(key, clusterTranslator))
2722 return getResourceByKey(key);
2723 } catch (InvalidResourceReferenceException e) {
2725 } catch (NumberFormatException e) {
2726 throw new InvalidResourceReferenceException(e);
2727 } catch (Throwable e) {
2728 e.printStackTrace();
2729 throw new InvalidResourceReferenceException(e);
2732 throw new InvalidResourceReferenceException("Resource does not exist " + randomAccessId);
2736 public boolean disposeRandomAccessId(String randomAccessId) throws InvalidResourceReferenceException {
2739 } catch (Throwable e) {
2740 e.printStackTrace();
2741 throw new InvalidResourceReferenceException(e);
2747 // Copied from old SessionImpl
2750 if (state.isClosed())
2751 throw new Error("Session closed.");
2756 public ResourceImpl getNewResource(long clusterId) {
2757 ClusterI cluster = clusterTable.getClusterByClusterIdOrThrow(clusterId);
2760 newId = cluster.createResource(clusterTranslator);
2761 } catch (DatabaseException e) {
2762 Logger.defaultLogError(e);
2765 return new ResourceImpl(resourceSupport, newId);
2768 public ResourceImpl getNewResource(Resource clusterSet)
2769 throws DatabaseException {
2770 long resourceId = clusterSet.getResourceId();
2771 Long clusterId = clusterSetsSupport.get(resourceId);
2772 if (null == clusterId)
2773 throw new ClusterSetExistException("Cluster set does not exist. Resource=" + clusterSet);
2774 if (Constants.NewClusterId == clusterId) {
2775 clusterId = getService(ClusteringSupport.class).createCluster();
2776 if ((serviceMode & SERVICE_MODE_CREATE) > 0)
2777 createdClusters.add(clusterId);
2778 clusterSetsSupport.put(resourceId, clusterId);
2779 return getNewResource(clusterId);
2781 ClusterBase cb = clusterTranslator.getClusterByClusterId(clusterId);
2782 ResourceImpl result;
2783 if (cb.getNumberOfResources(clusterTranslator) >= ClusterTable.CLUSTER_FILL_SIZE) {
2784 clusterId = getService(ClusteringSupport.class).createCluster();
2785 if ((serviceMode & SERVICE_MODE_CREATE) > 0)
2786 createdClusters.add(clusterId);
2787 clusterSetsSupport.put(resourceId, clusterId);
2788 return getNewResource(clusterId);
2790 result = getNewResource(clusterId);
2791 int resultKey = querySupport.getId(result);
2792 long resultCluster = querySupport.getClusterId(resultKey);
2793 if (clusterId != resultCluster)
2794 clusterSetsSupport.put(resourceId, resultCluster);
2800 public void getNewClusterSet(Resource clusterSet)
2801 throws DatabaseException {
2803 System.out.println("new cluster set=" + clusterSet);
2804 long resourceId = clusterSet.getResourceId();
2805 if (clusterSetsSupport.containsKey(resourceId))
2806 throw new ClusterSetExistException("Cluster set exist already. Resource=" + clusterSet);
2807 clusterSetsSupport.put(resourceId, Constants.NewClusterId);
2809 public boolean containsClusterSet(Resource clusterSet)
2810 throws ServiceException {
2811 long resourceId = clusterSet.getResourceId();
2812 return clusterSetsSupport.containsKey(resourceId);
2814 public Resource setDefaultClusterSet4NewResource(Resource clusterSet) {
2815 Resource r = defaultClusterSet;
2816 defaultClusterSet = clusterSet;
2819 void printDiagnostics() {
2823 final DataContainer<Long> totalMem = new DataContainer<Long>(0L);
2824 final DataContainer<Long> residentClusters = new DataContainer<Long>(0L);
2825 for(ClusterI cluster : clusterTable.getClusters()) {
2827 if (cluster.isLoaded() && !cluster.isEmpty()) {
2828 residentClusters.set(residentClusters.get() + 1);
2829 totalMem.set(totalMem.get() + cluster.getUsedSpace());
2831 } catch (DatabaseException e) {
2832 Logger.defaultLogError(e);
2836 System.out.println("--------------------------------");
2837 System.out.println("Cluster information:");
2838 System.out.println("-amount of resident clusters=" + residentClusters.get());
2839 System.out.println("-total memory usage=" + totalMem.get() / 1024 + "kB");
2841 for(ClusterI cluster : clusterTable.getClusters()) {
2842 System.out.print("Cluster " + cluster.getClusterId() + " [");
2844 if (!cluster.isLoaded())
2845 System.out.println("not loaded]");
2846 else if (cluster.isEmpty())
2847 System.out.println("is empty]");
2849 System.out.print("resources=" + cluster.getNumberOfResources(clusterTranslator));
2850 System.out.print(",references=" + cluster.getReferenceCount());
2851 System.out.println(",memory=" + cluster.getUsedSpace() / 1024 + "kB]");
2853 } catch (DatabaseException e) {
2854 Logger.defaultLogError(e);
2855 System.out.println("is corrupted]");
2859 queryProvider2.printDiagnostics();
2860 System.out.println("--------------------------------");
2865 public void fireStartReadTransaction() {
2868 System.out.println("StartReadTransaction");
2870 for (SessionEventListener listener : eventListeners) {
2872 listener.readTransactionStarted();
2873 } catch (Throwable t) {
2874 t.printStackTrace();
2880 public void fireFinishReadTransaction() {
2883 System.out.println("FinishReadTransaction");
2885 for (SessionEventListener listener : eventListeners) {
2887 listener.readTransactionFinished();
2888 } catch (Throwable t) {
2889 t.printStackTrace();
2895 public void fireStartWriteTransaction() {
2898 System.out.println("StartWriteTransaction");
2900 for (SessionEventListener listener : eventListeners) {
2902 listener.writeTransactionStarted();
2903 } catch (Throwable t) {
2904 t.printStackTrace();
2910 public void fireFinishWriteTransaction() {
2913 System.out.println("FinishWriteTransaction");
2915 for (SessionEventListener listener : eventListeners) {
2917 listener.writeTransactionFinished();
2918 } catch (Throwable t) {
2919 t.printStackTrace();
2923 Indexing.resetDependenciesIndexingDisabled();
2927 Resource deserialize(final long resourceId, final long clusterId) throws IOException {
2928 throw new Error("Not supported at the moment.");
2935 ClusterTable getClusterTable() {
2936 return clusterTable;
2939 public GraphSession getGraphSession() {
2940 return graphSession;
2943 public QueryProcessor getQueryProvider2() {
2944 return queryProvider2;
2947 ClientChangesImpl getClientChanges() {
2948 return clientChanges;
2951 boolean getWriteOnly() {
2955 static int counter = 0;
2957 public void onClusterLoaded(long clusterId) {
2959 clusterTable.updateSize();
2967 * Implementation of the interface RequestProcessor
2971 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
2976 assert(request != null);
2978 final DataContainer<T> result = new DataContainer<T>();
2979 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2981 syncRequest(request, new AsyncProcedure<T>() {
2984 public void execute(AsyncReadGraph graph, T t) {
2989 public void exception(AsyncReadGraph graph, Throwable t) {
2995 Throwable t = exception.get();
2997 if(t instanceof DatabaseException) throw (DatabaseException)t;
2998 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3001 return result.get();
3006 // public <T> T syncRequest(Read<T> request, AsyncListener<T> procedure) throws DatabaseException {
3007 // assertNotSession();
3008 // return syncRequest(request, (AsyncProcedure<T>)procedure);
3012 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure) throws DatabaseException {
3014 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
3018 public <T> T syncRequest(Read<T> request, final Listener<T> procedure) throws DatabaseException {
3020 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
3024 public <T> T syncRequest(final Read<T> request, final SyncProcedure<T> procedure) throws DatabaseException {
3026 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3030 public <T> T syncRequest(Read<T> request, Procedure<T> procedure) throws DatabaseException {
3032 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3036 public <T> T syncRequest(final AsyncRead<T> request) throws DatabaseException {
3040 assert(request != null);
3042 final DataContainer<T> result = new DataContainer<T>();
3043 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3045 syncRequest(request, new AsyncProcedure<T>() {
3048 public void execute(AsyncReadGraph graph, T t) {
3053 public void exception(AsyncReadGraph graph, Throwable t) {
3059 Throwable t = exception.get();
3061 if(t instanceof DatabaseException) throw (DatabaseException)t;
3062 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3065 return result.get();
3070 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure) throws DatabaseException {
3072 return syncRequest(request, (AsyncProcedure<T>)procedure);
3076 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure) throws DatabaseException {
3078 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
3082 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure) throws DatabaseException {
3084 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
3088 public <T> T syncRequest(AsyncRead<T> request, final SyncProcedure<T> procedure) throws DatabaseException {
3090 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3094 final public <T> T syncRequest(final AsyncRead<T> request, final Procedure<T> procedure) throws DatabaseException {
3096 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3100 public <T> Collection<T> syncRequest(MultiRead<T> request) throws DatabaseException {
3104 assert(request != null);
3106 final ArrayList<T> result = new ArrayList<T>();
3107 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3109 syncRequest(request, new SyncMultiProcedure<T>() {
3112 public void execute(ReadGraph graph, T t) {
3113 synchronized(result) {
3119 public void finished(ReadGraph graph) {
3123 public void exception(ReadGraph graph, Throwable t) {
3130 Throwable t = exception.get();
3132 if(t instanceof DatabaseException) throw (DatabaseException)t;
3133 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3141 public <T> Collection<T> syncRequest(MultiRead<T> arg0, SyncMultiProcedure<T> arg1) throws DatabaseException {
3143 throw new Error("Not implemented!");
3147 public <T> Collection<T> syncRequest(MultiRead<T> request, SyncMultiListener<T> procedure) throws DatabaseException {
3149 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
3153 public <T> Collection<T> syncRequest(MultiRead<T> request, MultiListener<T> procedure) throws DatabaseException {
3155 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
3159 public <T> Collection<T> syncRequest(MultiRead<T> request, MultiProcedure<T> procedure) throws DatabaseException {
3161 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
3165 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request) throws DatabaseException {
3169 assert(request != null);
3171 final ArrayList<T> result = new ArrayList<T>();
3172 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3174 syncRequest(request, new AsyncMultiProcedure<T>() {
3177 public void execute(AsyncReadGraph graph, T t) {
3178 synchronized(result) {
3184 public void finished(AsyncReadGraph graph) {
3188 public void exception(AsyncReadGraph graph, Throwable t) {
3195 Throwable t = exception.get();
3197 if (t instanceof DatabaseException)
3198 throw (DatabaseException) t;
3200 throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3208 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, AsyncMultiListener<T> procedure) throws DatabaseException {
3210 return syncRequest(request, (AsyncMultiProcedure<T>)procedure);
3214 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, SyncMultiListener<T> procedure) throws DatabaseException {
3216 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
3220 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, MultiListener<T> procedure) throws DatabaseException {
3222 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
3226 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, final SyncMultiProcedure<T> procedure) throws DatabaseException {
3228 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
3232 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request, final MultiProcedure<T> procedure) throws DatabaseException {
3234 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
3239 public <T> void asyncRequest(Read<T> request) {
3241 asyncRequest(request, new ProcedureAdapter<T>() {
3243 public void exception(Throwable t) {
3244 t.printStackTrace();
3251 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
3252 asyncRequest(request, (AsyncProcedure<T>)procedure);
3256 public <T> void asyncRequest(Read<T> request, final SyncListener<T> procedure) {
3257 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
3261 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
3262 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
3266 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
3267 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3271 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
3272 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3276 final public <T> void asyncRequest(final AsyncRead<T> request) {
3278 assert(request != null);
3280 asyncRequest(request, new ProcedureAdapter<T>() {
3282 public void exception(Throwable t) {
3283 t.printStackTrace();
3290 public <T> void asyncRequest(AsyncRead<T> request, AsyncListener<T> procedure) {
3291 scheduleRequest(request, procedure, procedure, null);
3295 public <T> void asyncRequest(AsyncRead<T> request, final SyncListener<T> procedure) {
3296 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
3300 public <T> void asyncRequest(AsyncRead<T> request, final Listener<T> procedure) {
3301 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
3305 public <T> void asyncRequest(AsyncRead<T> request, SyncProcedure<T> procedure) {
3306 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3310 final public <T> void asyncRequest(final AsyncRead<T> request, final Procedure<T> procedure) {
3311 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3315 public <T> void asyncRequest(MultiRead<T> request) {
3317 assert(request != null);
3319 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
3321 public void exception(ReadGraph graph, Throwable t) {
3322 t.printStackTrace();
3329 public <T> void asyncRequest(MultiRead<T> request, SyncMultiListener<T> procedure) {
3330 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
3334 public <T> void asyncRequest(MultiRead<T> request, MultiListener<T> procedure) {
3335 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
3339 public <T> void asyncRequest(MultiRead<T> request, SyncMultiProcedure<T> procedure) {
3340 scheduleRequest(request, procedure, null);
3344 public <T> void asyncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
3345 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
3349 final public <T> void asyncRequest(AsyncMultiRead<T> request) {
3351 assert(request != null);
3353 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
3355 public void exception(AsyncReadGraph graph, Throwable t) {
3356 t.printStackTrace();
3363 public <T> void asyncRequest(AsyncMultiRead<T> request, AsyncMultiListener<T> procedure) {
3364 asyncRequest(request, (AsyncMultiProcedure<T>)procedure);
3368 public <T> void asyncRequest(AsyncMultiRead<T> request, SyncMultiListener<T> procedure) {
3369 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
3373 public <T> void asyncRequest(AsyncMultiRead<T> request, MultiListener<T> procedure) {
3374 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
3378 public <T> void asyncRequest(AsyncMultiRead<T> request, SyncMultiProcedure<T> procedure) {
3379 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
3383 final public <T> void asyncRequest(AsyncMultiRead<T> request, final MultiProcedure<T> procedure) {
3384 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
3388 final public <T> void asyncRequest(final ExternalRead<T> request) {
3390 assert(request != null);
3392 asyncRequest(request, new ProcedureAdapter<T>() {
3394 public void exception(Throwable t) {
3395 t.printStackTrace();
3402 public <T> void asyncRequest(ExternalRead<T> request, final Listener<T> procedure) {
3403 asyncRequest(request, (Procedure<T>)procedure);
3406 boolean sameProvider(Write request) {
3407 if(writeState.getGraph().provider != null) {
3408 return writeState.getGraph().provider.equals(request.getProvider());
3410 return request.getProvider() == null;
3414 boolean plainWrite(WriteGraphImpl graph) {
3415 if(graph == null) return false;
3416 if(graph.writeSupport.writeOnly()) return false;
3421 public static final ThreadGroup SessionThreadGroup = new ThreadGroup("Session Thread Group");
3423 private void assertNotSession() throws DatabaseException {
3424 Thread current = Thread.currentThread();
3425 if (sessionThreads.contains(current))
3426 throw new ServiceException("Caller is already inside a transaction.");
3429 void assertAlive() {
3430 if (!state.isAlive())
3431 throw new RuntimeDatabaseException("Session has been shut down.");
3434 public InputStream getValueStream(ReadGraphImpl graph, Resource resource) {
3435 return querySupport.getValueStream(graph, querySupport.getId(resource));
3438 public byte[] getValue(ReadGraphImpl graph, Resource resource) {
3439 return querySupport.getValue(graph, querySupport.getId(resource));
3442 <T> void acquire(Semaphore semaphore, T request) throws DatabaseException {
3443 acquire(semaphore, request, null);
3446 private <T,P> void acquire(Semaphore semaphore, T request, P procedure) throws DatabaseException {
3450 // Loop until the semaphore is acquired reporting requests that take a long time.
3453 if (semaphore.tryAcquire(DebugPolicy.LONG_EXECUTION_REPORT_PERIOD, DebugPolicy.LONG_EXECUTION_REPORT_PERIOD_UNIT)) {
3457 if (DebugPolicy.REPORT_TIME_CONSUMING_REQUESTS) {
3459 long waitTime = DebugPolicy.LONG_EXECUTION_REPORT_PERIOD_UNIT
3460 .toMillis(DebugPolicy.LONG_EXECUTION_REPORT_PERIOD)
3462 System.err.println("DB client request '" + request + "' is taking long to execute, so far "
3463 + (waitTime) + " ms. (procedure=" + procedure + ")");
3470 } catch (InterruptedException e) {
3471 e.printStackTrace();
3472 // FIXME: Should perhaps do something else in this case ??
3479 // public boolean holdOnToTransactionAfterCancel() {
3480 // return transactionPolicy.holdOnToTransactionAfterCancel();
3484 // public boolean holdOnToTransactionAfterCommit() {
3485 // return transactionPolicy.holdOnToTransactionAfterCommit();
3489 // public boolean holdOnToTransactionAfterRead() {
3490 // return transactionPolicy.holdOnToTransactionAfterRead();
3494 // public void onRelinquish() {
3495 // transactionPolicy.onRelinquish();
3499 // public void onRelinquishDone() {
3500 // transactionPolicy.onRelinquishDone();
3504 // public void onRelinquishError() {
3505 // transactionPolicy.onRelinquishError();
3510 public Session getSession() {
3515 protected abstract VirtualGraph getProvider(VirtualGraph virtualGraph);
3516 protected abstract ResourceImpl getNewResource() throws DatabaseException;
3518 public void ceased(int thread) {
3520 requestManager.ceased(thread);
3524 public int getAmountOfQueryThreads() {
3525 // This must be a power of two
3527 // return Integer.highestOneBit(Runtime.getRuntime().availableProcessors());
3530 public Resource getResourceByKey(int key) throws ResourceNotFoundException {
3532 return new ResourceImpl(resourceSupport, key);
3536 public void acquireWriteOnly() {
3540 public void releaseWriteOnly(ReadGraphImpl graph) {
3542 queryProvider2.releaseWrite(graph);
3545 public void handleUpdatesAndMetadata(WriteGraphImpl writer) {
3547 long start = System.nanoTime();
3549 while(dirtyPrimitives) {
3550 dirtyPrimitives = false;
3551 getQueryProvider2().propagateChangesInQueryCache(writer);
3552 getQueryProvider2().listening.fireListeners(writer);
3555 fireMetadataListeners(writer, clientChanges);
3557 if(DebugPolicy.PERFORMANCE_DATA) {
3558 long end = System.nanoTime();
3559 System.err.println("handleUpdatesAndMetadata " + 1e-9*(end-start));
3564 void removeTemporaryData() {
3565 File platform = Platform.getLocation().toFile();
3566 File tempFiles = new File(platform, "tempFiles");
3567 File temp = new File(tempFiles, "db");
3571 Files.walkFileTree(temp.toPath(), new SimpleFileVisitor<Path>() {
3573 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
3576 } catch (IOException e) {
3577 Logger.defaultLogError(e);
3579 return FileVisitResult.CONTINUE;
3582 } catch (IOException e) {
3583 Logger.defaultLogError(e);
3587 public void handleCreatedClusters() {
3588 if ((serviceMode & SERVICE_MODE_CREATE) > 0) {
3589 createdClusters.forEach(new TLongProcedure() {
3592 public boolean execute(long value) {
3593 ClusterImpl cluster = clusterTable.getClusterByClusterId(value);
3594 cluster.setImmutable(true, clusterTranslator);
3599 createdClusters.clear();
3603 public Object getModificationCounter() {
3604 return queryProvider2.modificationCounter;
3608 public void markUndoPoint() {
3609 state.setCombine(false);