-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiStore.cpp
More file actions
304 lines (234 loc) · 8.49 KB
/
MultiStore.cpp
File metadata and controls
304 lines (234 loc) · 8.49 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*******************************************************************
// Copyright (c) 2002, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// 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
//
*******************************************************************/
// MultiStore.cpp: implementation of the CMultiStore class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMultiStore::CMultiStore()
{_STT();
m_dwRecord = 0;
m_pvIndex = NULL;
m_dwBits = 0;
m_dwDepth = 0;
m_dwNodeSize = 0;
m_dwMaxMem = 0;
m_dwMaxDisk = 0;
}
CMultiStore::~CMultiStore()
{_STT();
Destroy();
}
BOOL CMultiStore::Write(LPSMultiStoreInfo pMsi, void *pMem, DWORD dwMem, void *pDisk, DWORD dwDisk, BOOL bOverwrite )
{_STT();
// Allocate record if needed
if ( pMsi->dwIndex == 0 ) pMsi->dwIndex = ++m_dwRecord;
else if ( !bOverwrite ) return FALSE;
// Set file position
m_store.SetPtrPosBegin( pMsi->dwIndex * m_dwMaxDisk );
// Write data to memory
if ( !pMem ) dwMem = 0;
if ( dwMem > m_dwMaxMem ) dwMem = m_dwMaxMem;
pMsi->dwMemSize = dwMem;
if ( pMem && dwMem ) memcpy( MemPtr( pMsi ), pMem, dwMem );
// Write data to disk
if ( !pDisk ) dwDisk = 0;
if ( dwDisk > m_dwMaxDisk ) dwDisk = m_dwMaxDisk;
pMsi->dwDiskSize = dwDisk;
if ( pDisk && dwDisk ) if ( !m_store.Write( pDisk, dwDisk ) )
return FALSE;
return TRUE;
}
BOOL CMultiStore::Read(LPSMultiStoreInfo pMsi, void *pMem, DWORD dwMem, void *pDisk, DWORD dwDisk)
{_STT();
// Allocate record if needed
if ( pMsi->dwIndex == 0 ) return FALSE;
// Read from memory
if ( pMem && dwMem )
{ DWORD dwRead = dwMem;
if ( dwRead > m_dwMaxMem ) dwRead = m_dwMaxMem;
if ( dwRead > pMsi->dwMemSize ) dwRead = pMsi->dwMemSize;
memcpy( pMem, MemPtr( pMsi ), dwRead );
if ( dwRead < dwMem ) ( (LPBYTE)pMem )[ dwRead ] = 0;
} // end if
if ( pDisk && dwDisk )
{ DWORD dwRead = dwDisk;
if ( dwRead > m_dwMaxDisk ) dwRead = m_dwMaxDisk;
if ( dwRead > pMsi->dwDiskSize ) dwRead = pMsi->dwDiskSize;
// Set file position
if ( !m_store.SetPtrPosBegin( pMsi->dwIndex * m_dwMaxDisk ) )
return FALSE;
// Read the data from disk
if ( !m_store.Read( pDisk, dwRead, &dwRead ) || !dwRead )
return FALSE;
// NULL terminate if room
if ( dwDisk > dwRead ) ( (LPBYTE)pMem )[ dwRead ] = 0;
} // end if
return TRUE;
}
BOOL CMultiStore::Open( LPCTSTR pFile, DWORD dwMaxMem, DWORD dwMaxDisk, DWORD dwBits )
{_STT();
// Lose previous
Destroy();
// Save data params
m_dwMaxMem = dwMaxMem;
m_dwMaxDisk = dwMaxDisk;
// Number of bits to use
m_dwBits = ( dwBits <= 0 || dwBits >= 32 ) ? 8 : dwBits;
// Ensure it's a power of 2
if ( m_dwBits & ( m_dwBits - 1 ) ) m_dwBits = 8;
// Calculate index depth
m_dwDepth = 32 / m_dwBits;
// Calculate node size
m_dwNodeSize = 1 << m_dwBits;
// Open the data file
return m_store.Open( pFile, GENERIC_READ | GENERIC_WRITE );
}
void CMultiStore::Destroy()
{_STT();
// Lose the index
if ( m_pvIndex != NULL ) DestroyIndex( &m_pvIndex );
m_dwRecord = 0;
m_dwBits = 0;
m_dwDepth = 0;
m_dwNodeSize = 0;
m_dwMaxMem = 0;
m_dwMaxDisk = 0;
}
void CMultiStore::DestroyIndex(LPVOID *pvIndex, DWORD dwDepth)
{_STT();
if ( pvIndex == NULL || *pvIndex == NULL ) return;
// If memory depth
if ( dwDepth + 1 < m_dwDepth )
// Delete each sub array
for ( DWORD i = 0; i < m_dwNodeSize; i++ )
DestroyIndex( &( (LPVOID*)*pvIndex )[ i ], dwDepth + 1 );
// Delete the index
_PTR_DELETE_ARR( *pvIndex );
}
LPSMultiStoreInfo CMultiStore::GetIndexPtr(DWORD dwIndex, LPVOID *pvIndex, BOOL bCreate, DWORD dwDepth )
{_STT();
ASSERT( dwDepth <= m_dwDepth );
// Calculate sub index
DWORD dwShift = ( ( m_dwDepth - 1 ) - dwDepth ) * m_dwBits;
DWORD dwSubIndex = ( dwIndex >> dwShift ) & ( ( 1 << m_dwBits ) - 1 );
// Are we at item level?
if ( dwDepth + 1 >= m_dwDepth )
{
DWORD dwUnitSize = sizeof( SMultiStoreInfo ) + m_dwMaxMem;
// Allocate memory if needed
if ( *pvIndex == NULL )
{ if ( !bCreate ) return NULL;
DWORD dwSize = dwUnitSize * m_dwNodeSize;
*pvIndex = _PTR_NEW BYTE[ dwSize ];
ZeroMemory( *pvIndex, dwSize );
} // end if
// Return a pointer to this block
return (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
} // end if
if ( *pvIndex == NULL )
{ if ( !bCreate ) return NULL;
*pvIndex = _PTR_NEW LPVOID[ m_dwNodeSize ];
ZeroMemory( *pvIndex, m_dwNodeSize * sizeof( LPVOID ) );
} // end if
// Keep digging
return GetIndexPtr( dwIndex, &( (LPVOID*)*pvIndex )[ dwSubIndex ], bCreate, dwDepth + 1 );
}
LPSMultiStoreInfo CMultiStore::GetNextIndexPtr(DWORD dwIndex, LPVOID *pvIndex, DWORD dwDepth )
{_STT();
ASSERT( dwDepth <= m_dwDepth );
// Ensure valid pointer
if ( pvIndex == NULL || *pvIndex == NULL ) return NULL;
// Calculate sub index
DWORD dwShift = ( ( m_dwDepth - 1 ) - dwDepth ) * m_dwBits;
DWORD dwSubIndex = ( dwIndex >> dwShift ) & ( ( 1 << m_dwBits ) - 1 );
// Are we at item level?
if ( dwDepth + 1 >= m_dwDepth )
{
DWORD dwUnitSize = sizeof( SMultiStoreInfo ) + m_dwMaxMem;
// Find a valid item
LPSMultiStoreInfo pMsi = (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
while ( pMsi->dwIndex == 0 )
{ if ( ++dwSubIndex >= m_dwNodeSize ) return NULL;
pMsi = (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
} // end while
// Return a pointer to this block
return (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
} // end if
DWORD dwResetMask = 0;
for ( DWORD i = 0; i < dwShift; i++ )
dwResetMask <<=1, dwResetMask |= 1;
// Find the next valid array
while ( dwSubIndex < m_dwNodeSize )
{
// Verify we're still in a valid range
// if ( dwSubIndex >= m_dwNodeSize ) return NULL;
// Get the next pointer
LPVOID *pvNext = &( (LPVOID*)*pvIndex )[ dwSubIndex ];
// Dig for a valid object
LPSMultiStoreInfo pMsi = GetNextIndexPtr( dwIndex, pvNext, dwDepth + 1 );
if ( pMsi != NULL ) return pMsi;
// Next block
dwSubIndex++;
dwIndex &= ~dwResetMask;
} // end if
return NULL;
}
LPSMultiStoreInfo CMultiStore::GetPrevIndexPtr(DWORD dwIndex, LPVOID *pvIndex, DWORD dwDepth )
{_STT();
// Ensure valid pointer
if ( dwDepth > m_dwDepth || pvIndex == NULL || *pvIndex == NULL ) return NULL;
// Calculate sub index
DWORD dwShift = ( ( m_dwDepth - 1 ) - dwDepth ) * m_dwBits;
DWORD dwSubIndex = ( dwIndex >> dwShift ) & ( ( 1 << m_dwBits ) - 1 );
// Are we at item level?
if ( dwDepth + 1 >= m_dwDepth )
{
DWORD dwUnitSize = sizeof( SMultiStoreInfo ) + m_dwMaxMem;
// Find a valid item
LPSMultiStoreInfo pMsi = (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
while ( pMsi->dwIndex == 0 )
{ if ( !dwSubIndex ) return NULL; dwSubIndex--;
pMsi = (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
} // end while
// Return a pointer to this block
return (LPSMultiStoreInfo)&( (LPBYTE)*pvIndex )[ dwUnitSize * dwSubIndex ];
} // end if
DWORD dwResetMask = 0; // ( 1 << m_dwBits ) - 1;
for ( DWORD i = 0; i < dwShift; i++ )
dwResetMask <<=1, dwResetMask |= 1;
// Find the next valid array
do
{
// Get the next pointer
LPVOID *pvNext = &( (LPVOID*)*pvIndex )[ dwSubIndex ];
// Dig for a valid object
LPSMultiStoreInfo pMsi = GetPrevIndexPtr( dwIndex, pvNext, dwDepth + 1 );
if ( pMsi != NULL ) return pMsi;
// Next block
if ( dwSubIndex ) dwSubIndex--;
dwIndex |= dwResetMask;
} while ( dwSubIndex );
return NULL;
}