2 // DocumentNameReader.java
4 // 28 November 2006 -- Trevor Strohman
6 // BSD License (http://galagosearch.org/license)
11 import java
.io
.BufferedInputStream
;
12 import java
.io
.DataInputStream
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.IOException
;
15 import java
.util
.ArrayList
;
17 * Reads a binary file of document names produced by DocumentNameWriter.
18 * The names are loaded into RAM for quick access.
22 public class DocumentNameReader
{
23 private static class NameSlot
{
26 public int footerWidth
;
30 ArrayList
<NameSlot
> slots
;
33 /** Creates a new instance of DocumentNameReader */
34 public DocumentNameReader( String filename
) throws IOException
{
35 FileInputStream f
= new FileInputStream( filename
);
36 DataInputStream input
= new DataInputStream( new BufferedInputStream( f
) );
37 slots
= new ArrayList();
42 private String
getInSlot( NameSlot slot
, int footerIndex
) {
43 int footer
= slot
.footers
[footerIndex
-slot
.offset
];
44 String prefix
= slot
.prefix
;
47 if( slot
.footerWidth
== 0 ) {
48 documentName
= slot
.prefix
;
50 String format
= "%s-%0" + slot
.footerWidth
+ "d";
51 documentName
= String
.format( format
, prefix
, footer
);
57 public String
get( int index
) {
59 assert index
< documentCount
;
61 if( index
>= documentCount
)
67 int big
= slots
.size()-1;
70 while( big
-small
> 1 ) {
71 int middle
= small
+ (big
-small
)/2;
73 if( slots
.get(middle
).offset
>= index
)
79 NameSlot one
= slots
.get(small
);
80 NameSlot two
= slots
.get(big
);
83 if (two
.offset
<= index
)
84 result
= getInSlot(two
, index
);
86 result
= getInSlot(one
, index
);
91 public void read( DataInputStream input
) throws IOException
{
95 while( input
.available() > 0 ) {
97 int prefixLength
= input
.readInt();
98 byte[] prefixData
= new byte[prefixLength
];
99 input
.read(prefixData
);
102 int footerWidth
= input
.readInt();
103 int footerCount
= input
.readInt();
104 NameSlot slot
= new NameSlot();
106 slot
.footerWidth
= footerWidth
;
107 slot
.offset
= offset
;
108 slot
.prefix
= new String(prefixData
);
109 slot
.footers
= new int[footerCount
];
111 for( int i
=0; i
<footerCount
; i
++ ) {
112 slot
.footers
[i
] = input
.readInt();
116 offset
+= slot
.footers
.length
;
119 documentCount
= offset
;