forked from georgebarwood/Database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexFile.cs
196 lines (170 loc) · 5.15 KB
/
IndexFile.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
namespace DBNS {
using IO = System.IO;
using G = System.Collections.Generic;
/* An IndexFile allows random access to a large file by a particular key.
The keys are kept sorted in order in a B-Tree.
Each page in the B-Tree is represented by an IndexPage object.
*/
class IndexFile
{
public IndexFile( IO.Stream f, IndexFileInfo inf, DatabaseImp d, long indexId )
{
F = f; Inf = inf; Database = d; IndexId = indexId;
Initialise();
//System.Console.WriteLine("New IndexFile " + " Root=" + Root + " PageAlloc=" + PageAlloc );
//Dump();
}
public IO.Stream F; // The backing file.
public IndexFileInfo Inf; // Information about the number of keys and their datatypes ( see below for definition ).
DatabaseImp Database; // This is used to encode String and Binary keys.
public long IndexId;
public IndexPage Root;
public G.Dictionary<long,IndexPage> PageMap = new G.Dictionary<long,IndexPage>();
public long PageAlloc;
bool Saved;
public void PrepareToCommit() // Writes pages to the underlying file ( which will be fully buffered ).
{
if ( Saved ) return;
// Save the pages.
foreach ( G.KeyValuePair<long,IndexPage> pair in PageMap )
{
IndexPage p = pair.Value;
if ( !p.Saved )
{
p.WriteHeader();
F.Position = (long)p.PageId * IndexPage.PageSize;
// For last page, only write the amount actually used (so file size is minimised)
F.Write( p.Data, 0, p.PageId == PageAlloc-1 ? p.Size() : IndexPage.PageSize );
p.Saved = true;
}
}
Saved = true;
}
public void Commit()
{
F.Flush();
}
public void Rollback()
{
if ( Saved ) return;
PageMap.Clear();
Initialise();
}
void Initialise()
{
if ( F.Length == 0 ) // New file : create Root page.
{
Root = new IndexPage( Inf, null, Database );
AllocPageId( Root );
}
else
{
PageAlloc = (long) ( ( F.Length + IndexPage.PageSize - 1 ) / IndexPage.PageSize );
Root = GetPage( 0 ); // Maybe could delay this until first use of the index ( as index may never be used at all ).
}
Saved = true;
}
public G.IEnumerable<IndexFileRecord> From( IndexPage.DCompare seek, bool desc )
{
foreach ( IndexFileRecord v in Root.From(this,seek,desc) ) yield return v;
}
public void Insert( ref IndexFileRecord r )
{
Root.LeafOp( this, ref r, true );
Saved = false;
}
public void Delete( ref IndexFileRecord r )
{
Root.LeafOp( this, ref r, false );
Saved = false;
}
public IndexPage GetPage( long pageId )
{
IndexPage p;
PageMap.TryGetValue( pageId, out p );
if ( p == null )
{
var data = ReadPage( pageId );
p = ( 1 & data[0] ) == 0 ? new IndexPage( Inf, data, Database ) : new PageParent( Inf, data, Database );
p.PageId = pageId;
p.Saved = true;
PageMap[ pageId ] = p;
p.CheckPage();
}
return p;
}
byte[] ReadPage( long pageId ) // Reads a page from file as a byte array.
{
byte [] result = new byte[ IndexPage.PageSize ];
F.Position = pageId * IndexPage.PageSize;
int i = 0;
while ( i < IndexPage.PageSize )
{
int got = F.Read( result, i, IndexPage.PageSize-i );
if ( got == 0 ) break;
i += got;
}
return result;
}
public void AllocPageId( IndexPage p )
{
p.PageId = PageAlloc++;
PageMap[ p.PageId ] = p;
}
public IndexFileRecord ExtractKey( Value[] x, long id )
{
int n = Inf.BaseIx.Length;
IndexFileRecord result = new IndexFileRecord( n + 1 );
for ( int i = 0; i < n; i += 1 ) result.Col[ i ] = x[ Inf.BaseIx[ i ] ];
result.Col[ n ].L = id;
return result;
}
public void Dump()
{
System.Console.WriteLine( "IndexFile Dump PageSize=" + IndexPage.PageSize + " PageAlloc=" + PageAlloc );
Root.Dump( this );
System.Console.WriteLine( "End IndexFile Dump, PageSize=" + IndexPage.PageSize + " PageAlloc=" + PageAlloc);
System.Console.WriteLine();
}
} // end class IndexFile
struct IndexFileInfo
{
public DataType [] Types; // Types for the index columns.
public int KeyCount;
public int [] BaseIx; // Column indexes in base table for records in index ( used by ExtractKey ).
public long IndexId;
public int KeySize()
{
int result = 0;
for ( int i = 0; i < KeyCount; i += 1 )
result += DTI.Size( Types[i] );
return result;
}
}
struct IndexFileRecord
{
public Value [] Col;
public long Child;
public IndexFileRecord( int n )
{
Col = new Value[ n ];
Child = 0;
}
public IndexFileRecord Copy()
{
IndexFileRecord result = new IndexFileRecord( Col.Length );
result.Child = Child;
for ( int i=0; i<Col.Length; i += 1 ) result.Col[i] = Col[i];
return result;
}
public string ToString( IndexFileInfo Inf ) // For debugging / testing only.
{
string s = "";
for ( int i = 0 ; i < Inf.Types.Length; i+= 1 )
{
s += Util.ToString( Col[i], Inf.Types[i] ) + "|";
}
return s;
}
}
} // end namespace DBNS