KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > log > synraf > action > LogRecord


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.storage.raf.log.synraf.action;
25
26 import org.objectweb.jalisto.se.storage.raf.log.backup.LogObject;
27
28 import java.io.*;
29
30 public class LogRecord extends LogObject {
31
32     public LogRecord() {
33     }
34
35     public LogRecord(long address, byte[] before, int newDatasLength) {
36         this.address = address;
37         this.before = before;
38         this.newDatasLength = newDatasLength;
39     }
40
41     public long getAddress() {
42         return address;
43     }
44
45     public void setAddress(long address) {
46         this.address = address;
47     }
48
49     public byte[] getBefore() {
50         return before;
51     }
52
53     public void setBefore(byte[] before) {
54         this.before = before;
55     }
56
57     public int getNewDatasLength() {
58         return newDatasLength;
59     }
60
61     public void setNewDatasLength(int newDatasLength) {
62         this.newDatasLength = newDatasLength;
63     }
64
65     public Object JavaDoc internalReadFrom(DataInput in) throws IOException {
66         long address = in.readLong();
67         int newDatasLength = in.readInt();
68         byte[] before = new byte[in.readInt()];
69         in.readFully(before);
70         return new LogRecord(address, before, newDatasLength);
71     }
72
73     public void writeTo(DataOutput out) throws IOException {
74         LogObject.writeLogRecord(out, address, before, newDatasLength);
75     }
76
77     public void undoOn(RandomAccessFile raf) throws IOException {
78         System.out.println("undo " + this.toString());
79         if (raf.length() == (address + newDatasLength)) {
80             raf.setLength(address);
81         }
82         raf.seek(address);
83         raf.write(before);
84     }
85
86     public void corrupt(RandomAccessFile raf) throws IOException {
87         byte[] newDatas = new byte[newDatasLength / 2];
88         raf.seek(address);
89         raf.readFully(newDatas);
90
91         if (raf.length() == (address + newDatasLength)) {
92             raf.setLength(address);
93         }
94         raf.seek(address);
95         raf.write(before);
96
97         raf.seek(address);
98         raf.write(newDatas);
99     }
100
101
102     public String JavaDoc toString() {
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104         sb.append("LogRecord(");
105         sb.append(address).append(",");
106         sb.append(newDatasLength).append(",");
107         sb.append("b[").append(before.length).append("])");
108         return sb.toString();
109     }
110
111
112     private long address;
113     private int newDatasLength;
114     private byte[] before;
115
116
117     static final long serialVersionUID = -7589636669041161459L;
118 }
119
Popular Tags