1 package org.simantics.acorn.internal;
3 import java.util.ArrayList;
4 import java.util.Arrays;
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 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class UndoClusterUpdateProcessor extends ClusterUpdateProcessorBase {
21 private static final Logger LOGGER = LoggerFactory.getLogger(UndoClusterUpdateProcessor.class);
23 public final static boolean DEBUG = false;
25 final private ClusterChangeSet ccs;
27 private int oldValuesIndex = 0;
28 private int statementMaskIndex = 0;
30 final public List<Entry> entries = new ArrayList<>();
32 public UndoClusterUpdateProcessor(ClusterManager client, ClusterStreamChunk chunk, ClusterChangeSet ccs) throws DatabaseException {
33 super(client, readOperation(client, chunk, ccs));
37 private static byte[] readOperation(ClusterManager manager, ClusterStreamChunk chunk, ClusterChangeSet ccs) throws AcornAccessVerificationException, IllegalAcornStateException {
39 return chunk.getOperation(ccs.chunkOffset);
43 void create() throws DatabaseException {
47 void delete(int ri) throws DatabaseException {
49 byte[] old = ccs.oldValues.get(oldValuesIndex);
50 boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
54 entries.add(new Entry(ri, oldValueEx, old, null));
60 void modify(int resourceKey, long offset, int size, byte[] bytes, int pos)
61 throws DatabaseException {
66 void set(int resourceKey, byte[] bytes, int length)
67 throws DatabaseException {
69 byte[] old = ccs.oldValues.get(oldValuesIndex);
70 boolean oldValueEx = ccs.oldValueEx.get(oldValuesIndex) > 0;
73 entries.add(new Entry(resourceKey, oldValueEx, old, Arrays.copyOf(valueBuffer, length)));
78 void claim(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
79 throws DatabaseException {
81 boolean add = ccs.statementMask.get(statementMaskIndex++) > 0;
83 entries.add(new Entry(Type.ADD, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
89 void deny(int resourceKey, int predicateKey, int objectKey, ClusterUID puid, ClusterUID ouid)
90 throws DatabaseException {
92 boolean remove = ccs.statementMask.get(statementMaskIndex++) > 0;
94 entries.add(new Entry(Type.REMOVE, resourceKey, puid, predicateKey & 0xFFF, ouid, objectKey & 0xFFF));
100 void setImmutable(boolean value) {
101 LOGGER.error("Attempted to undo `setImmutable({})` cluster operation for cluster {} which is not supported.", value, ccs.cuid);