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