@@ -435,8 +435,30 @@ let search mbx (keys:('a)States.searchKeys) (buid:bool) :
435
435
let (module Mailbox ) = storage_factory mbx name () in
436
436
Mailbox.MailboxStorage. search_with Mailbox. this ~filter: (buid,keys) >> = fun acc -> return (`Ok acc)
437
437
438
+ let get_smallest_uid buid accs =
439
+ if buid = false then
440
+ return (Some 1 )
441
+ else
442
+ let (module Accessor : StorageAccessor_inst ) = accs in
443
+ Accessor.StorageAccessor. reader_metadata Accessor. this (`Position 1 ) >> = function
444
+ | `Eof -> return None
445
+ | `Ok metadata -> return (Some metadata.uid)
446
+
447
+ let get_iterator mbx name accs buid sequence =
448
+ let (module Mailbox ) = storage_factory mbx name () in
449
+ (if Iterator.SequenceIterator. single sequence then
450
+ return Int. max_value
451
+ else
452
+ Mailbox.MailboxStorage. get_mailbox_metadata Mailbox. this >> = fun metadata ->
453
+ if buid then return (metadata.uidnext-1 ) else return metadata.count
454
+ ) >> = fun max ->
455
+ get_smallest_uid buid accs >> = function
456
+ | None -> return None
457
+ | Some min -> return (Some (Iterator.SequenceIterator. create sequence min max))
458
+
438
459
let fetch (mbx :t ) (resp_writer :(string->unit) ) (sequence :States.sequence ) (fetchattr :States.fetch )
439
460
(buid :bool ) : [`NotExists|`NotSelectable|`Error of string|`Ok ] Deferred.t =
461
+ let open Interpreter in
440
462
match (selected_mbox mbx) with
441
463
| None -> return (`Error " Not selected" )
442
464
| Some name ->
@@ -445,21 +467,31 @@ let fetch (mbx:t) (resp_writer:(string->unit)) (sequence:States.sequence) (fetch
445
467
| `NotSelectable -> return (`NotSelectable )
446
468
| `ValidMailbox ->
447
469
let (module Mailbox ) = storage_factory mbx name () in
448
- Mailbox.MailboxStorage. fold Mailbox. this ~exclusive: true ~init: () ~f: (fun () accs ->
449
- let rec doread accs seq =
450
- let (module Accessor : StorageAccessor_inst ) = accs in
451
- let filter = Some (States. Key (States. Search_SeqSet sequence)) in
452
- Accessor.StorageAccessor. reader Accessor. this ?filter (`Position seq) >> = function
470
+ Mailbox.MailboxStorage. fold Mailbox. this ~exclusive: true
471
+ ~init: ()
472
+ ~f: (fun () accs ->
473
+ (* should add the smallest uid to the the index header TBD
474
+ *)
475
+ get_iterator mbx name accs buid sequence >> = function
476
+ | None -> return ()
477
+ | Some it ->
478
+ let (module Accessor : StorageAccessor_inst ) = accs in
479
+ let rec read = function
480
+ | `End -> return ()
481
+ | `Ok seq ->
482
+ let pos = if buid then (`UID seq) else (`Position seq) in
483
+ Accessor.StorageAccessor. reader Accessor. this pos >> = function
453
484
| `Eof -> return ()
454
- | `NotFound -> doread accs (seq + 1 )
485
+ | `NotFound -> read ( Iterator.SequenceIterator. next it )
455
486
| `Ok (message ,metadata ) ->
456
487
printf " ============= %s" (Email_message.Mailbox.Message. to_string message);
488
+ (* probably still need this since UID may not be sequential *)
457
489
let res = Interpreter. exec_fetch seq sequence message metadata fetchattr buid in
458
490
match res with
459
- | Some res -> resp_writer res; doread accs (seq + 1 )
460
- | None -> doread accs (seq + 1 )
491
+ | Some res -> resp_writer res; read ( Iterator.SequenceIterator. next it )
492
+ | None -> read ( Iterator.SequenceIterator. next it )
461
493
in
462
- doread accs 1
494
+ read ( Iterator.SequenceIterator. next it)
463
495
) >> = fun () -> return `Ok
464
496
465
497
let store (mbx :t ) (resp_writer :(string->unit) ) (sequence :States.sequence )
@@ -474,23 +506,29 @@ let store (mbx:t) (resp_writer:(string->unit)) (sequence:States.sequence)
474
506
| `ValidMailbox ->
475
507
let (module Mailbox ) = storage_factory mbx name () in
476
508
Mailbox.MailboxStorage. fold Mailbox. this ~exclusive: true ~init: () ~f: (fun () accs ->
477
- let rec doread accs seq =
478
- let (module Accessor : StorageAccessor_inst ) = accs in
479
- Accessor.StorageAccessor. reader_metadata Accessor. this (`Position seq) >> = function
509
+ let (module Accessor : StorageAccessor_inst ) = accs in
510
+ get_iterator mbx name accs buid sequence >> = function
511
+ | None -> return ()
512
+ | Some it ->
513
+ let rec doread = function
514
+ | `End -> return ()
515
+ | `Ok seq ->
516
+ let pos = if buid then (`UID seq) else (`Position seq) in
517
+ Accessor.StorageAccessor. reader_metadata Accessor. this pos >> = function
480
518
| `Eof -> return ()
519
+ | `NotFound -> doread (Iterator.SequenceIterator. next it)
481
520
| `Ok metadata ->
482
521
let update metadata seq =
483
- Accessor.StorageAccessor. writer_metadata Accessor. this metadata
484
- (`Position seq) >> = function
522
+ Accessor.StorageAccessor. writer_metadata Accessor. this metadata pos >> = function
485
523
| `Eof -> return ()
486
- | `Ok -> doread accs (seq + 1 )
524
+ | `NotFound | ` Ok -> doread ( Iterator.SequenceIterator. next it )
487
525
in
488
526
match Interpreter. exec_store metadata seq sequence storeattr flagsval buid with
489
- | `None -> doread accs (seq + 1 )
527
+ | `None -> doread ( Iterator.SequenceIterator. next it )
490
528
| `Silent metadata -> update metadata seq
491
529
| `Ok (metadata ,res ) -> resp_writer res;update metadata seq
492
530
in
493
- doread accs 1
531
+ doread ( Iterator.SequenceIterator. next it)
494
532
) >> = fun () -> return `Ok
495
533
496
534
(* * if copy fails it should restore the mailbox, so need to copy TBD **)
0 commit comments