forked from georgebarwood/Database
/
Stream.cs
262 lines (231 loc) · 6.83 KB
/
Stream.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
namespace DBNS {
using IO = System.IO;
using G = System.Collections.Generic;
class FullyBufferedStream : IO.Stream
{
// Implementation of IO.Stream in which all writes are buffered, and buffers are kept for every page access ( unless Rollback is called ).
// Writes are recorded in the supplied log file, which ensures atomic updates.
// Flush() must be called to write changes to the underlying stream, alternatively Rollback() may be called to discard changes.
public FullyBufferedStream ( Log log, long fileId, IO.FileStream f )
{
Log = log;
FileId = fileId;
BaseStream = f;
Len = BaseStream.Length;
Pos = 0;
ReadAvail = 0;
WriteAvail = 0;
}
readonly Log Log; // Log file to ensure atomic updates.
readonly long FileId; // For Log file.
readonly IO.FileStream BaseStream; // Underlying stream.
long Len; // File length
long Pos; // Current position in the file
readonly G.Dictionary<long,byte[]> Buffers = new G.Dictionary<long,byte[]>();
readonly G.SortedSet<long> UnsavedPageNums = new G.SortedSet<long>();
// Buffer size constants.
const int BufferShift = 12; // Log base 2 of BufferSize.
const int BufferSize = 1 << BufferShift;
byte [] CurBuffer; // The current buffer.
long CurBufferNum = -1; // The page number of the current buffer.
int CurIndex; // Index into current buffer ( equal to Pos % BufferSize ).
int WriteAvail; // Number of bytes available for writing in CurBuffer ( from CurIndex ).
int ReadAvail; // Number of bytes available for reading in CurBuffer ( from CurIndex ).
bool UnsavedAdded; // Current buffer has been added to UnsavedPageNums.
public override bool CanRead { get{ return true; } }
public override bool CanWrite { get{ return true; } }
public override bool CanSeek { get{ return true; } }
public override long Length { get{ return Len; } }
public override long Position
{
get{ return Pos; }
set
{
if ( value != Pos )
{
Pos = value;
WriteAvail = 0;
ReadAvail = 0;
}
}
}
public override long Seek( long to, System.IO.SeekOrigin how )
{
Position =
how == System.IO.SeekOrigin.Begin ? to
: how == System.IO.SeekOrigin.End ? Len + to
: Pos + to; // System.IO.SeekOrigin.Current
return Position;
}
public override int Read( byte[] b, int off, int n )
{
int request = n;
while ( n > 0 )
{
int got = n > ReadAvail ? ReadAvail : n;
if ( got > 0 )
{
for ( int i = 0; i < got; i += 1 ) b[ off + i ] = CurBuffer[ CurIndex + i ];
off += got;
n -= got;
Pos += got;
CurIndex += got;
ReadAvail -= got;
}
else if ( Pos < Len ) DoSeek( true );
else break;
}
return request - n;
}
// Instead of copying bytes, if possible Fastread returns the underlying buffer and an index into it.
public byte[] FastRead( int n, out int ix )
{
if ( ReadAvail == 0 ) DoSeek( true );
if ( ReadAvail >= n )
{
ix = CurIndex;
CurIndex += n;
Pos += n;
ReadAvail -= n;
return CurBuffer;
}
else
{
byte [] result = new byte[ n ];
Read( result, 0, n );
ix = 0;
return result;
}
}
public override void Write( byte[] b, int off, int n )
{
Log.LogWrite( FileId, Pos, b, off, n );
if ( Pos + n > Len ) Len = Pos + n;
while ( n > 0 )
{
int got = n > WriteAvail ? WriteAvail : n;
if ( got > 0 )
{
if ( !UnsavedAdded )
{
UnsavedPageNums.Add( CurBufferNum );
UnsavedAdded = true;
}
for ( int i = 0; i < got; i += 1 ) CurBuffer[ CurIndex + i ] = b[ off + i ];
off += got;
n -= got;
Pos += got;
CurIndex += got;
WriteAvail -= got;
}
else DoSeek( false );
}
}
// Version of Write which checks the first byte written is zero.
public bool Write( byte[] b, int off, int n, bool checkFirstByteZero )
{
Log.LogWrite( FileId, Pos, b, off, n );
if ( Pos + n > Len ) Len = Pos + n;
while ( n > 0 )
{
int got = n > WriteAvail ? WriteAvail : n;
if ( got > 0 )
{
if ( checkFirstByteZero )
{
if ( CurBuffer[ CurIndex ] != 0 ) return false;
checkFirstByteZero = false;
}
if ( !UnsavedAdded )
{
UnsavedPageNums.Add( CurBufferNum );
UnsavedAdded = true;
}
for ( int i = 0; i < got; i += 1 ) CurBuffer[ CurIndex + i ] = b[ off + i ];
off += got;
n -= got;
Pos += got;
CurIndex += got;
WriteAvail -= got;
}
else DoSeek( false );
}
return true;
}
public void Rollback()
{
Buffers.Clear();
UnsavedPageNums.Clear();
UnsavedAdded = false;
ReadAvail = 0;
WriteAvail = 0;
CurBufferNum = -1;
}
public override void Flush()
{
foreach ( long bufferNum in UnsavedPageNums )
{
byte [] b = GetBuffer( bufferNum );
long pos = bufferNum << BufferShift;
long n = Len - pos;
if ( n > BufferSize ) n = BufferSize;
if ( BaseStream.Position != pos ) BaseStream.Position = pos;
BaseStream.Write( b, 0, (int)n );
}
UnsavedPageNums.Clear();
UnsavedAdded = false;
BaseStream.SetLength( Len );
BaseStream.Flush( true );
}
public override void Close()
{
Rollback();
BaseStream.Close();
}
public override void SetLength( long x )
{
Log.SetLength( FileId, x );
Len = x;
ReadAvail = 0;
WriteAvail = 0;
}
void DoSeek( bool read )
{
if ( CurBufferNum != ( Pos >> BufferShift ) )
{
CurBufferNum = Pos >> BufferShift;
CurBuffer = GetBuffer( CurBufferNum );
UnsavedAdded = false;
}
CurIndex = (int) ( Pos & ( BufferSize - 1 ) );
if ( read )
{
ReadAvail = BufferSize - CurIndex;
if ( ReadAvail > Len - Pos ) ReadAvail = (int)( Len - Pos );
WriteAvail = 0;
}
else
{
WriteAvail = BufferSize - CurIndex;
ReadAvail = 0;
}
}
byte [] GetBuffer( long bufferNum )
{
byte [] result;
if ( Buffers.TryGetValue( bufferNum, out result ) ) return result;
result = new byte[ BufferSize ];
Buffers[ bufferNum ] = result;
long pos = bufferNum << BufferShift;
if ( BaseStream.Position != pos ) BaseStream.Position = pos;
int i = 0;
while ( i < BufferSize )
{
int got = BaseStream.Read( result, i, BufferSize - i );
if ( got == 0 ) break;
i += got;
}
return result;
}
} // end class FullyBufferedStream
} // end namespace DBNS