1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.impl;
18
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21
22
23
24
25
26
27 public final class TempPageHolder {
28
29 private int _pageNumber = PageChannel.INVALID_PAGE_NUMBER;
30 private final TempBufferHolder _buffer;
31
32
33
34 private int _bufferModCount;
35
36 private TempPageHolder(TempBufferHolder.Type type) {
37 _buffer = TempBufferHolder.newHolder(type, false);
38 _bufferModCount = _buffer.getModCount();
39 }
40
41
42
43
44
45 public static TempPageHolder newHolder(TempBufferHolder.Type type) {
46 return new TempPageHolder(type);
47 }
48
49
50
51
52 public int getPageNumber() {
53 return _pageNumber;
54 }
55
56
57
58
59
60 public ByteBuffer getPage(PageChannel pageChannel)
61 throws IOException
62 {
63 return setPage(pageChannel, _pageNumber, false);
64 }
65
66
67
68
69
70
71 public ByteBuffer setPage(PageChannel pageChannel, int pageNumber)
72 throws IOException
73 {
74 return setPage(pageChannel, pageNumber, true);
75 }
76
77 private ByteBuffer setPage(PageChannel pageChannel, int pageNumber,
78 boolean rewind)
79 throws IOException
80 {
81 ByteBuffer buffer = _buffer.getPageBuffer(pageChannel);
82 int modCount = _buffer.getModCount();
83 if((pageNumber != _pageNumber) || (_bufferModCount != modCount)) {
84 _pageNumber = pageNumber;
85 _bufferModCount = modCount;
86 pageChannel.readPage(buffer, _pageNumber);
87 } else if(rewind) {
88 buffer.rewind();
89 }
90
91 return buffer;
92 }
93
94
95
96
97
98 public ByteBuffer setNewPage(PageChannel pageChannel)
99 throws IOException
100 {
101
102 clear();
103
104 _pageNumber = pageChannel.allocateNewPage();
105
106 return _buffer.getPageBuffer(pageChannel);
107 }
108
109
110
111
112
113
114 public void invalidate() {
115 possiblyInvalidate(_pageNumber, null);
116 }
117
118
119
120
121
122
123
124 public void possiblyInvalidate(int modifiedPageNumber,
125 ByteBuffer modifiedBuffer) {
126 if(modifiedBuffer == _buffer.getExistingBuffer()) {
127
128
129 return;
130 }
131 if(modifiedPageNumber == _pageNumber) {
132 _pageNumber = PageChannel.INVALID_PAGE_NUMBER;
133 }
134 }
135
136
137
138
139
140
141 public void clear() {
142 invalidate();
143 _buffer.clear();
144 }
145
146 }