1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package 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.GraphChangeListenerSupport;
147 import org.simantics.db.service.InitSupport;
148 import org.simantics.db.service.LifecycleSupport;
149 import org.simantics.db.service.ManagementSupport;
150 import org.simantics.db.service.QueryControl;
151 import org.simantics.db.service.SerialisationSupport;
152 import org.simantics.db.service.ServerInformation;
153 import org.simantics.db.service.ServiceActivityMonitor;
154 import org.simantics.db.service.SessionEventSupport;
155 import org.simantics.db.service.SessionMonitorSupport;
156 import org.simantics.db.service.SessionUserSupport;
157 import org.simantics.db.service.StatementSupport;
158 import org.simantics.db.service.TransactionPolicySupport;
159 import org.simantics.db.service.TransactionSupport;
160 import org.simantics.db.service.TransferableGraphSupport;
161 import org.simantics.db.service.UndoRedoSupport;
162 import org.simantics.db.service.VirtualGraphSupport;
163 import org.simantics.db.service.XSupport;
164 import org.simantics.layer0.Layer0;
165 import org.simantics.utils.DataContainer;
166 import org.simantics.utils.Development;
167 import org.simantics.utils.threads.logger.ITask;
168 import org.simantics.utils.threads.logger.ThreadLogger;
170 import gnu.trove.procedure.TLongProcedure;
171 import gnu.trove.set.hash.TLongHashSet;
174 public abstract class SessionImplSocket implements Session, WriteRequestScheduleSupport {
176 protected static final boolean DEBUG = false;
178 private static final boolean DIAGNOSTICS = false;
180 private TransactionPolicySupport transactionPolicy;
182 final private ClusterControl clusterControl;
184 final protected BuiltinSupportImpl builtinSupport;
185 final protected VirtualGraphServerSupportImpl virtualGraphServerSupport;
186 final protected ClusterSetsSupport clusterSetsSupport;
187 final protected LifecycleSupportImpl lifecycleSupport;
189 protected QuerySupportImpl querySupport;
190 protected ResourceSupportImpl resourceSupport;
191 protected WriteSupport writeSupport;
192 public ClusterTranslator clusterTranslator;
194 boolean dirtyPrimitives = false;
196 public static final int SERVICE_MODE_CREATE = 2;
197 public static final int SERVICE_MODE_ALLOW = 1;
198 public int serviceMode = 0;
199 public boolean createdImmutableClusters = false;
200 public TLongHashSet createdClusters = new TLongHashSet();
205 * The service locator maintained by the workbench. These services are
206 * initialized during workbench during the <code>init</code> method.
208 final protected ServiceLocatorImpl serviceLocator = new ServiceLocatorImpl();
210 final public ResourceSerializerImpl resourceSerializer = new ResourceSerializerImpl();
212 final CopyOnWriteArrayList<ChangeListener> changeListeners2 = new CopyOnWriteArrayList<ChangeListener>();
214 final CopyOnWriteArrayList<ChangeListener> metadataListeners = new CopyOnWriteArrayList<ChangeListener>();
216 final CopyOnWriteArrayList<SessionEventListener> eventListeners = new CopyOnWriteArrayList<SessionEventListener>();
218 final HashSet<Thread> sessionThreads = new HashSet<Thread>();
220 final BijectionMap<MonitorContext, MonitorHandler> monitorContexts = new BijectionMap<MonitorContext, MonitorHandler>();
222 final protected State state = new State();
224 protected GraphSession graphSession = null;
226 protected SessionManager sessionManagerImpl = null;
228 protected UserAuthenticationAgent authAgent = null;
230 protected UserAuthenticator authenticator = null;
232 protected Resource user = null;
234 protected ClusterStream clusterStream = null;
236 protected SessionRequestManager requestManager = null;
238 public ClusterTable clusterTable = null;
240 public QueryProcessor queryProvider2 = null;
242 ClientChangesImpl clientChanges = null;
244 MonitorHandler[] monitorHandlers = new MonitorHandler[0];
246 // protected long newClusterId = Constants.NullClusterId;
248 protected int flushCounter = 0;
250 protected boolean writeOnly = false;
252 WriteState<?> writeState = null;
253 WriteStateBase<?> delayedWriteState = null;
254 protected Resource defaultClusterSet = null; // If not null then used for newResource().
256 public SessionImplSocket(SessionManager sessionManagerImpl, UserAuthenticationAgent authAgent) {
258 // if (authAgent == null)
259 // throw new IllegalArgumentException("null authentication agent");
261 File t = StaticSessionProperties.virtualGraphStoragePath;
264 this.clusterTable = new ClusterTable(this, t);
265 this.builtinSupport = new BuiltinSupportImpl(this);
266 this.sessionManagerImpl = sessionManagerImpl;
268 // this.authAgent = authAgent;
270 serviceLocator.registerService(Session.class, this);
271 serviceLocator.registerService(InitSupport.class, new InitSupportImpl(this));
272 serviceLocator.registerService(ManagementSupport.class, new ManagementSupportImpl(this));
273 serviceLocator.registerService(QueryControl.class, new QueryControlImpl(this));
274 serviceLocator.registerService(SessionUserSupport.class, new SessionUserSupportImpl(this));
275 serviceLocator.registerService(GraphChangeListenerSupport.class, new GraphChangeListenerSupportImpl(this));
276 serviceLocator.registerService(SessionEventSupport.class, new SessionEventSupportImpl(this));
277 serviceLocator.registerService(SerialisationSupport.class, new SerialisationSupportImpl(this));
278 serviceLocator.registerService(UndoRedoSupport.class, new UndoRedoSupportImpl(this));
279 serviceLocator.registerService(ClusteringSupport.class, new ClusteringSupportImpl(this));
280 serviceLocator.registerService(TransactionSupport.class, new TransactionSupportImpl(this));
281 serviceLocator.registerService(SessionMonitorSupport.class, new SessionMonitorSupportImpl(this));
282 serviceLocator.registerService(TransferableGraphSupport.class, new TransferableGraphSupportImpl(this));
283 serviceLocator.registerService(QueryDebug.class, new QueryDebugImpl(this));
284 serviceLocator.registerService(CollectionSupport.class, new CollectionSupportImpl(this));
285 serviceLocator.registerService(StatementSupport.class, new StatementSupportImpl(this));
286 serviceLocator.registerService(DirectQuerySupport.class, new DirectQuerySupportImpl(this));
287 serviceLocator.registerService(XSupport.class, new XSupportImpl(this));
288 serviceLocator.registerService(DebugSupport.class, new DebugSupportImpl());
289 serviceLocator.registerService(ExternalValueSupport.class, new ExternalValueSupportImpl(this));
290 serviceLocator.registerService(RandomAccessValueSupport.class, new RandomAccessValueSupportImpl());
291 serviceLocator.registerService(ServiceActivityMonitor.class, new ServiceActivityMonitorImpl());
292 ServiceActivityUpdaterForWriteTransactions.register(this);
294 this.virtualGraphServerSupport = new VirtualGraphServerSupportImpl(this, t);
295 serviceLocator.registerService(VirtualGraphSupport.class, virtualGraphServerSupport);
296 serviceLocator.registerService(VirtualGraphServerSupport.class, virtualGraphServerSupport);
297 this.lifecycleSupport = new LifecycleSupportImpl(this);
298 serviceLocator.registerService(LifecycleSupport.class, lifecycleSupport);
299 this.transactionPolicy = new TransactionPolicyRelease();
300 serviceLocator.registerService(TransactionPolicySupport.class, transactionPolicy);
301 this.clusterControl = new ClusterControlImpl(this);
302 serviceLocator.registerService(ClusterControl.class, clusterControl);
303 this.clusterSetsSupport = new ClusterSetsSupportImpl2(); // Using same path as virtual graphs.
304 this.clusterSetsSupport.setReadDirectory(t.toPath());
305 this.clusterSetsSupport.updateWriteDirectory(t.toPath());
306 serviceLocator.registerService(ClusterSetsSupport.class, clusterSetsSupport);
316 public Resource getRootLibrary() {
317 return queryProvider2.getRootLibraryResource();
320 void refresh(int thread, ClusterUID[] clusterUID, long csid) throws DatabaseException {
321 if (!graphSession.dbSession.refreshEnabled())
324 getClusterTable().refresh(csid, this, clusterUID);
325 } catch (Throwable t) {
326 Logger.defaultLogError("Refesh failed.", t);
330 static final class TaskHelper {
331 private final String name;
332 private Object result;
333 final Semaphore sema = new Semaphore(0);
334 private Throwable throwable = null;
335 final Consumer<DatabaseException> callback = e -> {
336 synchronized (TaskHelper.this) {
340 final Procedure<Object> proc = new Procedure<Object>() {
342 public void execute(Object result) {
343 callback.accept(null);
346 public void exception(Throwable t) {
347 if (t instanceof DatabaseException)
348 callback.accept((DatabaseException)t);
350 callback.accept(new DatabaseException("" + name + "operation failed.", t));
353 final WriteTraits writeTraits = new WriteTraits() {};
354 TaskHelper(String name) {
357 @SuppressWarnings("unchecked")
361 void setResult(Object result) {
362 this.result = result;
364 // Throwable throwableGet() {
367 synchronized void throwableSet(Throwable t) {
370 // void throwableSet(String t) {
371 // throwable = new InternalException("" + name + " operation failed. " + t);
373 synchronized void throwableCheck()
374 throws DatabaseException {
375 if (null != throwable)
376 if (throwable instanceof DatabaseException)
377 throw (DatabaseException)throwable;
379 throw new DatabaseException("Undo operation failed.", throwable);
381 void throw_(String message)
382 throws DatabaseException {
383 throw new DatabaseException("" + name + " operation failed. " + message);
389 private ListenerBase getListenerBase(Object procedure) {
390 if (procedure instanceof ListenerBase)
391 return (ListenerBase) procedure;
396 public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify) {
397 scheduleRequest(request, callback, notify, null);
401 * @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)
404 public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
406 assert (request != null);
408 if(Development.DEVELOPMENT) {
410 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
411 System.err.println("schedule write '" + request + "'");
412 } catch (Throwable t) {
413 Logger.defaultLogError(t);
417 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
419 requestManager.scheduleWrite(new SessionTask(true) {
422 public void run(int thread) {
424 if(Development.DEVELOPMENT) {
426 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
427 System.err.println("perform write '" + request + "'");
428 } catch (Throwable t) {
429 Logger.defaultLogError(t);
433 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
435 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
440 Disposable.safeDispose(clientChanges);
441 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
443 VirtualGraph vg = getProvider(request.getProvider());
445 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
446 writeState = new WriteState<Object>(writer, request, notify, new Procedure<Object>() {
449 public void execute(Object result) {
450 if(callback != null) callback.accept(null);
454 public void exception(Throwable t) {
455 if(callback != null) callback.accept((DatabaseException)t);
460 assert (null != writer);
461 // writer.state.barrier.inc();
464 request.perform(writer);
465 assert (null != writer);
466 } catch (Throwable t) {
467 if (!(t instanceof CancelTransactionException))
468 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", t);
469 writeState.except(t);
471 // writer.state.barrier.dec();
472 // writer.waitAsync(request);
476 assert(!queryProvider2.cache.dirty);
478 } catch (Throwable e) {
480 // Log it first, just to be safe that the error is always logged.
481 if (!(e instanceof CancelTransactionException))
482 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
484 // writeState.getGraph().state.barrier.dec();
485 // writeState.getGraph().waitAsync(request);
487 writeState.except(e);
490 // // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
491 // // All we can do here is to log those, can't really pass them anywhere.
492 // if (callback != null) {
493 // if(e instanceof DatabaseException) callback.run((DatabaseException)e);
494 // else callback.run(new DatabaseException(e));
496 // } catch (Throwable e2) {
497 // Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", e2);
499 // boolean empty = clusterStream.reallyFlush();
500 // int callerThread = -1;
501 // ClientChangesImpl cs = new ClientChangesImpl(SessionImplSocket.this);
502 // SynchronizeContext context = new SynchronizeContext(callerThread, SessionImplSocket.this, cs, 1);
504 // // Send all local changes to server so it can calculate correct reverse change set.
505 // // This will call clusterStream.accept().
506 // state.cancelCommit(context, clusterStream);
508 // if (!context.isOk()) // this is a blocking operation
509 // throw new InternalException("Cancel failed. This should never happen. Contact application support.");
510 // getQueryProvider2().performDirtyUpdates(writeState.getGraph());
512 // state.cancelCommit2(context, clusterStream);
513 // } catch (DatabaseException e3) {
514 // Logger.defaultLogError("Write request cancel caused an unexpected error.", e3);
516 // clusterStream.setOff(false);
517 // clientChanges = new ClientChangesImpl(SessionImplSocket.this);
522 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
528 if(Development.DEVELOPMENT) {
530 if(Development.<Boolean>getProperty(DevelopmentKeys.SESSION_LOG_WRITES, Bindings.BOOLEAN))
531 System.err.println("finish write '" + request + "'");
532 } catch (Throwable t) {
533 Logger.defaultLogError(t);
543 public <T> void scheduleRequest(final WriteResult<T> request, final Procedure<T> procedure, final Semaphore notify) {
544 scheduleRequest(request, procedure, notify, null);
548 * @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)
551 public <T> void scheduleRequest(final WriteResult<T> request, final Procedure<T> procedure, final Semaphore notify, Boolean combine) {
553 assert (request != null);
555 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
557 requestManager.scheduleWrite(new SessionTask(true) {
560 public void run(int thread) {
562 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
564 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
567 Disposable.safeDispose(clientChanges);
568 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
570 VirtualGraph vg = getProvider(request.getProvider());
571 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
574 WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, procedure);
575 writeState = writeStateT;
577 assert (null != writer);
578 // writer.state.barrier.inc();
579 writeStateT.setResult(request.perform(writer));
580 assert (null != writer);
582 // writer.state.barrier.dec();
583 // writer.waitAsync(null);
585 } catch (Throwable e) {
587 // writer.state.barrier.dec();
588 // writer.waitAsync(null);
590 writeState.except(e);
592 // state.stopWriteTransaction(clusterStream);
594 // } catch (Throwable e) {
595 // // Log it first, just to be safe that the error is always logged.
596 // Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
599 // // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
600 // // All we can do here is to log those, can't really pass them anywhere.
601 // if (procedure != null) {
602 // if(e instanceof DatabaseException) procedure.exception((DatabaseException)e);
603 // else procedure.exception(new DatabaseException(e));
605 // } catch (Throwable e2) {
606 // Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", e2);
609 // clientChanges = new ClientChangesImpl(SessionImplSocket.this);
611 // state.stopWriteTransaction(clusterStream);
614 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
617 // if(notify != null) notify.release();
627 public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify) {
628 scheduleRequest(request, callback, notify, null);
632 * @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)
635 public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
637 final ITask total = ThreadLogger.getInstance().begin("ScheduleDelayedWrite");
639 assert (request != null);
641 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
643 requestManager.scheduleWrite(new SessionTask(true) {
646 public void run(int thread) {
647 fireSessionVariableChange(SessionVariables.QUEUED_READS);
649 Procedure<Object> stateProcedure = new Procedure<Object>() {
651 public void execute(Object result) {
652 if (callback != null)
653 callback.accept(null);
656 public void exception(Throwable t) {
657 if (callback != null) {
658 if (t instanceof DatabaseException) callback.accept((DatabaseException) t);
659 else callback.accept(new DatabaseException(t));
661 Logger.defaultLogError("Unhandled exception", t);
665 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
666 delayedWriteState = new WriteStateBase<Object>(request, notify, stateProcedure);
667 DelayedWriteGraph dwg = null;
668 // newGraph.state.barrier.inc();
671 dwg = new DelayedWriteGraph(newGraph);
672 request.perform(dwg);
673 } catch (Throwable e) {
674 delayedWriteState.except(e);
679 // newGraph.state.barrier.dec();
680 // newGraph.waitAsync(request);
681 fireSessionVariableChange(SessionVariables.QUEUED_READS);
684 delayedWriteState = null;
686 ITask task2 = ThreadLogger.getInstance().begin("DelayedWriteCommit");
687 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
690 Disposable.safeDispose(clientChanges);
691 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
695 VirtualGraph vg = getProvider(request.getProvider());
696 WriteSupport writeSupport = vg != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
698 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
700 writeState = new WriteState<Object>(writer, request, notify, stateProcedure);
702 assert (null != writer);
703 // writer.state.barrier.inc();
707 dwg.commit(writer, request);
709 if(defaultClusterSet != null) {
710 XSupport xs = getService(XSupport.class);
711 defaultClusterSet = xs.convertDelayedResourceToResource(defaultClusterSet);
712 ClusteringSupport cs = getService(ClusteringSupport.class);
713 clusterSetsSupport.put(defaultClusterSet.getResourceId(), cs.getCluster(defaultClusterSet));
716 // This makes clusters available from server
717 clusterStream.reallyFlush();
719 releaseWriteOnly(writer);
720 handleUpdatesAndMetadata(writer);
722 } catch (ServiceException e) {
723 // writer.state.barrier.dec();
724 // writer.waitAsync(null);
726 // These shall be requested from server
727 clusterTable.removeWriteOnlyClusters();
728 // This makes clusters available from server
729 clusterStream.reallyFlush();
731 releaseWriteOnly(writer);
732 writeState.except(e);
734 // Debugging & Profiling
735 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
745 public <T> void scheduleRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure, final Semaphore notify) {
746 scheduleRequest(request, procedure, notify, null);
750 * @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)
753 public <T> void scheduleRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure, final Semaphore notify, Boolean combine) {
754 throw new Error("Not implemented");
757 protected ClusterImpl getNewResourceCluster() throws DatabaseException {
758 ClusterImpl cluster = clusterTable.getNewResourceCluster(clusterTranslator, graphSession, writeOnly);
759 if((serviceMode & SERVICE_MODE_CREATE) > 0) {
760 createdClusters.add(cluster.clusterId);
765 class WriteOnlySupport implements WriteSupport {
767 ClusterStream stream;
768 ClusterImpl currentCluster;
770 public WriteOnlySupport() {
771 this.stream = clusterStream;
775 public void claim(VirtualGraph provider, Resource subject, Resource predicate, Resource object) throws ServiceException {
776 claim(provider, ((ResourceImpl)subject).id, ((ResourceImpl)predicate).id, ((ResourceImpl)object).id );
780 public void claim(VirtualGraph provider, int s, int p, int o) throws ServiceException {
782 ClusterImpl cluster = clusterTable.getClusterByResourceKey(s);
784 if (cluster.getImmutable() && (serviceMode & SessionImplSocket.SERVICE_MODE_ALLOW) == 0)
785 if(s != queryProvider2.getRootLibrary())
786 throw new ImmutableException("Trying to modify immutable resource key=" + s);
789 maintainCluster(cluster, cluster.addRelation(s, p, o, clusterTranslator));
790 } catch (DatabaseException e) {
791 Logger.defaultLogError(e);
795 clientChanges.invalidate(s);
797 if (cluster.isWriteOnly())
799 queryProvider2.updateStatements(s, p);
804 public void claimValue(VirtualGraph provider, Resource resource, byte[] value) throws DatabaseException {
805 claimValue(provider, ((ResourceImpl)resource).id, value, value.length);
809 public void claimValue(VirtualGraph provider, int rid, byte[] value, int length) {
811 ClusterImpl cluster = clusterTable.getClusterByResourceKey(rid);
813 maintainCluster(cluster, cluster.setValue(rid, value, length, clusterTranslator));
814 } catch (DatabaseException e) {
815 Logger.defaultLogError(e);
818 clientChanges.invalidate(rid);
820 if (cluster.isWriteOnly())
822 queryProvider2.updateValue(rid);
827 public void claimValue(VirtualGraph provider, Resource resource, ByteReader reader, int amount) throws DatabaseException {
830 claimValue(provider,resource, reader.readBytes(null, amount));
834 byte[] bytes = new byte[65536];
836 int rid = ((ResourceImpl)resource).id;
837 ClusterImpl cluster = clusterTable.getClusterByResourceKey(rid);
841 int block = Math.min(left, 65536);
842 reader.readBytes(bytes, block);
843 maintainCluster(cluster, cluster.modiValueEx(rid, amount-left, block, bytes, 0, clusterTranslator));
846 } catch (DatabaseException e) {
847 Logger.defaultLogError(e);
850 clientChanges.invalidate(rid);
852 if (cluster.isWriteOnly())
854 queryProvider2.updateValue(rid);
858 private void maintainCluster(ClusterImpl before, ClusterI after_) {
859 if(after_ != null && after_ != before) {
860 ClusterImpl after = (ClusterImpl)after_;
861 if(currentCluster == before) {
862 currentCluster = after;
864 clusterTable.replaceCluster(after);
868 public int createResourceKey(int foreignCounter) throws DatabaseException {
869 if(currentCluster == null) {
870 currentCluster = getNewResourceCluster();
872 if(currentCluster.getNumberOfResources(clusterTranslator) == ClusterTable.CLUSTER_FILL_SIZE) {
873 ClusterWriteOnly newCluster = (ClusterWriteOnly)getNewResourceCluster();
874 newCluster.foreignLookup = new byte[foreignCounter];
875 currentCluster = newCluster;
877 System.err.println("foreignLookup " + currentCluster + " " + foreignCounter);
879 return currentCluster.createResource(clusterTranslator);
883 public Resource createResource(VirtualGraph provider) throws DatabaseException {
884 if(currentCluster == null) {
885 if (null != defaultClusterSet) {
886 ResourceImpl result = getNewResource(defaultClusterSet);
887 currentCluster = clusterTable.getClusterByResourceKey(result.id);
890 currentCluster = getNewResourceCluster();
893 if(currentCluster.getNumberOfResources(clusterTranslator) >= ClusterTable.CLUSTER_FILL_SIZE) {
894 if (null != defaultClusterSet) {
895 ResourceImpl result = getNewResource(defaultClusterSet);
896 currentCluster = clusterTable.getClusterByResourceKey(result.id);
899 currentCluster = getNewResourceCluster();
902 return new ResourceImpl(resourceSupport, currentCluster.createResource(clusterTranslator));
906 public Resource createResource(VirtualGraph provider, long clusterId)
907 throws DatabaseException {
908 return getNewResource(clusterId);
912 public Resource createResource(VirtualGraph provider, Resource clusterSet)
913 throws DatabaseException {
914 return getNewResource(clusterSet);
918 public void createClusterSet(VirtualGraph provider, Resource clusterSet)
919 throws DatabaseException {
920 getNewClusterSet(clusterSet);
924 public boolean hasClusterSet(VirtualGraph dummy, Resource clusterSet)
925 throws ServiceException {
926 return containsClusterSet(clusterSet);
929 public void selectCluster(long cluster) {
930 currentCluster = clusterTable.getClusterByClusterId(cluster);
931 long setResourceId = clusterSetsSupport.getSet(cluster);
932 clusterSetsSupport.put(setResourceId, cluster);
936 public Resource setDefaultClusterSet(Resource clusterSet)
937 throws ServiceException {
938 Resource result = setDefaultClusterSet4NewResource(clusterSet);
939 if(clusterSet != null) {
940 long id = clusterSetsSupport.get(clusterSet.getResourceId());
941 currentCluster = clusterTable.getClusterByClusterId(id);
944 currentCluster = null;
950 public void denyValue(VirtualGraph provider, Resource resource) throws ServiceException {
952 provider = getProvider(provider);
953 if (null == provider) {
954 int key = ((ResourceImpl)resource).id;
958 ClusterI cluster = clusterTable.getClusterProxyByResourceKey(key);
959 // if (cluster.getImmutable() && (serviceMode & SessionImplSocket.SERVICE_MODE_ALLOW) == 0)
960 // if(key != queryProvider2.getRootLibrary())
961 // throw new ImmutableException("Trying to modify immutable resource key=" + key);
964 // cluster.removeValue(key, clusterTranslator);
965 // } catch (DatabaseException e) {
966 // Logger.defaultLogError(e);
970 clusterTable.writeOnlyInvalidate(cluster);
973 int sResourceKey = ClusterTraits.getResourceIndexFromResourceKey(key);
974 clusterTranslator.addStatementIndex(cluster, sResourceKey, cluster.getClusterUID(), ClusterChange.DELETE_OPERATION);
975 clusterTranslator.removeValue(cluster);
976 } catch (DatabaseException e) {
977 Logger.defaultLogError(e);
980 queryProvider2.invalidateResource(key);
981 clientChanges.invalidate(key);
984 ((VirtualGraphImpl)provider).denyValue(((ResourceImpl) resource).id);
985 queryProvider2.updateValue(querySupport.getId(resource));
986 clientChanges.claimValue(resource);
993 public void flush(boolean intermediate) {
994 throw new UnsupportedOperationException();
998 public void flushCluster() {
999 clusterTable.flushCluster(graphSession);
1000 if(defaultClusterSet != null) {
1001 clusterSetsSupport.put(defaultClusterSet.getResourceId(), Constants.NewClusterId);
1003 currentCluster = null;
1007 public void flushCluster(Resource r) {
1008 throw new UnsupportedOperationException("flushCluster resource " + r);
1016 public boolean removeStatement(VirtualGraph provider, Resource subject, Resource predicate,
1019 int s = ((ResourceImpl)subject).id;
1020 int p = ((ResourceImpl)predicate).id;
1021 int o = ((ResourceImpl)object).id;
1023 provider = getProvider(provider);
1024 if (null == provider) {
1026 ClusterI cluster = clusterTable.getClusterProxyByResourceKey(s);
1027 clusterTable.writeOnlyInvalidate(cluster);
1031 int sResourceKey = ClusterTraits.getResourceIndexFromResourceKey(s);
1033 ClusterI pc = clusterTable.getClusterProxyByResourceKey(p);
1034 ClusterI oc = clusterTable.getClusterProxyByResourceKey(o);
1036 clusterTranslator.addStatementIndex(cluster, sResourceKey, cluster.getClusterUID(), ClusterChange.REMOVE_OPERATION);
1037 clusterTranslator.addStatementIndex(cluster, p, pc.getClusterUID(), ClusterStream.NULL_OPERATION);
1038 clusterTranslator.addStatementIndex(cluster, o, oc.getClusterUID(), ClusterStream.NULL_OPERATION);
1039 clusterTranslator.removeStatement(cluster);
1041 queryProvider2.invalidateResource(s);
1042 clientChanges.invalidate(s);
1044 } catch (DatabaseException e) {
1046 Logger.defaultLogError(e);
1054 ((VirtualGraphImpl)provider).deny(s, p, o);
1055 queryProvider2.invalidateResource(s);
1056 clientChanges.invalidate(s);
1065 public void setValue(VirtualGraph provider, Resource resource, byte[] value) {
1066 throw new UnsupportedOperationException();
1070 public boolean writeOnly() {
1075 public void performWriteRequest(WriteGraph graph, Write request) throws DatabaseException {
1076 writeSupport.performWriteRequest(graph, request);
1080 public <T> T performWriteRequest(WriteGraph graph, WriteResult<T> request) throws DatabaseException {
1081 throw new UnsupportedOperationException();
1085 public void performWriteRequest(WriteGraph graph, WriteOnly request) throws DatabaseException {
1086 throw new UnsupportedOperationException();
1090 public <T> void addMetadata(Metadata data) throws ServiceException {
1091 writeSupport.addMetadata(data);
1095 public <T extends Metadata> T getMetadata(Class<T> clazz) throws ServiceException {
1096 return writeSupport.getMetadata(clazz);
1100 public TreeMap<String, byte[]> getMetadata() {
1101 return writeSupport.getMetadata();
1105 public void commitDone(WriteTraits writeTraits, long csid) {
1106 writeSupport.commitDone(writeTraits, csid);
1110 public void clearUndoList(WriteTraits writeTraits) {
1111 writeSupport.clearUndoList(writeTraits);
1114 public int clearMetadata() {
1115 return writeSupport.clearMetadata();
1119 public void startUndo() {
1120 writeSupport.startUndo();
1124 class VirtualWriteOnlySupport implements WriteSupport {
1127 // public void addStatement(VirtualGraph provider, Resource subject, Resource predicate,
1128 // Resource object) {
1129 // throw new UnsupportedOperationException();
1133 public void claim(VirtualGraph provider, Resource subject, Resource predicate, Resource object) {
1135 TransientGraph impl = (TransientGraph)provider;
1136 impl.claim(querySupport.getId(subject), querySupport.getId(predicate), querySupport.getId(object));
1137 getQueryProvider2().updateStatements(querySupport.getId(subject), querySupport.getId(predicate));
1138 clientChanges.claim(subject, predicate, object);
1143 public void claim(VirtualGraph provider, int subject, int predicate, int object) {
1145 TransientGraph impl = (TransientGraph)provider;
1146 impl.claim(subject, predicate, object);
1147 getQueryProvider2().updateStatements(subject, predicate);
1148 clientChanges.claim(subject, predicate, object);
1153 public void claimValue(VirtualGraph provider, Resource resource, byte[] value) throws DatabaseException {
1154 claimValue(provider, ((ResourceImpl)resource).id, value, value.length);
1158 public void claimValue(VirtualGraph provider, int resource, byte[] value, int length) {
1159 ((VirtualGraphImpl)provider).claimValue(resource, value, length);
1160 getQueryProvider2().updateValue(resource);
1161 clientChanges.claimValue(resource);
1165 public void claimValue(VirtualGraph provider, Resource resource, ByteReader reader, int amount) throws DatabaseException {
1166 byte[] value = reader.readBytes(null, amount);
1167 claimValue(provider, resource, value);
1171 public Resource createResource(VirtualGraph provider) {
1172 TransientGraph impl = (TransientGraph)provider;
1173 return impl.getResource(impl.newResource(false));
1177 public Resource createResource(VirtualGraph provider, long clusterId) {
1178 throw new UnsupportedOperationException();
1182 public Resource createResource(VirtualGraph provider, Resource clusterSet)
1183 throws DatabaseException {
1184 throw new UnsupportedOperationException();
1188 public void createClusterSet(VirtualGraph provider, Resource clusterSet)
1189 throws DatabaseException {
1190 throw new UnsupportedOperationException();
1194 public boolean hasClusterSet(VirtualGraph provider, Resource clusterSet)
1195 throws ServiceException {
1196 throw new UnsupportedOperationException();
1200 public Resource setDefaultClusterSet(Resource clusterSet)
1201 throws ServiceException {
1206 public void denyValue(VirtualGraph provider, Resource resource) {
1207 ((VirtualGraphImpl)provider).denyValue(((ResourceImpl) resource).id);
1208 getQueryProvider2().updateValue(querySupport.getId(resource));
1209 // NOTE: this only keeps track of value changes by-resource.
1210 clientChanges.claimValue(resource);
1214 public void flush(boolean intermediate) {
1215 throw new UnsupportedOperationException();
1219 public void flushCluster() {
1220 throw new UnsupportedOperationException();
1224 public void flushCluster(Resource r) {
1225 throw new UnsupportedOperationException("Resource " + r);
1233 public boolean removeStatement(VirtualGraph provider, Resource subject, Resource predicate, Resource object) {
1234 TransientGraph impl = (TransientGraph) provider;
1235 impl.deny(querySupport.getId(subject), querySupport.getId(predicate), querySupport.getId(object));
1236 getQueryProvider2().updateStatements(querySupport.getId(subject), querySupport.getId(predicate));
1237 clientChanges.deny(subject, predicate, object);
1242 public void setValue(VirtualGraph provider, Resource resource, byte[] value) {
1243 throw new UnsupportedOperationException();
1247 public boolean writeOnly() {
1252 public void performWriteRequest(WriteGraph graph, Write request) throws DatabaseException {
1253 throw new UnsupportedOperationException();
1257 public <T> T performWriteRequest(WriteGraph graph, WriteResult<T> request) throws DatabaseException {
1258 throw new UnsupportedOperationException();
1262 public void performWriteRequest(WriteGraph graph, WriteOnly request) throws DatabaseException {
1263 throw new UnsupportedOperationException();
1267 public <T> void addMetadata(Metadata data) throws ServiceException {
1268 throw new UnsupportedOperationException();
1272 public <T extends Metadata> T getMetadata(Class<T> clazz) throws ServiceException {
1273 throw new UnsupportedOperationException();
1277 public TreeMap<String, byte[]> getMetadata() {
1278 throw new UnsupportedOperationException();
1282 public void commitDone(WriteTraits writeTraits, long csid) {
1286 public void clearUndoList(WriteTraits writeTraits) {
1290 public int clearMetadata() {
1295 public void startUndo() {
1299 private <T> void performWriteOnly(WriteOnlyResult<T> request, Semaphore notify, Procedure<T> callback) {
1303 //int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1305 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1308 Disposable.safeDispose(clientChanges);
1309 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
1313 WriteSupport writeSupport = request.getProvider() != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
1315 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, request.getProvider());
1317 WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, callback);
1318 writeState = writeStateT;
1320 assert (null != writer);
1321 // writer.state.barrier.inc();
1322 long start = System.nanoTime();
1323 T result = request.perform(writer);
1324 long duration = System.nanoTime() - start;
1326 System.err.println("################");
1327 System.err.println("WriteOnly duration " + 1e-9*duration);
1329 writeStateT.setResult(result);
1331 // This makes clusters available from server
1332 clusterStream.reallyFlush();
1334 // This will trigger query updates
1335 releaseWriteOnly(writer);
1336 assert (null != writer);
1338 handleUpdatesAndMetadata(writer);
1340 } catch (CancelTransactionException e) {
1342 releaseWriteOnly(writeState.getGraph());
1344 clusterTable.removeWriteOnlyClusters();
1345 state.stopWriteTransaction(clusterStream);
1347 } catch (Throwable e) {
1349 e.printStackTrace();
1351 releaseWriteOnly(writeState.getGraph());
1353 clusterTable.removeWriteOnlyClusters();
1355 if (callback != null)
1356 callback.exception(new DatabaseException(e));
1358 state.stopWriteTransaction(clusterStream);
1359 Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
1363 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1369 public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify) {
1370 scheduleRequest(request, callback, notify, null);
1374 public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
1378 assert (request != null);
1380 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1382 requestManager.scheduleWrite(new SessionTask(true) {
1385 public void run(int thread) {
1387 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
1391 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1394 Disposable.safeDispose(clientChanges);
1395 clientChanges = new ClientChangesImpl(SessionImplSocket.this);
1399 VirtualGraph vg = getProvider(request.getProvider());
1400 WriteSupport writeSupport = vg != null ? new VirtualWriteOnlySupport() : new WriteOnlySupport();
1402 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
1404 writeState = new WriteState<Object>(writer, request, notify, new Procedure<Object>() {
1407 public void execute(Object result) {
1408 if(callback != null) callback.accept(null);
1412 public void exception(Throwable t) {
1413 if(callback != null) callback.accept((DatabaseException)t);
1418 assert (null != writer);
1419 // writer.state.barrier.inc();
1423 request.perform(writer);
1425 } catch (Throwable e) {
1427 // writer.state.barrier.dec();
1428 // writer.waitAsync(null);
1430 releaseWriteOnly(writer);
1432 clusterTable.removeWriteOnlyClusters();
1434 if(!(e instanceof CancelTransactionException)) {
1435 if (callback != null)
1436 callback.accept(new DatabaseException(e));
1439 writeState.except(e);
1445 // This makes clusters available from server
1446 boolean empty = clusterStream.reallyFlush();
1447 // This was needed to make WO requests call metadata listeners.
1448 // NOTE: the calling event does not contain clientChanges information.
1449 if (!empty && clientChanges.isEmpty())
1450 clientChanges.setNotEmpty(true);
1451 releaseWriteOnly(writer);
1452 assert (null != writer);
1455 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
1468 public <T> void scheduleRequest(final WriteOnlyResult<T> request, final Procedure<T> callback, final Semaphore notify) {
1469 scheduleRequest(request, callback, notify, null);
1473 * @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)
1476 public <T> void scheduleRequest(final WriteOnlyResult<T> request, final Procedure<T> callback, final Semaphore notify, Boolean combine) {
1478 assert (request != null);
1480 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1482 requestManager.scheduleWrite(new SessionTask(true) {
1485 public void run(int thread) {
1487 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
1489 performWriteOnly(request, notify, callback);
1499 public <T> void scheduleRequest(final Read<T> request, final AsyncProcedure<T> procedure, final Semaphore notify, final DataContainer<Throwable> throwable, final DataContainer<T> result) {
1501 assert (request != null);
1502 assert (procedure != null);
1504 //int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1506 requestManager.scheduleRead(new SessionRead(throwable, notify) {
1509 public void run(int thread) {
1511 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1513 ListenerBase listener = getListenerBase(procedure);
1515 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1519 if (listener != null) {
1523 AsyncProcedure ap = new AsyncProcedure<T>() {
1526 public void exception(AsyncReadGraph graph, Throwable t) {
1527 procedure.exception(graph, t);
1528 if(throwable != null) {
1531 // ErrorLogger.defaultLogError("Unhandled exception", t);
1536 public void execute(AsyncReadGraph graph, T t) {
1537 if(result != null) result.set(t);
1538 procedure.execute(graph, t);
1543 QueryCache.runnerReadEntry(newGraph, request, null, listener, ap, true);
1545 } catch (Throwable t) {
1546 // This is handled by the AsyncProcedure
1547 //Logger.defaultLogError("Internal error", t);
1554 // newGraph.state.barrier.inc();
1556 T t = request.perform(newGraph);
1560 if(result != null) result.set(t);
1561 procedure.execute(newGraph, t);
1563 } catch (Throwable th) {
1565 if(throwable != null) {
1568 Logger.defaultLogError("Unhandled exception", th);
1573 } catch (Throwable t) {
1576 t.printStackTrace();
1578 if(throwable != null) {
1581 Logger.defaultLogError("Unhandled exception", t);
1586 procedure.exception(newGraph, t);
1588 } catch (Throwable t2) {
1590 if(throwable != null) {
1593 Logger.defaultLogError("Unhandled exception", t2);
1600 // newGraph.state.barrier.dec();
1601 // newGraph.waitAsync(request);
1607 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1617 public <T> void scheduleRequest(final AsyncRead<T> request, final AsyncProcedure<T> procedure, final ListenerBase listener, final Semaphore notify) {
1619 assert (request != null);
1620 assert (procedure != null);
1622 int thread = request.hashCode() & queryProvider2.THREAD_MASK;
1624 requestManager.scheduleRead(new SessionRead(null, notify) {
1627 public void run(int thread) {
1629 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1631 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
1635 if (listener != null) {
1638 QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
1639 //QueryCache.runnerAsyncReadEntry(newGraph, request, null, listener, procedure, true);
1640 //newGraph.processor.query(newGraph, request, null, procedure, listener);
1641 } catch (DatabaseException e) {
1642 Logger.defaultLogError(e);
1647 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
1648 // procedure, "request");
1650 BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph, procedure, request);
1654 request.perform(newGraph, wrap);
1657 } catch (Throwable t) {
1659 wrap.exception(newGraph, t);
1661 // wrapper.exception(newGraph, t);
1662 // newGraph.waitAsync(request);
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 run(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 run(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 run(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);
1822 // newGraph.processor.query(newGraph, request, null, new Procedure<T>() {
1825 // public void exception(Throwable t) {
1826 // procedure.exception(t);
1827 // if(throwable != null) {
1828 // throwable.set(t);
1833 // public void execute(T t) {
1834 // if(result != null) result.set(t);
1835 // procedure.execute(t);
1840 // newGraph.waitAsync(request);
1844 // newGraph.state.barrier.inc();
1846 request.register(newGraph, new Listener<T>() {
1849 public void exception(Throwable t) {
1850 if(throwable != null) throwable.set(t);
1851 procedure.exception(t);
1852 // newGraph.state.barrier.dec();
1856 public void execute(T t) {
1857 if(result != null) result.set(t);
1858 procedure.execute(t);
1859 // newGraph.state.barrier.dec();
1863 public boolean isDisposed() {
1869 // newGraph.waitAsync(request);
1875 fireSessionVariableChange(SessionVariables.QUEUED_READS);
1887 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
1889 scheduleRequest(request, procedure, null, null, null);
1894 public <T> T syncRequest(Read<T> request, AsyncProcedure<T> procedure) throws DatabaseException {
1896 Semaphore notify = new Semaphore(0);
1897 DataContainer<Throwable> container = new DataContainer<Throwable>();
1898 DataContainer<T> result = new DataContainer<T>();
1899 scheduleRequest(request, procedure, notify, container, result);
1900 acquire(notify, request);
1901 Throwable throwable = container.get();
1902 if(throwable != null) {
1903 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1904 else throw new DatabaseException("Unexpected exception", throwable);
1906 return result.get();
1910 public <T> T syncRequest(AsyncRead<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1912 Semaphore notify = new Semaphore(0);
1913 final DataContainer<Throwable> exceptionContainer = new DataContainer<Throwable>();
1914 final DataContainer<T> resultContainer = new DataContainer<T>();
1915 scheduleRequest(request, new AsyncProcedure<T>() {
1917 public void exception(AsyncReadGraph graph, Throwable throwable) {
1918 exceptionContainer.set(throwable);
1919 procedure.exception(graph, throwable);
1922 public void execute(AsyncReadGraph graph, T result) {
1923 resultContainer.set(result);
1924 procedure.execute(graph, result);
1926 }, getListenerBase(procedure), notify);
1927 acquire(notify, request, procedure);
1928 Throwable throwable = exceptionContainer.get();
1929 if (throwable != null) {
1930 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1931 else throw new DatabaseException("Unexpected exception", throwable);
1933 return resultContainer.get();
1938 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, final AsyncMultiProcedure<T> procedure) throws DatabaseException {
1940 Semaphore notify = new Semaphore(0);
1941 final DataContainer<Throwable> exceptionContainer = new DataContainer<Throwable>();
1942 scheduleRequest(request, new AsyncMultiProcedure<T>() {
1944 public void exception(AsyncReadGraph graph, Throwable throwable) {
1945 exceptionContainer.set(throwable);
1946 procedure.exception(graph, throwable);
1949 public void execute(AsyncReadGraph graph, T result) {
1950 procedure.execute(graph, result);
1953 public void finished(AsyncReadGraph graph) {
1954 procedure.finished(graph);
1957 acquire(notify, request, procedure);
1958 Throwable throwable = exceptionContainer.get();
1959 if (throwable != null) {
1960 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1961 else throw new DatabaseException("Unexpected exception", throwable);
1963 // TODO: implement return value
1964 System.err.println("TODO: implement return value for syncRequest(AsyncMultiRead, AsyncMultiProcedure)");
1969 public <T> T syncRequest(final ExternalRead<T> request) throws DatabaseException {
1970 return syncRequest(request, new ProcedureAdapter<T>());
1973 // assert(request != null);
1975 // final DataContainer<T> result = new DataContainer<T>();
1976 // final DataContainer<Throwable> exception = new DataContainer<Throwable>();
1978 // syncRequest(request, new Procedure<T>() {
1981 // public void execute(T t) {
1986 // public void exception(Throwable t) {
1987 // exception.set(t);
1992 // Throwable t = exception.get();
1994 // if(t instanceof DatabaseException) throw (DatabaseException)t;
1995 // else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(PrimitiveRead)", t);
1998 // return result.get();
2003 public <T> T syncRequest(ExternalRead<T> request, final Listener<T> procedure) throws DatabaseException {
2004 return syncRequest(request, (Procedure<T>)procedure);
2009 // public <T> T syncRequest(Read<T> request, AsyncProcedure<T> procedure) throws DatabaseException {
2010 // assertNotSession();
2011 // Semaphore notify = new Semaphore(0);
2012 // DataContainer<Throwable> container = new DataContainer<Throwable>();
2013 // DataContainer<T> result = new DataContainer<T>();
2014 // scheduleRequest(request, procedure, notify, container, result);
2015 // acquire(notify, request);
2016 // Throwable throwable = container.get();
2017 // if(throwable != null) {
2018 // if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
2019 // else throw new DatabaseException("Unexpected exception", throwable);
2021 // return result.get();
2026 public <T> T syncRequest(ExternalRead<T> request, final Procedure<T> procedure) throws DatabaseException {
2028 Semaphore notify = new Semaphore(0);
2029 final DataContainer<Throwable> container = new DataContainer<Throwable>();
2030 final DataContainer<T> result = new DataContainer<T>();
2031 scheduleRequest(request, procedure, notify, container, result);
2032 acquire(notify, request);
2033 Throwable throwable = container.get();
2034 if (throwable != null) {
2035 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
2036 else throw new DatabaseException("Unexpected exception", throwable);
2038 return result.get();
2042 public void syncRequest(Write request) throws DatabaseException {
2045 Semaphore notify = new Semaphore(0);
2046 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2047 scheduleRequest(request, e -> exception.set(e), notify);
2048 acquire(notify, request);
2049 if(exception.get() != null) throw exception.get();
2053 public <T> T syncRequest(WriteResult<T> request) throws DatabaseException {
2055 Semaphore notify = new Semaphore(0);
2056 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2057 final DataContainer<T> result = new DataContainer<T>();
2058 scheduleRequest(request, new Procedure<T>() {
2061 public void exception(Throwable t) {
2066 public void execute(T t) {
2071 acquire(notify, request);
2072 if(exception.get() != null) {
2073 Throwable t = exception.get();
2074 if(t instanceof DatabaseException) throw (DatabaseException)t;
2075 else throw new DatabaseException(t);
2077 return result.get();
2081 public <T> T syncRequest(WriteOnlyResult<T> request) throws DatabaseException {
2083 Semaphore notify = new Semaphore(0);
2084 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2085 final DataContainer<T> result = new DataContainer<T>();
2086 scheduleRequest(request, new Procedure<T>() {
2089 public void exception(Throwable t) {
2094 public void execute(T t) {
2099 acquire(notify, request);
2100 if(exception.get() != null) {
2101 Throwable t = exception.get();
2102 if(t instanceof DatabaseException) throw (DatabaseException)t;
2103 else throw new DatabaseException(t);
2105 return result.get();
2109 public <T> T syncRequest(DelayedWriteResult<T> request) throws DatabaseException {
2111 Semaphore notify = new Semaphore(0);
2112 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2113 final DataContainer<T> result = new DataContainer<T>();
2114 scheduleRequest(request, new Procedure<T>() {
2117 public void exception(Throwable t) {
2122 public void execute(T t) {
2127 acquire(notify, request);
2128 if(exception.get() != null) {
2129 Throwable t = exception.get();
2130 if(t instanceof DatabaseException) throw (DatabaseException)t;
2131 else throw new DatabaseException(t);
2133 return result.get();
2137 public void syncRequest(DelayedWrite request) throws DatabaseException {
2139 Semaphore notify = new Semaphore(0);
2140 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2141 scheduleRequest(request, e -> exception.set(e), notify);
2142 acquire(notify, request);
2143 if(exception.get() != null) throw exception.get();
2147 public void syncRequest(WriteOnly request) throws DatabaseException {
2150 Semaphore notify = new Semaphore(0);
2151 final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
2152 scheduleRequest(request, e -> exception.set(e), notify);
2153 acquire(notify, request);
2154 if(exception.get() != null) throw exception.get();
2159 * GraphRequestProcessor interface
2163 public <T> void asyncRequest(final AsyncRead<T> request, final AsyncProcedure<T> procedure) {
2165 scheduleRequest(request, procedure, null, null);
2170 public <T> void asyncRequest(final AsyncMultiRead<T> request, final AsyncMultiProcedure<T> procedure) {
2172 scheduleRequest(request, procedure, null);
2177 public <T> void asyncRequest(final ExternalRead<T> request, final Procedure<T> procedure) {
2179 scheduleRequest(request, procedure, null, null, null);
2184 public void asyncRequest(final Write request, final Consumer<DatabaseException> callback) {
2186 scheduleRequest(request, callback, null);
2191 public <T> void asyncRequest(final WriteResult<T> request, final Procedure<T> procedure) {
2193 scheduleRequest(request, procedure, null);
2198 public <T> void asyncRequest(final WriteOnlyResult<T> request, final Procedure<T> procedure) {
2200 scheduleRequest(request, procedure, null);
2205 public <T> void asyncRequest(final DelayedWriteResult<T> request, final Procedure<T> procedure) {
2207 scheduleRequest(request, procedure, null);
2212 public void asyncRequest(final DelayedWrite request, final Consumer<DatabaseException> callback) {
2214 scheduleRequest(request, callback, null);
2219 public void asyncRequest(final Write r) {
2220 asyncRequest(r, null);
2224 public void asyncRequest(final DelayedWrite r) {
2225 asyncRequest(r, null);
2229 public void asyncRequest(final WriteOnly request, final Consumer<DatabaseException> callback) {
2231 scheduleRequest(request, callback, null);
2236 public void asyncRequest(final WriteOnly request) {
2238 asyncRequest(request, null);
2243 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2244 r.request(this, procedure);
2248 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2249 r.request(this, procedure);
2253 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2254 r.request(this, procedure);
2258 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2259 r.request(this, procedure);
2263 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2264 r.request(this, procedure);
2268 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2269 r.request(this, procedure);
2273 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2274 return r.request(this);
2278 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2279 return r.request(this);
2283 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2284 r.request(this, procedure);
2288 public <T> void async(WriteInterface<T> r) {
2289 r.request(this, new ProcedureAdapter<T>());
2293 public void incAsync() {
2298 public void decAsync() {
2302 public long getCluster(ResourceImpl resource) {
2303 ClusterI cluster = clusterTable.getClusterByResourceKey(resource.id);
2304 return cluster.getClusterId();
2307 public long getCluster(int id) {
2308 if (clusterTable == null)
2309 System.out.println("SessionImplSocket.getCluster() clusterTable == null !!!!! how come");
2310 return clusterTable.getClusterIdByResourceKeyNoThrow(id);
2313 public ResourceImpl getResource(int id) {
2314 return new ResourceImpl(resourceSupport, id);
2317 public ResourceImpl getResource(int resourceIndex, long clusterId) {
2318 assert (!ClusterTraitsBase.isIllegalResourceIndex(resourceIndex));
2319 ClusterI proxy = clusterTable.getClusterByClusterId(clusterId);
2320 int key = proxy.getClusterKey();
2321 int resourceKey = ClusterTraitsBase.createResourceKeyNoThrow(key, resourceIndex);
2322 return new ResourceImpl(resourceSupport, resourceKey);
2325 public ResourceImpl getResource2(int id) {
2327 return new ResourceImpl(resourceSupport, id);
2330 final public int getId(ResourceImpl impl) {
2334 public static final Charset UTF8 = Charset.forName("utf-8");
2339 static boolean areVirtualStatementsLoaded(final VirtualGraphServerSupportImpl support, int subject) {
2340 for(TransientGraph g : support.providers) {
2341 if(g.isPending(subject)) return false;
2346 static boolean areVirtualStatementsLoaded(final VirtualGraphServerSupportImpl support, int subject, int predicate) {
2347 for(TransientGraph g : support.providers) {
2348 if(g.isPending(subject, predicate)) return false;
2353 static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, final Consumer<ReadGraphImpl> runnable) {
2355 Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
2357 AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
2360 public void accept(ReadGraphImpl graph) {
2361 if(ready.decrementAndGet() == 0) {
2362 runnable.accept(graph);
2368 for(TransientGraph g : support.providers) {
2369 if(g.isPending(subject)) {
2371 g.load(graph, subject, composite);
2372 } catch (DatabaseException e) {
2373 e.printStackTrace();
2376 composite.accept(graph);
2380 composite.accept(graph);
2384 static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, int predicate, final Consumer<ReadGraphImpl> runnable) {
2386 Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
2388 AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
2391 public void accept(ReadGraphImpl graph) {
2392 if(ready.decrementAndGet() == 0) {
2393 runnable.accept(graph);
2399 for(TransientGraph g : support.providers) {
2400 if(g.isPending(subject, predicate)) {
2402 g.load(graph, subject, predicate, composite);
2403 } catch (DatabaseException e) {
2404 e.printStackTrace();
2407 composite.accept(graph);
2411 composite.accept(graph);
2415 // void dumpHeap() {
2418 // ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
2419 // "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class).dumpHeap(
2420 // "d:/heap" + flushCounter + ".txt", true);
2421 // } catch (IOException e) {
2422 // e.printStackTrace();
2427 void fireReactionsToSynchronize(ChangeSet cs) {
2429 // Do not fire empty events
2433 ReadGraphImpl g = ReadGraphImpl.create(getQueryProvider2());
2434 // g.state.barrier.inc();
2438 if (!cs.isEmpty()) {
2439 ChangeEvent e2 = new ChangeEvent(g.getSession(), g, null, cs);
2440 for (ChangeListener l : changeListeners2) {
2443 } catch (Exception ex) {
2444 ex.printStackTrace();
2451 // g.state.barrier.dec();
2452 // g.waitAsync(null);
2458 void fireReactionsToCommit(ReadGraphImpl graph, ChangeSet cs2) {
2461 // Do not fire empty events
2466 // graph.state.barrier.inc();
2470 if (!cs2.isEmpty()) {
2472 ChangeEvent e2 = new ChangeEvent(graph.getSession(), graph, null, cs2);
2473 for (ChangeListener l : changeListeners2) {
2476 // System.out.println("changelistener " + l);
2477 } catch (Exception ex) {
2478 ex.printStackTrace();
2486 // graph.state.barrier.dec();
2487 // graph.waitAsync(null);
2490 } catch (Throwable t) {
2491 t.printStackTrace();
2495 void fireMetadataListeners(WriteGraphImpl graph, ChangeSet cs2) {
2499 // Do not fire empty events
2503 // Do not fire on virtual requests
2504 if(graph.getProvider() != null)
2507 WriteGraphImpl reactionGraph = WriteGraphImpl.create(graph.processor, writeSupport, null);
2511 ChangeEvent e2 = new ChangeEvent(graph.getSession(), reactionGraph, graph, cs2);
2512 for (ChangeListener l : metadataListeners) {
2515 } catch (Throwable ex) {
2516 ex.printStackTrace();
2524 } catch (Throwable t) {
2525 t.printStackTrace();
2534 * @see org.simantics.db.ServiceLocator#getService(java.lang.Class)
2537 public <T> T getService(Class<T> api) {
2538 T t = peekService(api);
2540 if (state.isClosed())
2541 throw new ServiceNotFoundException(this, api, "Session has been shut down");
2542 throw new ServiceNotFoundException(this, api);
2550 protected abstract ServerInformation getCachedServerInformation();
2552 private Class<?> serviceKey1 = null;
2553 private Class<?> serviceKey2 = null;
2554 private Object service1 = null;
2555 private Object service2 = null;
2561 * fi.vtt.simantics.db.connection.ServiceLocator#peekService(java.lang.Class)
2563 @SuppressWarnings("unchecked")
2565 public synchronized <T> T peekService(Class<T> api) {
2567 if(serviceKey1 == api) {
2569 } else if (serviceKey2 == api) {
2571 Object result = service2;
2572 service2 = service1;
2573 serviceKey2 = serviceKey1;
2579 if (Layer0.class == api)
2581 if (ServerInformation.class == api)
2582 return (T) getCachedServerInformation();
2583 else if (WriteGraphImpl.class == api)
2584 return (T) writeState.getGraph();
2585 else if (ClusterBuilder.class == api)
2586 return (T)new ClusterBuilderImpl(this, (WriteOnlySupport)writeState.getGraph().writeSupport);
2587 else if (ClusterBuilderFactory.class == api)
2588 return (T)new ClusterBuilderFactoryImpl(this);
2590 service2 = service1;
2591 serviceKey2 = serviceKey1;
2593 service1 = serviceLocator.peekService(api);
2604 * fi.vtt.simantics.db.connection.ServiceLocator#hasService(java.lang.Class)
2607 public boolean hasService(Class<?> api) {
2608 return serviceLocator.hasService(api);
2612 * @param api the api that must be implemented by the specified service
2613 * @param service the service implementation
2616 public <T> void registerService(Class<T> api, T service) {
2617 if(Layer0.class == api) {
2618 L0 = (Layer0)service;
2621 serviceLocator.registerService(api, service);
2622 if (TransactionPolicySupport.class == api) {
2623 transactionPolicy = (TransactionPolicySupport)service;
2624 state.resetTransactionPolicy();
2626 if (api == serviceKey1)
2628 else if (api == serviceKey2)
2636 void fireSessionVariableChange(String variable) {
2637 for (MonitorHandler h : monitorHandlers) {
2638 MonitorContext ctx = monitorContexts.getLeft(h);
2640 // SafeRunner functionality repeated here to avoid dependency.
2642 h.valuesChanged(ctx);
2643 } catch (Exception e) {
2644 Logger.defaultLogError("monitor handler notification produced the following exception", e);
2645 } catch (LinkageError e) {
2646 Logger.defaultLogError("monitor handler notification produced a linkage error", e);
2652 class ResourceSerializerImpl implements ResourceSerializer {
2654 public long createRandomAccessId(int id) throws DatabaseException {
2657 int index = ClusterTraitsBase.getResourceIndexFromResourceKey(id);
2658 long cluster = getCluster(id);
2660 return 0; // Better to return 0 then invalid id.
2661 long result = ClusterTraitsBase.createResourceId(cluster, index);
2666 public long getRandomAccessId(Resource resource) throws DatabaseException {
2668 ResourceImpl resourceImpl = (ResourceImpl) resource;
2669 return createRandomAccessId(resourceImpl.id);
2674 public int getTransientId(Resource resource) throws DatabaseException {
2676 ResourceImpl resourceImpl = (ResourceImpl) resource;
2677 return resourceImpl.id;
2682 public String createRandomAccessId(Resource resource)
2683 throws InvalidResourceReferenceException {
2685 if(resource == null) throw new IllegalArgumentException();
2687 ResourceImpl resourceImpl = (ResourceImpl) resource;
2689 if (resourceImpl.id < 0) return String.valueOf(resourceImpl.id) + "_0";
2693 r = ClusterTraits.getResourceIndexFromResourceKey(resourceImpl.id);
2694 } catch (DatabaseException e1) {
2695 throw new InvalidResourceReferenceException(e1);
2698 // Serialize as '<resource index>_<cluster id>'
2699 return "" + r + "_" + getCluster(resourceImpl);
2700 } catch (Throwable e) {
2701 e.printStackTrace();
2702 throw new InvalidResourceReferenceException(e);
2707 public int getTransientId(long serialized) throws DatabaseException {
2708 if (serialized <= 0)
2709 return (int)serialized;
2710 int index = ClusterTraitsBase.getResourceIndexFromResourceId(serialized);
2711 long c = ClusterTraitsBase.getClusterIdFromResourceId(serialized);
2712 ClusterI cluster = clusterTranslator.getClusterByClusterId(c);
2713 if (cluster == null)
2714 throw new DatabaseException("Couldn't load cluster for id " + c + " for resource id " + serialized);
2715 int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), index);
2720 public Resource getResource(long randomAccessId) throws DatabaseException {
2721 return getResourceByKey(getTransientId(randomAccessId));
2725 public Resource getResource(int transientId) throws DatabaseException {
2726 return getResourceByKey(transientId);
2730 public Resource getResource(String randomAccessId)
2731 throws InvalidResourceReferenceException {
2733 int i = randomAccessId.indexOf('_');
2735 throw new InvalidResourceReferenceException("Could not parse resource id + cluster id from '"
2736 + randomAccessId + "'");
2737 int r = Integer.parseInt(randomAccessId.substring(0, i));
2738 if(r < 0) return getResourceByKey(r);
2739 long c = Long.parseLong(randomAccessId.substring(i + 1));
2740 ClusterI cluster = clusterTranslator.getClusterByClusterId(c);
2741 int key = ClusterTraits.createResourceKey(cluster.getClusterKey(), r);
2742 if (cluster.hasResource(key, clusterTranslator))
2743 return getResourceByKey(key);
2744 } catch (InvalidResourceReferenceException e) {
2746 } catch (NumberFormatException e) {
2747 throw new InvalidResourceReferenceException(e);
2748 } catch (Throwable e) {
2749 e.printStackTrace();
2750 throw new InvalidResourceReferenceException(e);
2753 throw new InvalidResourceReferenceException("Resource does not exist " + randomAccessId);
2757 public boolean disposeRandomAccessId(String randomAccessId) throws InvalidResourceReferenceException {
2760 } catch (Throwable e) {
2761 e.printStackTrace();
2762 throw new InvalidResourceReferenceException(e);
2768 // Copied from old SessionImpl
2771 if (state.isClosed())
2772 throw new Error("Session closed.");
2777 public ResourceImpl getNewResource(long clusterId) {
2778 ClusterI cluster = clusterTable.getClusterByClusterIdOrThrow(clusterId);
2781 newId = cluster.createResource(clusterTranslator);
2782 } catch (DatabaseException e) {
2783 Logger.defaultLogError(e);
2786 return new ResourceImpl(resourceSupport, newId);
2789 public ResourceImpl getNewResource(Resource clusterSet)
2790 throws DatabaseException {
2791 long resourceId = clusterSet.getResourceId();
2792 Long clusterId = clusterSetsSupport.get(resourceId);
2793 if (null == clusterId)
2794 throw new ClusterSetExistException("Cluster set does not exist. Resource=" + clusterSet);
2795 if (Constants.NewClusterId == clusterId) {
2796 clusterId = getService(ClusteringSupport.class).createCluster();
2797 if ((serviceMode & SERVICE_MODE_CREATE) > 0)
2798 createdClusters.add(clusterId);
2799 clusterSetsSupport.put(resourceId, clusterId);
2800 return getNewResource(clusterId);
2802 ClusterBase cb = clusterTranslator.getClusterByClusterId(clusterId);
2803 ResourceImpl result;
2804 if (cb.getNumberOfResources(clusterTranslator) >= ClusterTable.CLUSTER_FILL_SIZE) {
2805 clusterId = getService(ClusteringSupport.class).createCluster();
2806 if ((serviceMode & SERVICE_MODE_CREATE) > 0)
2807 createdClusters.add(clusterId);
2808 clusterSetsSupport.put(resourceId, clusterId);
2809 return getNewResource(clusterId);
2811 result = getNewResource(clusterId);
2812 int resultKey = querySupport.getId(result);
2813 long resultCluster = querySupport.getClusterId(resultKey);
2814 if (clusterId != resultCluster)
2815 clusterSetsSupport.put(resourceId, resultCluster);
2821 public void getNewClusterSet(Resource clusterSet)
2822 throws DatabaseException {
2824 System.out.println("new cluster set=" + clusterSet);
2825 long resourceId = clusterSet.getResourceId();
2826 if (clusterSetsSupport.containsKey(resourceId))
2827 throw new ClusterSetExistException("Cluster set exist already. Resource=" + clusterSet);
2828 clusterSetsSupport.put(resourceId, Constants.NewClusterId);
2830 public boolean containsClusterSet(Resource clusterSet)
2831 throws ServiceException {
2832 long resourceId = clusterSet.getResourceId();
2833 return clusterSetsSupport.containsKey(resourceId);
2835 public Resource setDefaultClusterSet4NewResource(Resource clusterSet) {
2836 Resource r = defaultClusterSet;
2837 defaultClusterSet = clusterSet;
2840 void printDiagnostics() {
2844 final DataContainer<Long> totalMem = new DataContainer<Long>(0L);
2845 final DataContainer<Long> residentClusters = new DataContainer<Long>(0L);
2846 for(ClusterI cluster : clusterTable.getClusters()) {
2848 if (cluster.isLoaded() && !cluster.isEmpty()) {
2849 residentClusters.set(residentClusters.get() + 1);
2850 totalMem.set(totalMem.get() + cluster.getUsedSpace());
2852 } catch (DatabaseException e) {
2853 Logger.defaultLogError(e);
2857 System.out.println("--------------------------------");
2858 System.out.println("Cluster information:");
2859 System.out.println("-amount of resident clusters=" + residentClusters.get());
2860 System.out.println("-total memory usage=" + totalMem.get() / 1024 + "kB");
2862 for(ClusterI cluster : clusterTable.getClusters()) {
2863 System.out.print("Cluster " + cluster.getClusterId() + " [");
2865 if (!cluster.isLoaded())
2866 System.out.println("not loaded]");
2867 else if (cluster.isEmpty())
2868 System.out.println("is empty]");
2870 System.out.print("resources=" + cluster.getNumberOfResources(clusterTranslator));
2871 System.out.print(",references=" + cluster.getReferenceCount());
2872 System.out.println(",memory=" + cluster.getUsedSpace() / 1024 + "kB]");
2874 } catch (DatabaseException e) {
2875 Logger.defaultLogError(e);
2876 System.out.println("is corrupted]");
2880 queryProvider2.printDiagnostics();
2881 System.out.println("--------------------------------");
2886 public void fireStartReadTransaction() {
2889 System.out.println("StartReadTransaction");
2891 for (SessionEventListener listener : eventListeners) {
2893 listener.readTransactionStarted();
2894 } catch (Throwable t) {
2895 t.printStackTrace();
2901 public void fireFinishReadTransaction() {
2904 System.out.println("FinishReadTransaction");
2906 for (SessionEventListener listener : eventListeners) {
2908 listener.readTransactionFinished();
2909 } catch (Throwable t) {
2910 t.printStackTrace();
2916 public void fireStartWriteTransaction() {
2919 System.out.println("StartWriteTransaction");
2921 for (SessionEventListener listener : eventListeners) {
2923 listener.writeTransactionStarted();
2924 } catch (Throwable t) {
2925 t.printStackTrace();
2931 public void fireFinishWriteTransaction() {
2934 System.out.println("FinishWriteTransaction");
2936 for (SessionEventListener listener : eventListeners) {
2938 listener.writeTransactionFinished();
2939 } catch (Throwable t) {
2940 t.printStackTrace();
2944 Indexing.resetDependenciesIndexingDisabled();
2948 Resource deserialize(final long resourceId, final long clusterId) throws IOException {
2949 throw new Error("Not supported at the moment.");
2956 ClusterTable getClusterTable() {
2957 return clusterTable;
2960 public GraphSession getGraphSession() {
2961 return graphSession;
2964 public QueryProcessor getQueryProvider2() {
2965 return queryProvider2;
2968 ClientChangesImpl getClientChanges() {
2969 return clientChanges;
2972 boolean getWriteOnly() {
2976 static int counter = 0;
2978 public void onClusterLoaded(long clusterId) {
2980 clusterTable.updateSize();
2988 * Implementation of the interface RequestProcessor
2992 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
2997 assert(request != null);
2999 final DataContainer<T> result = new DataContainer<T>();
3000 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3002 syncRequest(request, new AsyncProcedure<T>() {
3005 public void execute(AsyncReadGraph graph, T t) {
3010 public void exception(AsyncReadGraph graph, Throwable t) {
3016 Throwable t = exception.get();
3018 if(t instanceof DatabaseException) throw (DatabaseException)t;
3019 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3022 return result.get();
3027 // public <T> T syncRequest(Read<T> request, AsyncListener<T> procedure) throws DatabaseException {
3028 // assertNotSession();
3029 // return syncRequest(request, (AsyncProcedure<T>)procedure);
3033 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure) throws DatabaseException {
3035 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
3039 public <T> T syncRequest(Read<T> request, final Listener<T> procedure) throws DatabaseException {
3041 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
3045 public <T> T syncRequest(final Read<T> request, final SyncProcedure<T> procedure) throws DatabaseException {
3047 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3051 public <T> T syncRequest(Read<T> request, Procedure<T> procedure) throws DatabaseException {
3053 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3057 public <T> T syncRequest(final AsyncRead<T> request) throws DatabaseException {
3061 assert(request != null);
3063 final DataContainer<T> result = new DataContainer<T>();
3064 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3066 syncRequest(request, new AsyncProcedure<T>() {
3069 public void execute(AsyncReadGraph graph, T t) {
3074 public void exception(AsyncReadGraph graph, Throwable t) {
3080 Throwable t = exception.get();
3082 if(t instanceof DatabaseException) throw (DatabaseException)t;
3083 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3086 return result.get();
3091 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure) throws DatabaseException {
3093 return syncRequest(request, (AsyncProcedure<T>)procedure);
3097 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure) throws DatabaseException {
3099 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
3103 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure) throws DatabaseException {
3105 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
3109 public <T> T syncRequest(AsyncRead<T> request, final SyncProcedure<T> procedure) throws DatabaseException {
3111 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3115 final public <T> T syncRequest(final AsyncRead<T> request, final Procedure<T> procedure) throws DatabaseException {
3117 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3121 public <T> Collection<T> syncRequest(MultiRead<T> request) throws DatabaseException {
3125 assert(request != null);
3127 final ArrayList<T> result = new ArrayList<T>();
3128 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3130 syncRequest(request, new SyncMultiProcedure<T>() {
3133 public void execute(ReadGraph graph, T t) {
3134 synchronized(result) {
3140 public void finished(ReadGraph graph) {
3144 public void exception(ReadGraph graph, Throwable t) {
3151 Throwable t = exception.get();
3153 if(t instanceof DatabaseException) throw (DatabaseException)t;
3154 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3162 public <T> Collection<T> syncRequest(MultiRead<T> arg0, SyncMultiProcedure<T> arg1) throws DatabaseException {
3164 throw new Error("Not implemented!");
3168 public <T> Collection<T> syncRequest(MultiRead<T> request, SyncMultiListener<T> procedure) throws DatabaseException {
3170 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
3174 public <T> Collection<T> syncRequest(MultiRead<T> request, MultiListener<T> procedure) throws DatabaseException {
3176 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
3180 public <T> Collection<T> syncRequest(MultiRead<T> request, MultiProcedure<T> procedure) throws DatabaseException {
3182 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
3186 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request) throws DatabaseException {
3190 assert(request != null);
3192 final ArrayList<T> result = new ArrayList<T>();
3193 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3195 syncRequest(request, new AsyncMultiProcedure<T>() {
3198 public void execute(AsyncReadGraph graph, T t) {
3199 synchronized(result) {
3205 public void finished(AsyncReadGraph graph) {
3209 public void exception(AsyncReadGraph graph, Throwable t) {
3216 Throwable t = exception.get();
3218 if(t instanceof DatabaseException) throw (DatabaseException)t;
3219 else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
3227 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, AsyncMultiListener<T> procedure) throws DatabaseException {
3229 return syncRequest(request, (AsyncMultiProcedure<T>)procedure);
3233 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, SyncMultiListener<T> procedure) throws DatabaseException {
3235 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
3239 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, MultiListener<T> procedure) throws DatabaseException {
3241 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
3245 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request, final SyncMultiProcedure<T> procedure) throws DatabaseException {
3247 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
3251 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request, final MultiProcedure<T> procedure) throws DatabaseException {
3253 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
3258 public <T> void asyncRequest(Read<T> request) {
3260 asyncRequest(request, new ProcedureAdapter<T>() {
3262 public void exception(Throwable t) {
3263 t.printStackTrace();
3270 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
3271 asyncRequest(request, (AsyncProcedure<T>)procedure);
3275 public <T> void asyncRequest(Read<T> request, final SyncListener<T> procedure) {
3276 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
3280 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
3281 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
3285 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
3286 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3290 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
3291 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3295 final public <T> void asyncRequest(final AsyncRead<T> request) {
3297 assert(request != null);
3299 asyncRequest(request, new ProcedureAdapter<T>() {
3301 public void exception(Throwable t) {
3302 t.printStackTrace();
3309 public <T> void asyncRequest(AsyncRead<T> request, AsyncListener<T> procedure) {
3310 scheduleRequest(request, procedure, procedure, null);
3314 public <T> void asyncRequest(AsyncRead<T> request, final SyncListener<T> procedure) {
3315 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
3319 public <T> void asyncRequest(AsyncRead<T> request, final Listener<T> procedure) {
3320 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
3324 public <T> void asyncRequest(AsyncRead<T> request, SyncProcedure<T> procedure) {
3325 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
3329 final public <T> void asyncRequest(final AsyncRead<T> request, final Procedure<T> procedure) {
3330 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
3334 public <T> void asyncRequest(MultiRead<T> request) {
3336 assert(request != null);
3338 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
3340 public void exception(ReadGraph graph, Throwable t) {
3341 t.printStackTrace();
3348 public <T> void asyncRequest(MultiRead<T> request, SyncMultiListener<T> procedure) {
3349 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
3353 public <T> void asyncRequest(MultiRead<T> request, MultiListener<T> procedure) {
3354 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
3358 public <T> void asyncRequest(MultiRead<T> request, SyncMultiProcedure<T> procedure) {
3359 scheduleRequest(request, procedure, null);
3363 public <T> void asyncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
3364 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
3368 final public <T> void asyncRequest(AsyncMultiRead<T> request) {
3370 assert(request != null);
3372 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
3374 public void exception(AsyncReadGraph graph, Throwable t) {
3375 t.printStackTrace();
3382 public <T> void asyncRequest(AsyncMultiRead<T> request, AsyncMultiListener<T> procedure) {
3383 asyncRequest(request, (AsyncMultiProcedure<T>)procedure);
3387 public <T> void asyncRequest(AsyncMultiRead<T> request, SyncMultiListener<T> procedure) {
3388 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
3392 public <T> void asyncRequest(AsyncMultiRead<T> request, MultiListener<T> procedure) {
3393 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
3397 public <T> void asyncRequest(AsyncMultiRead<T> request, SyncMultiProcedure<T> procedure) {
3398 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
3402 final public <T> void asyncRequest(AsyncMultiRead<T> request, final MultiProcedure<T> procedure) {
3403 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
3407 final public <T> void asyncRequest(final ExternalRead<T> request) {
3409 assert(request != null);
3411 asyncRequest(request, new ProcedureAdapter<T>() {
3413 public void exception(Throwable t) {
3414 t.printStackTrace();
3421 public <T> void asyncRequest(ExternalRead<T> request, final Listener<T> procedure) {
3422 asyncRequest(request, (Procedure<T>)procedure);
3427 void check(Throwable t) throws DatabaseException {
3429 if(t instanceof DatabaseException) throw (DatabaseException)t;
3430 else throw new DatabaseException("Unexpected exception", t);
3434 void check(DataContainer<Throwable> container) throws DatabaseException {
3435 Throwable t = container.get();
3437 if(t instanceof DatabaseException) throw (DatabaseException)t;
3438 else throw new DatabaseException("Unexpected exception", t);
3447 boolean sameProvider(Write request) {
3448 if(writeState.getGraph().provider != null) {
3449 return writeState.getGraph().provider.equals(request.getProvider());
3451 return request.getProvider() == null;
3455 boolean plainWrite(WriteGraphImpl graph) {
3456 if(graph == null) return false;
3457 if(graph.writeSupport.writeOnly()) return false;
3462 public static final ThreadGroup SessionThreadGroup = new ThreadGroup("Session Thread Group");
3463 private void assertNotSession() throws DatabaseException {
3464 Thread current = Thread.currentThread();
3465 if(sessionThreads.contains(current)) throw new DatabaseException("Caller is already inside a transaction.");
3468 void assertAlive() {
3469 if (!state.isAlive())
3470 throw new RuntimeDatabaseException("Session has been shut down.");
3473 public InputStream getValueStream(ReadGraphImpl graph, Resource resource) {
3474 return querySupport.getValueStream(graph, querySupport.getId(resource));
3477 public byte[] getValue(ReadGraphImpl graph, Resource resource) {
3478 return querySupport.getValue(graph, querySupport.getId(resource));
3481 <T> void acquire(Semaphore semaphore, T request) throws DatabaseException {
3482 acquire(semaphore, request, null);
3485 private <T,P> void acquire(Semaphore semaphore, T request, P procedure) throws DatabaseException {
3489 // Loop until the semaphore is acquired reporting requests that take a long time.
3492 if (semaphore.tryAcquire(DebugPolicy.LONG_EXECUTION_REPORT_PERIOD, DebugPolicy.LONG_EXECUTION_REPORT_PERIOD_UNIT)) {
3496 if (DebugPolicy.REPORT_TIME_CONSUMING_REQUESTS) {
3498 long waitTime = DebugPolicy.LONG_EXECUTION_REPORT_PERIOD_UNIT
3499 .toMillis(DebugPolicy.LONG_EXECUTION_REPORT_PERIOD)
3501 System.err.println("DB client request '" + request + "' is taking long to execute, so far "
3502 + (waitTime) + " ms. (procedure=" + procedure + ")");
3509 } catch (InterruptedException e) {
3510 e.printStackTrace();
3511 // FIXME: Should perhaps do something else in this case ??
3518 // public boolean holdOnToTransactionAfterCancel() {
3519 // return transactionPolicy.holdOnToTransactionAfterCancel();
3523 // public boolean holdOnToTransactionAfterCommit() {
3524 // return transactionPolicy.holdOnToTransactionAfterCommit();
3528 // public boolean holdOnToTransactionAfterRead() {
3529 // return transactionPolicy.holdOnToTransactionAfterRead();
3533 // public void onRelinquish() {
3534 // transactionPolicy.onRelinquish();
3538 // public void onRelinquishDone() {
3539 // transactionPolicy.onRelinquishDone();
3543 // public void onRelinquishError() {
3544 // transactionPolicy.onRelinquishError();
3549 public Session getSession() {
3554 protected abstract VirtualGraph getProvider(VirtualGraph virtualGraph);
3555 protected abstract ResourceImpl getNewResource() throws DatabaseException;
3557 public void ceased(int thread) {
3559 requestManager.ceased(thread);
3563 public int getAmountOfQueryThreads() {
3564 // This must be a power of two
3566 // return Integer.highestOneBit(Runtime.getRuntime().availableProcessors());
3569 public Resource getResourceByKey(int key) throws ResourceNotFoundException {
3571 return new ResourceImpl(resourceSupport, key);
3575 public void acquireWriteOnly() {
3579 public void releaseWriteOnly(ReadGraphImpl graph) {
3581 queryProvider2.releaseWrite(graph);
3584 public void handleUpdatesAndMetadata(WriteGraphImpl writer) {
3586 long start = System.nanoTime();
3588 while(dirtyPrimitives) {
3589 dirtyPrimitives = false;
3590 getQueryProvider2().performDirtyUpdates(writer);
3591 getQueryProvider2().performScheduledUpdates(writer);
3594 fireMetadataListeners(writer, clientChanges);
3596 if(DebugPolicy.PERFORMANCE_DATA) {
3597 long end = System.nanoTime();
3598 System.err.println("handleUpdatesAndMetadata " + 1e-9*(end-start));
3603 void removeTemporaryData() {
3604 File platform = Platform.getLocation().toFile();
3605 File tempFiles = new File(platform, "tempFiles");
3606 File temp = new File(tempFiles, "db");
3610 Files.walkFileTree(temp.toPath(), new SimpleFileVisitor<Path>() {
3612 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
3615 } catch (IOException e) {
3616 Logger.defaultLogError(e);
3618 return FileVisitResult.CONTINUE;
3621 } catch (IOException e) {
3622 Logger.defaultLogError(e);
3626 public void handleCreatedClusters() {
3627 if ((serviceMode & SERVICE_MODE_CREATE) > 0) {
3628 createdClusters.forEach(new TLongProcedure() {
3631 public boolean execute(long value) {
3632 ClusterImpl cluster = clusterTable.getClusterByClusterId(value);
3633 cluster.setImmutable(true, clusterTranslator);
3638 createdClusters.clear();
3642 public Object getModificationCounter() {
3643 return queryProvider2.modificationCounter;
3647 public void markUndoPoint() {
3648 state.setCombine(false);