1 package org.simantics.acorn.lru;
3 import java.util.ArrayList;
5 import org.simantics.acorn.ClusterManager;
6 import org.simantics.acorn.exception.AcornAccessVerificationException;
7 import org.simantics.acorn.exception.IllegalAcornStateException;
8 import org.simantics.acorn.internal.Change;
9 import org.simantics.acorn.internal.ClusterChange;
10 import org.simantics.db.procore.cluster.ClusterTraits;
11 import org.simantics.db.service.ClusterUID;
13 import gnu.trove.list.array.TByteArrayList;
15 public class ClusterChangeSet {
21 public static class Entry {
25 final short predicate;
27 final ClusterUID predicateUID;
28 final ClusterUID objectUID;
29 final boolean oldValueEx;
30 final byte[] oldValue;
31 final byte[] newValue;
33 public Entry(Type type, int subject, ClusterUID predicateUID, int predicate, ClusterUID objectUID, int object) {
35 this.subject = (short)(subject & 0xFFF);
36 this.predicate = (short)predicate;
37 this.object = (short)object;
38 this.predicateUID = predicateUID;
39 this.objectUID = objectUID;
40 this.oldValueEx = false;
45 public Entry(int subject, boolean oldValueEx, byte[] oldValue, byte[] newValue) throws IllegalAcornStateException {
46 if(oldValue == null && newValue == null)
47 throw new IllegalAcornStateException("oldValue == null && newValue == null");
48 this.type = Type.VALUE;
49 this.subject = (short)(subject & 0xFFF);
52 this.predicateUID = null;
53 this.objectUID = null;
54 this.oldValueEx = oldValueEx;
55 this.oldValue = oldValue;
56 this.newValue = newValue;
59 public void process(ClusterManager clusters, ClusterChange cs, int clusterKey) throws AcornAccessVerificationException {
63 if(e.type == Type.VALUE) {
65 if(e.oldValue != null) {
66 cs.setValue(e.subject, e.oldValue, e.oldValue.length);
68 Change change = new Change();
69 change.addStatementIndex(e.subject, null, ClusterChange.DELETE_OPERATION);
73 } else if(e.type == Type.ADD) {
75 int s = ClusterTraits.createResourceKeyNoThrow(clusterKey, e.subject);
76 int p = clusters.getResourceKey(e.predicateUID, e.predicate);
77 int o = clusters.getResourceKey(e.objectUID, e.object);
78 Change change = new Change();
79 change.addStatementIndex(s, null, ClusterChange.REMOVE_OPERATION);
80 change.addStatementIndex(p, e.predicateUID, (byte)0);
81 change.addStatementIndex(o, e.objectUID, (byte)0);
84 } else if(e.type == Type.REMOVE) {
86 int s = ClusterTraits.createResourceKeyNoThrow(clusterKey, e.subject);
87 int p = clusters.getResourceKey(e.predicateUID, e.predicate);
88 int o = clusters.getResourceKey(e.objectUID, e.object);
89 Change change = new Change();
90 change.addStatementIndex(s, null, ClusterChange.ADD_OPERATION);
91 change.addStatementIndex(p, e.predicateUID, (byte)0);
92 change.addStatementIndex(o, e.objectUID, (byte)0);
101 final public String id;
102 final public ClusterUID cuid;
103 public String chunkKey;
104 public int chunkOffset = -1;
106 public TByteArrayList statementMask = new TByteArrayList();
107 public TByteArrayList oldValueEx = new TByteArrayList();
108 public ArrayList<byte[]> oldValues = new ArrayList<byte[]>();
110 public ClusterChangeSet(String id ,ClusterUID cuid) {
113 String[] ss = id.split("\\.");
115 chunkOffset = Integer.parseInt(ss[1]);
118 public ClusterStreamChunk getChunk(ClusterManager manager) throws AcornAccessVerificationException {
119 return manager.streamLRU.get(chunkKey);