]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/UndoClusterUpdateProcessor.java
Sharing org.simantics.acorn for everyone to use
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / UndoClusterUpdateProcessor.java
diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/UndoClusterUpdateProcessor.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/UndoClusterUpdateProcessor.java
new file mode 100644 (file)
index 0000000..d545e51
--- /dev/null
@@ -0,0 +1,112 @@
+package org.simantics.acorn.internal;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.simantics.acorn.ClusterManager;
+import org.simantics.acorn.lru.ClusterChangeSet;
+import org.simantics.acorn.lru.ClusterStreamChunk;
+import org.simantics.acorn.lru.ClusterChangeSet.Entry;
+import org.simantics.acorn.lru.ClusterChangeSet.Type;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.ClusterUID;
+
+public class UndoClusterUpdateProcessor extends ClusterUpdateProcessorBase {
+       
+       public final static boolean DEBUG = false;
+
+       final private ClusterChangeSet ccs;
+       
+       private int oldValuesIndex = 0;
+       private int statementMaskIndex = 0;
+       
+       final public List<Entry> entries = new ArrayList<Entry>();
+       
+       public UndoClusterUpdateProcessor(ClusterManager client, ClusterStreamChunk chunk, ClusterChangeSet ccs) throws DatabaseException {
+               super(client, readOperation(client, chunk, ccs));
+               this.ccs = ccs;
+       }
+       
+       private static byte[] readOperation(ClusterManager manager, ClusterStreamChunk chunk, ClusterChangeSet ccs) {
+               
+//             ClusterStreamChunk chunk;
+//             manager.streamLRU.acquireMutex();
+//             try {
+//                     chunk = ccs.getChunk(manager);
+//             } catch (Throwable t) {
+//                     throw new IllegalStateException(t);
+//             } finally {
+//                     manager.streamLRU.releaseMutex();
+//             }
+//
+//             chunk.acquireMutex();
+//             try {
+//             chunk.ve
+                       chunk.makeResident();
+                       return chunk.getOperation(ccs.chunkOffset);
+//             } catch (Throwable t) {
+//                     throw new IllegalStateException(t);
+//             } finally {
+//                     chunk.releaseMutex();
+//             }
+       }
+       
+       @Override
+       void create() throws DatabaseException {
+       }
+
+       @Override
+       void delete(int ri) throws DatabaseException {
+               
+               byte[] old = ccs.oldValues.get(oldValuesIndex);
+               boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
+               oldValuesIndex++;
+               
+               if(old != null) {
+                       entries.add(new Entry(ri, oldValueEx, old, null));
+               }
+               
+       }
+
+       @Override
+       void modify(int resourceKey, long offset, int size, byte[] bytes, int pos)
+                       throws DatabaseException {
+               
+       }
+
+       @Override
+       void set(int resourceKey, byte[] bytes, int length)
+                       throws DatabaseException {
+
+               byte[] old = ccs.oldValues.get(oldValuesIndex);
+               boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
+               oldValuesIndex++;
+
+               entries.add(new Entry(resourceKey, oldValueEx, old, Arrays.copyOf(valueBuffer, length)));
+               
+       }
+
+       @Override
+       void claim(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
+                       throws DatabaseException {
+               
+               boolean add = ccs.statementMask.get(statementMaskIndex++) > 0;
+               if(add) {
+                       entries.add(new Entry(Type.ADD, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
+               }
+               
+       }
+
+       @Override
+       void deny(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
+                       throws DatabaseException {
+               
+               boolean remove = ccs.statementMask.get(statementMaskIndex++) > 0;
+               if(remove) {
+                       entries.add(new Entry(Type.REMOVE, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
+               }
+
+       }
+       
+}