LogFileNode xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.activeio.journal.active;
    19
    20  import java.io.DataInput;
    21  import java.io.DataOutput;
    22  import java.io.IOException;
    23
    24  /***
    25   * @version $Revision: 1.1 $
    26   */
    27  final class LogFileNode {
    28
    29      static final public int SERIALIZED_SIZE = 10;
    30
    31      private final LogFile logFile;
    32      private LogFileNode next;
    33
    34      /*** The id of the log file. */
    35      private int id;
    36      /*** Does it have live records in it? */
    37      private boolean active = false;
    38      /*** Is the log file in readonly mode */
    39      private boolean readOnly;
    40      /*** The location of the next append offset */
    41      private int appendOffset = 0;
    42
    43      public LogFileNode(LogFile logFile) {
    44          this.logFile = logFile;
    45      }
    46
    47      public LogFile getLogFile() {
    48          return logFile;
    49      }
    50
    51      /////////////////////////////////////////////////////////////
    52      //
    53      // Method used to mange the state of the log file.
    54      //
    55      /////////////////////////////////////////////////////////////
    56
    57      public void activate(int id) {
    58          if (active)
    59              throw new IllegalStateException("Log allready active.");
    60
    61          this.id = id;
    62          this.readOnly = false;
    63          this.active = true;
    64          this.appendOffset = 0;
    65      }
    66
    67      public int getId() {
    68          return id;
    69      }
    70
    71      public void setReadOnly(boolean enable) {
    72          if (!active)
    73              throw new IllegalStateException("Log not active.");
    74          this.readOnly = enable;
    75      }
    76
    77      public void reinitialize() throws IOException {
    78          if (active)
    79              throw new IllegalStateException("Cannot reinitialize an active log.");
    80
    81          this.id = -1;
    82          this.readOnly = true;
    83          this.appendOffset = 0;
    84          getLogFile().resize();
    85      }
    86
    87      public boolean isActive() {
    88          return active;
    89      }
    90
    91      public void setActive(boolean enable) {
    92          active = enable;
    93      }
    94
    95      public int getAppendOffset() {
    96          return appendOffset;
    97      }
    98
    99
    100     public Location getFirstRecordLocation() {
    101         if (isActive() && appendOffset > 0)
    102             return new Location(getId(), 0);
    103         return null;
    104     }
    105
    106     public boolean isReadOnly() {
    107         return readOnly;
    108     }
    109
    110     public void appended(int i) {
    111         appendOffset += i;
    112     }
    113
    114     /////////////////////////////////////////////////////////////
    115     //
    116     // Method used to maitain the list of LogFileNodes used by
    117     // the LogFileManager
    118     //
    119     /////////////////////////////////////////////////////////////
    120
    121     public LogFileNode getNext() {
    122         return next;
    123     }
    124
    125     public void setNext(LogFileNode state) {
    126         next = state;
    127     }
    128
    129     public LogFileNode getNextActive() {
    130         if (getNext().isActive())
    131             return getNext();
    132         return null;
    133     }
    134
    135     public LogFileNode getNextInactive() {
    136         if (!getNext().isActive())
    137             return getNext();
    138         return null;
    139     }
    140
    141     /***
    142      * @param data
    143      * @throws IOException
    144      */
    145     public void writeExternal(DataOutput data) throws IOException {
    146         data.writeInt(id);
    147         data.writeBoolean(active);
    148         data.writeBoolean(readOnly);
    149         data.writeInt(appendOffset);
    150     }
    151
    152     /***
    153      * @param data
    154      * @throws IOException
    155      */
    156     public void readExternal(DataInput data) throws IOException {
    157         id = data.readInt();
    158         active = data.readBoolean();
    159         readOnly = data.readBoolean();
    160         appendOffset = data.readInt();
    161     }
    162
    163     public void setAppendOffset(int offset) {
    164         appendOffset = offset;
    165     }
    166
    167 }