]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/GraphSessionVirtual.java
Merge "Better emptying of trash bin"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / GraphSessionVirtual.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 fi.vtt.simantics.procore.internal;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
21 import java.util.Set;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 import org.simantics.db.ClusterCreator;
25 import org.simantics.db.Database;
26 import org.simantics.db.SessionReference;
27 import org.simantics.db.Database.Session;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.impl.support.VirtualGraphServerSupport;
30 import org.simantics.db.procore.protocol.Constants;
31 import org.simantics.db.server.ProCoreException;
32 import org.simantics.db.service.ClusterUID;
33
34 class GraphSessionVirtual extends GraphSession { // Äsh! This extends relation was/is a pure design choice.
35     private static String FileName = "virtualGraph.builtins.dat";
36     private static long BuiltinClusterId = -1; // Virtual graph doesn't have clusters.
37     static class VirtualSession implements Session {
38         @Override
39         public Database getDatabase() {
40             return null;
41         }
42         @Override
43         public void close() throws ProCoreException {
44         }
45         @Override
46         public void open() throws ProCoreException {
47         }
48         @Override
49         public boolean isClosed() throws ProCoreException {
50             return false;
51         }
52         @Override
53         public String execute(String command) throws ProCoreException {
54             return null;
55         }
56         @Override
57         public void acceptCommit(long transactionId, long changeSetId, byte[] metadata) throws ProCoreException {
58         }
59         @Override
60         public long cancelCommit(long transactionId, long changeSetId, byte[] metadata, OnChangeSetUpdate on) throws ProCoreException {
61             return 0;
62         }
63         @Override
64         public Transaction askReadTransaction() throws ProCoreException {
65             return null;
66         }
67         @Override
68         public Transaction askWriteTransaction(long transactionId) throws ProCoreException {
69             return null;
70         }
71         @Override
72         public long endTransaction(long transactionId) throws ProCoreException {
73             return 0;
74         }
75         @Override
76         public byte[] getChangeSetMetadata(long changeSetId) throws ProCoreException {
77             return null;
78         }
79         @Override
80         public ChangeSetData getChangeSetData(long minChangeSetId, long maxChangeSetId, OnChangeSetUpdate on) throws ProCoreException {
81             return null;
82         }
83         @Override
84         public ChangeSetIds getChangeSetIds() throws ProCoreException {
85             return null;
86         }
87         @Override
88         public Cluster getCluster(byte[] clusterId) throws ProCoreException {
89             return null;
90         }
91         @Override
92         public ClusterChanges getClusterChanges(long changeSetId, byte[] clusterId) throws ProCoreException {
93             return null;
94         }
95         @Override
96         public ClusterIds getClusterIds() throws ProCoreException {
97             return null;
98         }
99         @Override
100         public Information getInformation() throws ProCoreException {
101             return null;
102         }
103         @Override
104         public Refresh getRefresh(long changeSetId) throws ProCoreException {
105             return null;
106         }
107         @Override
108         public ResourceSegment getResourceSegment(byte[] clusterUID, int resourceIndex, long offset, short size) throws ProCoreException {
109             return null;
110         }
111         @Override
112         public long reserveIds(int count) throws ProCoreException {
113             return 0;
114         }
115         @Override
116         public void updateCluster(byte[] operations) throws ProCoreException {
117         }
118         @Override
119         public boolean undo(long[] cChangeSetIds, OnChangeSetUpdate onChangeSetUpdate) throws ProCoreException {
120             return false;
121         }
122         @Override
123         public <T> T clone(ClusterUID clusterUID, ClusterCreator clusterCreator) throws DatabaseException {
124             throw new UnsupportedOperationException();
125         }
126         @Override
127         public boolean refreshEnabled() {
128             return false;
129         }
130         @Override
131         public boolean rolledback() {
132             return false;
133         }
134     }
135     private AtomicLong changeSetId = new AtomicLong(Constants.NullChangeSetId);
136     private AtomicLong transactionId = new AtomicLong(Constants.NullTransactionId);
137     public static ServerInformationImpl serverInfo = new ServerInformationImpl("serverId", "protocolId", "databaseId", 0);
138     private VirtualGraphServerSupport virtualGraphServerSupport;
139         public GraphSessionVirtual(SessionImplSocket sessionImpl, SessionReference sessionReference, VirtualGraphServerSupport vgss) {
140         super(sessionImpl, sessionReference, new VirtualSession()); // Super does not expect null session.
141         virtualGraphServerSupport = vgss;
142     }
143     protected THashMap<String, BuiltinData> initBuiltinMap()
144     throws DatabaseException {
145         try {
146             Properties props = new Properties();
147             File from = new File(FileName);
148             InputStream in = new FileInputStream(from);
149             props.load(in);
150             THashMap<String, BuiltinData>  builtins = new THashMap<String, BuiltinData>();
151             Set<String> keys = props.stringPropertyNames();
152             for (String key : keys) {
153                 long resourceId = Long.parseLong(key);
154                 long cluster = BuiltinClusterId;
155                 int resourceIndex = (int)resourceId;
156                 String uri = props.getProperty(key);
157                 BuiltinData bd = new BuiltinData(resourceIndex, cluster);
158                 builtins.put(uri, bd);
159             }
160             return builtins;
161         } catch(IOException e) {
162             if (DEBUG)
163                 e.printStackTrace();
164             throw new DatabaseException("Failed to read builtins.", e);
165         }
166     }
167     @Override
168     protected ServerInformationImpl getServerInformation() {
169         return serverInfo;
170     }
171     @Override
172     public void acceptCommit(long transactionId, long csid, byte[] metadata)
173     throws DatabaseException {
174         changeSetId.incrementAndGet();
175     }
176
177     @Override
178     public void cancelCommit(long transactionId, long csid, byte[] metadata, SynchronizeContextI context)
179     throws DatabaseException {
180         changeSetId.incrementAndGet();
181     }
182
183     @Override
184     public void endTransaction(long transactionId, boolean write)
185     throws DatabaseException {
186
187     }
188
189     @Override
190     public long askWriteTransaction(int thread, long transactionId)
191     throws DatabaseException {
192         if (Constants.NullTransactionId != transactionId)
193             return transactionId;
194         else
195             return this.transactionId.incrementAndGet();
196     }
197     @Override
198     public long askReadTransaction(int thread)
199     throws DatabaseException {
200         return transactionId.incrementAndGet();
201     }
202     @Override
203     public void stop() throws DatabaseException {
204         if (DEBUG)
205             System.out.println("GraphSessionVirtual: stop.");
206
207     }
208     @Override
209     public long reserveIds(int count)
210     throws DatabaseException {
211 //        VirtualGraphServerSupport s = session.getService(VirtualGraphServerSupport.class);
212         long id = Constants.NullSubjectId;
213         for (int i=0; i<count; ++i)
214            id = virtualGraphServerSupport.createVirtual();
215         return id;
216     }
217 }