1 package org.apache.lucene.benchmark.byTask.feeds;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.Random;
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;
28 * Simple implementation of a random facet source
30 * Supports the following parameters:
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.
39 public class RandomFacetSource extends FacetSource {
43 private int maxDocFacets = 10;
44 private int maxFacetDepth = 3;
45 private int maxValue = maxDocFacets * maxFacetDepth;
48 public CategoryContainer getNextFacets(CategoryContainer facets) throws NoMoreDataException, IOException {
50 facets = new CategoryContainer();
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)));
62 facets.addCategory(cp);
63 addBytes(cp.toString().length()); // very rough approximation
69 public void close() throws IOException {
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;