5 // 8 January 2007 -- tds
7 // BSD License (http://galagosearch.org/license)
10 #include "DocumentNameReader.hpp"
12 void DocumentNameReader::getInSlot( std::string
& result
, const NameSlot
& slot
, int footerIndex
) const {
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;
25 for( char* current
= end
-1; current
>= start
; current
-- ) {
26 char digit
= (footer
% 10) + '0';
34 std::string
DocumentNameReader::operator[] ( int index
) const {
38 std::string
DocumentNameReader::get( int index
) const {
40 assert( index
< _documentCount
);
42 if( index
>= _documentCount
)
48 int big
= _slots
.size()-1;
51 while( big
-small
> 1 ) {
52 int middle
= small
+ (big
-small
)/2;
54 if( _slots
[middle
].offset
>= index
)
60 const NameSlot
& one
= _slots
[small
];
61 const NameSlot
& two
= _slots
[big
];
64 if (two
.offset
<= index
)
65 getInSlot(result
, two
, index
);
67 getInSlot(result
, one
, index
);
72 void DocumentNameReader::read( const std::string
& filename
) {
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
);
96 slot
.footerWidth
= footerWidth
;
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();
110 _documentCount
= offset
;