]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/request/DelayedWrite.java
22d68e19a42e3a7d314c72bf371f0adfea3bb708
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / request / DelayedWrite.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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 java.util.Map;
15
16 import org.simantics.db.ChangeSetIdentifier;
17 import org.simantics.db.Session;
18 import org.simantics.db.VirtualGraph;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.CancelTransactionException;
21 import org.simantics.db.exception.DatabaseException;
22
23 /**
24  * The <code>GraphRequest</code> interface is used to create transaction
25  * requests to Simantics database implementations. Both read and write
26  * transaction requests use the same interface.
27  * 
28  * <p>
29  * The actual work carried out by the implemented request should be done in the
30  * <code>perform</code> method. It receives a <code>Graph</code> instance as
31  * the only argument which is the interface for actually reading and writing the
32  * graph data model.
33  * 
34  * <p>
35  * Transaction requests can be made to the database by creating your own
36  * <code>GraphRequest</code> instance and putting it in the request queue of
37  * the database session through the {@link Session} interface. The database
38  * session is responsible for executing the queued requests in a thread of its
39  * choice, or possibly/preferably multiple threads. The database session can
40  * allow multiple read-only requests to occur simultaneously, but read-write
41  * requests require exclusive database access. In other words only one
42  * read-write request can be in execution simultaneously.
43  * 
44  * <p>
45  * This interface also has two callbacks - <code>handleException</code> for
46  * allowing handling any exceptions thrown by <code>perform</code> and
47  * <code>requestCompleted</code> for performing actions after a request has
48  * been successfully completed.
49  * 
50  * <p>
51  * Clients of this interface are encouraged to extend the provided abstract
52  * implementations or this class or extend their own helper implementations for
53  * ones particular needs. The provided abstract implementations are:
54  * <ul>
55  * <li>{@link ReadGraphRequestAdapter} provides default implementations for
56  * everything except {@link #perform(WriteGraph)}.</li>
57  * <li>{@link SimpleGraphRequest} replaces {@link #perform(WriteGraph)} with
58  * {@link SimpleGraphRequest#run(WriteGraph)} for easier request implementation in
59  * simple cases.</li>
60  * <li>{@link GraphRequestWithResult} makes it easier for the user to return
61  * a single result Object from the request.</li>
62  * </ul>
63  * 
64  * @author Tuukka Lehtonen
65  * @see ReadGraphRequestAdapter
66  * @see GraphRequestRunner
67  * @see GraphRequestStatus
68  * @see GraphRequestWithResult
69  * @see Session
70  * @see SimpleGraphRequest
71  */
72 public interface DelayedWrite extends WriteTraits {
73
74     /**
75      * When a <code>GraphRequest</code> is serviced by the database session
76      * the method <code>perform</code> is invoked.
77      * 
78      * <p>
79      * Perform receives an object instance implementing the <code>Graph</code>
80      * interface which provides the only way to read/write the graph data model.
81      * The Graph instance must only be valid during the execution of the
82      * <code>perform</code> method and therefore should not be stored for use
83      * outside of its execution.
84      * 
85      * <p>
86      * The general contract of the method <code>perform</code> is that it may
87      * take any action whatsoever which involves reading or writing the data
88      * model through the received Graph instance.
89      * 
90      * @param g an interface for reading and writing the data model
91      * @return the result status of the request which affects how the
92      *         transaction proceeds, see GraphRequestStatus for more information
93      * @throws Exception when anything goes wrong inside the request thread
94      * @throws CancelTransactionException to indicate that the request needs to
95      *         be cancelled and any changes rolled back
96      */
97     void perform(WriteGraph graph) throws DatabaseException;
98     
99     ChangeSetIdentifier getIdentifier();
100     
101     VirtualGraph getProvider();
102     
103     void fillMetadata(Map<String, String> metadata);
104 }