Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 80 / 96d14f5037013426a63e3f5520f473c6c927f5
1 Return-Path: <james@jameswestby.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id DB168431FC0\r
6         for <notmuch@notmuchmail.org>; Fri, 18 Dec 2009 16:12:02 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id gubYVN8FosCK for <notmuch@notmuchmail.org>;\r
11         Fri, 18 Dec 2009 16:12:01 -0800 (PST)\r
12 Received: from jameswestby.net (jameswestby.net [89.145.97.141])\r
13         by olra.theworths.org (Postfix) with ESMTP id 68580431FBF\r
14         for <notmuch@notmuchmail.org>; Fri, 18 Dec 2009 16:12:01 -0800 (PST)\r
15 Received: from cpc4-aztw22-2-0-cust59.aztw.cable.virginmedia.com\r
16         ([94.169.116.60] helo=flash)\r
17         by jameswestby.net with esmtpa (Exim 4.69)\r
18         (envelope-from <james@jameswestby.net>)\r
19         id 1NLmvP-0006Ca-TL; Sat, 19 Dec 2009 00:12:00 +0000\r
20 Received: by flash (Postfix, from userid 1000)\r
21         id 7075C6E546A; Sat, 19 Dec 2009 00:11:54 +0000 (GMT)\r
22 From: James Westby <jw+debian@jameswestby.net>\r
23 To: notmuch@notmuchmail.org\r
24 Date: Sat, 19 Dec 2009 00:11:48 +0000\r
25 Message-Id: <1261181508-22222-1-git-send-email-jw+debian@jameswestby.net>\r
26 X-Mailer: git-send-email 1.6.3.3\r
27 In-Reply-To: <871virzzjy.fsf@yoom.home.cworth.org>\r
28 References: <871virzzjy.fsf@yoom.home.cworth.org>\r
29 Subject: [notmuch] [PATCH v2] Store the size of the file for each message\r
30 X-BeenThere: notmuch@notmuchmail.org\r
31 X-Mailman-Version: 2.1.12\r
32 Precedence: list\r
33 List-Id: "Use and development of the notmuch mail system."\r
34         <notmuch.notmuchmail.org>\r
35 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
36         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
37 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
38 List-Post: <mailto:notmuch@notmuchmail.org>\r
39 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
40 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
42 X-List-Received-Date: Sat, 19 Dec 2009 00:12:03 -0000\r
43 \r
44 When indexing a message store the filesize along with it so that\r
45 when we store all the filenames for a message-id we can know if\r
46 any of them have different content cheaply.\r
47 \r
48 The value stored is defined to be the largest filesize of any\r
49 of the files for that message.\r
50 \r
51 This changes the API for efficiency reasons. The size is often\r
52 known to the caller, and so we save a second stat by asking them\r
53 to provide it. If they don't know it they can pass -1 and the\r
54 stat will be done for them.\r
55 \r
56 We store the filesize such that we can query a range. Thus it\r
57 would be possible to query "filesize:0..100" if you somehow\r
58 knew the raw message was less that 100 bytes.\r
59 ---\r
60 \r
61   With new, improved, working, filesize:.. search.\r
62 \r
63  lib/database.cc       |    7 +++++++\r
64  lib/message.cc        |   25 +++++++++++++++++++++++++\r
65  lib/notmuch-private.h |    8 +++++++-\r
66  lib/notmuch.h         |    5 +++++\r
67  notmuch-new.c         |    2 +-\r
68  5 files changed, 45 insertions(+), 2 deletions(-)\r
69 \r
70 diff --git a/lib/database.cc b/lib/database.cc\r
71 index b6c4d07..d834d94 100644\r
72 --- a/lib/database.cc\r
73 +++ b/lib/database.cc\r
74 @@ -463,6 +463,8 @@ notmuch_database_open (const char *path,\r
75      struct stat st;\r
76      int err;\r
77      unsigned int i;\r
78 +    Xapian::NumberValueRangeProcessor *filesize_proc = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_FILESIZE,\r
79 +                        "filesize:", true);\r
80  \r
81      if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {\r
82         notmuch_path = NULL;\r
83 @@ -508,6 +510,7 @@ notmuch_database_open (const char *path,\r
84         notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));\r
85         notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);\r
86         notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);\r
87 +       notmuch->query_parser->add_valuerangeprocessor (filesize_proc);\r
88  \r
89         for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {\r
90             prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];\r
91 @@ -889,6 +892,7 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
92  notmuch_status_t\r
93  notmuch_database_add_message (notmuch_database_t *notmuch,\r
94                               const char *filename,\r
95 +                             const off_t size,\r
96                               notmuch_message_t **message_ret)\r
97  {\r
98      notmuch_message_file_t *message_file;\r
99 @@ -992,6 +996,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
100         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
101             _notmuch_message_set_filename (message, filename);\r
102             _notmuch_message_add_term (message, "type", "mail");\r
103 +           ret = _notmuch_message_set_filesize (message, filename, size);\r
104 +           if (ret)\r
105 +               goto DONE;\r
106         } else {\r
107             ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
108             goto DONE;\r
109 diff --git a/lib/message.cc b/lib/message.cc\r
110 index 49519f1..2bfc5ed 100644\r
111 --- a/lib/message.cc\r
112 +++ b/lib/message.cc\r
113 @@ -426,6 +426,31 @@ _notmuch_message_set_filename (notmuch_message_t *message,\r
114      message->doc.set_data (s);\r
115  }\r
116  \r
117 +notmuch_status_t\r
118 +_notmuch_message_set_filesize (notmuch_message_t *message,\r
119 +                              const char *filename,\r
120 +                              const off_t size)\r
121 +{\r
122 +    struct stat st;\r
123 +    off_t realsize = size;\r
124 +    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
125 +\r
126 +    if (realsize < 0) {\r
127 +       if (stat (filename, &st)) {\r
128 +           ret = NOTMUCH_STATUS_FILE_ERROR;\r
129 +           goto DONE;\r
130 +       } else {\r
131 +           realsize = st.st_size;\r
132 +       }\r
133 +    }\r
134 +\r
135 +    message->doc.add_value (NOTMUCH_VALUE_FILESIZE,\r
136 +                        Xapian::sortable_serialise (realsize));\r
137 +\r
138 +  DONE:\r
139 +    return ret;\r
140 +}\r
141 +\r
142  const char *\r
143  notmuch_message_get_filename (notmuch_message_t *message)\r
144  {\r
145 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
146 index 116f63d..1ba3055 100644\r
147 --- a/lib/notmuch-private.h\r
148 +++ b/lib/notmuch-private.h\r
149 @@ -100,7 +100,8 @@ _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);\r
150  \r
151  typedef enum {\r
152      NOTMUCH_VALUE_TIMESTAMP = 0,\r
153 -    NOTMUCH_VALUE_MESSAGE_ID\r
154 +    NOTMUCH_VALUE_MESSAGE_ID,\r
155 +    NOTMUCH_VALUE_FILESIZE\r
156  } notmuch_value_t;\r
157  \r
158  /* Xapian (with flint backend) complains if we provide a term longer\r
159 @@ -193,6 +194,11 @@ void\r
160  _notmuch_message_set_filename (notmuch_message_t *message,\r
161                                const char *filename);\r
162  \r
163 +notmuch_status_t\r
164 +_notmuch_message_set_filesize (notmuch_message_t *message,\r
165 +                              const char *filename,\r
166 +                              const off_t size);\r
167 +\r
168  void\r
169  _notmuch_message_ensure_thread_id (notmuch_message_t *message);\r
170  \r
171 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
172 index 60834fb..5d0d224 100644\r
173 --- a/lib/notmuch.h\r
174 +++ b/lib/notmuch.h\r
175 @@ -32,6 +32,7 @@\r
176  NOTMUCH_BEGIN_DECLS\r
177  \r
178  #include <time.h>\r
179 +#include <stdlib.h>\r
180  \r
181  #ifndef FALSE\r
182  #define FALSE 0\r
183 @@ -241,6 +242,9 @@ notmuch_database_get_timestamp (notmuch_database_t *database,\r
184   * notmuch database will reference the filename, and will not copy the\r
185   * entire contents of the file.\r
186   *\r
187 + * 'size' should be the number of bytes in the file, or -1 if you are\r
188 + * not sure.\r
189 + *\r
190   * If 'message' is not NULL, then, on successful return '*message'\r
191   * will be initialized to a message object that can be used for things\r
192   * such as adding tags to the just-added message. The user should call\r
193 @@ -265,6 +269,7 @@ notmuch_database_get_timestamp (notmuch_database_t *database,\r
194  notmuch_status_t\r
195  notmuch_database_add_message (notmuch_database_t *database,\r
196                               const char *filename,\r
197 +                             const off_t size,\r
198                               notmuch_message_t **message);\r
199  \r
200  /* Find a message with the given message_id.\r
201 diff --git a/notmuch-new.c b/notmuch-new.c\r
202 index 9d20616..cea66c2 100644\r
203 --- a/notmuch-new.c\r
204 +++ b/notmuch-new.c\r
205 @@ -235,7 +235,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
206                     fflush (stdout);\r
207                 }\r
208  \r
209 -               status = notmuch_database_add_message (notmuch, next, &message);\r
210 +               status = notmuch_database_add_message (notmuch, next, st->st_size, &message);\r
211                 switch (status) {\r
212                     /* success */\r
213                     case NOTMUCH_STATUS_SUCCESS:\r
214 -- \r
215 1.6.3.3\r
216 \r