Skip to content

Commit

Permalink
✨ Add Set/Range/Enum/etc methods to SequenceSet
Browse files Browse the repository at this point in the history
The version of SequenceSet in net-imap prior to this commit was merely a
placeholder, needed in order to complete `tagged-ext` for #225.

This updates it with a full API, inspired by Set, Range, and Array.
This allows it to be more broadly useful, e.g. for storing and working
with mailbox state.

In addition to Integer, Range, and enumerables, any object with
`#to_sequence_set` can now be used to create a sequence set.  For
compatibility with MessageSet, `ThreadMember#to_sequence_set` collects
all child seqno into a SequenceSet.

Because mailbox state can be _very_ large, inputs are stored in an
internal sorted array of ranges.  These are stored as `[start, stop]`
tuples, not Range objects, for simpler manipulation.  A future
optimization could convert all tuples to a flat one-dimensional Array
(to reduce object allocations).  Storing the data in sorted range tuples
allows many of the important operations to be `O(lg n)`.

Although updates do use `Array#insert` and `Array#slice!`—which are
technically `O(n)`—they tend to be fast until the number of elements is
very large.  Count and index-based methods are also `O(n)`.  A future
optimization could cache the count and compose larger sets from a sorted
tree of smaller sets, to preserve `O(lg n)` for most operations.

SequenceSet can be used to replace MessageSet (which is used internally
to validate, format, and send certain command args).  Some notable
differences between the two:
* Most validation is done up-front, when initializing or adding values.
* A ThreadMember to `sequence-set` bug has been fixed.
* The generated string is sorted and adjacent ranges are combined.

TODO in future PRs:
* #index_lte => get the index of a number in the set, or if the number
  isn't in the set, the number before it.
* Replace or supplement the UID set implementation in UIDPlusData.
* fully replace MessageSet (probably not before v0.5.0)
  • Loading branch information
nevans committed Dec 11, 2023
1 parent 8bb86c2 commit 8a7604f
Show file tree
Hide file tree
Showing 7 changed files with 2,155 additions and 48 deletions.
13 changes: 13 additions & 0 deletions lib/net/imap/response_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,19 @@ class ThreadMember < Struct.new(:seqno, :children)
#
# An array of Net::IMAP::ThreadMember objects for mail items that are
# children of this in the thread.

# Returns a SequenceSet containing #seqno and all #children's seqno,
# recursively.
def to_sequence_set
SequenceSet.new all_seqnos
end

protected

def all_seqnos(node = self)
[node.seqno].concat node.children.flat_map { _1.all_seqnos }
end

end

# Net::IMAP::BodyStructure is included by all of the structs that can be
Expand Down
2 changes: 1 addition & 1 deletion lib/net/imap/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def unescape_quoted(quoted)
def sequence_set
str = combine_adjacent(*SEQUENCE_SET_TOKENS)
if Patterns::SEQUENCE_SET_STR.match?(str)
SequenceSet.new(str)
SequenceSet[str]
else
parse_error("unexpected atom %p, expected sequence-set", str)
end
Expand Down
Loading

0 comments on commit 8a7604f

Please sign in to comment.