pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / RandomFacetSource.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Random;
22
23 import org.apache.lucene.benchmark.byTask.utils.Config;
24 import org.apache.lucene.facet.index.CategoryContainer;
25 import org.apache.lucene.facet.taxonomy.CategoryPath;
26
27 /**
28  * Simple implementation of a random facet source
29  * <p>
30  * Supports the following parameters:
31  * <ul>
32  * <li><b>rand.seed</b> - defines the seed to initialize Random with (default: <b>13</b>).
33  * <li><b>max.doc.facets</b> - maximal #facets per doc (default: <b>10</b>).
34  *    Actual number of facets in a certain doc would be anything between 1 and that number.
35  * <li><b>max.facet.depth</b> - maximal #components in a facet (default: <b>3</b>).
36  *    Actual number of components in a certain facet would be anything between 1 and that number.
37  * </ul>
38  */
39 public class RandomFacetSource extends FacetSource {
40
41   Random random;
42   
43   private int maxDocFacets = 10;
44   private int maxFacetDepth = 3;
45   private int maxValue = maxDocFacets * maxFacetDepth;
46   
47   @Override
48   public CategoryContainer getNextFacets(CategoryContainer facets) throws NoMoreDataException, IOException {
49     if (facets == null) {
50       facets = new CategoryContainer();
51     } else {
52       facets.clear();
53     }
54     int numFacets = 1 + random.nextInt(maxDocFacets-1); // at least one facet to each doc
55     for (int i=0; i<numFacets; i++) {
56       CategoryPath cp = new CategoryPath();
57       int depth = 1 + random.nextInt(maxFacetDepth-1); // depth 0 is not useful
58       for (int k=0; k<depth; k++) {
59         cp.add(Integer.toString(random.nextInt(maxValue)));
60         addItem();
61       }
62       facets.addCategory(cp);
63       addBytes(cp.toString().length()); // very rough approximation
64     }
65     return facets;
66   }
67
68   @Override
69   public void close() throws IOException {
70     // nothing to do here
71   }
72
73   @Override
74   public void setConfig(Config config) {
75     super.setConfig(config);
76     random = new Random(config.get("rand.seed", 13));
77     maxDocFacets = config.get("max.doc.facets", 200);
78     maxFacetDepth = config.get("max.facet.depth", 10);
79     maxValue = maxDocFacets * maxFacetDepth;
80   }
81 }