1 package com.daffodilwoods.daffodildb.server.sessionsystem; 2 3 import java.util.ArrayList ; 4 import com.daffodilwoods.database.resource.DException; 5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype; 6 import com.daffodilwoods.daffodildb.utils.field.FieldLong; 7 import com.daffodilwoods.daffodildb.utils.BufferRange; 8 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 9 import com.daffodilwoods.daffodildb.utils.comparator.CTbnfUzqfDpnqbsbups; 10 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 11 import com.daffodilwoods.database.utility.P; 12 13 public class KeyTracer { 14 15 boolean isValidToPerform; 16 int count = 0; 17 int maxCount = 100; 18 ArrayList insertList; 19 ArrayList updateList; 20 ArrayList deleteList; 21 ArrayList sessionIdList = new ArrayList (); 22 private FieldBase lastSessionId = null; static final CTbnfUzqfDpnqbsbups stc = new CTbnfUzqfDpnqbsbups(); 24 25 public void insert( Object sessionId , Object key ) throws DException { 26 initailizeAll(); 27 performOperation(sessionId, insertList, key); 28 } 29 30 public void update( Object sessionId , Object key ) throws DException { 31 initailizeAll(); 32 performOperation(sessionId, updateList, key); 33 } 34 35 public void delete( Object sessionId , Object key ) throws DException { 36 initailizeAll(); 37 performOperation(sessionId, deleteList, key); 38 } 39 40 public SavePointTracer getSavePointTracer(Object sessionId) throws DException { 41 for (int i = 0, size = sessionIdList.size(); i < size; i++) { 42 SavePointTracer spt = (SavePointTracer) sessionIdList.get(i); 43 if(spt.equals(sessionId)) 44 return spt; 45 } 46 throw new DException("DSE0",new Object []{"key does not match"}); 47 } 48 49 public void removeSavePoint() { 50 Object removedId = sessionIdList.remove(sessionIdList.size()-1); 51 52 if(sessionIdList.size() == 0) 53 resetAll(); 54 } 55 56 public void rollBackSavePoint() { 57 if(insertList != null){ 58 SavePointTracer spt = (SavePointTracer) sessionIdList.get(sessionIdList. 59 size() - 1); 60 int lastId = spt.insertId; 61 for (int i = insertList.size()-1; i >= lastId; i--){ 62 insertList.remove(i); 63 } 64 lastId = spt.updateId; 65 for (int i = updateList.size()-1; i >= lastId; i--) 66 updateList.remove(i); 67 68 lastId = spt.deleteId; 69 for (int i = deleteList.size()-1; i >= lastId; i--) 70 deleteList.remove(i); 71 } 72 73 removeSavePoint(); 74 } 75 76 private void resetAll() { 77 count = 0; 78 isValidToPerform = false; 79 insertList.clear(); 80 updateList.clear(); 81 deleteList.clear(); 82 sessionIdList.clear(); 83 } 84 85 private void performOperation(Object sessionId, ArrayList list, Object key) throws DException { 86 int size = sessionIdList.size(); 87 if(size == 0 || !(size > 0 && sessionIdList.get(size-1).equals(sessionId))){ 88 if(lastSessionId !=null && stc.compare(lastSessionId, sessionId) >= 0) 89 return; 90 sessionIdList.add(new SavePointTracer(insertList.size() 91 ,updateList.size(),deleteList.size(), sessionId)); 92 } 93 if(++count == maxCount) { 94 resetAll(); 95 lastSessionId = (FieldBase) sessionId; 96 return; 97 } 98 isValidToPerform = true; 99 list.add(key); 100 } 101 102 private void initailizeAll() { 103 if(insertList == null) { 104 insertList = new ArrayList (); 105 deleteList = new ArrayList (); 106 updateList = new ArrayList (); 107 } 108 } 109 110 public void resetLastSessionId() { 111 lastSessionId = null; 112 } 113 114 public void showMap( String operation ){ 115 } 116 117 118 public void ensureParentSession(ArrayList sessionList) throws DException{ 119 if (lastSessionId != null) 120 return; 121 sessionIdList.size(); 122 initailizeAll(); 123 Object sessionId = null; 124 125 for (int i = 1; i < sessionList.size(); i++) { 126 sessionId = sessionList.get(i); 127 if (isSessionIdPresentInList(sessionId)) 128 continue; 129 sessionIdList.add(new SavePointTracer(insertList.size(),updateList.size(), 130 deleteList.size(), sessionId)); 131 132 } 133 } 134 135 136 137 private boolean isSessionIdPresentInList(Object sessionId ) throws DException{ 138 for (int i = 0; i <sessionIdList.size() ; i++) { 139 if( ((SavePointTracer)sessionIdList.get(i) ).equals(sessionId) ) 140 return true; 141 } 142 return false; 143 } 144 145 182 183 } 184 | Popular Tags |