Class: DiscordStore::Codec::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_store/codec.rb

Overview

Accumulates records until the next one would overflow a message.

Instance Method Summary collapse

Constructor Details

#initialize(budget) ⇒ Batch

Returns a new instance of Batch.



136
137
138
139
140
# File 'lib/discord_store/codec.rb', line 136

def initialize(budget)
  @budget = budget
  @pairs = []
  @size = 0
end

Instance Method Details

#add(record, envelope) ⇒ Object



151
152
153
154
# File 'lib/discord_store/codec.rb', line 151

def add(record, envelope)
  @size += envelope.length + (@pairs.empty? ? 0 : SEPARATOR.length)
  @pairs << [record, envelope]
end

#any? ⇒ Boolean

Returns:

  • (Boolean)


142
# File 'lib/discord_store/codec.rb', line 142

def any? = @pairs.any?

#drain ⇒ Array

Returns the accumulated pairs, resetting the batch.

Returns:

  • (Array) —

    the accumulated pairs, resetting the batch



157
158
159
160
161
162
# File 'lib/discord_store/codec.rb', line 157

def drain
  pairs = @pairs
  @pairs = []
  @size = 0
  pairs
end

#fits?(envelope) ⇒ Boolean

Returns whether envelope still fits alongside what is held.

Returns:

  • (Boolean) —

    whether envelope still fits alongside what is held



145
146
147
148
149
# File 'lib/discord_store/codec.rb', line 145

def fits?(envelope)
  return true if @pairs.empty?

  @size + envelope.length + SEPARATOR.length <= @budget
end