1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
* @(#)DocumentCompressor.java 1.16 06/10/30
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @date 1/13/98
* @author Jacek R. Ambroziak
* @group Sun Microsystems Laboratories
*/
package com.sun.java.help.search;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
class DocumentCompressor
{
static public final int NConceptsInGroup = 16;
static public final int BitsInLabel = 4;
static public final int DefaultSize = 32;
private int _nGroups;
private int _nExtents;
private int _freeComp;
private int _sizeComp = DefaultSize;
private int _kk;
private Compressor _currentCompressor = null;
private Compressor[] _compressors = new Compressor[DefaultSize];
private Compressor _kCompr = new Compressor();
private Compressor _lCompr = new Compressor();
private Compressor _mCompr = new Compressor();
private Compressor _posCompressor = new Compressor();
private IntegerArray _kTable = new IntegerArray();// k's for the series
private IntegerArray _lTable = new IntegerArray();// lengths of the C/P groups
private IntegerArray _maxConcepts = new IntegerArray();// maximal concepts in CP
private IntegerArray _concepts = new IntegerArray();
private IntegerArray _documents = new IntegerArray();
private IntegerArray _offsets = new IntegerArray();
private IntegerArray _titles = new IntegerArray();
private IntegerArray _positions = new IntegerArray();
private IntegerArray _labels = new IntegerArray();
private RandomAccessFile _posFile;
public DocumentCompressor(URL url) throws Exception
{
URL posURL = new URL(url, "POSITIONS");
if (isFileURL(posURL))
{
// this code should not be repeated
try {
URL offURL = new URL(url, "OFFSETS");
URLConnection connect = offURL.openConnection();
BufferedInputStream in =
new BufferedInputStream(connect.getInputStream());
int k1 = in.read();
StreamDecompressor sddocs = new StreamDecompressor(in);
sddocs.ascDecode(k1, _documents);
int k2 = in.read();
StreamDecompressor sdoffsets = new StreamDecompressor(in);
sdoffsets.ascDecode(k2, _offsets);
// decompress titles' ids table
int k3 = in.read();
StreamDecompressor sdtitles = new StreamDecompressor(in);
sdtitles.decode(k3, _titles);
in.close();
}
catch (java.io.FileNotFoundException e) {;}
_posFile =
new RandomAccessFile(Utilities.URLDecoder(posURL.getFile()),
"rw");
}
else
throw new IOException();
}
private boolean isFileURL(URL url) {
return url.getProtocol().equalsIgnoreCase("file");
}
public void close(String indexFile) throws java.io.IOException
{
_posFile.close();
writeOutOffsets(indexFile);
}
public void compress(int docID, int titleID,
ConceptLocation[] locations, int count,
ConceptLocation[] extents, int extCount)
throws java.io.IOException
{
long start = System.currentTimeMillis();
encode(locations, count, NConceptsInGroup);
if (extCount > 0)
encodeExtents(extents, extCount);
finalizeEncoding();
debug((System.currentTimeMillis() - start) + " msec proc");
int nBytes = byteCount();
start = System.currentTimeMillis();
long currentEnd = _posFile.length();
_documents.add(docID);
_offsets.add((int)currentEnd);
_titles.add(titleID);
_posFile.seek(currentEnd);
writeOut(_posFile);
debug((System.currentTimeMillis() - start) + " msec file");
debug("nGroups = " + _nGroups);
}
private void writeOutOffsets(String indexFile) throws java.io.IOException
{
Compressor documents = new Compressor();
int k1 = documents.compressAscending(_documents);
Compressor offsets = new Compressor();
int k2 = offsets.compressAscending(_offsets);
Compressor titles = new Compressor();
int k3 = titles.minimize(_titles, 8); // the starting k
int nBytes = documents.byteCount();
RandomAccessFile out = new RandomAccessFile(indexFile, "rw");
out.seek(0); // position at beginning
out.write(k1);
documents.write(out);
out.write(k2);
offsets.write(out);
out.write(k3);
titles.write(out);
out.close();
}
private void encode(ConceptLocation[] locations, int count, int nConcepts)
{
final int initK = 4;
// first sort by concept only
ConceptLocation.sortByConcept(locations, 0, count);
// using the fact that concepts are already sorted
// count of groups of 'nConcepts'
// go for differences directly
clear();
int conceptCounter = 0;
int fromIndex = 0;
int prevMax = 0;
int last = locations[0].getConcept(); // init w/ first ID
nextCompressor();
_concepts.add(last);
for (int i = 0;;)
{
for (; i < count && locations[i].getConcept() == last; i++)
locations[i].setConcept(conceptCounter);
if (i == count)
{
if (_concepts.cardinality() > 0)
{
++_nGroups;
_kTable.add(_currentCompressor.minimize(_concepts, initK));
}
encodePositions(locations, fromIndex, i, BitsInLabel);
break;
}
else // new concept (group?)
{
if (++conceptCounter == nConcepts)
{
++_nGroups;
// we are looking at the beginning of a new group
// last is maximal for the group just finished
// it won't be stored in concepts array but maxConcepts
_concepts.popLast();
_maxConcepts.add(last - prevMax);
prevMax = last;
_kTable.add(_currentCompressor.minimize(_concepts, initK));
encodePositions(locations, fromIndex, i, BitsInLabel);
fromIndex = i;
nextCompressor();
_concepts.clear();
conceptCounter = 0;
}
_concepts.add(locations[i].getConcept() - last);
last = locations[i].getConcept();
}
}
}
private void encodePositions(ConceptLocation[] locations, int from, int to,
int cK)
{
final int initK = 3;
int lastPos, k;
// sort in place by positions only
ConceptLocation.sortByPosition(locations, from, to);
_positions.clear();
_labels.clear();
_positions.add(lastPos = locations[from].getBegin());
_labels.add(locations[from].getConcept()); // now: a label
// skip duplicates
for (int i = from, j = from + 1; j < to; j++)
if (locations[i].equals(locations[j]) == false)
{
i = j;
_positions.add(locations[i].getBegin() - lastPos);
lastPos = locations[i].getBegin();
_labels.add(locations[i].getConcept()); // now: a label
}
// first find k by minimizing just positions w/o labels
_kTable.add(k = _posCompressor.minimize(_positions, initK));
_posCompressor.clear();
_posCompressor.encode(_positions, _labels, k, cK);
_currentCompressor.concatenate(_posCompressor);
}
private void encodeExtents(ConceptLocation[] extents, int extCount)
{
// side effects:
// 'k3' added to _kTable
// a number of compressors populated: header + lengths' lists
final int initK = 4;
int c = 0;
IntegerArray concepts = new IntegerArray(extCount); // differences
IntegerArray lengths = new IntegerArray();
IntegerArray kTable = new IntegerArray();
IntegerArray lTable = new IntegerArray();
// reserve a compressor for concatenated tables
nextCompressor();
Compressor extentsHeader = _currentCompressor;
for (int i = 0; i < extCount; i++)
if (extents[i].getConcept() != c)
{
if (c != 0)
{
_nExtents++;
nextCompressor();
kTable.add(_currentCompressor.minimize(lengths, initK));
lTable.add(_currentCompressor.byteCount());
}
concepts.add(extents[i].getConcept() - c);
c = extents[i].getConcept();
lengths.clear();
lengths.add(extents[i].getLength());
}
else
lengths.add(extents[i].getLength());
// last table of lengths
nextCompressor();
kTable.add(_currentCompressor.minimize(lengths, initK));
lTable.add(_currentCompressor.byteCount());
Compressor compressor1 = new Compressor();
kTable.add(compressor1.minimize(lTable, initK));
Compressor compressor2 = new Compressor();
kTable.add(compressor2.minimize(concepts, initK));
_kTable.add(extentsHeader.minimize(kTable, initK)); // k3
extentsHeader.concatenate(compressor1);
extentsHeader.concatenate(compressor2);
}
private void finalizeEncoding()
{
if (_nGroups > 1)
{
// if extents follow C/P groups we need the length of the last group
int limit = _nExtents > 0 ? _freeComp : _freeComp - 1;
for (int j = 0; j < limit; j++) // length of last not saved
_lTable.add(_compressors[j].byteCount());
_kTable.add(_mCompr.minimize(_maxConcepts, 3));
_kTable.add(_lCompr.minimize(_lTable, 3));
_kk = _kCompr.minimize(_kTable, 3);
_kCompr.concatenate(_lCompr);
_kCompr.concatenate(_mCompr);
}
else if (_nGroups == 1 && _nExtents > 0)
{
// length of the single C/P group packed with k-s
_kTable.add(_compressors[0].byteCount());
_kk = _kCompr.minimize(_kTable, 3);
}
debug("compr: "+byteCount()+" bytes");
}
private void writeOut(DataOutput out) throws java.io.IOException
{
if (_nExtents == 0)
if (_nGroups > 1)
{
out.write(0x80 | _kk);
_kCompr.write(out); // concatenated k,l,m
for (int j = 0; j < _freeComp; j++)
_compressors[j].write(out);
}
else // single group, no extents; code: 00
{
out.write(_kTable.at(0)); // k1
out.write(_kTable.at(1)); // k2
_compressors[0].write(out); // C/P
}
else // extents
{
out.write((_nGroups > 1 ? 0xC0 : 0x40) | _kk);
_kCompr.write(out);
for (int j = 0; j < _freeComp; j++)
_compressors[j].write(out);
}
}
private Compressor nextCompressor()
{
if (_freeComp == _sizeComp)
{
Compressor[] newArray = new Compressor[_sizeComp *= 2];
System.arraycopy(_compressors, 0, newArray, 0, _freeComp);
_compressors = newArray;
}
if (_compressors[_freeComp] == null)
_compressors[_freeComp] = new Compressor();
return _currentCompressor = _compressors[_freeComp++];
}
private int byteCount()
{
if (_nGroups == 1 && _nExtents == 0)
return 2 + _compressors[0].byteCount();
else
{
int result = 1; // initial kk
result += _kCompr.byteCount();
for (int j = 0; j < _freeComp; j++)
result += _compressors[j].byteCount();
return result;
}
}
private void clear()
{
_nGroups = 0;
_nExtents = 0;
_kTable.clear();
_lTable.clear();
_concepts.clear();
_maxConcepts.clear();
_kCompr.clear();
_lCompr.clear();
_mCompr.clear();
for (int i = 0; i < _sizeComp; i++)
if (_compressors[i] != null)
_compressors[i].clear();
_freeComp = 0;
_currentCompressor = null;
}
/**
* Debug code
*/
private static boolean debug=false;
private static void debug(String msg) {
if (debug) {
System.err.println("DocumentCompressor: "+msg);
}
}
}
|