Class: Redstruct::SortedSet::Slice

Inherits:
Factory::Object show all
Defined in:
lib/redstruct/sorted_set/slice.rb

Overview

Utility class to allow operations on portions of the set only TODO: Support #length property (using LIMIT offset count) of the different range commands, so a slice could be defined as starting at offset X and having length Y, instead of just starting at X and finishing at Y.

Instance Attribute Summary collapse

Attributes inherited from Factory::Object

#factory

Instance Method Summary collapse

Methods inherited from Factory::Object

#connection

Methods included from Utils::Inspectable

#inspect

Constructor Details

#initialize(set, lower: nil, upper: nil, lex: false, exclusive: false) ⇒ Slice

Returns a new instance of Slice.

Parameters:

  • lower (String, Float) (defaults to: nil)

    lower bound for the slice operation

  • upper (String, Float) (defaults to: nil)

    upper bound for the slice operation

  • lex (Boolean) (defaults to: false)

    if true, uses lexicographic operations

  • exclusive (Boolean) (defaults to: false)

    if true, assumes bounds are exclusive



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redstruct/sorted_set/slice.rb', line 29

def initialize(set, lower: nil, upper: nil, lex: false, exclusive: false)
  super(factory: set.factory)

  @key = set.key
  @lex = lex
  @exclusive = exclusive

  lower ||= -Float::INFINITY
  upper ||= Float::INFINITY

  if @lex
    @lower = parse_lex_bound(lower)
    @upper = parse_lex_bound(upper)
  else
    @lower = parse_bound(lower)
    @upper = parse_bound(upper)
  end
end

Instance Attribute Details

#exclusiveBoolean (readonly)

Returns if true, assumes the range bounds are exclusive.

Returns:

  • (Boolean)

    if true, assumes the range bounds are exclusive



23
24
25
# File 'lib/redstruct/sorted_set/slice.rb', line 23

def exclusive
  @exclusive
end

#keyString (readonly)

Returns the key for the underlying sorted set.

Returns:

  • (String)

    the key for the underlying sorted set



11
12
13
# File 'lib/redstruct/sorted_set/slice.rb', line 11

def key
  @key
end

#lexBoolean (readonly)

Returns if true, then assumes the slice is lexicographically sorted.

Returns:

  • (Boolean)

    if true, then assumes the slice is lexicographically sorted



20
21
22
# File 'lib/redstruct/sorted_set/slice.rb', line 20

def lex
  @lex
end

#lowerString, Float (readonly)

Returns the lower bound of the slice.

Returns:

  • (String, Float)

    the lower bound of the slice



14
15
16
# File 'lib/redstruct/sorted_set/slice.rb', line 14

def lower
  @lower
end

#upperString, Float (readonly)

Returns the upper bound of the slice.

Returns:

  • (String, Float)

    the upper bound of the slice



17
18
19
# File 'lib/redstruct/sorted_set/slice.rb', line 17

def upper
  @upper
end

Instance Method Details

#inspectable_attributesObject



90
91
92
# File 'lib/redstruct/sorted_set/slice.rb', line 90

def inspectable_attributes
  { lower: @lower, upper: @upper, lex: @lex, exclusive: @exclusive, key: @key }
end

#removeInteger

Returns the number of elements removed.

Returns:

  • (Integer)

    the number of elements removed



67
68
69
70
71
72
73
# File 'lib/redstruct/sorted_set/slice.rb', line 67

def remove
  if @lex
    self.connection.zremrangebylex(@key, @lower, @upper)
  else
    self.connection.zremrangebyscore(@key, @lower, @upper)
  end
end

#reverseArray<String>

Returns an array of values reversed

Returns:

  • (Array<String>)

    returns an array of values reversed



58
59
60
61
62
63
64
# File 'lib/redstruct/sorted_set/slice.rb', line 58

def reverse
  if @lex
    self.connection.zrevrangebylex(@key, @lower, @upper)
  else
    self.connection.zrevrangebyscore(@key, @lower, @upper)
  end
end

#sizeInteger

Returns number of elements in the slice.

Returns:

  • (Integer)

    number of elements in the slice



76
77
78
79
80
81
82
# File 'lib/redstruct/sorted_set/slice.rb', line 76

def size
  if @lex
    self.connection.zlexcount(@key, @lower, @upper)
  else
    self.connection.zcount(@key, @lower, @upper)
  end
end

#to_aArray<String>

Returns an array of values for the given bounds

Returns:

  • (Array<String>)

    returns an array of values for the given bounds



49
50
51
52
53
54
55
# File 'lib/redstruct/sorted_set/slice.rb', line 49

def to_a
  if @lex
    self.connection.zrangebylex(@key, @lower, @upper)
  else
    self.connection.zrangebyscore(@key, @lower, @upper)
  end
end

#to_set::Set

TODO: consider using SortedSet, some other data structure, or nothing

Returns:

  • (::Set)

    an unordered set representation of the slice



86
87
88
# File 'lib/redstruct/sorted_set/slice.rb', line 86

def to_set
  ::Set.new(to_a)
end