You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ho...@apache.org on 2007/08/24 09:28:36 UTC

svn commit: r569279 - in /lucene/solr/trunk: CHANGES.txt src/java/org/apache/solr/schema/UUIDField.java src/test/org/apache/solr/schema/UUIDFieldTest.java

Author: hossman
Date: Fri Aug 24 00:28:36 2007
New Revision: 569279

URL: http://svn.apache.org/viewvc?rev=569279&view=rev
Log:
SOLR-308: UUIDField class, can generate random UUIDs when special value of 'NEW' is used

Added:
    lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java   (with props)
    lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java   (with props)
Modified:
    lucene/solr/trunk/CHANGES.txt

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=569279&r1=569278&r2=569279&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Fri Aug 24 00:28:36 2007
@@ -122,6 +122,11 @@
     and a PHP response writer "php" that may be used by eval.
     (Nick Jenkin, Paul Borgermans, Pieter Berkel via yonik)
 
+23. SOLR-308: A new UUIDField class which accepts UUID string values, 
+    as well as the special value of "NEW" which triggers generation of 
+    a new random UUID.
+    (Thomas Peuss via hossman)
+
 Changes in runtime behavior
 
 Optimizations

Added: lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java?rev=569279&view=auto
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java (added)
+++ lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java Fri Aug 24 00:28:36 2007
@@ -0,0 +1,100 @@
+package org.apache.solr.schema;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.lucene.document.Fieldable;
+import org.apache.lucene.search.SortField;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.request.TextResponseWriter;
+import org.apache.solr.request.XMLWriter;
+
+/**
+ * This FieldType accepts UUID string values, as well as the special value 
+ * of "NEW" which triggers generation of a new random UUID.
+ *
+ * @see UUID#toString
+ * @see UUID#randomUUID
+ * @version $Id:$
+ */
+public class UUIDField extends FieldType {
+  private static final String NEW = "NEW";
+  private static final char DASH='-';
+
+  @Override
+  protected void init(IndexSchema schema, Map<String, String> args) {
+    super.init(schema, args);
+
+    // Tokenizing makes no sense
+    restrictProps(TOKENIZED);
+  }
+
+  @Override
+  public SortField getSortField(SchemaField field, boolean reverse) {
+    return getStringSort(field, reverse);
+  }
+
+  @Override
+  public void write(XMLWriter xmlWriter, String name, Fieldable f)
+      throws IOException {
+    xmlWriter.writeStr(name, f.stringValue());
+  }
+
+  @Override
+  public void write(TextResponseWriter writer, String name, Fieldable f)
+      throws IOException {
+    writer.writeStr(name, f.stringValue(), false);
+  }
+
+  /**
+   * Generates a UUID if val is either null, empty or "NEW".
+   * 
+   * Otherwise it behaves much like a StrField but checks that the value given
+   * is indeed a valid UUID.
+   * 
+   * @param val The value of the field
+   * @see org.apache.solr.schema.FieldType#toInternal(java.lang.String)
+   */
+  @Override
+  public String toInternal(String val) {
+    if (val == null || 0==val.length() || NEW.equals(val)) {
+      return UUID.randomUUID().toString().toLowerCase();
+    } else {
+      // we do some basic validation if 'val' looks like an UUID
+      if (val.length() != 36 || val.charAt(8) != DASH || val.charAt(13) != DASH
+          || val.charAt(18) != DASH || val.charAt(23) != DASH) {
+        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
+            "Invalid UUID String: '" + val + "'");
+      }
+
+      return val.toLowerCase();
+    }
+  }
+
+  public String toInternal(UUID uuid) {
+    return uuid.toString().toLowerCase();
+  }
+
+  @Override
+  public UUID toObject(Fieldable f) {
+    return UUID.fromString(f.stringValue());
+  }
+}

Propchange: lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/src/java/org/apache/solr/schema/UUIDField.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java?rev=569279&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java Fri Aug 24 00:28:36 2007
@@ -0,0 +1,54 @@
+package org.apache.solr.schema;
+
+import java.util.UUID;
+
+import junit.framework.TestCase;
+
+import org.apache.solr.common.SolrException;
+
+public class UUIDFieldTest extends TestCase {
+  public void testToInternal() {
+    boolean ok = false;
+    UUIDField uuidfield = new UUIDField();
+
+    try {
+      uuidfield.toInternal((String) null);
+      ok = true;
+    } catch (SolrException se) {
+      ok = false;
+    }
+    assertTrue("ID generation from null failed", ok);
+
+    try {
+      uuidfield.toInternal("");
+      ok = true;
+    } catch (SolrException se) {
+      ok = false;
+    }
+    assertTrue("ID generation from empty string failed", ok);
+
+    try {
+      uuidfield.toInternal("NEW");
+      ok = true;
+    } catch (SolrException se) {
+      ok = false;
+    }
+    assertTrue("ID generation from 'NEW' failed", ok);
+
+    try {
+      uuidfield.toInternal("d574fb6a-5f79-4974-b01a-fcd598a19ef5");
+      ok = true;
+    } catch (SolrException se) {
+      ok = false;
+    }
+    assertTrue("ID generation from UUID failed", ok);
+
+    try {
+      uuidfield.toInternal("This is a test");
+      ok = false;
+    } catch (SolrException se) {
+      ok = true;
+    }
+    assertTrue("Bad UUID check failed", ok);
+  }
+}

Propchange: lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/src/test/org/apache/solr/schema/UUIDFieldTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL