1 package org.simantics.acorn.lru;
3 import java.util.ArrayList;
5 import org.simantics.acorn.ClusterManager;
6 import org.simantics.acorn.internal.Change;
7 import org.simantics.acorn.internal.ClusterChange;
8 import org.simantics.db.procore.cluster.ClusterTraits;
9 import org.simantics.db.service.ClusterUID;
11 import gnu.trove.list.array.TByteArrayList;
13 public class ClusterChangeSet {
19 public static class Entry {
23 final short predicate;
25 final ClusterUID predicateUID;
26 final ClusterUID objectUID;
27 final boolean oldValueEx;
28 final byte[] oldValue;
29 final byte[] newValue;
31 public Entry(Type type, int subject, ClusterUID predicateUID, int predicate, ClusterUID objectUID, int object) {
33 this.subject = (short)(subject & 0xFFF);
34 this.predicate = (short)predicate;
35 this.object = (short)object;
36 this.predicateUID = predicateUID;
37 this.objectUID = objectUID;
38 this.oldValueEx = false;
43 public Entry(int subject, boolean oldValueEx, byte[] oldValue, byte[] newValue) {
44 if(oldValue == null && newValue == null)
45 throw new IllegalStateException();
46 this.type = Type.VALUE;
47 this.subject = (short)(subject & 0xFFF);
50 this.predicateUID = null;
51 this.objectUID = null;
52 this.oldValueEx = oldValueEx;
53 this.oldValue = oldValue;
54 this.newValue = newValue;
57 public void process(ClusterManager clusters, ClusterChange cs, int clusterKey) {
61 if(e.type == Type.VALUE) {
63 if(e.oldValue != null) {
64 cs.setValue(e.subject, e.oldValue, e.oldValue.length);
66 Change change = new Change();
67 change.addStatementIndex(e.subject, null, ClusterChange.DELETE_OPERATION);
71 } else if(e.type == Type.ADD) {
73 int s = ClusterTraits.createResourceKeyNoThrow(clusterKey, e.subject);
74 int p = clusters.getResourceKey(e.predicateUID, e.predicate);
75 int o = clusters.getResourceKey(e.objectUID, e.object);
76 Change change = new Change();
77 change.addStatementIndex(s, null, ClusterChange.REMOVE_OPERATION);
78 change.addStatementIndex(p, e.predicateUID, (byte)0);
79 change.addStatementIndex(o, e.objectUID, (byte)0);
82 } else if(e.type == Type.REMOVE) {
84 int s = ClusterTraits.createResourceKeyNoThrow(clusterKey, e.subject);
85 int p = clusters.getResourceKey(e.predicateUID, e.predicate);
86 int o = clusters.getResourceKey(e.objectUID, e.object);
87 Change change = new Change();
88 change.addStatementIndex(s, null, ClusterChange.ADD_OPERATION);
89 change.addStatementIndex(p, e.predicateUID, (byte)0);
90 change.addStatementIndex(o, e.objectUID, (byte)0);
99 final public String id;
100 final public ClusterUID cuid;
101 public String chunkKey;
102 public int chunkOffset = -1;
104 public TByteArrayList statementMask = new TByteArrayList();
105 public TByteArrayList oldValueEx = new TByteArrayList();
106 public ArrayList<byte[]> oldValues = new ArrayList<byte[]>();
108 public ClusterChangeSet(String id ,ClusterUID cuid) {
111 String[] ss = id.split("\\.");
113 chunkOffset = Integer.parseInt(ss[1]);
116 public ClusterStreamChunk getChunk(ClusterManager manager) {
117 return manager.streamLRU.get(chunkKey);