aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/healthmarketscience/jackcess/UsageMap.java
blob: 5639cb405b0d0556c3fdee676e83f6cae5c6d084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright (c) 2005 Health Market Science, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
USA

You can contact Health Market Science at info@healthmarketscience.com
or at the following address:

Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
*/

package com.healthmarketscience.jackcess;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Describes which database pages a particular table uses
 * @author Tim McCune
 */
public abstract class UsageMap {
  
  private static final Log LOG = LogFactory.getLog(UsageMap.class);
  
  /** Inline map type */
  public static final byte MAP_TYPE_INLINE = 0x0;
  /** Reference map type, for maps that are too large to fit inline */
  public static final byte MAP_TYPE_REFERENCE = 0x1;
  
  /** Index of the current page, incremented after calling getNextPage */
  private int _currentPageIndex = 0;
  /** Page number of the map declaration */
  private int _dataPageNum;
  /** Offset of the data page at which the usage map data starts */
  private int _startOffset;
  /** Offset of the data page at which the usage map declaration starts */
  private short _rowStart;
  /** Format of the database that contains this usage map */
  private JetFormat _format;
  /** List of page numbers used (Integer) */
  private List _pageNumbers = new ArrayList();
  /** Buffer that contains the usage map declaration page */
  private ByteBuffer _dataBuffer;
  /** Used to read in pages */
  private PageChannel _pageChannel;
  
  /**
   * @param pageChannel Used to read in pages
   * @param pageNum Page number that this usage map is contained in
   * @param rowNum Number of the row on the page that contains this usage map
   * @param format Format of the database that contains this usage map
   * @return Either an InlineUsageMap or a ReferenceUsageMap, depending on which
   *    type of map is found
   */
  public static UsageMap read(PageChannel pageChannel, int pageNum, byte rowNum, JetFormat format)
  throws IOException
  {
    ByteBuffer dataBuffer = pageChannel.createPageBuffer();
    pageChannel.readPage(dataBuffer, pageNum);
    short rowStart = dataBuffer.getShort(format.OFFSET_ROW_START + 2 * rowNum);
    int rowEnd;
    if (rowNum == 0) {
      rowEnd = format.PAGE_SIZE - 1;
    } else {
      rowEnd = (dataBuffer.getShort(format.OFFSET_ROW_START + (rowNum - 1) * 2) & 0x0FFF) - 1;
    }
    dataBuffer.limit(rowEnd + 1);
    byte mapType = dataBuffer.get(rowStart);
    UsageMap rtn;
    if (mapType == MAP_TYPE_INLINE) {
      rtn = new InlineUsageMap(pageChannel, dataBuffer, pageNum, format, rowStart);   
    } else if (mapType == MAP_TYPE_REFERENCE) {
      rtn = new ReferenceUsageMap(pageChannel, dataBuffer, pageNum, format, rowStart);
    } else {
      throw new IOException("Unrecognized map type: " + mapType);
    }
    return rtn;
  }
  
  /**
   * @param pageChannel Used to read in pages
   * @param dataBuffer Buffer that contains this map's declaration
   * @param pageNum Page number that this usage map is contained in
   * @param format Format of the database that contains this usage map
   * @param rowStart Offset at which the declaration starts in the buffer
   */
  public UsageMap(PageChannel pageChannel, ByteBuffer dataBuffer, int pageNum,
      JetFormat format, short rowStart)
  throws IOException
  {
    _pageChannel = pageChannel;
    _dataBuffer = dataBuffer;
    _dataPageNum = pageNum;
    _format = format;
    _rowStart = rowStart;
    _dataBuffer.position((int) _rowStart + format.OFFSET_MAP_START);
    _startOffset = _dataBuffer.position();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Usage map block:\n" + ByteUtil.toHexString(_dataBuffer, _rowStart,
          dataBuffer.limit() - _rowStart));
    }
  }
  
  protected short getRowStart() {
    return _rowStart;
  }
  
  public List getPageNumbers() {
    return _pageNumbers;
  }
  
  protected void setStartOffset(int startOffset) {
    _startOffset = startOffset;
  }
  
  protected int getStartOffset() {
    return _startOffset;
  }
  
  protected ByteBuffer getDataBuffer() {
    return _dataBuffer;
  }
  
  protected int getDataPageNumber() {
    return _dataPageNum;
  }
  
  protected PageChannel getPageChannel() {
    return _pageChannel;
  }
  
  protected JetFormat getFormat() {
    return _format;
  }
  
  /**
   * After calling this method, getNextPage will return the first page in the map
   */
  public void reset() {
    _currentPageIndex = 0;
  }
  
  /**
   * @param buffer Buffer to read the next page into
   * @return Whether or not there was another page to read
   */
  public boolean getNextPage(ByteBuffer buffer) throws IOException {
    if (_pageNumbers.size() > _currentPageIndex) {
      Integer pageNumber = (Integer) _pageNumbers.get(_currentPageIndex++);
      _pageChannel.readPage(buffer, pageNumber.intValue());
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Read in the page numbers in this inline map
   */
  protected void processMap(ByteBuffer buffer, int pageIndex, int startPage) {
    int byteCount = 0;
    while (buffer.hasRemaining()) {
      byte b = buffer.get();
      for (int i = 0; i < 8; i++) {
        if ((b & (1 << i)) != 0) {
          Integer pageNumber = new Integer((startPage + byteCount * 8 + i) +
              (pageIndex * _format.PAGES_PER_USAGE_MAP_PAGE));
          _pageNumbers.add(pageNumber);
        }
      }
      byteCount++;
    }
  }
  
  /**
   * Add a page number to this usage map
   */
  public void addPageNumber(int pageNumber) throws IOException {
    //Sanity check, only on in debug mode for performance considerations
    if (LOG.isDebugEnabled() && _pageNumbers.contains(new Integer(pageNumber))) {
      throw new IOException("Page number " + pageNumber + " already in usage map");
    }
    addOrRemovePageNumber(pageNumber, true);
  }
  
  /**
   * Remove a page number from this usage map
   */
  public void removePageNumber(int pageNumber) throws IOException {
    addOrRemovePageNumber(pageNumber, false);
  }
  
  protected void updateMap(int absolutePageNumber, int relativePageNumber,
      int bitmask, ByteBuffer buffer, boolean add)
  {
    //Find the byte to apply the bitmask to
    int offset = relativePageNumber / 8;
    byte b = buffer.get(_startOffset + offset);
    //Apply the bitmask
    if (add) {
      b |= bitmask;
      _pageNumbers.add(new Integer(absolutePageNumber));
    } else {
      b &= ~bitmask;
    }
    buffer.put(_startOffset + offset, b);
  }
  
  public String toString() {
    return "page numbers: " + _pageNumbers;
  }
  
  /**
   * @param pageNumber Page number to add or remove from this map
   * @param add True to add it, false to remove it
   */
  protected abstract void addOrRemovePageNumber(int pageNumber, boolean add) throws IOException;
  
}