1 5 package org.h2.store; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Constants; 10 import org.h2.engine.Database; 11 import org.h2.engine.Session; 12 import org.h2.message.Message; 13 import org.h2.util.ObjectArray; 14 15 public class UndoLog { 16 private Session session; 17 private Database database; 18 private ObjectArray records = new ObjectArray(); 20 private FileStore file; 21 private DataPage rowBuff; 22 private int memoryUndo; 23 24 public UndoLog(Session session) { 25 this.session = session; 26 this.database = session.getDatabase(); 27 } 28 29 public int size() { 30 if(Constants.CHECK && memoryUndo > records.size()) { 31 throw Message.getInternalError(); 32 } 33 return records.size(); 34 } 35 36 public void clear() { 37 records.clear(); 38 memoryUndo = 0; 39 if(file != null) { 40 file.closeAndDeleteSilently(); 41 file = null; 42 rowBuff = null; 43 } 44 } 45 46 public UndoLogRecord getAndRemoveLast() throws SQLException { 47 int i = records.size() - 1; 48 UndoLogRecord entry = (UndoLogRecord) records.get(i); 49 if(entry.isStored()) { 50 int start = Math.max(0, i-database.getMaxMemoryUndo()/2); 51 UndoLogRecord first = null; 52 for(int j=start; j<=i; j++) { 53 UndoLogRecord e = (UndoLogRecord) records.get(j); 54 if(e.isStored()) { 55 e.load(rowBuff, file, session); 56 memoryUndo++; 57 if(first == null) { 58 first = e; 59 } 60 } 61 } 62 first.seek(file); 63 } 64 records.remove(i); 65 memoryUndo--; 66 return entry; 67 } 68 69 public void add(UndoLogRecord entry) throws SQLException { 70 records.add(entry); 71 memoryUndo++; 72 if(memoryUndo > database.getMaxMemoryUndo() && database.isPersistent()) { 73 if(file == null) { 74 String fileName = database.createTempFile(); 75 file = database.openFile(fileName, false); 76 file.autoDelete(); 77 file.seek(FileStore.HEADER_LENGTH); 78 rowBuff = DataPage.create(database, Constants.DEFAULT_DATA_PAGE_SIZE); 79 } 80 DataPage buff = rowBuff; 81 for(int i=0; i<records.size(); i++) { 82 UndoLogRecord r = (UndoLogRecord) records.get(i); 83 if(!r.isStored() && r.canStore()) { 84 r.save(buff, file); 85 memoryUndo--; 86 } 87 } 88 } 89 } 90 91 } 92 | Popular Tags |