You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@lucene.apache.org by ro...@apache.org on 2005/06/26 11:59:25 UTC

svn commit: r201819 - /incubator/lucene4c/trunk/src/cmdline/main.c

Author: rooneg
Date: Sat Jun 25 20:08:29 2005
New Revision: 201819

URL: http://svn.apache.org/viewcvs?rev=201819&view=rev
Log:
Add a start at an 'lcn index' command, which adds a number of documents
to an index.  So far it only includes the paths for the docs in the index,
but eventually it will be expanded to include the contents as well, which
will be rather more useful ;-)

* src/cmdline/main.c
  (lcn_index_cmd): new command.
  (index_entry): helper function for lcn_index_cmd.

Modified:
    incubator/lucene4c/trunk/src/cmdline/main.c

Modified: incubator/lucene4c/trunk/src/cmdline/main.c
URL: http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/cmdline/main.c?rev=201819&r1=201818&r2=201819&view=diff
==============================================================================
--- incubator/lucene4c/trunk/src/cmdline/main.c (original)
+++ incubator/lucene4c/trunk/src/cmdline/main.c Sat Jun 25 20:08:29 2005
@@ -18,6 +18,8 @@
 #include <stdio.h>
 
 #include <apr.h>
+#include <apr_file_info.h>
+#include <apr_strings.h>
 
 #include "lcn_init.h"
 #include "lcn_types.h"
@@ -25,12 +27,13 @@
 #include "lcn_query_parser.h"
 #include "lcn_searcher.h"
 #include "lcn_document.h"
+#include "lcn_index_writer.h"
 
 typedef lcn_error_t *(lcn_subcommand_t) (int argc,
                                          char *argv[],
                                          apr_pool_t *pool);
 
-lcn_subcommand_t lcn_search_cmd;
+lcn_subcommand_t lcn_search_cmd, lcn_index_cmd;
 
 typedef struct {
   const char *name;
@@ -39,6 +42,7 @@
 
 subcommand_desc_t dispatch_table[] = {
   { "search", lcn_search_cmd },
+  { "index",  lcn_index_cmd },
   { NULL }
 };
 
@@ -107,6 +111,117 @@
         }
 
       lcn_pool_destroy (subpool);
+
+      return LCN_NO_ERROR;
+    }
+}
+
+static lcn_error_t *
+index_entry (lcn_index_writer_t *writer,
+             const char *entry,
+             int *nindexed,
+             apr_pool_t *pool)
+{
+  apr_finfo_t finfo;
+  apr_status_t rv;
+
+  rv = apr_stat (&finfo, entry, APR_FINFO_TYPE | APR_FINFO_SIZE, pool);
+  if (rv)
+    return lcn_error_create (LCN_ERR_IO_EXCEPTION, "couldn't stat file");
+
+  if (finfo.filetype == APR_DIR)
+    {
+      apr_pool_t *subpool;
+      apr_dir_t *dir;
+
+      rv = apr_dir_open (&dir, entry, pool);
+      if (rv)
+        return lcn_error_create (LCN_ERR_IO_EXCEPTION, "couldn't open dir");
+
+      subpool = lcn_pool_create (pool);
+
+      for (rv = apr_dir_read (&finfo, APR_FINFO_NAME, dir);
+           rv == APR_SUCCESS;
+           rv = apr_dir_read (&finfo, APR_FINFO_NAME, dir))
+        {
+          apr_pool_clear (subpool);
+
+          if (strcmp (finfo.name, ".") == 0 || strcmp (finfo.name, "..") == 0)
+            continue;
+
+          LCN_ERR (index_entry (writer,
+                                apr_psprintf (subpool,
+                                              "%s/%s",
+                                              entry,
+                                              finfo.name),
+                                nindexed,
+                                subpool));
+        }
+
+      lcn_pool_destroy (subpool);
+
+      if (! APR_STATUS_IS_ENOENT (rv))
+        return lcn_error_create (LCN_ERR_IO_EXCEPTION, "couldn't read dir");
+    }
+  else
+    {
+      lcn_document_t *doc;
+      lcn_field_t *field;
+
+      LCN_ERR (lcn_document_create (&doc, pool));
+
+      printf ("indexing '%s'\n", entry);
+
+      /* XXX should be a keyword or something, so it isn't tokenized */
+      LCN_ERR (lcn_field_text (&field, "path", entry, pool));
+
+      LCN_ERR (lcn_document_add_field (doc, field));
+
+      /* XXX should obviously index contents... */
+
+      LCN_ERR (lcn_index_writer_add_document (writer, doc, pool));
+
+      if (++*nindexed % 10 == 0)
+        {
+          printf ("optimizing index\n");
+          LCN_ERR (lcn_index_writer_optimize (writer));
+        }
+    }
+
+  return LCN_NO_ERROR;
+}
+
+lcn_error_t *
+lcn_index_cmd (int argc, char *argv[], apr_pool_t *pool)
+{
+  if (argc < 2)
+    {
+      printf ("usage: lcn index <index> <sourcedocs>\n");
+      return LCN_NO_ERROR;
+    }
+  else
+    {
+      lcn_index_writer_t *writer;
+      lcn_analyzer_t *analyzer;
+      apr_pool_t *subpool;
+      int i, nindexed = 0;
+
+      LCN_ERR (lcn_analyzer_standard_create (&analyzer, pool));
+
+      LCN_ERR (lcn_index_writer_create (&writer, argv[0], analyzer, pool));
+
+      subpool = lcn_pool_create (pool);
+
+      for (i = 0; i < argc - 1; ++i)
+        {
+          apr_pool_clear (subpool);
+
+          LCN_ERR (index_entry (writer, argv[i + 1], &nindexed, subpool));
+        }
+
+      lcn_pool_destroy (subpool);
+
+      LCN_ERR (lcn_index_writer_optimize (writer));
 
       return LCN_NO_ERROR;
     }