]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / request / AsyncRead.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.request;
13
14   import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.Session;
16 import org.simantics.db.exception.CancelTransactionException;
17 import org.simantics.db.procedure.AsyncProcedure;
18
19 /**
20  * TODO: fix this documentation, it is completely out-of-date!
21  * 
22  * The <code>GraphRequest</code> interface is used to create transaction
23  * requests to Simantics database implementations. Both read and write
24  * transaction requests use the same interface.
25  * 
26  * <p>
27  * The actual work carried out by the implemented request should be done in the
28  * <code>perform</code> method. It receives a <code>Graph</code> instance as
29  * the only argument which is the interface for actually reading and writing the
30  * graph data model.
31  * 
32  * <p>
33  * Transaction requests can be made to the database by creating your own
34  * <code>GraphRequest</code> instance and putting it in the request queue of
35  * the database session through the {@link Session} interface. The database
36  * session is responsible for executing the queued requests in a thread of its
37  * choice, or possibly/preferably multiple threads. The database session can
38  * allow multiple read-only requests to occur simultaneously, but read-write
39  * requests require exclusive database access. In other words only one
40  * read-write request can be in execution simultaneously.
41  * 
42  * <p>
43  * This interface also has two callbacks - <code>handleException</code> for
44  * allowing handling any exceptions thrown by <code>perform</code> and
45  * <code>requestCompleted</code> for performing actions after a request has
46  * been successfully completed.
47  * 
48  * @author Antti Villberg
49  * @see AsyncReadGraph
50  * @see AsyncProcedure
51  * @see Session
52  */
53 @FunctionalInterface
54 public interface AsyncRead<Result> {
55
56     /**
57      * When a <code>GraphRequest</code> is serviced by the database session
58      * the method <code>perform</code> is invoked.
59      * 
60      * <p>
61      * Perform receives an object instance implementing the <code>Graph</code>
62      * interface which provides the only way to read/write the graph data model.
63      * The Graph instance must only be valid during the execution of the
64      * <code>perform</code> method and therefore should not be stored for use
65      * outside of its execution.
66      * 
67      * <p>
68      * The general contract of the method <code>perform</code> is that it may
69      * take any action whatsoever which involves reading or writing the data
70      * model through the received Graph instance.
71      * 
72      * @param g an interface for reading and writing the data model
73      * @return the result status of the request which affects how the
74      *         transaction proceeds, see GraphRequestStatus for more information
75      * @throws Exception when anything goes wrong inside the request thread
76      * @throws CancelTransactionException to indicate that the request needs to
77      *         be cancelled and any changes rolled back
78      */
79     void perform(AsyncReadGraph graph, AsyncProcedure<Result> procedure);
80
81     default int getFlags() {
82         return 0;
83     }
84
85     default int threadHash() {
86         return hashCode();
87     }
88
89 }