]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/ClusterStream.java
Merge commit 'fd452722e97db9cf876f4f03a9e44fe750625a92'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / ClusterStream.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.acorn.internal;
13
14 import java.util.ArrayList;
15
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.service.ClusterUID;
18
19 final public class ClusterStream {
20
21 //    // public static long duration2 = 0;
22 //
23     public static final boolean DEBUG = false;
24     public static final byte NULL_OPERATION = 0;
25     public static final byte CREATE_OPERATION = 1;
26     public static final byte SET_OPERATION = 4;
27     public static final byte MODI_OPERATION = 6;
28     public static final byte KILL_OPERATION = 7;
29 //    boolean off = false;
30 //    public GraphSession graphSession;
31 //    final SessionImplSocket session;
32 ////    private int flushCount = 0;
33 //    final private boolean alwaysOff;
34 //    private int stamp;
35 //    private int acceptedStamp;
36 //    private boolean dirty = false;
37 ////    final private ArrayList<ClusterChange> clusterChanges = new ArrayList<ClusterChange>();
38 //    
39 //    final ClusterChangeManager changes = new ClusterChangeManager();
40 //    
41 ////    final TLongObjectHashMap<ClusterChange> clusterChanges = new TLongObjectHashMap<ClusterChange>();
42 //
43 //    // private final Change lastChange = new Change();
44 //    ClusterStream(SessionImplSocket session, GraphSession graphSession,
45 //            boolean alwaysOff) {
46 //        this.session = session;
47 //        this.graphSession = graphSession;
48 //        this.alwaysOff = alwaysOff;
49 //    }
50 //
51 //    
52 //    boolean isDirty() {
53 //      return dirty;
54 //    }
55 //    
56 //    void markDirty() {
57 //      dirty = true;
58 //    }
59 //    
60 //    void setOff(boolean value) {
61 //        if (alwaysOff) {
62 //            off = true;
63 //        } else {
64 //            off = value;
65 //        }
66 //    }
67 //
68 //    boolean getOff() {
69 //        return off;
70 //    }
71 //
72 //    void createResource(ClusterChange cc, short operationIndex, ClusterUID clusterUID) {
73 //        if (off)
74 //            return;
75 //        assert (null != cc);
76 //        assert (0 != operationIndex);
77 //        assert (!ClusterUID.Null.equals(clusterUID));
78 //        if (DEBUG)
79 //            System.out.println("DEBUG: Created resource index=" + operationIndex + " cluster=" + clusterUID);
80 //        cc.createResource(operationIndex);
81 //    }
82 //
83 //    final void addStatementIndex(Change change, int key, ClusterUID clusterUID, byte op) {
84 //        if (off)
85 //            return;
86 //        assert (key > 0);
87 //        assert (null != change);
88 //        assert (!ClusterUID.Null.equals(clusterUID));
89 //        change.addStatementIndex(key, clusterUID, op);
90 //    }
91 //
92 //    void addStatement(ClusterChange cc, Change change) {
93 //        if (off)
94 //            return;
95 //        assert (null != cc);
96 //        assert (null != change);
97 //        cc.addChange(change);
98 //    }
99 //
100 //    void cancelStatement(Change change) {
101 //        if (off)
102 //            return;
103 //        assert (null != change);
104 //        change.init();
105 //    }
106 //
107 //    void removeStatement(ClusterChange cc, Change change, long clusterId) {
108 //        if (off)
109 //            return;
110 //        assert (null != cc);
111 //        assert (null != change);
112 //        cc.addChange(change);
113 //    }
114 //
115 //    void cancelValue(Change change) {
116 //        if (off)
117 //            return;
118 //        assert (null != change);
119 //        change.init();
120 //    }
121 //
122 //    void removeValue(ClusterChange cc, Change change, long clusterId) {
123 //        if (off)
124 //            return;
125 //        // ClusterChange cc = getClusterChange(clusterId);
126 //        assert (null != cc);
127 //        assert (null != change);
128 //        cc.addChange(change);
129 //    }
130 //
131 //    void setValue(ClusterChange cc, Change change, long clusterId, byte[] bytes, int length) {
132 //        if (off)
133 //            return;
134 //        assert (null != cc);
135 //        assert (null != change);
136 //        // ClusterChange cc = getClusterChange(clusterId);
137 //        cc.setValue(change, bytes, length);
138 //    }
139 //
140 //    void modiValue(ClusterChange cc, Change change, long clusterId,
141 //            long voffset, int length, byte[] bytes, int offset) {
142 //        assert (null != cc);
143 //        assert (null != change);
144 //        cc.modiValue(change, voffset, length, bytes, offset);
145 //    }
146 //
147 //    void undoValueEx(ClusterChange cc, Change change, int resourceIndex) {
148 //        cc.undoValueEx(resourceIndex);
149 //    }
150 //    void setImmutable(ClusterChange cc, Change change, long clusterId, boolean immutable) {
151 //        if (off)
152 //            return;
153 //        cc.setImmutable(immutable);
154 //    }
155 //    public void corruptCluster(ClusterChange cc, long clusterId)
156 //            throws DatabaseException {
157 //        if (off)
158 //            return;
159 //        if (DEBUG)
160 //            System.out.println("ClusterStream.corrupt cid=" + clusterId + ".");
161 //        assert (null != cc);
162 //        cc.corrupt();
163 //    }
164 //
165 //    int getStamp() {
166 //        return stamp;
167 //    }
168 //
169 //    void flush() {
170 //        if (off)
171 //            return;
172 ////        flushCount++;
173 //        return;
174 //    }
175 //
176 //    void flush(long clusterId) {
177 //        if (off)
178 //            return;
179 //        ClusterUID clusterUID = session.clusterTable.clusterIds.getClusterUID(clusterId);
180 //        ArrayList<ClusterChange> ccs = new ArrayList<ClusterChange>();
181 //        for(ClusterChange cc : changes.get()) {
182 //            if(cc.clusterUID.equals(clusterUID)) {
183 //                if (cc.flush(graphSession, cc.clusterUID)) {
184 //                    ccs.add(cc);
185 //                    if (stamp == acceptedStamp)
186 //                        ++stamp;
187 //                } else {
188 ////                    System.err.println("kasdi");
189 //                }
190 //            }
191 //        }
192 //        changes.remove(ccs);
193 //    }
194 //
195 //    /**
196 //     * @return true if the stream has accepted all changes
197 //     */
198 //    public boolean reallyFlush() {
199 //        // Last possibility to mark clusters immutable before write only clusters are gone 
200 //        session.handleCreatedClusters();
201 //        // These shall be requested from server
202 //        session.clusterTable.removeWriteOnlyClusters();
203 //        if (!off && changes.size() > 0) {
204 //            for(ClusterChange cc : changes.get()) {
205 //                if (cc.flush(graphSession, cc.clusterUID))
206 //                    if (stamp == acceptedStamp)
207 //                        ++stamp;
208 //            }
209 //            changes.clear();
210 //        }
211 //        dirty = false;
212 //        return hasAcceptedAllChanges();
213 //    }
214 //
215 //    /**
216 //     * Clear all changes and set stream status to empty.
217 //     */
218 //    public void clear() {
219 //        changes.clear();
220 //        acceptedStamp = stamp;
221 //        dirty = false;
222 //    }
223 //
224 //    private boolean hasAcceptedAllChanges() {
225 //        return stamp == acceptedStamp;
226 //    }
227 //
228 //    void accept() {
229 //        acceptedStamp = stamp;
230 //    }
231 //
232 //
233
234     static class DebugInfo {
235         long nStms;
236         long nLocal;
237         long nPartly;
238         long nForeign;
239         long nValues;
240         long sValues;
241         long sForeign;
242         long tot;
243
244         void clear() {
245             nStms = 0;
246             nLocal = 0;
247             nPartly = 0;
248             nForeign = 0;
249             sForeign = 0;
250             nValues = 0;
251             sValues = 0;
252             tot = 0;
253         }
254
255         void add(DebugInfo di) {
256             nStms += di.nStms;
257             nLocal += di.nLocal;
258             nPartly += di.nPartly;
259             nForeign += di.nForeign;
260             sForeign += di.sForeign;
261             nValues += di.nValues;
262             sValues += di.sValues;
263             tot += di.tot;
264         }
265
266         @Override
267         public String toString() {
268             return "val=" + nValues + " stm=" + nStms + " loc=" + nLocal
269                     + " par=" + nPartly + " ful=" + nForeign + " for="
270                     + sForeign + " vat=" + sValues + " tot=" + tot;
271         }
272     }
273
274     enum StmEnum {
275         Add(0, (byte) 0), Remove(1, (byte) 0x20);
276         StmEnum(int ordinal, byte mask) {
277             this.ordinal = ordinal;
278             this.mask = mask;
279         }
280
281         public int ordinal;
282         private byte mask;
283
284         byte getOrMask() {
285             return mask;
286         }
287     }
288
289     final static class Data {
290         
291         final byte mask; // or mask for operation code (don't care bits are zero)
292         final short bits; // how many bits are reserved for resource index (0,2,4,6)
293         final int bytes;
294
295         Data(int mask, int bits, ClusterEnum a, ClusterEnum b) {
296             this.mask = (byte) (mask << bits);
297             this.bits = (short) bits;
298             this.bytes = bytes(bits, a, b);
299         }
300         
301         private static int bytes(int bits, ClusterEnum a, ClusterEnum b) {
302             int left = 6 - bits;
303             if (a != ClusterEnum.ForeignShort) {
304                 left += 6;
305             }
306             if (b != ClusterEnum.ForeignShort) {
307                 left += 6;
308             }
309             int bytes = left >>> 3;
310             if ((left & 7) != 0)
311                 bytes++;
312             return bytes;
313         }
314         
315     }
316
317     enum ClusterEnum {
318         Local(0), ForeignShort(1), ForeignLong(2);
319         public int ordinal;
320
321         ClusterEnum(int ordinal) {
322             this.ordinal = ordinal;
323         }
324
325         static Data[][][] maps = new Data[2][3][3];
326         static {
327                 // mask: 00000000
328                 // op: 000000|r12-13
329                 // p1
330                 // o1
331                 // r0-7
332                 // o2 | p2 | r8-11
333             maps[StmEnum.Add.ordinal][Local.ordinal][Local.ordinal] = new Data(
334                     0, 2, Local, Local);
335                 // mask: 11000000
336                 // op: 1100 | r10-13
337                 // p1
338                 // o for index
339                 // r0-7
340                 // p2 | ri 8-9
341             maps[StmEnum.Add.ordinal][Local.ordinal][ForeignShort.ordinal] = new Data(
342                     12, 4, Local, ForeignShort);
343                 // mask: 00001000
344             // op: 000010 | r12-13 
345             maps[StmEnum.Add.ordinal][Local.ordinal][ForeignLong.ordinal] = new Data(
346                     2, 2, Local, ForeignLong);
347                 // mask: 11010000
348             // op: 1101 | r10-13 
349             maps[StmEnum.Add.ordinal][ForeignShort.ordinal][Local.ordinal] = new Data(
350                     13, 4, ForeignShort, Local);
351
352             // mask: 01000000
353             // op: 01 | r8-13
354             // p for index
355             // o for index
356                 // r0-7
357             maps[StmEnum.Add.ordinal][ForeignShort.ordinal][ForeignShort.ordinal] = new Data(
358                     1, 6, ForeignShort, ForeignShort);
359             // mask: 11100000
360             // op: 1110 | r10-13 
361             maps[StmEnum.Add.ordinal][ForeignShort.ordinal][ForeignLong.ordinal] = new Data(
362                     14, 4, ForeignShort, ForeignLong);
363             // mask: 00010000
364             // op: 000100 | r12-13 
365             maps[StmEnum.Add.ordinal][ForeignLong.ordinal][Local.ordinal] = new Data(
366                     4, 2, ForeignLong, Local);
367             // mask: 11110000
368             // op: 1111 | r10-13 
369             maps[StmEnum.Add.ordinal][ForeignLong.ordinal][ForeignShort.ordinal] = new Data(
370                     15, 4, ForeignLong, ForeignShort);
371             // mask: 00011000
372             // op: 000110 | r12-13 
373             maps[StmEnum.Add.ordinal][ForeignLong.ordinal][ForeignLong.ordinal] = new Data(
374                     6, 2, ForeignLong, ForeignLong);
375
376             // mask: 00000100
377             // op: 000001 | r12-13 
378             maps[StmEnum.Remove.ordinal][Local.ordinal][Local.ordinal] = new Data(
379                     1, 2, Local, Local);
380             // mask: 01100001
381                 // op: 01100001
382                 // p1
383                 // o for index
384                 // r0-7
385                 // p2 | ri 8-13
386             maps[StmEnum.Remove.ordinal][Local.ordinal][ForeignShort.ordinal] = new Data(
387                     49, 0, Local, ForeignShort);
388             // mask: 00001100
389             // op: 000011 | r12-13 
390             maps[StmEnum.Remove.ordinal][Local.ordinal][ForeignLong.ordinal] = new Data(
391                     3, 2, Local, ForeignLong);
392             // mask: 00100000
393             // op: 0010 | r10-13 
394             maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][Local.ordinal] = new Data(
395                     2, 4, ForeignShort, Local);
396             // mask: 10000000
397             // op: 10 | r8-13
398             maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][ForeignShort.ordinal] = new Data(
399                     2, 6, ForeignShort, ForeignShort);
400             // mask: 00110010
401             // op: 00110010
402             maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][ForeignLong.ordinal] = new Data(
403                     50, 0, ForeignShort, ForeignLong);
404             // mask: 00010100
405             // op: 000101 | r12-13 
406             maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][Local.ordinal] = new Data(
407                     5, 2, ForeignLong, Local);
408             // mask: 00110011
409             // op: 00110011
410             maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][ForeignShort.ordinal] = new Data(
411                     51, 0, ForeignLong, ForeignShort);
412             // mask: 00011100
413             // op: 000111 | r12-13 
414             maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][ForeignLong.ordinal] = new Data(
415                     7, 2, ForeignLong, ForeignLong);
416         }
417
418         static Data getData(StmEnum s, ClusterEnum a, ClusterEnum b) {
419             return maps[s.ordinal][a.ordinal][b.ordinal];
420             // return maps.get(s).get(a).get(b);
421         }
422     }
423
424     enum OpEnum {
425         Create((byte) 52), Set((byte) 53), SetShort((byte) 56), Delete(
426                 (byte) 54), Modify((byte) 55);
427         OpEnum(byte mask) {
428             this.mask = mask;
429         }
430
431         public byte getOrMask() {
432             return mask;
433         }
434
435         private byte mask;
436     }
437 }