|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.tools.command; |
|
21 |
| |
|
22 |
| import org.apache.xindice.client.xmldb.services.CollectionManager; |
|
23 |
| import org.apache.xindice.tools.XMLTools; |
|
24 |
| import org.apache.xindice.xml.TextWriter; |
|
25 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
|
26 |
| import org.apache.xindice.xml.dom.DOMParser; |
|
27 |
| import org.apache.xindice.util.XindiceException; |
|
28 |
| |
|
29 |
| import org.w3c.dom.Document; |
|
30 |
| import org.w3c.dom.Element; |
|
31 |
| import org.xmldb.api.DatabaseManager; |
|
32 |
| import org.xmldb.api.base.Collection; |
|
33 |
| |
|
34 |
| import java.io.File; |
|
35 |
| import java.io.InputStream; |
|
36 |
| import java.io.FileInputStream; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public class AddIndexer extends Command { |
|
45 |
| |
|
46 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
|
47 |
| |
|
48 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
|
49 |
0
| System.out.println("ERROR : Collection and switch required");
|
|
50 |
0
| return false;
|
|
51 |
| } |
|
52 |
| |
|
53 |
0
| if ("".equals(table.getString(XMLTools.FILE_PATH)) &&
|
|
54 |
| (table.getString(XMLTools.NAME_OF) == null || table.getString(XMLTools.PATTERN) == null)) { |
|
55 |
0
| System.out.println("ERROR : Name and Pattern required or File path required");
|
|
56 |
0
| return false;
|
|
57 |
| } |
|
58 |
| |
|
59 |
0
| Document config;
|
|
60 |
0
| if (!"".equals(table.getString(XMLTools.FILE_PATH))) {
|
|
61 |
| |
|
62 |
0
| config = readConfig(table);
|
|
63 |
| } else { |
|
64 |
| |
|
65 |
0
| config = buildConfig(table);
|
|
66 |
| } |
|
67 |
| |
|
68 |
0
| Collection col = null;
|
|
69 |
0
| try {
|
|
70 |
| |
|
71 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
|
72 |
| table.getBoolean(XMLTools.LOCAL)); |
|
73 |
| |
|
74 |
0
| col = DatabaseManager.getCollection(colstring);
|
|
75 |
0
| if (col == null) {
|
|
76 |
0
| System.out.println("ERROR : Collection not found!");
|
|
77 |
0
| return false;
|
|
78 |
| } |
|
79 |
| |
|
80 |
| |
|
81 |
0
| CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);
|
|
82 |
| |
|
83 |
| |
|
84 |
0
| colman.createIndexer(config);
|
|
85 |
| |
|
86 |
0
| System.out.println("CREATED : " + config.getDocumentElement().getAttributeNode("name").getNodeValue());
|
|
87 |
| } finally { |
|
88 |
0
| if (col != null) {
|
|
89 |
0
| col.close();
|
|
90 |
| } |
|
91 |
| } |
|
92 |
| |
|
93 |
0
| return true;
|
|
94 |
| } |
|
95 |
| |
|
96 |
0
| private Document readConfig(XMLTools.Config table) throws Exception {
|
|
97 |
0
| InputStream fis = null;
|
|
98 |
0
| try {
|
|
99 |
0
| File file = new File(table.getString(XMLTools.FILE_PATH));
|
|
100 |
0
| fis = new FileInputStream(file);
|
|
101 |
| |
|
102 |
0
| return DOMParser.toDocument(fis);
|
|
103 |
| } catch (XindiceException e) { |
|
104 |
0
| throw new Exception("Indexer configuration could not be parsed", e);
|
|
105 |
| } finally { |
|
106 |
0
| if (fis != null) {
|
|
107 |
0
| fis.close();
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| } |
|
111 |
| |
|
112 |
0
| private Document buildConfig(XMLTools.Config table) throws Exception {
|
|
113 |
0
| Document config = new DocumentImpl();
|
|
114 |
| |
|
115 |
| |
|
116 |
0
| Element idxEle = config.createElement("index");
|
|
117 |
0
| idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
|
|
118 |
0
| idxEle.setAttribute("name", table.getString(XMLTools.NAME_OF));
|
|
119 |
| |
|
120 |
| |
|
121 |
0
| String type = table.getString(XMLTools.TYPE);
|
|
122 |
0
| if (type != null) {
|
|
123 |
0
| if (type.equalsIgnoreCase("name")) {
|
|
124 |
0
| idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
|
|
125 |
0
| } else if (type.equalsIgnoreCase("text")) {
|
|
126 |
0
| idxEle.setAttribute("class", XINDICE_TEXT_INDEXER);
|
|
127 |
| } else { |
|
128 |
0
| idxEle.setAttribute("type", type);
|
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
0
| if (idxEle.getAttribute("class").equals(XINDICE_TEXT_INDEXER)) {
|
|
134 |
0
| addPatterns(config, idxEle, table.getString(XMLTools.PATTERN));
|
|
135 |
| } else { |
|
136 |
0
| idxEle.setAttribute("pattern", table.getString(XMLTools.PATTERN));
|
|
137 |
| |
|
138 |
0
| if (table.getString(XMLTools.PAGE_SIZE) != null) {
|
|
139 |
0
| idxEle.setAttribute(XMLTools.PAGE_SIZE, table.getString(XMLTools.PAGE_SIZE));
|
|
140 |
| } |
|
141 |
0
| if (table.getString(XMLTools.MAX_KEY_SIZE) != null) {
|
|
142 |
0
| idxEle.setAttribute(XMLTools.MAX_KEY_SIZE, table.getString(XMLTools.MAX_KEY_SIZE));
|
|
143 |
| } |
|
144 |
0
| if (table.getString(XMLTools.PAGE_COUNT) != null) {
|
|
145 |
0
| idxEle.setAttribute(XMLTools.PAGE_COUNT, table.getString(XMLTools.PAGE_COUNT));
|
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
0
| config.appendChild(idxEle);
|
|
151 |
| |
|
152 |
| |
|
153 |
0
| if (table.getBoolean(XMLTools.VERBOSE)) {
|
|
154 |
0
| String indexstr = TextWriter.toString(config);
|
|
155 |
0
| System.out.println("Index node element = ");
|
|
156 |
0
| System.out.println("\t" + indexstr + "\n");
|
|
157 |
| } |
|
158 |
| |
|
159 |
0
| return config;
|
|
160 |
| } |
|
161 |
| |
|
162 |
0
| private void addPatterns(Document doc, Element idxEle, String conf) {
|
|
163 |
0
| String[] patterns = conf.split(";");
|
|
164 |
| |
|
165 |
0
| for (int i = 0; i < patterns.length; i++) {
|
|
166 |
0
| String[] st = patterns[i].split("=");
|
|
167 |
0
| if (st.length != 2) {
|
|
168 |
0
| System.out.println("ERROR : mismatched patterns and aliases in " + conf);
|
|
169 |
| } |
|
170 |
| |
|
171 |
0
| Element pattern = doc.createElement("pattern");
|
|
172 |
0
| pattern.setAttribute("pattern", st[0]);
|
|
173 |
0
| pattern.setAttribute("alias", st[1]);
|
|
174 |
| |
|
175 |
0
| idxEle.appendChild(pattern);
|
|
176 |
| } |
|
177 |
| } |
|
178 |
| |
|
179 |
0
| public void usage() {
|
|
180 |
0
| System.out.println("Format: xindice ai -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
|
181 |
0
| System.out.println();
|
|
182 |
0
| System.out.println("Creates an index for a collection based on given pattern");
|
|
183 |
0
| System.out.println();
|
|
184 |
0
| System.out.println("Command-specific switches:");
|
|
185 |
0
| System.out.println(" -n|--nameOf <name>");
|
|
186 |
0
| System.out.println(" Name for the indexer to be created, must be present");
|
|
187 |
0
| System.out.println(" unless indexer configuration is specified");
|
|
188 |
0
| System.out.println(" -p|--pattern <pattern>");
|
|
189 |
0
| System.out.println(" Index pattern, must be present unless indexer configuration");
|
|
190 |
0
| System.out.println(" is specified. It is either single pattern for NameIndexer");
|
|
191 |
0
| System.out.println(" and ValueIndexer, or semicolon delimited list of patterns for");
|
|
192 |
0
| System.out.println(" LuceneIndexer in the form pattern=alias");
|
|
193 |
0
| System.out.println(" -f|--filepath <file>");
|
|
194 |
0
| System.out.println(" Name of the file that holds indexer configuration. If");
|
|
195 |
0
| System.out.println(" specified, the rest of command-specific parameters is");
|
|
196 |
0
| System.out.println(" ignored");
|
|
197 |
0
| System.out.println(" -t|--type <type>");
|
|
198 |
0
| System.out.println(" Specify the data type in collection index. Type can be");
|
|
199 |
0
| System.out.println(" one of the following:");
|
|
200 |
0
| System.out.println(" string (ValueIndexer)");
|
|
201 |
0
| System.out.println(" trimmed (ValueIndexer)");
|
|
202 |
0
| System.out.println(" short (ValueIndexer)");
|
|
203 |
0
| System.out.println(" int (ValueIndexer)");
|
|
204 |
0
| System.out.println(" long (ValueIndexer)");
|
|
205 |
0
| System.out.println(" float (ValueIndexer)");
|
|
206 |
0
| System.out.println(" double (ValueIndexer)");
|
|
207 |
0
| System.out.println(" byte (ValueIndexer)");
|
|
208 |
0
| System.out.println(" char (ValueIndexer)");
|
|
209 |
0
| System.out.println(" boolean (ValueIndexer)");
|
|
210 |
0
| System.out.println(" name (NameIndexer)");
|
|
211 |
0
| System.out.println(" text (LuceneIndexer)");
|
|
212 |
0
| System.out.println(" (default: string/trimmed)");
|
|
213 |
0
| System.out.println(" --pagesize <number>");
|
|
214 |
0
| System.out.println(" Page size for file pages (default: 4096). Does not apply to");
|
|
215 |
0
| System.out.println(" LuceneIndexer");
|
|
216 |
0
| System.out.println(" --maxkeysize <number>");
|
|
217 |
0
| System.out.println(" The maximum size for file keys (default: 0=none). Does not");
|
|
218 |
0
| System.out.println(" apply to LuceneIndexer");
|
|
219 |
0
| System.out.println(" --pagecount <number>");
|
|
220 |
0
| System.out.println(" Number of pages in the primary storage (default: 1024). Does");
|
|
221 |
0
| System.out.println(" not apply to LuceneIndexer");
|
|
222 |
0
| System.out.println();
|
|
223 |
| } |
|
224 |
| } |