Bringing tree up to date.
[galago.git] / cpp / galago / src / DocumentNameReader.cpp
blob1d1520a0d8f9d300510f492ba92b9c87070921dd
2 //
3 // DocumentNameReader
4 //
5 // 8 January 2007 -- tds
6 //
7 // BSD License (http://galagosearch.org/license)
8 //
10 #include "DocumentNameReader.hpp"
12 void DocumentNameReader::getInSlot( std::string& result, const NameSlot& slot, int footerIndex ) const {
13 char buffer[1024];
14 int footer = slot.footers[footerIndex-slot.offset];
16 strncpy( buffer, slot.prefix.c_str(), sizeof buffer );
18 char* start = buffer + slot.prefix.size();
19 char* end = start + slot.footerWidth + 1;
21 *start = '-';
22 *end = 0;
23 start++;
25 for( char* current = end-1; current >= start; current-- ) {
26 char digit = (footer % 10) + '0';
27 footer /= 10;
28 *current = digit;
31 result = buffer;
34 std::string DocumentNameReader::operator[] ( int index ) const {
35 return get(index);
38 std::string DocumentNameReader::get( int index ) const {
39 assert( index >= 0 );
40 assert( index < _documentCount );
42 if( index >= _documentCount )
43 return "unknown";
45 if( index < 0 )
46 return "unknown";
48 int big = _slots.size()-1;
49 int small = 0;
51 while( big-small > 1 ) {
52 int middle = small + (big-small)/2;
54 if( _slots[middle].offset >= index )
55 big = middle;
56 else
57 small = middle;
60 const NameSlot& one = _slots[small];
61 const NameSlot& two = _slots[big];
62 std::string result;
64 if (two.offset <= index)
65 getInSlot(result, two, index);
66 else
67 getInSlot(result, one, index);
69 return result;
72 void DocumentNameReader::read( const std::string& filename ) {
73 int offset = 0;
75 // mmap the file
76 indri::file::File inputFile;
78 inputFile.openRead( filename );
79 UINT64 length = inputFile.size();
80 indri::file::FileMapping* mapping = inputFile.mapFileRead();
81 const UINT8* region = (const UINT8*) mapping->region;
82 const UINT8* end = region + length;
84 while( region < end ) {
85 UINT32 prefixLength = UncompressedRead::read_u32( region );
87 char* prefixData = new char[prefixLength+1];
88 memcpy( prefixData, region, prefixLength );
89 prefixData[prefixLength] = 0;
90 region += prefixLength;
92 UINT32 footerWidth = UncompressedRead::read_u32( region );
93 UINT32 footerCount = UncompressedRead::read_u32( region );
95 NameSlot slot;
96 slot.footerWidth = footerWidth;
97 slot.offset = offset;
98 slot.prefix = prefixData;
100 for( int i=0; i<footerCount; i++ ) {
101 UINT32 footer = UncompressedRead::read_u32( region );
102 slot.footers.push_back( footer );
105 _slots.push_back(slot);
106 offset += slot.footers.size();
107 delete[] prefixData;
110 _documentCount = offset;
112 delete mapping;
113 inputFile.close();