-
Notifications
You must be signed in to change notification settings - Fork 1
/
fst_reader.cpp
75 lines (57 loc) · 1.74 KB
/
fst_reader.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
/*********************************************************************
* File: fst_reader.cpp
* Desc:
*
*********************************************************************/
#include <fstream>
#include <indexlib/fst/fst_reader.h>
#include <indexlib/fst/fstlib.h>
using namespace std;
FstReader::FstReader(): m_keyCnt(0), m_size(0), m_fstByteArr(NULL) {}
FstReader::~FstReader() {
if (m_fstByteArr) {
delete m_fstByteArr;
m_fstByteArr = NULL;
}
m_size = 0;
}
bool FstReader::Init(const std::string &fstMmapFile) {
ifstream fin(fstMmapFile, ios_base::binary);
if (!fin) {
// LOG(ERROR, "cannot open: %s", fstMmapFile.c_str());
return false;
}
fin.seekg(0, ios_base::end);
auto size = fin.tellg();
fin.seekg(0, ios_base::beg);
m_keyCnt = 0;
fin.read((char*)(&m_keyCnt), sizeof(size_t));
// LOG(INFO, "read fst file: %s, file size=%lu, key count=%lu", fstMmapFile.c_str(), m_keyCnt, size);
if (0 == m_keyCnt) return true;
size -= sizeof(size_t);
m_size = size;
m_fstByteArr = new char[m_size];
fin.read(m_fstByteArr, m_size);
fin.close();
return true;
}
bool FstReader::ExactMatch(const char *key, output_t &pMatchPos) {
if (0 == m_keyCnt) return false;
auto outputs = fst::exact_match_search<output_t>(m_fstByteArr, m_size, key);
if(outputs.empty()) {
pMatchPos = -1;
} else {
pMatchPos = (output_t)(outputs[0]);
}
return (pMatchPos != -1);
}
output_t FstReader::GetUnitPos(const char *key)
{
output_t matchPos = -1;
ExactMatch(key, matchPos);
return matchPos;
}
uint32_t FstReader::GetSlotNum()
{
return m_keyCnt;
}