]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java
DB query swapping to file system
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplDb.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 java.io.File;
15
16 import org.simantics.db.Database;
17 import org.simantics.db.SessionManager;
18 import org.simantics.db.SessionReference;
19 import org.simantics.db.VirtualGraph;
20 import org.simantics.db.authentication.UserAuthenticationAgent;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.InvalidAuthenticationException;
23 import org.simantics.db.exception.InvalidUserException;
24 import org.simantics.db.impl.ClusterI;
25 import org.simantics.db.impl.ClusterSupport;
26 import org.simantics.db.impl.ClusterTranslator;
27 import org.simantics.db.impl.ResourceImpl;
28 import org.simantics.db.impl.graph.WriteSupport;
29 import org.simantics.db.impl.query.QueryProcessor;
30 import org.simantics.db.impl.query.QuerySupport;
31 import org.simantics.db.service.ServerInformation;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 final public class SessionImplDb extends SessionImplSocket {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(SessionImplDb.class);
38
39     /**
40      * Cached ServerInformation structure fetched from the server at connection
41      * time. It should never change during a single session and therefore it
42      * should be perfectly safe to cache it.
43      */
44     protected ServerInformationImpl serverInfo;
45
46     public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {
47         super(sessionManager, authAgent);
48     }
49
50     @Override
51     protected VirtualGraph getProvider(VirtualGraph vg) {
52         return vg;
53     }
54
55     @Override
56     public ResourceImpl getNewResource() throws DatabaseException {
57         if (null != defaultClusterSet)
58             return getNewResource(defaultClusterSet);
59         ClusterI cluster = getNewResourceCluster();
60         int newId;
61         try {
62             newId = cluster.createResource(clusterTranslator);
63         } catch (DatabaseException e) {
64             LOGGER.error("createResource failed", e);
65             return null;
66         }
67         return new ResourceImpl(resourceSupport, newId);
68     }
69
70     public void connect(SessionReference sessionReference, final Database.Session dbSession) throws Exception {
71         if (null == clusterTable) {
72             File t = StaticSessionProperties.virtualGraphStoragePath;
73             if (null == t)
74                 t = new File(".");
75             clusterTable = new ClusterTable(this, t);
76         }
77
78         graphSession = new GraphSessionSocket(this, sessionReference, dbSession);
79
80         try {
81
82             clusterStream = new ClusterStream(this, graphSession, false);
83
84             clusterTranslator = new ClusterTranslatorImpl(this);
85             serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);
86             serviceLocator.registerService(ClusterSupport.class, clusterTranslator);
87
88             resourceSupport = new ResourceSupportImpl(this);
89
90             requestManager = new SessionRequestManager(this, state);
91
92             querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
93             serviceLocator.registerService(QuerySupport.class, querySupport);
94
95             queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
96             
97             if("true".equals(System.getProperty("org.simantics.db.persistQueries")))
98                 queryProvider2.restore();
99
100             writeSupport = new WriteSupportImpl(this);
101             serviceLocator.registerService(WriteSupport.class, writeSupport);
102
103             state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
104
105             this.serverInfo = graphSession.getServerInformation();
106             
107 //            authenticator = authAgent.getAuthenticator(serverInfo);
108 //            if (authenticator == null)
109 //                throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");
110 //            if (authenticator instanceof BackdoorAuthenticator)
111 //                user = authenticator.getUser(this);
112
113         } catch (InvalidAuthenticationException e) {
114             throw e;
115         } catch (InvalidUserException e) {
116             throw e;
117 //        } catch (IOException e) {
118 //            Logger.defaultLogError("I/O error. See exception for details.", e);
119 //            graphSession = null;
120 //            clusterTable.dispose(); clusterTable = null;
121 //            throw e;
122         } catch (Throwable e) {
123             LOGGER.error("Unhandled error. See exception for details.", e);
124             graphSession = null;
125             clusterTable.dispose(); clusterTable = null;
126             throw new Exception(e);
127         }
128
129         String databaseId = serverInfo.getDatabaseId();
130         String serverId = serverInfo.getServerId();
131         String id = databaseId + "." + serverId;
132         virtualGraphServerSupport.connect(id);
133         clusterSetsSupport.connect(id);
134
135     }
136
137     @Override
138     protected ServerInformation getCachedServerInformation() {
139         return serverInfo;
140     }
141
142 }