01: /* Copyright (C) 2003 Internet Archive.
02: *
03: * This file is part of the Heritrix web crawler (crawler.archive.org).
04: *
05: * Heritrix is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU Lesser Public License as published by
07: * the Free Software Foundation; either version 2.1 of the License, or
08: * any later version.
09: *
10: * Heritrix is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser Public License
16: * along with Heritrix; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * LongFPSetCache.java
20: * Created on Oct 21, 2003
21: *
22: * $Header$
23: */
24: package org.archive.util.fingerprint;
25:
26: /**
27: * Like a MemLongFPSet, but with fixed capacity and maximum size.
28: * When an add would expand past the maximum size, an old entry
29: * is deleted via a clock/counter algorithm.
30: *
31: * @author gojomo
32: *
33: */
34: public class LongFPSetCache extends MemLongFPSet {
35:
36: private static final long serialVersionUID = -5307436423975825566L;
37:
38: long sweepHand = 0;
39:
40: public LongFPSetCache() {
41: super ();
42: }
43:
44: public LongFPSetCache(int capacityPowerOfTwo, float loadFactor) {
45: super (capacityPowerOfTwo, loadFactor);
46: }
47:
48: protected void noteAccess(long index) {
49: if (slots[(int) index] < Byte.MAX_VALUE) {
50: slots[(int) index]++;
51: }
52: }
53:
54: protected void makeSpace() {
55: discard(1);
56: }
57:
58: private void discard(int i) {
59: int toDiscard = i;
60: while (toDiscard > 0) {
61: if (slots[(int) sweepHand] == 0) {
62: removeAt(sweepHand);
63: toDiscard--;
64: } else {
65: if (slots[(int) sweepHand] > 0) {
66: slots[(int) sweepHand]--;
67: }
68: }
69: sweepHand++;
70: if (sweepHand == slots.length) {
71: sweepHand = 0;
72: }
73: }
74: }
75: }
|