File: BtreeDictCompactor.java

package info (click to toggle)
javahelp2 2.0.05.ds1-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,552 kB
  • sloc: java: 28,795; xml: 1,631; makefile: 16; sh: 2
file content (267 lines) | stat: -rw-r--r-- 8,027 bytes parent folder | download | duplicates (6)
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
/*
 * @(#)BtreeDictCompactor.java	1.9 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   5/1/98
 * @author Jacek R. Ambroziak
 * @group  Sun Microsystems Laboratories
 */

package com.sun.java.help.search;

import java.net.URL;

public class BtreeDictCompactor extends FullBtreeDict
{
  private CompactorDictBlock _currentLeaf;
  private byte[]          _lastKey = new byte[MaxKeyLength];
  private int             _lastKeyLength = 0;
  private int             _limit;
  private int             _nEntries = 0;
  private int             _entry = 0;
  
  private int             _counter = 0;
  private InternalBlockState _parent = null;
  
  private final class CompactorDictBlock extends FullDictBlock
  {
    private int restoreKey1(int entry, byte[] buffer)
    {
      int howMany = entryKeyLength(entry);
      int where = entryCompression(entry);
      int from = entryKey(entry);
      while (howMany-- > 0)
	buffer[where++] = data[from++];
      return where;
    }
  
    protected void doMap1(BtreeDictCompactor owner, BtreeDictCompactor target)
      throws Exception
    {
      byte[] buffer = new byte[MaxKeyLength];
      final int freeSpace = free();
      int entry = firstEntry();
      if (isLeaf)
	while (entry < freeSpace) {
	  target.store(buffer, restoreKey1(entry, buffer), entryID(entry));
	  entry = nextEntry(entry);
	}
      else
	{
	  owner.lock(this);
	  int entryIndex  = 0;
	  while (entry < freeSpace) {
	    owner.accessBlock1(getChildIdx(entryIndex)).doMap1(owner,target);
	    target.store(buffer, restoreKey1(entry, buffer), entryID(entry));
	    entry = nextEntry(entry);
	    ++entryIndex;
	  }
	  owner.accessBlock1(getChildIdx(entryIndex)).doMap1(owner, target);
	  owner.unlock(this);
	}
    }
  } // end of internal class

  private final class InternalBlockState
  {
    private CompactorDictBlock _block;
    private byte[]             _lastKey = new byte[MaxKeyLength];
    private int               _lastKeyLength;
    private int             _entry;
    private int             _nEntries;
    private int             _limit;
    private InternalBlockState _parent;
  
    public InternalBlockState(int leftChild)
    {
      debug("NEW ROOT " + _counter);
      debug(params.toString());
      params.setRoot(_counter);
      _block = new CompactorDictBlock();
      _block.isLeaf = false;
      _block.setBlockNumber(_counter++);
      init(leftChild);
    }
    
    private void init(int leftChild)
    {
      _entry = _block.firstEntry();
      _nEntries = 0;
      _lastKeyLength = 0;
      _limit = 4*(lastPtrIndex - 1);
      _block.setChildIndex(0, leftChild);
    }
  
    public void store(byte[] buffer, int keyLen, int id, int newBlock)
    {
      //      System.out.println(new String(buffer, 0, keyLen) + " " + id);
      int cpr = 0;
      while (cpr < _lastKeyLength && _lastKey[cpr] == buffer[cpr])
	++cpr;
      int needed = ENTHEADERLEN + keyLen - cpr;
      if (_entry + needed <= _limit)
	{
	  _block.makeEntry(_entry, buffer, id, keyLen - cpr, cpr);
	  _entry += needed;
	  _nEntries++;
	  _block.setChildIndex(_nEntries, newBlock);
	  _limit -= 4;
	  _lastKeyLength = keyLen;
	  System.arraycopy(buffer, cpr, _lastKey, cpr, keyLen - cpr);
	}
      else
	{
	  debug("NEW: SPLIT INTERNAL");
	  _block.setFree(_entry);
	  _block.setNumberOfEntries(_nEntries);
	  if (_parent == null)
	    _parent = new InternalBlockState(_block.number);
	  _parent.store(buffer, keyLen, id, newBlock(_block));
	  init(newBlock);
	}
    }
    
    public void close() throws java.io.IOException
    {
      _block.setFree(_entry);
      _block.setNumberOfEntries(_nEntries);
      blockManager.writeBlock(_block);
      if (_parent == null)
	debug("root: " + _block.number);
      else
	_parent.close();
    }
  } // end of internal class

  public BtreeDictCompactor(BtreeDictParameters params, boolean update)
    throws Exception
  {
    init(params, update, new BlockFactory() {
      public Block makeBlock() { return new CompactorDictBlock(); }
    });
    _currentLeaf = new CompactorDictBlock();
    _currentLeaf.setBlockNumber(_counter++);
    _limit = DATALEN - 2;
    _entry = _currentLeaf.firstEntry();
    this.params = params;
  }

  protected CompactorDictBlock accessBlock1(int index) throws Exception {
    return (CompactorDictBlock)blockManager.accessBlock(index);
  }
  
  public void store(byte[] buffer, int keyLen, int id)
  {
    //    System.out.println(new String(buffer, 0, keyLen));
    if (id > 0)
      {
	int cpr = 0;
	while (cpr < _lastKeyLength && _lastKey[cpr] == buffer[cpr])
	  ++cpr;
	int needed = ENTHEADERLEN + keyLen - cpr;
	if (_entry + needed <= _limit)
	  {
	    _currentLeaf.makeEntry(_entry, buffer, id, keyLen - cpr, cpr);
	    _entry += needed;
	    _nEntries++;
	    _lastKeyLength = keyLen;
	    System.arraycopy(buffer, cpr, _lastKey, cpr, keyLen - cpr);
	  }
	else
	  {
	    _currentLeaf.setFree(_entry);
	    _currentLeaf.setNumberOfEntries(_nEntries);
	    if (_parent == null)
	      _parent = new InternalBlockState(_currentLeaf.number);
	    _parent.store(buffer, keyLen, id, newBlock(_currentLeaf));
	    _entry = _currentLeaf.firstEntry();
	    _nEntries = 0;
	    _lastKeyLength = 0;
	  }
      }
  }

  private int newBlock(DictBlock block)
  {
    int number = _counter++;
    try {
      blockManager.writeBlock(block); // write out full
      block.setBlockNumber(number); // recycle
      blockManager.writeBlock(block); // reserve space for new
    }
    catch (java.io.IOException e) {
      e.printStackTrace();
    }
    return number;
  }

  public void close() throws java.io.IOException
  {
    _currentLeaf.setFree(_entry);
    _currentLeaf.setNumberOfEntries(_nEntries);
    blockManager.writeBlock(_currentLeaf);
    if (_parent == null)
      debug("root: " + _currentLeaf.number);
    else
      _parent.close();
    blockManager.close();
  }

  public void compact(BtreeDictParameters params) throws Exception
  {
    final BtreeDictCompactor target = new BtreeDictCompactor(params, true);
    ((CompactorDictBlock)blockManager.accessBlock(root)).doMap1(this, target);
    target.close();
    blockManager.close();
  }
  
  public static void main(String[] args)
  {
    try {
      Schema schema = new Schema(null, args[0], false);
      BtreeDictParameters params = new BtreeDictParameters(schema, "TMAP");
      BtreeDictCompactor source = new BtreeDictCompactor(params, false);
      URL url = new URL("file", "", args[1]);
      BtreeDictParameters params2 = new BtreeDictParameters(url, 2048, 0, 24);
      source.compact(params2);
    }
    catch (Exception e) {
      System.err.println(e);
      e.printStackTrace();
    }
  }
  /**
   * Debug code
   */

  private boolean debug=false;
  private void debug(String msg) {
    if (debug) {
      System.err.println("BtreeDictCompactor: "+msg);
    }
  }
}