summaryrefslogtreecommitdiffstats
path: root/src/java/com/healthmarketscience/jackcess/InlineUsageMap.java
blob: daf6ae440ef3ff60e387dd18964d12e4ec005c34 (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
/*
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;

/**
 * Usage map whose map is written inline in the same page.  This type of map
 * can contain a maximum of 512 pages, and is always used for free space maps.
 * It has a start page, which all page numbers in its map are calculated as
 * starting from.
 * @author Tim McCune
 */
public class InlineUsageMap extends UsageMap {
  
  /** Size in bytes of the map */
  private static final int MAP_SIZE = 64;
  
  /** First page that this usage map applies to */
  private int _startPage = 0;
  
  /**
   * @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 InlineUsageMap(PageChannel pageChannel, ByteBuffer dataBuffer,
      int pageNum, JetFormat format, short rowStart)
  throws IOException
  {
    super(pageChannel, dataBuffer, pageNum, format, rowStart);
    _startPage = dataBuffer.getInt(rowStart + 1);
    processMap(dataBuffer, 0, _startPage);
  }
  
  //Javadoc copied from UsageMap
  protected void addOrRemovePageNumber(final int pageNumber, boolean add)
  throws IOException
  {
    if (add && pageNumber < _startPage) {
      throw new IOException("Can't add page number " + pageNumber +
          " because it is less than start page " + _startPage);
    }
    int relativePageNumber = pageNumber - _startPage;
    ByteBuffer buffer = getDataBuffer();
    if ((!add && !getPageNumbers().remove(new Integer(pageNumber))) || (add &&
        (relativePageNumber > MAP_SIZE * 8 - 1)))
    {
      //Increase the start page to the current page and clear out the map.
      _startPage = pageNumber;
      buffer.position(getRowStart() + 1);
      buffer.putInt(_startPage);
      getPageNumbers().clear();
      if (!add) {
        for (int j = 0; j < MAP_SIZE; j++) {
          buffer.put((byte) 0xff); //Fill bitmap with 1s
        }
        for (int j = _startPage; j < _startPage + MAP_SIZE * 8; j++) {
          getPageNumbers().add(new Integer(j)); //Fill our list with page numbers
        }
      }
      getPageChannel().writePage(buffer, getDataPageNumber());
      relativePageNumber = pageNumber - _startPage;
    }
    updateMap(pageNumber, relativePageNumber, 1 << (relativePageNumber % 8), buffer, add);
    //Write the updated map back to disk
    getPageChannel().writePage(buffer, getDataPageNumber());
  }
  
}