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