xapian-core  1.4.21
xapian-metadata.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007-2022 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <xapian.h>
24 
25 #include <iostream>
26 #include <string>
27 
28 #include <cstdlib> // For exit().
29 #include <cstring>
30 
31 using namespace std;
32 
33 #define PROG_NAME "xapian-metadata"
34 #define PROG_DESC "Read and write user metadata"
35 
36 static void show_usage() {
37  cout << "Usage: " PROG_NAME " get PATH_TO_DATABASE KEY\n"
38  " " PROG_NAME " list PATH_TO_DATABASE [PREFIX]\n"
39  " " PROG_NAME " set PATH_TO_DATABASE KEY VALUE\n";
40 }
41 
42 int
43 main(int argc, char **argv)
44 try {
45  const char * command = argv[1];
46  if (!command) {
47 syntax_error:
48  show_usage();
49  exit(1);
50  }
51 
52  if (command[0] == '-') {
53  if (strcmp(command, "--help") == 0) {
54  cout << PROG_NAME " - " PROG_DESC "\n\n";
55  show_usage();
56  exit(0);
57  }
58  if (strcmp(command, "--version") == 0) {
59  cout << PROG_NAME " - " PACKAGE_STRING "\n";
60  exit(0);
61  }
62  }
63 
64  if (strcmp(command, "get") == 0) {
65  if (argc != 4) goto syntax_error;
66  Xapian::Database db(argv[2]);
67  cout << db.get_metadata(argv[3]) << '\n';
68  } else if (strcmp(command, "list") == 0) {
69  if (argc != 3 && argc != 4) goto syntax_error;
70  Xapian::Database db(argv[2]);
71  string prefix;
72  if (argc == 4) prefix = argv[3];
73  for (Xapian::TermIterator t = db.metadata_keys_begin(prefix);
74  t != db.metadata_keys_end(prefix);
75  ++t) {
76  cout << *t << '\n';
77  }
78  } else if (strcmp(command, "set") == 0) {
79  if (argc != 5) goto syntax_error;
81  db.set_metadata(argv[3], argv[4]);
82  db.commit();
83  } else {
84  goto syntax_error;
85  }
86 } catch (const Xapian::Error &e) {
87  cout << e.get_description() << '\n';
88  exit(1);
89 }
static void show_usage()
This class is used to access a database, or a group of databases.
Definition: database.h:68
STL namespace.
const int DB_CREATE_OR_OPEN
Create database if it doesn&#39;t already exist.
Definition: constants.h:35
void set_metadata(const std::string &key, const std::string &metadata)
Set the user-specified metadata associated with a given key.
Definition: omdatabase.cc:1064
std::string get_metadata(const std::string &key) const
Get the user-specified metadata associated with a given key.
Definition: omdatabase.cc:758
#define PROG_NAME
Class for iterating over a list of terms.
Definition: termiterator.h:41
int main(int argc, char **argv)
This class provides read/write access to a database.
Definition: database.h:785
Public interfaces for the Xapian library.
void commit()
Commit any pending modifications made to the database.
Definition: omdatabase.cc:857
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
#define PACKAGE_STRING
Definition: config.h:337
Xapian::TermIterator metadata_keys_begin(const std::string &prefix=std::string()) const
An iterator which returns all user-specified metadata keys.
Definition: omdatabase.cc:768
#define PROG_DESC
Xapian::TermIterator metadata_keys_end(const std::string &=std::string()) const
Corresponding end iterator to metadata_keys_begin().
Definition: database.h:506