1 19 package org.netbeans.mdr.persistence.memoryimpl; 20 21 import java.util.*; 22 import org.netbeans.mdr.persistence.*; 23 24 public class MultivalueLog { 25 26 private static final byte OP_ADD = 0; 27 private static final byte OP_REMOVE = 1; 28 private static final byte OP_REPLACE = 2; 29 private static final byte OP_ADD_ALL = 3; 30 31 private static final int NULL_POSITION = -1; 32 33 private ArrayList log = new ArrayList (); 34 private MultivaluedIndexImpl index; 35 private boolean rollingBack = false; 36 37 39 public MultivalueLog(MultivaluedIndexImpl index) { 40 this.index = index; 41 } 42 43 45 48 public void clear () { 49 log.clear (); 50 } 51 52 55 public void logAdd (Object key, Object value) { 56 if (rollingBack) 57 return; 58 Record rec = new Record (OP_REMOVE, key, value); 59 log.add (0, rec); 60 } 61 62 65 public void logAdd (Object key, Object value, int position) { 66 if (rollingBack) 67 return; 68 Record rec = new Record (OP_REMOVE, key, value, position); 69 log.add (0, rec); 70 } 71 72 75 public void logRemove (Object key, Object value) { 76 if (rollingBack) 77 return; 78 Record rec = new Record (OP_ADD, key, value); 79 log.add (0, rec); 80 } 81 82 85 public void logRemove (Object key, Object value, int position) { 86 if (rollingBack) 87 return; 88 Record rec = new Record (OP_ADD, key, value, position); 89 log.add (0, rec); 90 } 91 92 95 public void logRemoveKey (Object key, Object value) { 96 if (rollingBack) 97 return; 98 Record rec = new Record (OP_ADD_ALL, key, value); 99 log.add (0, rec); 100 } 101 102 105 public void logReplace (Object key, Object oldValue, int position) { 106 if (rollingBack) 107 return; 108 Record rec = new Record (OP_REPLACE, key, oldValue, position); 109 log.add (0, rec); 110 } 111 112 115 public void rollBack () throws StorageException { 116 rollingBack = true; 117 Iterator iter = log.iterator(); 118 while(iter.hasNext()) { 119 Record rec = (Record) iter.next(); 120 switch (rec.opCode) { 121 case OP_ADD: 122 if (rec.position == NULL_POSITION) 123 index.add (rec.key, rec.value); 124 else 125 ((MultivaluedOrderedIndex) index).add (rec.key, rec.position, rec.value); 126 break; 127 case OP_REMOVE: 128 if (rec.position == NULL_POSITION) 129 index.remove (rec.key, rec.value); 130 else 131 ((MultivaluedOrderedIndex) index).remove (rec.key, rec.position); 132 break; 133 case OP_REPLACE: 134 ((MultivaluedOrderedIndex) index).replace (rec.key, rec.position, rec.value); 135 break; 136 case OP_ADD_ALL: 137 index.setKey (rec.key, rec.value); 138 } 139 } 140 rollingBack = false; 141 } 142 143 145 private static class Record { 146 147 byte opCode; Object key; 149 Object value; 150 int position; 151 152 public Record (byte opCode, Object key, Object value, int position) { 153 this.opCode = opCode; 154 this.key = key; 155 this.value = value; 156 this.position = position; 157 } 158 159 public Record (byte opCode, Object key, Object value) { 160 this.opCode = opCode; 161 this.key = key; 162 this.value = value; 163 this.position = NULL_POSITION; 164 } 165 166 } 168 } 169 | Popular Tags |