Bringing tree up to date.
[galago.git] / java / galago / src / galago / index / DocumentNameReader.java
blob13da6c126c9dfe5be49a3a81ed6b6348c5b1b385
1 //
2 // DocumentNameReader.java
3 //
4 // 28 November 2006 -- Trevor Strohman
5 //
6 // BSD License (http://galagosearch.org/license)
7 //
9 package galago.index;
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;
16 /**
17 * Reads a binary file of document names produced by DocumentNameWriter.
18 * The names are loaded into RAM for quick access.
20 * @author trevor
22 public class DocumentNameReader {
23 private static class NameSlot {
24 public String prefix;
25 public int offset;
26 public int footerWidth;
27 public int[] footers;
30 ArrayList<NameSlot> slots;
31 int documentCount;
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();
38 read( input );
39 input.close();
42 private String getInSlot( NameSlot slot, int footerIndex ) {
43 int footer = slot.footers[footerIndex-slot.offset];
44 String prefix = slot.prefix;
45 String documentName;
47 if( slot.footerWidth == 0 ) {
48 documentName = slot.prefix;
49 } else {
50 String format = "%s-%0" + slot.footerWidth + "d";
51 documentName = String.format( format, prefix, footer );
54 return documentName;
57 public String get( int index ) {
58 assert index >= 0;
59 assert index < documentCount;
61 if( index >= documentCount )
62 return "unknown";
64 if( index < 0 )
65 return "unknown";
67 int big = slots.size()-1;
68 int small = 0;
70 while( big-small > 1 ) {
71 int middle = small + (big-small)/2;
73 if( slots.get(middle).offset >= index )
74 big = middle;
75 else
76 small = middle;
79 NameSlot one = slots.get(small);
80 NameSlot two = slots.get(big);
81 String result = "";
83 if (two.offset <= index)
84 result = getInSlot(two, index);
85 else
86 result = getInSlot(one, index);
88 return result;
91 public void read( DataInputStream input ) throws IOException {
92 int offset = 0;
94 // open a file
95 while( input.available() > 0 ) {
96 // read the prefix
97 int prefixLength = input.readInt();
98 byte[] prefixData = new byte[prefixLength];
99 input.read(prefixData);
101 // read the footers
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();
115 slots.add(slot);
116 offset += slot.footers.length;
119 documentCount = offset;