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 org.simantics.acorn.internal;
14 final public class ClusterStream {
16 // // public static long duration2 = 0;
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;
30 // private int acceptedStamp;
31 // private boolean dirty = false;
32 //// final private ArrayList<ClusterChange> clusterChanges = new ArrayList<ClusterChange>();
34 // final ClusterChangeManager changes = new ClusterChangeManager();
36 //// final TLongObjectHashMap<ClusterChange> clusterChanges = new TLongObjectHashMap<ClusterChange>();
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;
47 // boolean isDirty() {
55 // void setOff(boolean value) {
67 // void createResource(ClusterChange cc, short operationIndex, ClusterUID clusterUID) {
70 // assert (null != cc);
71 // assert (0 != operationIndex);
72 // assert (!ClusterUID.Null.equals(clusterUID));
74 // System.out.println("DEBUG: Created resource index=" + operationIndex + " cluster=" + clusterUID);
75 // cc.createResource(operationIndex);
78 // final void addStatementIndex(Change change, int key, ClusterUID clusterUID, byte op) {
82 // assert (null != change);
83 // assert (!ClusterUID.Null.equals(clusterUID));
84 // change.addStatementIndex(key, clusterUID, op);
87 // void addStatement(ClusterChange cc, Change change) {
90 // assert (null != cc);
91 // assert (null != change);
92 // cc.addChange(change);
95 // void cancelStatement(Change change) {
98 // assert (null != change);
102 // void removeStatement(ClusterChange cc, Change change, long clusterId) {
105 // assert (null != cc);
106 // assert (null != change);
107 // cc.addChange(change);
110 // void cancelValue(Change change) {
113 // assert (null != change);
117 // void removeValue(ClusterChange cc, Change change, long clusterId) {
120 // // ClusterChange cc = getClusterChange(clusterId);
121 // assert (null != cc);
122 // assert (null != change);
123 // cc.addChange(change);
126 // void setValue(ClusterChange cc, Change change, long clusterId, byte[] bytes, int length) {
129 // assert (null != cc);
130 // assert (null != change);
131 // // ClusterChange cc = getClusterChange(clusterId);
132 // cc.setValue(change, bytes, length);
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);
142 // void undoValueEx(ClusterChange cc, Change change, int resourceIndex) {
143 // cc.undoValueEx(resourceIndex);
145 // void setImmutable(ClusterChange cc, Change change, long clusterId, boolean immutable) {
148 // cc.setImmutable(immutable);
150 // public void corruptCluster(ClusterChange cc, long clusterId)
151 // throws DatabaseException {
155 // System.out.println("ClusterStream.corrupt cid=" + clusterId + ".");
156 // assert (null != cc);
171 // void flush(long clusterId) {
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)) {
180 // if (stamp == acceptedStamp)
183 //// System.err.println("kasdi");
187 // changes.remove(ccs);
191 // * @return true if the stream has accepted all changes
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)
207 // return hasAcceptedAllChanges();
211 // * Clear all changes and set stream status to empty.
213 // public void clear() {
215 // acceptedStamp = stamp;
219 // private boolean hasAcceptedAllChanges() {
220 // return stamp == acceptedStamp;
224 // acceptedStamp = stamp;
229 static class DebugInfo {
250 void add(DebugInfo di) {
253 nPartly += di.nPartly;
254 nForeign += di.nForeign;
255 sForeign += di.sForeign;
256 nValues += di.nValues;
257 sValues += di.sValues;
262 public String toString() {
263 return "val=" + nValues + " stm=" + nStms + " loc=" + nLocal
264 + " par=" + nPartly + " ful=" + nForeign + " for="
265 + sForeign + " vat=" + sValues + " tot=" + tot;
270 Add(0, (byte) 0), Remove(1, (byte) 0x20);
271 StmEnum(int ordinal, byte mask) {
272 this.ordinal = ordinal;
284 final static class Data {
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)
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);
296 private static int bytes(int bits, ClusterEnum a, ClusterEnum b) {
298 if (a != ClusterEnum.ForeignShort) {
301 if (b != ClusterEnum.ForeignShort) {
304 int bytes = left >>> 3;
313 Local(0), ForeignShort(1), ForeignLong(2);
316 ClusterEnum(int ordinal) {
317 this.ordinal = ordinal;
320 static Data[][][] maps = new Data[2][3][3];
328 maps[StmEnum.Add.ordinal][Local.ordinal][Local.ordinal] = new Data(
336 maps[StmEnum.Add.ordinal][Local.ordinal][ForeignShort.ordinal] = new Data(
337 12, 4, Local, ForeignShort);
339 // op: 000010 | r12-13
340 maps[StmEnum.Add.ordinal][Local.ordinal][ForeignLong.ordinal] = new Data(
341 2, 2, Local, ForeignLong);
344 maps[StmEnum.Add.ordinal][ForeignShort.ordinal][Local.ordinal] = new Data(
345 13, 4, ForeignShort, Local);
352 maps[StmEnum.Add.ordinal][ForeignShort.ordinal][ForeignShort.ordinal] = new Data(
353 1, 6, ForeignShort, ForeignShort);
356 maps[StmEnum.Add.ordinal][ForeignShort.ordinal][ForeignLong.ordinal] = new Data(
357 14, 4, ForeignShort, ForeignLong);
359 // op: 000100 | r12-13
360 maps[StmEnum.Add.ordinal][ForeignLong.ordinal][Local.ordinal] = new Data(
361 4, 2, ForeignLong, Local);
364 maps[StmEnum.Add.ordinal][ForeignLong.ordinal][ForeignShort.ordinal] = new Data(
365 15, 4, ForeignLong, ForeignShort);
367 // op: 000110 | r12-13
368 maps[StmEnum.Add.ordinal][ForeignLong.ordinal][ForeignLong.ordinal] = new Data(
369 6, 2, ForeignLong, ForeignLong);
372 // op: 000001 | r12-13
373 maps[StmEnum.Remove.ordinal][Local.ordinal][Local.ordinal] = new Data(
381 maps[StmEnum.Remove.ordinal][Local.ordinal][ForeignShort.ordinal] = new Data(
382 49, 0, Local, ForeignShort);
384 // op: 000011 | r12-13
385 maps[StmEnum.Remove.ordinal][Local.ordinal][ForeignLong.ordinal] = new Data(
386 3, 2, Local, ForeignLong);
389 maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][Local.ordinal] = new Data(
390 2, 4, ForeignShort, Local);
393 maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][ForeignShort.ordinal] = new Data(
394 2, 6, ForeignShort, ForeignShort);
397 maps[StmEnum.Remove.ordinal][ForeignShort.ordinal][ForeignLong.ordinal] = new Data(
398 50, 0, ForeignShort, ForeignLong);
400 // op: 000101 | r12-13
401 maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][Local.ordinal] = new Data(
402 5, 2, ForeignLong, Local);
405 maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][ForeignShort.ordinal] = new Data(
406 51, 0, ForeignLong, ForeignShort);
408 // op: 000111 | r12-13
409 maps[StmEnum.Remove.ordinal][ForeignLong.ordinal][ForeignLong.ordinal] = new Data(
410 7, 2, ForeignLong, ForeignLong);
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);
420 Create((byte) 52), Set((byte) 53), SetShort((byte) 56), Delete(
421 (byte) 54), Modify((byte) 55);
426 public byte getOrMask() {