1 package org.galagosearch.core.index;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 public class ExtentListBuffer implements IndexElement {
7 public ExtentListBuffer() {
8 super();
9 documentCount = 0;
10 extentCount = 0;
11 endCount = 0;
12 data = new CompressedByteBuffer();
13 header = new CompressedByteBuffer();
14 documentExtents = new CompressedByteBuffer();
15 }
16
17 public long dataLength() {
18 long listLength = 0;
19 listLength += header.length();
20 listLength += data.length();
21 return listLength;
22 }
23
24 public void write(final OutputStream output) throws IOException {
25 output.write(header.getBytes(), 0, header.length());
26 output.write(data.getBytes(), 0, data.length());
27 }
28
29 public byte[] key() {
30 return word;
31 }
32
33 public void setWord(byte[] word) {
34 this.word = word;
35 this.lastDocument = 0;
36 this.lastPosition = 0;
37 this.extentCount = 0;
38 }
39
40 void setValue(long value) {
41 this.value = value;
42 }
43
44 private void storePreviousDocument() {
45 if (data.position > 0) {
46 data.add(extentCount);
47 data.add(documentExtents);
48 documentExtents.clear();
49 }
50 }
51
52 public void addDocument(long documentID) {
53 storePreviousDocument();
54 data.add(documentID - lastDocument);
55 lastDocument = documentID;
56 lastStartExtent = 0;
57 extentCount = 0;
58 endCount = 0;
59 value = 0;
60 documentCount++;
61 }
62
63 public void addBegin(int begin) {
64 extentCount++;
65 documentExtents.add(begin - lastStartExtent);
66 lastStartExtent = begin;
67 }
68
69 public void addEnd(int end) {
70 endCount++;
71
72 if (endCount > extentCount) {
73 documentExtents.add(0);
74 extentCount++;
75 }
76 documentExtents.add(end - lastStartExtent);
77 documentExtents.add(value);
78 }
79
80 public void close() {
81 int options = 0;
82 storePreviousDocument();
83 header.add(options);
84 header.add(documentCount);
85 }
86
87 private long value;
88 private long lastDocument;
89 private int lastStartExtent;
90 private int lastPosition;
91 private int extentCount;
92 private int endCount;
93 private int documentCount;
94 public byte[] word;
95 public CompressedByteBuffer header;
96 public CompressedByteBuffer data;
97 public CompressedByteBuffer documentExtents;
98 }