Skip to content

Commit c0cadb1

Browse files
committed
✨ Add Set/Range/Enum/etc methods to SequenceSet
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)
1 parent 8bb86c2 commit c0cadb1

File tree

7 files changed

+2157
-48
lines changed

7 files changed

+2157
-48
lines changed

lib/net/imap/response_data.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,19 @@ class ThreadMember < Struct.new(:seqno, :children)
763763
#
764764
# An array of Net::IMAP::ThreadMember objects for mail items that are
765765
# children of this in the thread.
766+
767+
# Returns a SequenceSet containing #seqno and all #children's seqno,
768+
# recursively.
769+
def to_sequence_set
770+
SequenceSet.new all_seqnos
771+
end
772+
773+
protected
774+
775+
def all_seqnos(node = self)
776+
[node.seqno].concat node.children.flat_map { _1.all_seqnos }
777+
end
778+
766779
end
767780

768781
# Net::IMAP::BodyStructure is included by all of the structs that can be

lib/net/imap/response_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def unescape_quoted(quoted)
464464
def sequence_set
465465
str = combine_adjacent(*SEQUENCE_SET_TOKENS)
466466
if Patterns::SEQUENCE_SET_STR.match?(str)
467-
SequenceSet.new(str)
467+
SequenceSet[str]
468468
else
469469
parse_error("unexpected atom %p, expected sequence-set", str)
470470
end

lib/net/imap/sequence_set.rb

Lines changed: 1318 additions & 36 deletions
Large diffs are not rendered by default.

test/net/imap/fixtures/response_parser/status_responses.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@
5151
NUM: 1
5252
SEQ: !ruby/struct:Net::IMAP::ExtensionData
5353
data: !ruby/object:Net::IMAP::SequenceSet
54-
atom: 1234:5,*:789654
54+
string: 1234:5,*:789654
55+
tuples:
56+
- - 5
57+
- 1234
58+
- - 789654
59+
- 4294967296
5560
COMP-EMPTY: !ruby/struct:Net::IMAP::ExtensionData
5661
data: []
5762
COMP-QUOTED: !ruby/struct:Net::IMAP::ExtensionData

test/net/imap/test_imap.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -591,42 +591,60 @@ def test_send_invalid_number
591591
sock = server.accept
592592
begin
593593
sock.print("* OK test server\r\n")
594-
sock.gets
594+
sock.gets # Integer: 0
595595
sock.print("RUBY0001 OK TEST completed\r\n")
596-
sock.gets
596+
sock.gets # Integer: 2**32 - 1
597597
sock.print("RUBY0002 OK TEST completed\r\n")
598-
sock.gets
598+
sock.gets # MessageSet: 1
599599
sock.print("RUBY0003 OK TEST completed\r\n")
600-
sock.gets
600+
sock.gets # MessageSet: 2**32 - 1
601601
sock.print("RUBY0004 OK TEST completed\r\n")
602-
sock.gets
602+
sock.gets # SequenceSet: -1 => "*"
603+
sock.print("RUBY0005 OK TEST completed\r\n")
604+
sock.gets # SequenceSet: 1
605+
sock.print("RUBY0006 OK TEST completed\r\n")
606+
sock.gets # SequenceSet: 2**32 - 1
607+
sock.print("RUBY0007 OK TEST completed\r\n")
608+
sock.gets # LOGOUT
603609
sock.print("* BYE terminating connection\r\n")
604-
sock.print("RUBY0005 OK LOGOUT completed\r\n")
610+
sock.print("RUBY0008 OK LOGOUT completed\r\n")
605611
ensure
606612
sock.close
607613
server.close
608614
end
609615
end
610616
begin
617+
# regular numbers may be any uint32
611618
imap = Net::IMAP.new(server_addr, :port => port)
612619
assert_raise(Net::IMAP::DataFormatError) do
613620
imap.__send__(:send_command, "TEST", -1)
614621
end
615622
imap.__send__(:send_command, "TEST", 0)
616-
imap.__send__(:send_command, "TEST", 4294967295)
623+
imap.__send__(:send_command, "TEST", 2**32 - 1)
617624
assert_raise(Net::IMAP::DataFormatError) do
618-
imap.__send__(:send_command, "TEST", 4294967296)
625+
imap.__send__(:send_command, "TEST", 2**32)
619626
end
627+
# MessageSet numbers may be non-zero uint32
620628
assert_raise(Net::IMAP::DataFormatError) do
621629
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(-1))
622630
end
623631
assert_raise(Net::IMAP::DataFormatError) do
624632
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(0))
625633
end
626634
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(1))
627-
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967295))
635+
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(2**32 - 1))
636+
assert_raise(Net::IMAP::DataFormatError) do
637+
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(2**32))
638+
end
639+
# SequenceSet numbers may be non-zero uint3, and -1 is translated to *
640+
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(-1))
641+
assert_raise(Net::IMAP::DataFormatError) do
642+
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(0))
643+
end
644+
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(1))
645+
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(2**32-1))
628646
assert_raise(Net::IMAP::DataFormatError) do
629-
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967296))
647+
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(2**32))
630648
end
631649
imap.logout
632650
ensure

test/net/imap/test_imap_response_data.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,23 @@ def test_uidplus_copyuid__uid_mapping
3535
)
3636
end
3737

38+
def test_thread_member_to_sequence_set
39+
# copied from the fourth example in RFC5256: (3 6 (4 23)(44 7 96))
40+
thmember = Net::IMAP::ThreadMember.method :new
41+
thread = thmember.(3, [
42+
thmember.(6, [
43+
thmember.(4, [
44+
thmember.(23, [])
45+
]),
46+
thmember.(44, [
47+
thmember.(7, [
48+
thmember.(96, [])
49+
])
50+
])
51+
])
52+
])
53+
expected = Net::IMAP::SequenceSet.new("3:4,6:7,23,44,96")
54+
assert_equal(expected, thread.to_sequence_set)
55+
end
56+
3857
end

0 commit comments

Comments
 (0)