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;
14 import gnu.trove.map.hash.THashMap;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
22 import java.util.concurrent.atomic.AtomicLong;
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;
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 {
39 public Database getDatabase() {
43 public void close() throws ProCoreException {
46 public void open() throws ProCoreException {
49 public boolean isClosed() throws ProCoreException {
53 public String execute(String command) throws ProCoreException {
57 public void acceptCommit(long transactionId, long changeSetId, byte[] metadata) throws ProCoreException {
60 public long cancelCommit(long transactionId, long changeSetId, byte[] metadata, OnChangeSetUpdate on) throws ProCoreException {
64 public Transaction askReadTransaction() throws ProCoreException {
68 public Transaction askWriteTransaction(long transactionId) throws ProCoreException {
72 public long endTransaction(long transactionId) throws ProCoreException {
76 public byte[] getChangeSetMetadata(long changeSetId) throws ProCoreException {
80 public ChangeSetData getChangeSetData(long minChangeSetId, long maxChangeSetId, OnChangeSetUpdate on) throws ProCoreException {
84 public ChangeSetIds getChangeSetIds() throws ProCoreException {
88 public Cluster getCluster(byte[] clusterId) throws ProCoreException {
92 public ClusterChanges getClusterChanges(long changeSetId, byte[] clusterId) throws ProCoreException {
96 public ClusterIds getClusterIds() throws ProCoreException {
100 public Information getInformation() throws ProCoreException {
104 public Refresh getRefresh(long changeSetId) throws ProCoreException {
108 public ResourceSegment getResourceSegment(byte[] clusterUID, int resourceIndex, long offset, short size) throws ProCoreException {
112 public long reserveIds(int count) throws ProCoreException {
116 public void updateCluster(byte[] operations) throws ProCoreException {
119 public boolean undo(long[] cChangeSetIds, OnChangeSetUpdate onChangeSetUpdate) throws ProCoreException {
123 public <T> T clone(ClusterUID clusterUID, ClusterCreator clusterCreator) throws DatabaseException {
124 throw new UnsupportedOperationException();
127 public boolean refreshEnabled() {
131 public boolean rolledback() {
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;
143 protected THashMap<String, BuiltinData> initBuiltinMap()
144 throws DatabaseException {
146 Properties props = new Properties();
147 File from = new File(FileName);
148 InputStream in = new FileInputStream(from);
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);
161 } catch(IOException e) {
164 throw new DatabaseException("Failed to read builtins.", e);
168 protected ServerInformationImpl getServerInformation() {
172 public void acceptCommit(long transactionId, long csid, byte[] metadata)
173 throws DatabaseException {
174 changeSetId.incrementAndGet();
178 public void cancelCommit(long transactionId, long csid, byte[] metadata, SynchronizeContextI context)
179 throws DatabaseException {
180 changeSetId.incrementAndGet();
184 public void endTransaction(long transactionId, boolean write)
185 throws DatabaseException {
190 public long askWriteTransaction(int thread, long transactionId)
191 throws DatabaseException {
192 if (Constants.NullTransactionId != transactionId)
193 return transactionId;
195 return this.transactionId.incrementAndGet();
198 public long askReadTransaction(int thread)
199 throws DatabaseException {
200 return transactionId.incrementAndGet();
203 public void stop() throws DatabaseException {
205 System.out.println("GraphSessionVirtual: stop.");
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();