]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ManagementSupportImpl.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ManagementSupportImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Map;
8
9 import org.simantics.db.ChangeSet;
10 import org.simantics.db.ChangeSetIdentifier;
11 import org.simantics.db.Metadata;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Session;
14 import org.simantics.db.common.request.UniqueRead;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.InternalException;
18 import org.simantics.db.service.ManagementSupport;
19 import org.simantics.db.service.XSupport;
20
21 public class ManagementSupportImpl implements ManagementSupport {
22
23     final private SessionImplSocket session;
24
25     ManagementSupportImpl(SessionImplSocket session) {
26         this.session = session;
27     }
28     @Override
29     public Collection<ChangeSet> fetchChangeSets(final ReadGraph graph, final long min, final long max) throws DatabaseException {
30         if (min < 1 || min > max)
31             throw new IllegalArgumentException("Illegal range: min=" + min + " max=" + max);
32         return graph.sync(new UniqueRead<Collection<ChangeSet>>() {
33             @Override
34             public Collection<ChangeSet> perform(ReadGraph graph) throws DatabaseException {
35                 int size = (int)(max - min + 1);
36                 ClientChangesImpl csi = new ClientChangesImpl(session);
37                 SynchronizeContext context = new SynchronizeContext(session, csi, size, true);
38                 boolean failed = session.graphSession.getChangeSets(min, max, context);
39                 if (failed)
40                     throw new InternalException("Trouble with server execution.");
41                 final boolean undo = false;
42                 if (!context.isOk(undo)) // this is a blocking operation
43                     throw new InternalException("Trouble with server reply.");
44                 return context.getChangeSets();
45             }
46         });
47     }
48     @Override
49     public Collection<ChangeSetIdentifier> getChangeSetIdentifiers(long from, long to) throws DatabaseException {
50         return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
51     }
52     @Override
53     @Deprecated
54     public Collection<ChangeSetIdentifier> getChangeSets(long from, long to) throws DatabaseException {
55         return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
56     }
57     @Override
58     public <T> Collection<T> getMetadata(ReadGraph graph, long from, long to, Class<? extends Metadata> dataClass)
59             throws DatabaseException {
60         return this.getMetadata(from, to, dataClass);
61     }
62
63     @SuppressWarnings("unchecked")
64     @Override
65     public <T> Collection<T> getMetadata(long from, long to, Class<? extends Metadata> dataClass)
66             throws DatabaseException {
67         ArrayList<T> results = new ArrayList<T>();
68         try {
69             Method m = dataClass.getMethod("deserialise", Session.class, byte[].class);
70             Collection<ChangeSetIdentifier> css = getChangeSets(from, to);
71             for(ChangeSetIdentifier cs : css) {
72                 Map<String, byte[]> md = cs.getMetadata();
73                 if (null != md) {
74                     byte[] result = md.get(dataClass.getName());
75                     if (null != result) {
76                         try {
77                             Object value = m.invoke(null, session, result);
78                             results.add((T)value);
79                         } catch (SecurityException e) {
80                             Logger.defaultLogError(e);
81                         } catch (IllegalArgumentException e) {
82                             Logger.defaultLogError(e);
83                         } catch (IllegalAccessException e) {
84                             Logger.defaultLogError(e);
85                         } catch (InvocationTargetException e) {
86                             Logger.defaultLogError(e.getCause());
87                         }
88                     }
89                 }
90             }
91         } catch (SecurityException e) {
92             Logger.defaultLogError(e);
93         } catch (NoSuchMethodException e) {
94             Logger.defaultLogError(e);
95         } catch (IllegalArgumentException e) {
96             Logger.defaultLogError(e);
97         }
98         return results;
99     }
100
101     @Override
102     public void dumpRevision(long lastChangeSetId)
103     throws DatabaseException {
104         XSupport xs = session.getService(XSupport.class);
105         String s = xs.execute("dumpRevision " + lastChangeSetId);
106         long outChangeSetId = Long.parseLong(s);
107         if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
108             throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
109     }
110
111     @Override
112     public void dumpChangeSets(long lastChangeSetId)
113     throws DatabaseException {
114         XSupport xs = session.getService(XSupport.class);
115         String s = xs.execute("dumpChangeSets " + lastChangeSetId);
116         long outChangeSetId = Long.parseLong(s);
117         if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
118             throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
119     }
120
121     @Override
122     public long getHeadRevisionId()
123     throws DatabaseException {
124         return session.state.getHeadRevisionId();
125         // Better to use this to match getChangeSets implementation above.
126         // or not return session.graphSession.getLastChangeSetId();
127     }
128
129     @Override
130     public long getFirstRevisionId() throws DatabaseException {
131         return session.graphSession.dbSession.getDatabase().serverGetTailChangeSetId();
132     }
133
134     @Override
135     public void subscribe(ChangeSetListener changeSetListener) {
136         session.graphSession.addChangeSetListener(changeSetListener);
137     }
138     @Override
139     public void cancel(ChangeSetListener changeSetlistener) {
140         session.graphSession.removeChangeSetListener(changeSetlistener);
141     }
142 }