-
Notifications
You must be signed in to change notification settings - Fork 0
/
XTBIndexDBReader.cpp
205 lines (167 loc) · 4.35 KB
/
XTBIndexDBReader.cpp
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
//
// XTBIndexDBReader.cpp
// XTBook
//
// Created by Kawada Tomoaki on 7/28/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
#include "XTBIndexDBReader.h"
#include <tcw/twLock.h>
#include <tcw/twSDLSemaphore.h>
#include "XTBFileStream.h"
#include <assert.h>
#include "XTBException.h"
XTBIndexDBReader::XTBIndexDBReader(const XTBSysString& path):
m_path(path),
m_dbStream(NULL),
m_mapStream(NULL){
try{
m_dbStream=XTBFileStream::streamForReadingAtPath(pathForDb());
m_mapStream=XTBFileStream::streamForReadingAtPath(pathForMap());
}catch(...){
if(m_dbStream)
delete m_dbStream;
if(m_mapStream)
delete m_mapStream;
throw;
}
m_dbSemaphore=new twSDLSemaphore();
m_mapSemaphore=new twSDLSemaphore();
m_entryCount=m_mapStream->readBEInt32();
if(m_entryCount==0){
XTBThrowException(L"XTBInvalidFormatException", NULL);
}
}
XTBIndexDBReader::~XTBIndexDBReader(){
// make sure I/O is done
m_dbSemaphore->lock();
m_mapSemaphore->lock();
delete m_dbSemaphore;
delete m_mapSemaphore;
delete m_dbStream;
delete m_mapStream;
}
XTBSysString XTBIndexDBReader::pathForDb(){
return m_path+XTBSysText(".indexdb");
}
XTBSysString XTBIndexDBReader::pathForMap(){
return m_path+XTBSysText(".indexmap");
}
XTBOffset XTBIndexDBReader::offsetForIndex(unsigned int index){
twLock lock(m_mapSemaphore);
m_mapStream->seekToOffset(4+index*4);
return (XTBOffset)m_mapStream->readBEInt32();
}
XTBIndexDBEntry XTBIndexDBReader::entryAtIndex(unsigned int index){
twLock lock(m_dbSemaphore);
// seek to entry.
m_dbStream->seekToOffset(offsetForIndex(index));
// read sizes.
std::string sizes=m_dbStream->readToString(2);
size_t keyLength=(unsigned char)sizes[0];
size_t titleLength=(unsigned char)sizes[1];
// read both.
std::string combined=m_dbStream->readToString(keyLength+titleLength);
// expand.
XTBIndexDBEntry entry;
if(titleLength){
// non-optimized.
entry.key=combined.substr(0, keyLength);
entry.title=combined.substr(keyLength);
}else{
// optimized
entry.key.swap(combined);
entry.title=entry.key;
}
return entry;
}
void XTBIndexDBReader::readKeyAtIndex(unsigned int index,
char *buffer,
size_t maxLength){
twLock lock(m_dbSemaphore);
// seek to entry.
m_dbStream->seekToOffset(offsetForIndex(index));
// read sizes.
unsigned char sizes[2];
m_dbStream->readToBuffer(sizes, 2);
size_t keyLength=sizes[0];
if(keyLength<maxLength){
buffer[keyLength]=0;
}else{
keyLength=maxLength;
}
// read key.
m_dbStream->readToBuffer(buffer, keyLength);
}
class XTBIndexDBBinarySearchBuffer{
char *m_buffer;
unsigned int m_index;
XTBIndexDBReader *m_reader;
bool m_valid;
size_t m_maxLength;
void readBuffer(){
if(m_valid)
return;
m_reader->readKeyAtIndex(m_index, m_buffer, m_maxLength);
m_valid=true;
}
public:
XTBIndexDBBinarySearchBuffer(size_t size, XTBIndexDBReader *r){
m_buffer=new char[size];
m_reader=r;
m_valid=false;
m_maxLength=size;
}
void setIndex(unsigned int i){
if(i==m_index)
return;
m_index=i;
m_valid=false;
}
unsigned int index() const{return m_index;}
char *buffer(){
readBuffer();
return m_buffer;
}
~XTBIndexDBBinarySearchBuffer(){
delete[] m_buffer;
}
private:
void operator =(const XTBIndexDBBinarySearchBuffer&){}
};
unsigned int XTBIndexDBReader::lowerBoundForKey(const std::string &key){
if(key.empty())
return 0;
XTBIndexDBBinarySearchBuffer buf1(key.size(), this);
XTBIndexDBBinarySearchBuffer buf2(key.size(), this);
XTBIndexDBBinarySearchBuffer buf3(key.size(), this);
buf1.setIndex(0);
buf2.setIndex(m_entryCount-1);
while(buf1.index()<buf2.index()){
buf3.setIndex((buf1.index()+buf2.index())>>1);
if(strncmp(buf3.buffer(), key.c_str(), key.size())<0){
buf1.setIndex(buf3.index()+1);
}else{
buf2.setIndex(buf3.index());
}
}
return buf1.index();
}
unsigned int XTBIndexDBReader::upperBoundForKey(const std::string &key){
if(key.empty())
return 0;
XTBIndexDBBinarySearchBuffer buf1(key.size(), this);
XTBIndexDBBinarySearchBuffer buf2(key.size(), this);
XTBIndexDBBinarySearchBuffer buf3(key.size(), this);
buf1.setIndex(0);
buf2.setIndex(m_entryCount-1);
while(buf1.index()<buf2.index()){
buf3.setIndex((buf1.index()+buf2.index())>>1);
if(strncmp(buf3.buffer(), key.c_str(), key.size())<=0){
buf1.setIndex(buf3.index()+1);
}else{
buf2.setIndex(buf3.index());
}
}
return buf1.index();
}