]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/UndoClusterUpdateProcessor.java
Merge commit 'b809a171b6dfb81ed9ef9e84870dcbcbc5912f0e'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / UndoClusterUpdateProcessor.java
1 package org.simantics.acorn.internal;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.simantics.acorn.ClusterManager;
8 import org.simantics.acorn.lru.ClusterChangeSet;
9 import org.simantics.acorn.lru.ClusterStreamChunk;
10 import org.simantics.acorn.lru.ClusterChangeSet.Entry;
11 import org.simantics.acorn.lru.ClusterChangeSet.Type;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.service.ClusterUID;
14
15 public class UndoClusterUpdateProcessor extends ClusterUpdateProcessorBase {
16         
17         public final static boolean DEBUG = false;
18
19         final private ClusterChangeSet ccs;
20         
21         private int oldValuesIndex = 0;
22         private int statementMaskIndex = 0;
23         
24         final public List<Entry> entries = new ArrayList<Entry>();
25         
26         public UndoClusterUpdateProcessor(ClusterManager client, ClusterStreamChunk chunk, ClusterChangeSet ccs) throws DatabaseException {
27                 super(client, readOperation(client, chunk, ccs));
28                 this.ccs = ccs;
29         }
30         
31         private static byte[] readOperation(ClusterManager manager, ClusterStreamChunk chunk, ClusterChangeSet ccs) {
32                 
33 //              ClusterStreamChunk chunk;
34 //              manager.streamLRU.acquireMutex();
35 //              try {
36 //                      chunk = ccs.getChunk(manager);
37 //              } catch (Throwable t) {
38 //                      throw new IllegalStateException(t);
39 //              } finally {
40 //                      manager.streamLRU.releaseMutex();
41 //              }
42 //
43 //              chunk.acquireMutex();
44 //              try {
45 //              chunk.ve
46                         chunk.makeResident();
47                         return chunk.getOperation(ccs.chunkOffset);
48 //              } catch (Throwable t) {
49 //                      throw new IllegalStateException(t);
50 //              } finally {
51 //                      chunk.releaseMutex();
52 //              }
53         }
54         
55         @Override
56         void create() throws DatabaseException {
57         }
58
59         @Override
60         void delete(int ri) throws DatabaseException {
61                 
62                 byte[] old = ccs.oldValues.get(oldValuesIndex);
63                 boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
64                 oldValuesIndex++;
65                 
66                 if(old != null) {
67                         entries.add(new Entry(ri, oldValueEx, old, null));
68                 }
69                 
70         }
71
72         @Override
73         void modify(int resourceKey, long offset, int size, byte[] bytes, int pos)
74                         throws DatabaseException {
75                 
76         }
77
78         @Override
79         void set(int resourceKey, byte[] bytes, int length)
80                         throws DatabaseException {
81
82                 byte[] old = ccs.oldValues.get(oldValuesIndex);
83                 boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
84                 oldValuesIndex++;
85
86                 entries.add(new Entry(resourceKey, oldValueEx, old, Arrays.copyOf(valueBuffer, length)));
87                 
88         }
89
90         @Override
91         void claim(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
92                         throws DatabaseException {
93                 
94                 boolean add = ccs.statementMask.get(statementMaskIndex++) > 0;
95                 if(add) {
96                         entries.add(new Entry(Type.ADD, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
97                 }
98                 
99         }
100
101         @Override
102         void deny(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
103                         throws DatabaseException {
104                 
105                 boolean remove = ccs.statementMask.get(statementMaskIndex++) > 0;
106                 if(remove) {
107                         entries.add(new Entry(Type.REMOVE, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
108                 }
109
110         }
111         
112 }