pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / SortableSingleDocSource.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.util.Properties;
21 import java.util.Random;
22
23 import org.apache.lucene.benchmark.byTask.utils.Config;
24
25 /**
26  * Adds fields appropriate for sorting: country, random_string and sort_field
27  * (int). Supports the following parameters:
28  * <ul>
29  * <li><b>sort.rng</b> - defines the range for sort-by-int field (default
30  * <b>20000</b>).
31  * <li><b>rand.seed</b> - defines the seed to initialize Random with (default
32  * <b>13</b>).
33  * </ul>
34  */
35 public class SortableSingleDocSource extends SingleDocSource {
36   
37   private static String[] COUNTRIES = new String[] {
38     "European Union", "United States", "Japan", "Germany", "China (PRC)", 
39     "United Kingdom", "France", "Italy", "Spain", "Canada", "Brazil", "Russia",
40     "India", "South Korea", "Australia", "Mexico", "Netherlands", "Turkey", 
41     "Sweden", "Belgium", "Indonesia", "Switzerland", "Poland", "Norway", 
42     "Republic of China", "Saudi Arabia", "Austria", "Greece", "Denmark", "Iran", 
43     "South Africa", "Argentina", "Ireland", "Thailand", "Finland", "Venezuela", 
44     "Portugal", "Hong Kong", "United Arab Emirates", "Malaysia", 
45     "Czech Republic", "Colombia", "Nigeria", "Romania", "Chile", "Israel", 
46     "Singapore", "Philippines", "Pakistan", "Ukraine", "Hungary", "Algeria", 
47     "New Zealand", "Egypt", "Kuwait", "Peru", "Kazakhstan", "Slovakia", 
48     "Morocco", "Bangladesh", "Vietnam", "Qatar", "Angola", "Libya", "Iraq", 
49     "Croatia", "Luxembourg", "Sudan", "Slovenia", "Cuba", "Belarus", "Ecuador", 
50     "Serbia", "Oman", "Bulgaria", "Lithuania", "Syria", "Dominican Republic", 
51     "Tunisia", "Guatemala", "Azerbaijan", "Sri Lanka", "Kenya", "Latvia", 
52     "Turkmenistan", "Costa Rica", "Lebanon", "Uruguay", "Uzbekistan", "Yemen", 
53     "Cyprus", "Estonia", "Trinidad and Tobago", "Cameroon", "El Salvador", 
54     "Iceland", "Panama", "Bahrain", "Ivory Coast", "Ethiopia", "Tanzania", 
55     "Jordan", "Ghana", "Bosnia and Herzegovina", "Macau", "Burma", "Bolivia", 
56     "Brunei", "Botswana", "Honduras", "Gabon", "Uganda", "Jamaica", "Zambia", 
57     "Senegal", "Paraguay", "Albania", "Equatorial Guinea", "Georgia", 
58     "Democratic Republic of the Congo", "Nepal", "Afghanistan", "Cambodia", 
59     "Armenia", "Republic of the Congo", "Mozambique", "Republic of Macedonia", 
60     "Malta", "Namibia", "Madagascar", "Chad", "Burkina Faso", "Mauritius", 
61     "Mali", "The Bahamas", "Papua New Guinea", "Nicaragua", "Haiti", "Benin", 
62     "alestinian flag West Bank and Gaza", "Jersey", "Fiji", "Guinea", "Moldova", 
63     "Niger", "Laos", "Mongolia", "French Polynesia", "Kyrgyzstan", "Barbados", 
64     "Tajikistan", "Malawi", "Liechtenstein", "New Caledonia", "Kosovo", 
65     "Rwanda", "Montenegro", "Swaziland", "Guam", "Mauritania", "Guernsey", 
66     "Isle of Man", "Togo", "Somalia", "Suriname", "Aruba", "North Korea", 
67     "Zimbabwe", "Central African Republic", "Faroe Islands", "Greenland", 
68     "Sierra Leone", "Lesotho", "Cape Verde", "Eritrea", "Bhutan", "Belize", 
69     "Antigua and Barbuda", "Gibraltar", "Maldives", "San Marino", "Guyana", 
70     "Burundi", "Saint Lucia", "Djibouti", "British Virgin Islands", "Liberia", 
71     "Seychelles", "The Gambia", "Northern Mariana Islands", "Grenada", 
72     "Saint Vincent and the Grenadines", "Saint Kitts and Nevis", "East Timor", 
73     "Vanuatu", "Comoros", "Samoa", "Solomon Islands", "Guinea-Bissau", 
74     "American Samoa", "Dominica", "Micronesia", "Tonga", "Cook Islands", 
75     "Palau", "Marshall Islands", "S�o Tom� and Pr�ncipe", "Anguilla", 
76     "Kiribati", "Tuvalu", "Niue" };
77
78   private int sortRange;
79   private Random r;
80
81   @Override
82   public DocData getNextDocData(DocData docData) throws NoMoreDataException {
83     docData = super.getNextDocData(docData);
84     Properties props = new Properties();
85
86     // random int
87     props.put("sort_field", Integer.toString(r.nextInt(sortRange)));
88
89     // random string
90     int len = nextInt(2, 20);
91     char[] buffer = new char[len];
92     for (int i = 0; i < len; i++) {
93       buffer[i] = (char) r.nextInt(0x80); 
94     }
95     props.put("random_string", new String(buffer));
96
97     // random country
98     props.put("country", COUNTRIES[r.nextInt(COUNTRIES.length)]);
99     docData.setProps(props);
100     return docData;
101   }
102
103   private int nextInt(int start, int end) {
104     return start + r.nextInt(end - start);
105   }
106
107   @Override
108   public void setConfig(Config config) {
109     super.setConfig(config);
110     sortRange = config.get("sort.rng", 20000);
111     r = new Random(config.get("rand.seed", 13));
112   }
113   
114 }