add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / taxonomy / writercache / cl2o / Cl2oTaxonomyWriterCache.java
1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
2
3 import org.apache.lucene.facet.taxonomy.CategoryPath;
4 import org.apache.lucene.facet.taxonomy.writercache.TaxonomyWriterCache;
5
6 /**
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements.  See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License.  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 /**
24  * {@link TaxonomyWriterCache} using {@link CompactLabelToOrdinal}. Although
25  * called cache, it maintains in memory all the mappings from category to
26  * ordinal, relying on that {@link CompactLabelToOrdinal} is an efficient
27  * mapping for this purpose.
28  * 
29  * @lucene.experimental
30  */
31 public class Cl2oTaxonomyWriterCache implements TaxonomyWriterCache {  
32
33   private CompactLabelToOrdinal cache;
34
35   public Cl2oTaxonomyWriterCache(int initialCapcity, float loadFactor, int numHashArrays) {
36     this.cache = new CompactLabelToOrdinal(initialCapcity, loadFactor, numHashArrays);
37   }
38
39   public void close() {
40     cache=null;
41   }
42
43   public boolean hasRoom(int n) {
44     // This cache is unlimited, so we always have room for remembering more:
45     return true;
46   }
47
48   public int get(CategoryPath categoryPath) {
49     return cache.getOrdinal(categoryPath);
50   }
51
52   public int get(CategoryPath categoryPath, int length) {
53     if (length<0 || length>categoryPath.length()) {
54       length = categoryPath.length();
55     }
56     return cache.getOrdinal(categoryPath, length);
57   }
58
59   public boolean put(CategoryPath categoryPath, int ordinal) {
60     cache.addLabel(categoryPath, ordinal);
61     // Tell the caller we didn't clear part of the cache, so it doesn't
62     // have to flush its on-disk index now
63     return false;
64   }
65
66   public boolean put(CategoryPath categoryPath, int prefixLen, int ordinal) {
67     cache.addLabel(categoryPath, prefixLen, ordinal);
68     // Tell the caller we didn't clear part of the cache, so it doesn't
69     // have to flush its on-disk index now
70     return false;
71   }
72
73   /**
74    * Returns the number of bytes in memory used by this object.
75    * @return Number of bytes in memory used by this object.
76    */
77   public int getMemoryUsage() {
78     int memoryUsage = (this.cache == null) ? 0 : this.cache.getMemoryUsage();
79     return memoryUsage;
80   }
81
82 }