1
package org.apache.solr.schema;
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.Locale;
23
import java.util.UUID;
25
import org.apache.lucene.document.Fieldable;
26
import org.apache.lucene.search.SortField;
27
import org.apache.solr.common.SolrException;
28
import org.apache.solr.response.TextResponseWriter;
29
import org.apache.solr.response.XMLWriter;
32
* This FieldType accepts UUID string values, as well as the special value
33
* of "NEW" which triggers generation of a new random UUID.
36
* @see UUID#randomUUID
37
* @version $Id: UUIDField.java 945270 2010-05-17 17:45:18Z rmuir $
39
public class UUIDField extends FieldType {
40
private static final String NEW = "NEW";
41
private static final char DASH='-';
44
protected void init(IndexSchema schema, Map<String, String> args) {
45
super.init(schema, args);
47
// Tokenizing makes no sense
48
restrictProps(TOKENIZED);
52
public SortField getSortField(SchemaField field, boolean reverse) {
53
return getStringSort(field, reverse);
57
public void write(XMLWriter xmlWriter, String name, Fieldable f)
59
xmlWriter.writeStr(name, f.stringValue());
63
public void write(TextResponseWriter writer, String name, Fieldable f)
65
writer.writeStr(name, f.stringValue(), false);
69
* Generates a UUID if val is either null, empty or "NEW".
71
* Otherwise it behaves much like a StrField but checks that the value given
72
* is indeed a valid UUID.
74
* @param val The value of the field
75
* @see org.apache.solr.schema.FieldType#toInternal(java.lang.String)
78
public String toInternal(String val) {
79
if (val == null || 0==val.length() || NEW.equals(val)) {
80
return UUID.randomUUID().toString().toLowerCase(Locale.ENGLISH);
82
// we do some basic validation if 'val' looks like an UUID
83
if (val.length() != 36 || val.charAt(8) != DASH || val.charAt(13) != DASH
84
|| val.charAt(18) != DASH || val.charAt(23) != DASH) {
85
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
86
"Invalid UUID String: '" + val + "'");
89
return val.toLowerCase(Locale.ENGLISH);
93
public String toInternal(UUID uuid) {
94
return uuid.toString().toLowerCase(Locale.ENGLISH);
98
public UUID toObject(Fieldable f) {
99
return UUID.fromString(f.stringValue());