1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
3 import org.apache.lucene.facet.taxonomy.CategoryPath;
4 import org.apache.lucene.facet.taxonomy.writercache.TaxonomyWriterCache;
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
29 * @lucene.experimental
31 public class Cl2oTaxonomyWriterCache implements TaxonomyWriterCache {
33 private CompactLabelToOrdinal cache;
35 public Cl2oTaxonomyWriterCache(int initialCapcity, float loadFactor, int numHashArrays) {
36 this.cache = new CompactLabelToOrdinal(initialCapcity, loadFactor, numHashArrays);
43 public boolean hasRoom(int n) {
44 // This cache is unlimited, so we always have room for remembering more:
48 public int get(CategoryPath categoryPath) {
49 return cache.getOrdinal(categoryPath);
52 public int get(CategoryPath categoryPath, int length) {
53 if (length<0 || length>categoryPath.length()) {
54 length = categoryPath.length();
56 return cache.getOrdinal(categoryPath, length);
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
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
74 * Returns the number of bytes in memory used by this object.
75 * @return Number of bytes in memory used by this object.
77 public int getMemoryUsage() {
78 int memoryUsage = (this.cache == null) ? 0 : this.cache.getMemoryUsage();