Logging a lack of uniqueKey field in schema.xml

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Logging a lack of uniqueKey field in schema.xml

Darren Vengroff-2
I just ran across a situation where, in building my schema.xml file, I
forgot to specify the <field /> for the field that I was using as my
<uniqueKey />.  It was totally my error, but having no idea what the error
was, I had to spend some time debugging.  Once I realized what was going on,
I made a little change to IndexSchema.java to ensure that something appears
in the log in this situation.  The previous behavior was to simply throw a
RuntimeException, which, in Tomcat at least, did not result in anything easy
to spot in any of the logs I checked.  The patch for this is below if you
want to apply it.  I know it would have saved me some time today.

Note also that the code that looks for a <defaultSearchField /> already
assumed that getIndexedField(String) could return a null, although that code
path was unreachable given the throw that this patch removed.

It makes me think that having an XML Schema for schema.xml (there's a
palindrome for you) would be a nice idea to.

Thanks,

-D


Index: C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
===================================================================
--- C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
(revision 412846)
+++ C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
(working copy)
@@ -178,14 +178,7 @@
   }
 
   private SchemaField getIndexedField(String fname) {
-    SchemaField f = getFields().get(fname);
-    if (f==null) {
-      throw new RuntimeException("unknown field '" + fname + "'");
-    }
-    if (!f.indexed()) {
-      throw new RuntimeException("'"+fname+"' is not an indexed field:" +
f);
-    }
-    return f;
+    return getFields().get(fname);
   }
 
 
@@ -369,10 +362,15 @@
     if (node==null) {
       log.warning("no uniqueKey specified in schema.");
     } else {
-      uniqueKeyField=getIndexedField(node.getNodeValue().trim());
-      uniqueKeyFieldName=uniqueKeyField.getName();
-      uniqueKeyFieldType=uniqueKeyField.getType();
-      log.info("unique key field: "+uniqueKeyFieldName);
+      String fieldName = node.getNodeValue().trim();
+      uniqueKeyField = getIndexedField(fieldName);
+      if(uniqueKeyField == null) {
+        log.warning("Unique key field '" + fieldName + "' undefined.");
+      } else {
+        uniqueKeyFieldName=uniqueKeyField.getName();
+        uniqueKeyFieldType=uniqueKeyField.getType();
+        log.info("unique key field: "+uniqueKeyFieldName);
+      }
     }
 
     /////////////// parse out copyField commands ///////////////


Reply | Threaded
Open this post in threaded view
|

Re: Logging a lack of uniqueKey field in schema.xml

Yonik Seeley
On 6/8/06, Darren Vengroff <[hidden email]> wrote:
> I just ran across a situation where, in building my schema.xml file, I
> forgot to specify the <field /> for the field that I was using as my
> <uniqueKey />.  It was totally my error, but having no idea what the error
> was, I had to spend some time debugging.  Once I realized what was going on,
> I made a little change to IndexSchema.java to ensure that something appears
> in the log in this situation.  The previous behavior was to simply throw a
> RuntimeException, which, in Tomcat at least, did not result in anything easy
> to spot in any of the logs I checked.

Hmmm, I just tried removing my "id" field in Tomcat, and I did get an
exception chain ending with
Caused by: java.lang.RuntimeException: unknown field 'id'

If the schema defines a uniqueKeyField, and that field doesn't exist,
shouldn't we throw an exception rather than just logging it?  It seems
like a rather fatal error.

> Note also that the code that looks for a <defaultSearchField /> already
> assumed that getIndexedField(String) could return a null, although that code
> path was unreachable given the throw that this patch removed.

Must have been a late night :-)  I assume I meant
      defaultSearchFieldName = defName!=null ? getIndexedField(defName) : null;
rather than
      defaultSearchFieldName = getIndexedField(defName)!=null ? defName : null;


-Yonik
Reply | Threaded
Open this post in threaded view
|

RE: Logging a lack of uniqueKey field in schema.xml

Darren Vengroff-2
>Must have been a late night :-)  

Must have been too late a night for me last night.  I'm now seeing the
exceptions.  Ignore that patch.

-D