]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterChangeSet.java
Merge commit '0e8cf1e'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ClusterChangeSet.java
1 package org.simantics.acorn.lru;
2
3 import java.util.ArrayList;
4
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;
10
11 import gnu.trove.list.array.TByteArrayList;
12
13 public class ClusterChangeSet {
14
15         public enum Type {
16                 ADD,REMOVE,VALUE
17         }
18         
19         public static class Entry {
20                 
21                 final Type type;
22                 final short subject;
23                 final short predicate;
24                 final short object;
25                 final ClusterUID predicateUID;
26                 final ClusterUID objectUID;
27                 final boolean oldValueEx;
28                 final byte[] oldValue;
29                 final byte[] newValue;
30                 
31                 public Entry(Type type, int subject, ClusterUID predicateUID, int predicate, ClusterUID objectUID, int object) {
32                         this.type = type;
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;
39                         this.oldValue = null;
40                         this.newValue = null;
41                 }
42
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);
48                         this.predicate = 0;
49                         this.object = 0;
50                         this.predicateUID = null;
51                         this.objectUID = null;
52                         this.oldValueEx = oldValueEx;
53                         this.oldValue = oldValue;
54                         this.newValue = newValue;
55                 }
56                 
57                 public void process(ClusterManager clusters, ClusterChange cs, int clusterKey) {
58                         
59                         Entry e = this;
60                         
61                         if(e.type == Type.VALUE) {
62                                 
63                                 if(e.oldValue != null) {
64                                         cs.setValue(e.subject, e.oldValue, e.oldValue.length);
65                                 } else {
66                                         Change change = new Change();
67                                         change.addStatementIndex(e.subject, null, ClusterChange.DELETE_OPERATION);
68                                         cs.addChange(change);
69                                 }
70                                 
71                         } else if(e.type == Type.ADD) {
72                                 
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);
80                                 cs.addChange(change);
81                                 
82                         } else if(e.type == Type.REMOVE) {
83                                 
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);
91                                 cs.addChange(change);
92                                 
93                         }
94                         
95                 }
96                 
97         }
98         
99         final public String id;
100         final public ClusterUID cuid;
101         public String chunkKey;
102         public int chunkOffset = -1;
103         
104         public TByteArrayList statementMask = new TByteArrayList();
105         public TByteArrayList oldValueEx = new TByteArrayList();
106         public ArrayList<byte[]> oldValues = new ArrayList<byte[]>();
107         
108         public ClusterChangeSet(String id ,ClusterUID cuid) {
109                 this.id = id;
110                 this.cuid = cuid;
111                 String[] ss = id.split("\\.");
112                 chunkKey = ss[0];
113                 chunkOffset = Integer.parseInt(ss[1]);
114         }
115         
116         public ClusterStreamChunk getChunk(ClusterManager manager) {
117                 return manager.streamLRU.get(chunkKey);
118         }
119
120 }