1 package org.apache.lucene.facet.taxonomy;
3 import java.io.Closeable;
4 import java.io.IOException;
6 import org.apache.lucene.util.TwoPhaseCommit;
9 * Licensed to the Apache Software Foundation (ASF) under one or more
10 * contributor license agreements. See the NOTICE file distributed with
11 * this work for additional information regarding copyright ownership.
12 * The ASF licenses this file to You under the Apache License, Version 2.0
13 * (the "License"); you may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
26 * TaxonomyWriter is the interface which the faceted-search library uses
27 * to dynamically build the taxonomy at indexing time.
29 * Notes about concurrent access to the taxonomy:
31 * An implementation must allow multiple readers and a single writer to be
32 * active concurrently. Readers follow so-called "point in time" semantics,
33 * i.e., a reader object will only see taxonomy entries which were available
34 * at the time it was created. What the writer writes is only available to
35 * (new) readers after the writer's commit() is called.
37 * Faceted search keeps two indices - namely Lucene's main index, and this
38 * taxonomy index. When one or more readers are active concurrently with the
39 * writer, care must be taken to avoid an inconsistency between the state of
40 * these two indices: When writing to the indices, the taxonomy must always
41 * be committed to disk *before* the main index, because the main index
42 * refers to categories listed in the taxonomy.
43 * Such control can best be achieved by turning off the main index's
44 * "autocommit" feature, and explicitly calling commit() for both indices
45 * (first for the taxonomy, then for the main index).
46 * In old versions of Lucene (2.2 or earlier), when autocommit could not be
47 * turned off, a more complicated solution needs to be used. E.g., use
48 * some sort of (possibly inter-process) locking to ensure that a reader
49 * is being opened only right after both indices have been flushed (and
50 * before anything else is written to them).
52 * @lucene.experimental
54 public interface TaxonomyWriter extends Closeable, TwoPhaseCommit {
57 * addCategory() adds a category with a given path name to the taxonomy,
58 * and returns its ordinal. If the category was already present in
59 * the taxonomy, its existing ordinal is returned.
61 * Before adding a category, addCategory() makes sure that all its
62 * ancestor categories exist in the taxonomy as well. As result, the
63 * ordinal of a category is guaranteed to be smaller then the ordinal of
64 * any of its descendants.
66 public int addCategory(CategoryPath categoryPath) throws IOException;
69 * getParent() returns the ordinal of the parent category of the category
70 * with the given ordinal.
72 * When a category is specified as a path name, finding the path of its
73 * parent is as trivial as dropping the last component of the path.
74 * getParent() is functionally equivalent to calling getPath() on the
75 * given ordinal, dropping the last component of the path, and then calling
76 * getOrdinal() to get an ordinal back.
78 * If the given ordinal is the ROOT_ORDINAL, an INVALID_ORDINAL is returned.
79 * If the given ordinal is a top-level category, the ROOT_ORDINAL is returned.
80 * If an invalid ordinal is given (negative or beyond the last available
81 * ordinal), an ArrayIndexOutOfBoundsException is thrown. However, it is
82 * expected that getParent will only be called for ordinals which are
83 * already known to be in the taxonomy.
84 * TODO (Facet): instead of a getParent(ordinal) method, consider having a
86 * getCategory(categorypath, prefixlen) which is similar to addCategory
87 * except it doesn't add new categories; This method can be used to get
88 * the ordinals of all prefixes of the given category, and it can use
89 * exactly the same code and cache used by addCategory() so it means less code.
91 public int getParent(int ordinal) throws IOException;
94 * getSize() returns the number of categories in the taxonomy.
96 * Because categories are numbered consecutively starting with 0, it
97 * means the taxonomy contains ordinals 0 through getSize()-1.
99 * Note that the number returned by getSize() is often slightly higher
100 * than the number of categories inserted into the taxonomy; This is
101 * because when a category is added to the taxonomy, its ancestors
102 * are also added automatically (including the root, which always get
105 public int getSize();