* modb-standard.el (modb-standard-loaded-message-id): Return nil
[elisp/wanderlust.git] / elmo / modb-standard.el
1 ;;; modb-standard.el --- Standartd Implement of MODB.
2
3 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;;      Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
8
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 ;;
26
27 ;;; Commentary:
28 ;;
29
30 ;;; Code:
31 ;;
32 (eval-when-compile (require 'cl))
33
34 (require 'elmo-util)
35 (require 'modb)
36
37 (defcustom modb-standard-divide-number 500
38   "*Standard modb divide entity number."
39   :type '(choice (const :tag "Not divide" nil)
40                  number)
41   :group 'elmo)
42
43 (defcustom modb-standard-economize-entity-size nil
44   "*Economize message entity size.
45 When non-nil, redundunt message-id string are not saved."
46   :type 'boolean
47   :group 'elmo)
48
49 (defvar modb-standard-entity-filename "entity"
50   "Message entity database.")
51
52 (defvar modb-standard-flag-filename "flag"
53   "Message number <=> Flag status database.")
54
55 (defvar modb-standard-msgid-filename "msgid"
56   "Message number <=> Message-Id database.")
57
58 (eval-and-compile
59   (luna-define-class modb-standard (modb-generic)
60                      (number-list       ; sorted list of message numbers.
61                       entity-map        ; number, msg-id -> entity mapping.
62                       flag-map          ; number -> flag-list mapping
63                       flag-count        ; list of (FLAG . COUNT)
64                       ))
65   (luna-define-internal-accessors 'modb-standard))
66
67 ;; for internal use only
68 (defsubst modb-standard-key (number)
69   (concat "#" (number-to-string number)))
70
71 (defsubst modb-standard-entity-id (entity)
72   (if (eq 'autoload (car-safe entity))
73       (cddr entity)
74     (elmo-msgdb-message-entity-field
75      (elmo-message-entity-handler entity)
76      entity 'message-id)))
77
78 (defsubst modb-standard-entity-map (modb)
79   (or (modb-standard-entity-map-internal modb)
80       (modb-standard-set-entity-map-internal
81        modb
82        (elmo-make-hash (elmo-msgdb-length modb)))))
83
84 (defsubst modb-standard-flag-map (modb)
85   (or (modb-standard-flag-map-internal modb)
86       (modb-standard-set-flag-map-internal
87        modb
88        (elmo-make-hash (elmo-msgdb-length modb)))))
89
90 (defsubst modb-standard-set-message-modified (modb number)
91   (if modb-standard-divide-number
92       (let ((section (/ number modb-standard-divide-number))
93             (modified (modb-generic-message-modified-internal modb)))
94         (unless (memq section modified)
95           (modb-generic-set-message-modified-internal
96            modb (cons section modified))))
97     (modb-generic-set-message-modified-internal modb t)))
98
99 (defsubst modb-standard-set-flag-modified (modb number)
100   (modb-generic-set-flag-modified-internal modb t))
101
102 (defsubst modb-standard-message-flags (modb number)
103   (cdr (elmo-get-hash-val (modb-standard-key number)
104                           (modb-standard-flag-map-internal modb))))
105
106 (defsubst modb-standard-match-flags (check-flags flags)
107   (catch 'done
108     (while check-flags
109       (when (memq (car check-flags) flags)
110         (throw 'done t))
111       (setq check-flags (cdr check-flags)))))
112
113 (defsubst modb-standard-countup-flags (modb flags &optional delta)
114   (let ((flag-count (modb-standard-flag-count-internal modb))
115         (delta (or delta 1))
116         elem)
117     (dolist (flag flags)
118       (if (setq elem (assq flag flag-count))
119           (setcdr elem (+ (cdr elem) delta))
120         (setq flag-count (cons (cons flag delta) flag-count))))
121     (modb-standard-set-flag-count-internal modb flag-count)))
122
123 ;; save and load functions
124 (defun modb-standard-load-msgid (modb path)
125   (let* ((alist (elmo-object-load
126                  (expand-file-name modb-standard-msgid-filename path)))
127          (table (or (modb-standard-entity-map-internal modb)
128                     (elmo-make-hash (length alist))))
129          numbers info)
130     (dolist (pair alist)
131       (setq info (cons 'autoload pair))
132       (elmo-set-hash-val (modb-standard-key (car pair)) info table)
133       (elmo-set-hash-val (cdr pair) info table)
134       (setq numbers (cons (car pair) numbers)))
135     (modb-standard-set-number-list-internal modb (nreverse numbers))
136     (modb-standard-set-entity-map-internal modb table)))
137
138 (defun modb-standard-save-msgid (modb path)
139   (let ((table (modb-standard-entity-map-internal modb))
140         entity alist)
141     (dolist (number (modb-standard-number-list-internal modb))
142       (setq entity (elmo-get-hash-val (modb-standard-key number) table))
143       (setq alist (cons (cons number (modb-standard-entity-id entity))
144                         alist)))
145     (elmo-object-save
146      (expand-file-name modb-standard-msgid-filename path)
147      (nreverse alist))))
148
149 (defun modb-standard-load-flag (modb path)
150   (let ((table (or (modb-standard-flag-map-internal modb)
151                    (elmo-make-hash (elmo-msgdb-length modb)))))
152     (dolist (info (elmo-object-load
153                    (expand-file-name modb-standard-flag-filename path)))
154       (modb-standard-countup-flags modb (cdr info))
155       (elmo-set-hash-val (modb-standard-key (car info)) info table))
156     (modb-standard-set-flag-map-internal modb table)))
157
158 (defun modb-standard-save-flag (modb path)
159   (let (table flist info)
160     (when (setq table (modb-standard-flag-map-internal modb))
161       (mapatoms
162        (lambda (atom)
163          (setq info (symbol-value atom))
164          (when (cdr info)
165            (setq flist (cons info flist))))
166        table))
167     (elmo-object-save
168      (expand-file-name modb-standard-flag-filename path)
169      flist)))
170
171 (defsubst modb-standard-entity-filename (section)
172   (if section
173       (concat modb-standard-entity-filename
174               "-"
175               (number-to-string section))
176     modb-standard-entity-filename))
177
178 (defsubst modb-standard-loaded-message-id (msgdb number)
179   "Get message-id for autoloaded entity."
180   (let ((ret (elmo-get-hash-val
181               (modb-standard-key number)
182               (modb-standard-entity-map-internal msgdb))))
183     (cond
184      ((and ret (eq (car-safe ret) 'autoload))
185       (cdr (cdr ret))) ; message-id.
186      ((and ret (stringp (car-safe ret)))
187       ;; Already loaded.
188       (car ret))
189      ((null ret)
190       ;; Garbage entity.
191       (elmo-clear-hash-val (modb-standard-key number)
192                            (modb-standard-entity-map-internal msgdb))
193       nil)                              ; return nil.
194      (t (error "Internal error: invalid msgdb status")))))
195
196 (defun modb-standard-load-entity (modb path &optional section)
197   (let ((table (or (modb-standard-entity-map-internal modb)
198                    (elmo-make-hash (elmo-msgdb-length modb))))
199         (inhibit-quit t)
200         number msgid)
201     (dolist (entity (elmo-object-load
202                      (expand-file-name
203                       (modb-standard-entity-filename section)
204                       path)))
205       (setq number (elmo-msgdb-message-entity-number
206                     (elmo-message-entity-handler entity)
207                     entity)
208             msgid (modb-standard-loaded-message-id modb number))
209       (when msgid
210         (setcar entity msgid)
211         (elmo-set-hash-val msgid entity table)
212         (elmo-set-hash-val (modb-standard-key number) entity table)))
213     (modb-standard-set-entity-map-internal modb table)))
214
215 (defsubst modb-standard-save-entity-1 (modb path &optional section)
216   (let ((table (modb-standard-entity-map-internal modb))
217         (filename (expand-file-name
218                    (modb-standard-entity-filename section) path))
219         entity entities)
220     (dolist (number (modb-standard-number-list-internal modb))
221       (when (and (or (null section)
222                      (= section (/ number modb-standard-divide-number)))
223                  (setq entity (elmo-msgdb-message-entity modb number)))
224         (when modb-standard-economize-entity-size
225           (when (stringp (car entity)) (setcar entity t)))
226         (setq entities (cons entity entities))))
227     (if entities
228         (elmo-object-save filename entities)
229       (ignore-errors (delete-file filename)))))
230
231 (defun modb-standard-save-entity (modb path)
232   (let ((sections (modb-generic-message-modified-internal modb)))
233     (cond ((listp sections)
234            (dolist (section sections)
235              (modb-standard-save-entity-1 modb path section)))
236           (sections
237            (modb-standard-save-entity-1 modb path)))))
238
239 ;;; Implement
240 ;;
241 (luna-define-method elmo-msgdb-load ((msgdb modb-standard))
242   (let ((inhibit-quit t)
243         (path (elmo-msgdb-location msgdb)))
244     (when (file-exists-p (expand-file-name modb-standard-flag-filename path))
245       (modb-standard-load-msgid msgdb path)
246       (modb-standard-load-flag msgdb path)
247       (unless modb-standard-divide-number
248         (modb-standard-load-entity msgdb path))
249       t)))
250
251 (luna-define-method elmo-msgdb-save ((msgdb modb-standard))
252   (let ((path (elmo-msgdb-location msgdb)))
253     (when (elmo-msgdb-message-modified-p msgdb)
254       (modb-standard-save-msgid  msgdb path)
255       (modb-standard-save-entity msgdb path)
256       (modb-generic-set-message-modified-internal msgdb nil))
257     (when (elmo-msgdb-flag-modified-p msgdb)
258       (modb-standard-save-flag msgdb path)
259       (modb-generic-set-flag-modified-internal msgdb nil))))
260
261 (luna-define-method elmo-msgdb-append :around ((msgdb modb-standard)
262                                                msgdb-append)
263   (when (> (elmo-msgdb-length msgdb-append) 0)
264     (if (eq (luna-class-name msgdb-append) 'modb-standard)
265         (let ((numbers (modb-standard-number-list-internal msgdb-append))
266               duplicates)
267           ;; number-list
268           (modb-standard-set-number-list-internal
269            msgdb
270            (nconc (modb-standard-number-list-internal msgdb)
271                   numbers))
272           ;; entity-map
273           (let ((table (modb-standard-entity-map msgdb))
274                 entity msg-id)
275             (dolist (number numbers)
276               (setq entity (elmo-msgdb-message-entity msgdb-append number)
277                     msg-id (modb-standard-entity-id entity))
278               (if (elmo-get-hash-val msg-id table)
279                   (setq duplicates (cons number duplicates))
280                 (elmo-set-hash-val msg-id entity table))
281               (elmo-set-hash-val (modb-standard-key number)
282                                  entity
283                                  table)))
284           ;; flag-map
285           (let ((table (modb-standard-flag-map msgdb)))
286             (mapatoms
287              (lambda (atom)
288                (elmo-set-hash-val (symbol-name atom)
289                                   (symbol-value atom)
290                                   table))
291              (modb-standard-flag-map msgdb-append)))
292           ;; flag-count
293           (dolist (pair (modb-standard-flag-count-internal msgdb-append))
294             (modb-standard-countup-flags msgdb (list (car pair)) (cdr pair)))
295           ;; modification flags
296           (dolist (number (modb-standard-number-list-internal msgdb-append))
297             (modb-standard-set-message-modified msgdb number)
298             (modb-standard-set-flag-modified msgdb number))
299           duplicates)
300       (luna-call-next-method))))
301
302 (luna-define-method elmo-msgdb-clear :after ((msgdb modb-standard))
303   (modb-standard-set-number-list-internal msgdb nil)
304   (modb-standard-set-entity-map-internal msgdb nil)
305   (modb-standard-set-flag-map-internal msgdb nil)
306   (modb-standard-set-flag-count-internal msgdb nil))
307
308 (luna-define-method elmo-msgdb-length ((msgdb modb-standard))
309   (length (modb-standard-number-list-internal msgdb)))
310
311 (luna-define-method elmo-msgdb-flag-available-p ((msgdb modb-standard) flag)
312   t)
313
314 (luna-define-method elmo-msgdb-flags ((msgdb modb-standard) number)
315   (modb-standard-message-flags msgdb number))
316
317 (luna-define-method elmo-msgdb-set-flag ((msgdb modb-standard)
318                                          number flag)
319   (case flag
320     (read
321      (elmo-msgdb-unset-flag msgdb number 'unread))
322     (uncached
323      (elmo-msgdb-unset-flag msgdb number 'cached))
324     (t
325      (let ((cur-flags (modb-standard-message-flags msgdb number))
326            new-flags diff)
327        (unless (memq flag cur-flags)
328          (setq new-flags (cons flag cur-flags))
329          (setq diff (elmo-list-diff new-flags cur-flags))
330          (modb-standard-countup-flags msgdb (car diff))
331          (modb-standard-countup-flags msgdb (cadr diff) -1)
332          (elmo-set-hash-val (modb-standard-key number)
333                             (cons number new-flags)
334                             (modb-standard-flag-map msgdb))
335          (modb-standard-set-flag-modified msgdb number))))))
336
337 (luna-define-method elmo-msgdb-unset-flag ((msgdb modb-standard)
338                                            number flag)
339   (case flag
340     (read
341      (elmo-msgdb-set-flag msgdb number 'unread))
342     (uncached
343      (elmo-msgdb-set-flag msgdb number 'cached))
344     (all
345      (modb-standard-countup-flags msgdb
346                                   (modb-standard-message-flags msgdb number)
347                                   -1)
348      (elmo-clear-hash-val (modb-standard-key number)
349                           (modb-standard-flag-map msgdb)))
350     (t
351      (let ((cur-flags (modb-standard-message-flags msgdb number))
352            new-flags diff)
353        (when (memq flag cur-flags)
354          (setq new-flags (delq flag (copy-sequence cur-flags)))
355          (setq diff (elmo-list-diff new-flags cur-flags))
356          (modb-standard-countup-flags msgdb (car diff))
357          (modb-standard-countup-flags msgdb (cadr diff) -1)
358          (elmo-set-hash-val (modb-standard-key number)
359                             (cons number new-flags)
360                             (modb-standard-flag-map msgdb))
361          (modb-standard-set-flag-modified msgdb number))
362        (when (eq flag 'unread)
363          (elmo-msgdb-unset-flag msgdb number 'new))))))
364
365 (luna-define-method elmo-msgdb-flag-count ((msgdb modb-standard))
366   (modb-standard-flag-count-internal msgdb))
367
368 (luna-define-method elmo-msgdb-list-messages ((msgdb modb-standard))
369   (copy-sequence
370    (modb-standard-number-list-internal msgdb)))
371
372 (luna-define-method elmo-msgdb-list-flagged ((msgdb modb-standard) flag)
373   (let (entry matched)
374     (case flag
375       (read
376        (dolist (number (modb-standard-number-list-internal msgdb))
377          (unless (memq 'unread (modb-standard-message-flags msgdb number))
378            (setq matched (cons number matched)))))
379       (uncached
380        (dolist (number (modb-standard-number-list-internal msgdb))
381          (unless (memq 'cached (modb-standard-message-flags msgdb number))
382            (setq matched (cons number matched)))))
383       (any
384        (mapatoms
385         (lambda (atom)
386           (setq entry (symbol-value atom))
387           (unless (and (eq (length (cdr entry)) 1)
388                        (eq (car (cdr entry)) 'cached))
389             ;; If there is a flag other than cached, then the message
390             ;; matches to `any'.
391             (setq matched (cons (car entry) matched))))
392         (modb-standard-flag-map msgdb)))
393       (digest
394        (let ((flags (append elmo-digest-flags
395                             (elmo-get-global-flags t t))))
396          (mapatoms
397           (lambda (atom)
398             (setq entry (symbol-value atom))
399             (when (modb-standard-match-flags flags (cdr entry))
400               (setq matched (cons (car entry) matched))))
401           (modb-standard-flag-map msgdb))))
402       (t
403        (mapatoms
404         (lambda (atom)
405           (setq entry (symbol-value atom))
406           (when (memq flag (cdr entry))
407             (setq matched (cons (car entry) matched))))
408         (modb-standard-flag-map msgdb))))
409     matched))
410
411 (luna-define-method elmo-msgdb-search ((msgdb modb-standard)
412                                        condition &optional numbers)
413   (if (vectorp condition)
414       (let ((key (elmo-filter-key condition))
415             results)
416         (cond
417          ((and (string= key "flag")
418                (eq (elmo-filter-type condition) 'match))
419           (setq results (elmo-msgdb-list-flagged
420                          msgdb
421                          (intern (elmo-filter-value condition))))
422           (if numbers
423               (elmo-list-filter numbers results)
424             results))
425          ((member key '("first" "last"))
426           (let* ((numbers (or numbers
427                               (modb-standard-number-list-internal msgdb)))
428                  (len (length numbers))
429                  (lastp (string= key "last"))
430                  (value (string-to-number (elmo-filter-value condition))))
431             (when (eq (elmo-filter-type condition) 'unmatch)
432               (setq lastp (not lastp)
433                     value (- len value)))
434             (if lastp
435                 (nthcdr (max (- len value) 0) numbers)
436               (when (> value 0)
437                 (let* ((numbers (copy-sequence numbers))
438                        (last (nthcdr (1- value) numbers)))
439                   (when last
440                     (setcdr last nil))
441                   numbers)))))
442          (t
443           t)))
444     t))
445
446 (luna-define-method elmo-msgdb-append-entity ((msgdb modb-standard)
447                                               entity &optional flags)
448   (when entity
449     (let ((number (elmo-msgdb-message-entity-number
450                    (elmo-message-entity-handler entity) entity))
451           (msg-id (elmo-msgdb-message-entity-field
452                    (elmo-message-entity-handler entity) entity 'message-id))
453           duplicate)
454       (when msg-id
455         ;; number-list
456         (modb-standard-set-number-list-internal
457          msgdb
458          (nconc (modb-standard-number-list-internal msgdb)
459                 (list number)))
460         ;; entity-map
461         (let ((table (modb-standard-entity-map msgdb)))
462           (setq duplicate (elmo-get-hash-val msg-id table))
463           (elmo-set-hash-val (modb-standard-key number) entity table)
464           (elmo-set-hash-val msg-id entity table))
465         ;; modification flags
466         (modb-standard-set-message-modified msgdb number)
467         ;; flag-map
468         (when flags
469           (elmo-set-hash-val
470            (modb-standard-key number)
471            (cons number flags)
472            (modb-standard-flag-map msgdb))
473           (modb-standard-countup-flags msgdb flags)
474           (modb-standard-set-flag-modified msgdb number))
475         duplicate))))
476
477 (luna-define-method elmo-msgdb-delete-messages ((msgdb modb-standard)
478                                                 numbers)
479   (let ((number-list (modb-standard-number-list-internal msgdb))
480         (entity-map (modb-standard-entity-map-internal msgdb))
481         (flag-map (modb-standard-flag-map-internal msgdb))
482         key entity)
483     (dolist (number numbers)
484       (setq key (modb-standard-key number)
485             entity (elmo-get-hash-val key entity-map))
486       (when entity
487         ;; number-list
488         (setq number-list (delq number number-list))
489         ;; entity-map
490         (elmo-clear-hash-val key entity-map)
491         (elmo-clear-hash-val (modb-standard-entity-id entity) entity-map)
492         ;; flag-count (must be BEFORE flag-map)
493         (modb-standard-countup-flags
494          msgdb
495          (modb-standard-message-flags msgdb number)
496          -1)
497         ;; flag-map
498         (elmo-clear-hash-val key flag-map)
499         (modb-standard-set-message-modified msgdb number)
500         (modb-standard-set-flag-modified msgdb number)))
501     (modb-standard-set-number-list-internal msgdb number-list)
502     (modb-standard-set-entity-map-internal msgdb entity-map)
503     (modb-standard-set-flag-map-internal msgdb flag-map)
504     t))
505
506 (luna-define-method elmo-msgdb-sort-entities ((msgdb modb-standard)
507                                               predicate &optional app-data)
508   (message "Sorting...")
509   (let ((numbers (modb-standard-number-list-internal msgdb)))
510     (modb-standard-set-number-list-internal
511      msgdb
512      (sort numbers (lambda (a b)
513                      (funcall predicate
514                               (elmo-msgdb-message-entity msgdb a)
515                               (elmo-msgdb-message-entity msgdb b)
516                               app-data))))
517     (message "Sorting...done")
518     msgdb))
519
520 (defun modb-standard-message-entity (msgdb key load)
521   (let ((ret (elmo-get-hash-val
522               key
523               (modb-standard-entity-map-internal msgdb))))
524     (if (eq 'autoload (car-safe ret))
525         (when (and load modb-standard-divide-number)
526           (modb-standard-load-entity
527            msgdb
528            (elmo-msgdb-location msgdb)
529            (/ (nth 1 ret) modb-standard-divide-number))
530           (modb-standard-message-entity msgdb key nil))
531       ret)))
532
533 (luna-define-method elmo-msgdb-message-number ((msgdb modb-standard)
534                                                message-id)
535   (let ((ret (elmo-get-hash-val
536               message-id
537               (modb-standard-entity-map-internal msgdb))))
538     (if (eq 'autoload (car-safe ret))
539         ;; Not loaded yet but can return number.
540         (nth 1 ret)
541       (elmo-message-entity-number ret))))
542
543 (luna-define-method elmo-msgdb-message-field ((msgdb modb-standard)
544                                               number field)
545   (let ((ret (elmo-get-hash-val
546               (modb-standard-key number)
547               (modb-standard-entity-map-internal msgdb))))
548     (if (and (eq 'autoload (car-safe ret)) (eq field 'message-id))
549         ;; Not loaded yet but can return message-id
550         (cdr (cdr ret))
551       (elmo-message-entity-field (elmo-msgdb-message-entity
552                                   msgdb (modb-standard-key number))
553                                  field))))
554
555 (luna-define-method elmo-msgdb-message-entity ((msgdb modb-standard) key)
556   (when key
557     (modb-standard-message-entity
558      msgdb
559      (cond ((stringp key) key)
560            ((numberp key) (modb-standard-key key)))
561      'autoload)))
562
563 (require 'product)
564 (product-provide (provide 'modb-standard) (require 'elmo-version))
565
566 ;;; modb-standard.el ends here