Author: rooneg
Date: Sun Mar  6 06:50:30 2005
New Revision: 156316

URL: http://svn.apache.org/viewcvs?view=rev&rev=156316
Log:
Add an initial cut at a query parser.

Right now all it does is split a string into terms and combine them into
a boolean query using the default occur the parser was created with.  This
will clearly need to have a lot more work done on it, but at least it's
something.

* include/lcn_query_parser.h: new, prototypes for query parser code.

* src/query_parser/parser.c: impl of query parser.

* test/query_parser/parser_test.c: new, tests the query parser.

* test/abts_tests.h
  (alltests): add test_query_parser.

* test/lcn_tests.h
  (test_query_parser): new prototype.

* Makefile.am: add new files to build.

Added:
    incubator/lucene4c/trunk/include/lcn_query_parser.h
    incubator/lucene4c/trunk/src/query_parser/   (with props)
    incubator/lucene4c/trunk/src/query_parser/parser.c
    incubator/lucene4c/trunk/test/query_parser/   (with props)
    incubator/lucene4c/trunk/test/query_parser/parser_test.c
Modified:
    incubator/lucene4c/trunk/Makefile.am
    incubator/lucene4c/trunk/test/abts_tests.h
    incubator/lucene4c/trunk/test/lcn_tests.h

Modified: incubator/lucene4c/trunk/Makefile.am
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/Makefile.am?view=diff&r1=156315&r2=156316
==============================================================================
--- incubator/lucene4c/trunk/Makefile.am (original)
+++ incubator/lucene4c/trunk/Makefile.am Sun Mar  6 06:50:30 2005
@@ -22,6 +22,7 @@
                         src/store/directory.c \
                         src/search/query.c \
                         src/search/scorer.c \
+                        src/query_parser/parser.c \
                         src/index/index.c \
                         src/index/segments.c \
                         src/index/segment.c \
@@ -48,6 +49,7 @@
                      test/index/frequencies_test.c \
                      test/store/istream_test.c \
                      test/search/scorer_test.c \
+                     test/query_parser/parser_test.c \
                      test/store/directory_test.c
 
 src_cmdline_lcn_SOURCES = src/cmdline/main.c

Added: incubator/lucene4c/trunk/include/lcn_query_parser.h
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/include/lcn_query_parser.h?view=auto&rev=156316
==============================================================================
--- incubator/lucene4c/trunk/include/lcn_query_parser.h (added)
+++ incubator/lucene4c/trunk/include/lcn_query_parser.h Sun Mar  6 06:50:30 2005
@@ -0,0 +1,54 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * @file lcn_query_parser.h
+ * @brief Routines for creating queries from a user provided string
+ */
+
+#ifndef _LCN_QUERY_PARSER_H
+#define _LCN_QUERY_PARSER_H
+
+#include "lcn_types.h"
+#include "lcn_query.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/** A query parser. */
+typedef struct lcn_query_parser_t lcn_query_parser_t;
+
+/** Create a query parser @a parser, which uses @a default_field and
+ * @a default_occur and is allocated from @a pool. */
+lcn_error_t *
+lcn_query_parser_create (lcn_query_parser_t **parser,
+                         lcn_char_t *default_field,
+                         lcn_boolean_clause_occur_t default_occur,
+                         apr_pool_t *pool);
+
+/** Use @a parser to create @a query from @a input, allocated in @a pool. */
+lcn_error_t *
+lcn_query_parser_parse (lcn_query_t **query,
+                        lcn_query_parser_t *parser,
+                        lcn_char_t *input,
+                        apr_pool_t *pool);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _LCN_QUERY_PARSER_H */

Propchange: incubator/lucene4c/trunk/src/query_parser/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Mar  6 06:50:30 2005
@@ -0,0 +1,2 @@
+.dirstamp
+.deps

Added: incubator/lucene4c/trunk/src/query_parser/parser.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/query_parser/parser.c?view=auto&rev=156316
==============================================================================
--- incubator/lucene4c/trunk/src/query_parser/parser.c (added)
+++ incubator/lucene4c/trunk/src/query_parser/parser.c Sun Mar  6 06:50:30 2005
@@ -0,0 +1,148 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed 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.
+ */
+
+#include "lcn_query_parser.h"
+
+struct lcn_query_parser_t {
+  lcn_char_t *default_field;
+
+  lcn_boolean_clause_occur_t default_occur;
+};
+
+lcn_error_t *
+lcn_query_parser_create (lcn_query_parser_t **parser,
+                         lcn_char_t *default_field,
+                         lcn_boolean_clause_occur_t default_occur,
+                         apr_pool_t *pool)
+{
+  lcn_query_parser_t *p = apr_pcalloc (pool, sizeof (*p));
+
+  p->default_field = lcn_strcpy (default_field, pool);
+
+  p->default_occur = default_occur;
+
+  *parser = p;
+
+  return LCN_NO_ERROR;
+}
+
+lcn_error_t *
+lcn_query_parser_parse (lcn_query_t **query,
+                        lcn_query_parser_t *parser,
+                        lcn_char_t *input,
+                        apr_pool_t *pool)
+{
+  apr_pool_t *subpool = lcn_pool_create (pool);
+  
+  apr_array_header_t *terms = apr_array_make (subpool,
+                                              5,
+                                              sizeof (lcn_char_t *));
+  int idx, prev = 0;
+
+  /* XXX this will need to be reworked to take into account non-default
+   * fields, grouped terms via parentheses, and probably a ton of other
+   * stuff, but hey, it's a start. */
+
+  for (idx = 0; idx < lcn_strlen (input); ++idx)
+    {
+      if (input[idx] == ' ')
+        {
+          if (prev)
+            {
+              lcn_char_t *tmp;
+
+              if (prev == 1)
+                prev = 0;
+
+              tmp = apr_pcalloc (subpool,
+                                 (idx - prev + 1) * sizeof (lcn_char_t));
+
+              memcpy (tmp, input + prev, (idx - prev) * sizeof (lcn_char_t));
+
+              APR_ARRAY_PUSH (terms, lcn_char_t *) = tmp;
+
+              prev = 0;
+            }
+        }
+      else
+        {
+          if (prev == 0)
+            prev = idx;
+        }
+    }
+
+  if (prev)
+    {
+      lcn_char_t *tmp
+        = apr_pcalloc (subpool,
+                       (lcn_strlen (input) - prev + 1) * sizeof (lcn_char_t));
+
+      memcpy (tmp,
+              input + prev,
+              (lcn_strlen (input) - prev) * sizeof (lcn_char_t));
+
+      APR_ARRAY_PUSH (terms, lcn_char_t *) = tmp;
+    }
+
+  switch (terms->nelts)
+    {
+      case 0:
+        return lcn_error_create (APR_EINVAL,
+                                 NULL,
+                                 "a query needs at least one term");
+        break;
+
+      case 1:
+        LCN_ERR (lcn_term_query_create (query,
+                                        lcn_term_create (APR_ARRAY_IDX
+                                                           (terms,
+                                                            0,
+                                                            lcn_char_t *),
+                                                         parser->default_field,
+                                                         pool),
+                                        pool));
+        break;
+
+      default:
+        LCN_ERR (lcn_boolean_query_create (query, pool));
+
+        /* XXX eventually we want to check if terms are things like AND,
+         * OR, etc, and altering the occur parameter accordingly. */
+
+        for (idx = 0; idx < terms->nelts; ++idx)
+          {
+            lcn_query_t *tq;
+            
+            LCN_ERR (lcn_term_query_create
+                      (&tq,
+                       lcn_term_create (APR_ARRAY_IDX (terms,
+                                                       idx,
+                                                       lcn_char_t *),
+                                        parser->default_field,
+                                        pool),
+                       pool));
+
+            LCN_ERR (lcn_boolean_query_add (*query,
+                                            tq,
+                                            parser->default_occur));
+          }
+        break;
+    }
+
+  lcn_pool_destroy (subpool);
+
+  return LCN_NO_ERROR;
+}

Modified: incubator/lucene4c/trunk/test/abts_tests.h
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/test/abts_tests.h?view=diff&r1=156315&r2=156316
==============================================================================
--- incubator/lucene4c/trunk/test/abts_tests.h (original)
+++ incubator/lucene4c/trunk/test/abts_tests.h Sun Mar  6 06:50:30 2005
@@ -33,7 +33,8 @@
   { test_terminfos },
   { test_frequencies },
   { test_index },
-  { test_scorer }
+  { test_scorer },
+  { test_query_parser }
 };
 
 #endif /* ABTS_TEST_H */

Modified: incubator/lucene4c/trunk/test/lcn_tests.h
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/test/lcn_tests.h?view=diff&r1=156315&r2=156316
==============================================================================
--- incubator/lucene4c/trunk/test/lcn_tests.h (original)
+++ incubator/lucene4c/trunk/test/lcn_tests.h Sun Mar  6 06:50:30 2005
@@ -55,6 +55,7 @@
 abts_suite *test_index (abts_suite *);
 abts_suite *test_string (abts_suite *);
 abts_suite *test_scorer (abts_suite *);
+abts_suite *test_query_parser (abts_suite *);
 
 #ifdef __cplusplus
 }

Propchange: incubator/lucene4c/trunk/test/query_parser/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Mar  6 06:50:30 2005
@@ -0,0 +1,2 @@
+.dirstamp
+.deps

Added: incubator/lucene4c/trunk/test/query_parser/parser_test.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/test/query_parser/parser_test.c?view=auto&rev=156316
==============================================================================
--- incubator/lucene4c/trunk/test/query_parser/parser_test.c (added)
+++ incubator/lucene4c/trunk/test/query_parser/parser_test.c Sun Mar  6 
06:50:30 2005
@@ -0,0 +1,66 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed 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.
+ */
+
+#include "lcn_query_parser.h"
+
+#include "lcn_tests.h"
+
+#include "abts.h"
+
+static void
+test_basics (abts_case *tc, void *data)
+{
+  lcn_query_parser_t *qp;
+  lcn_query_t *query;
+
+  CHK_ERR (lcn_query_parser_create (&qp,
+                                    lcn_str_from_cstring ("contents", p),
+                                    LCN_MUST,
+                                    p));
+
+  CHK_ERR (lcn_query_parser_parse (&query,
+                                   qp,
+                                   lcn_str_from_cstring ("foo bar", p),
+                                   p));
+
+  CHK_ERR (lcn_query_parser_parse (&query,
+                                   qp,
+                                   lcn_str_from_cstring ("foo", p),
+                                   p));
+
+  {
+    lcn_error_t *err = lcn_query_parser_parse (&query,
+                                               qp,
+                                               lcn_str_from_cstring ("", p),
+                                               p);
+
+    ABTS_PTR_NOTNULL (tc, err);
+
+    ABTS_INT_EQUAL (tc, APR_EINVAL, err->apr_err);
+  }
+
+  apr_pool_clear (p);
+}
+
+abts_suite *
+test_query_parser (abts_suite *suite)
+{
+  suite = ADD_SUITE (suite);
+
+  abts_run_test (suite, test_basics, NULL);
+
+  return suite;
+}


Reply via email to