KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > entry > INLogEntry


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: INLogEntry.java,v 1.36 2006/11/17 23:47:24 mark Exp $
7  */

8
9 package com.sleepycat.je.log.entry;
10
11 import java.nio.ByteBuffer JavaDoc;
12
13 import com.sleepycat.je.DatabaseException;
14 import com.sleepycat.je.dbi.DatabaseId;
15 import com.sleepycat.je.dbi.EnvironmentImpl;
16 import com.sleepycat.je.log.LogEntryType;
17 import com.sleepycat.je.log.LogUtils;
18 import com.sleepycat.je.log.LoggableObject;
19 import com.sleepycat.je.tree.IN;
20 import com.sleepycat.je.utilint.DbLsn;
21
22 /**
23  * INLogEntry embodies all IN log entries. These entries contain an IN and a
24  * databaseId. This class can both write out an entry and read one in.
25  */

26 public class INLogEntry
27     implements LogEntry, LoggableObject, NodeLogEntry, INContainingEntry {
28
29     /* Objects contained in an IN entry */
30     private IN in;
31     private DatabaseId dbId;
32
33     /*
34      * obsoleteFile was added in version 1, and changed to obsoleteLsn in
35      * version 2. If the offset is zero in the LSN, we read a version 1 entry
36      * since only the file number was stored.
37      */

38     private long obsoleteLsn;
39     
40     private long nodeId;
41     private Class JavaDoc logClass;
42
43     /**
44      * Construct a log entry for reading.
45      */

46     public INLogEntry(Class JavaDoc logClass) {
47         this.logClass = logClass;
48     }
49
50     /**
51      * Construct a log entry for writing to the log.
52      */

53     public INLogEntry(IN in) {
54         this.in = in;
55         this.dbId = in.getDatabase().getId();
56         this.logClass = in.getClass();
57         this.nodeId = in.getNodeId();
58         this.obsoleteLsn = in.getLastFullVersion();
59     }
60
61     /*
62      * Read support
63      */

64
65     /**
66      * Read in an IN entry.
67      */

68     public void readEntry(ByteBuffer JavaDoc entryBuffer,
69               int entrySize,
70                           byte entryTypeVersion,
71               boolean readFullItem)
72         throws DatabaseException {
73
74         entryTypeVersion &= LogEntryType.clearProvisional(entryTypeVersion);
75
76         try {
77             if (readFullItem) {
78                 /* Read IN and get node ID. */
79                 in = (IN) logClass.newInstance();
80                 in.readFromLog(entryBuffer, entryTypeVersion);
81                 nodeId = in.getNodeId();
82             } else {
83                 /* Calculate position following IN. */
84                 int position = entryBuffer.position() + entrySize;
85                 if (entryTypeVersion == 1) {
86                     /* Subtract size of obsoleteFile */
87                     position -= LogUtils.UNSIGNED_INT_BYTES;
88                 } else if (entryTypeVersion >= 2) {
89                     /* Subtract size of obsoleteLsn */
90                     position -= LogUtils.LONG_BYTES;
91                 }
92                 /* Subtract size of dbId */
93                 position -= LogUtils.INT_BYTES;
94                 /* Read node ID and position after IN. */
95                 nodeId = LogUtils.readLong(entryBuffer);
96                 entryBuffer.position(position);
97                 in = null;
98             }
99             dbId = new DatabaseId();
100             dbId.readFromLog(entryBuffer, entryTypeVersion);
101             if (entryTypeVersion < 1) {
102                 obsoleteLsn = DbLsn.NULL_LSN;
103             } else if (entryTypeVersion == 1) {
104                 long fileNum = LogUtils.getUnsignedInt(entryBuffer);
105                 if (fileNum == 0xffffffffL) {
106                     obsoleteLsn = DbLsn.NULL_LSN;
107                 } else {
108                     obsoleteLsn = DbLsn.makeLsn(fileNum, 0);
109                 }
110             } else {
111                 obsoleteLsn = LogUtils.readLong(entryBuffer);
112             }
113         } catch (IllegalAccessException JavaDoc e) {
114             throw new DatabaseException(e);
115         } catch (InstantiationException JavaDoc e) {
116             throw new DatabaseException(e);
117         }
118     }
119
120     /**
121      * Returns the LSN of the prior version of this node. Used for counting
122      * the prior version as obsolete. If the offset of the LSN is zero, only
123      * the file number is known because we read a version 1 log entry.
124      */

125     public long getObsoleteLsn() {
126
127         return obsoleteLsn;
128     }
129
130     /**
131      * Print out the contents of an entry.
132      */

133     public StringBuffer JavaDoc dumpEntry(StringBuffer JavaDoc sb, boolean verbose) {
134         in.dumpLog(sb, verbose);
135         dbId.dumpLog(sb, verbose);
136         return sb;
137     }
138
139     /**
140      * @return the item in the log entry
141      */

142     public Object JavaDoc getMainItem() {
143         return in;
144     }
145
146     public Object JavaDoc clone()
147         throws CloneNotSupportedException JavaDoc {
148
149         return super.clone();
150     }
151
152     /**
153      * @see LogEntry#isTransactional
154      */

155     public boolean isTransactional() {
156     return false;
157     }
158
159     /**
160      * @see LogEntry#getTransactionId
161      */

162     public long getTransactionId() {
163     return 0;
164     }
165
166     /*
167      * Writing support
168      */

169
170     /**
171      * @see LoggableObject#getLogType
172      */

173     public LogEntryType getLogType() {
174         return in.getLogType();
175     }
176
177     /**
178      * @see LoggableObject#marshallOutsideWriteLatch
179      * Ask the in if it can be marshalled outside the log write latch.
180      */

181     public boolean marshallOutsideWriteLatch() {
182         return in.marshallOutsideWriteLatch();
183     }
184
185     /**
186      * @see LoggableObject#countAsObsoleteWhenLogged
187      */

188     public boolean countAsObsoleteWhenLogged() {
189         return false;
190     }
191
192     /**
193      * @see LoggableObject#postLogWork
194      */

195     public void postLogWork(long justLoggedLsn) {
196     }
197
198     /**
199      * @see LoggableObject#getLogSize
200      */

201     public int getLogSize() {
202         return (in.getLogSize() +
203         dbId.getLogSize() +
204                 LogUtils.LONG_BYTES);
205     }
206
207     /**
208      * @see LoggableObject#writeToLog
209      */

210     public void writeToLog(ByteBuffer JavaDoc destBuffer) {
211         in.writeToLog(destBuffer);
212         dbId.writeToLog(destBuffer);
213         LogUtils.writeLong(destBuffer, obsoleteLsn);
214     }
215
216     /*
217      * Access the in held within the entry.
218      * @see INContainingEntry#getIN()
219      */

220     public IN getIN(EnvironmentImpl env)
221         throws DatabaseException {
222                 
223         return in;
224     }
225
226     /**
227      * @see NodeLogEntry#getNodeId
228      */

229     public long getNodeId() {
230         return nodeId;
231     }
232
233     /**
234      * @see INContainingEntry#getDbId()
235      */

236     public DatabaseId getDbId() {
237
238         return dbId;
239     }
240
241     /**
242      * @return the LSN that represents this IN. For a vanilla IN entry, it's
243      * the last lsn read by the log reader.
244      */

245     public long getLsnOfIN(long lastReadLsn) {
246         return lastReadLsn;
247     }
248 }
249
Popular Tags