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