Class: ActiveRecordJsonStreamer::Streamer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_json_streamer/streamer.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
1000
DEFAULT_LIMIT =
50_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, batch_size: DEFAULT_BATCH_SIZE, limit: DEFAULT_LIMIT, cursor_column: :created_at, primary_key: :id, order: :desc, &field_mapper) ⇒ Streamer

Returns a new instance of Streamer.



12
13
14
15
16
17
18
19
20
# File 'lib/active_record_json_streamer/streamer.rb', line 12

def initialize(relation, batch_size: DEFAULT_BATCH_SIZE, limit: DEFAULT_LIMIT, cursor_column: :created_at, primary_key: :id, order: :desc, &field_mapper)
  @relation = relation
  @batch_size = batch_size
  @limit = limit
  @cursor_column = cursor_column
  @primary_key = primary_key
  @order = order.to_sym
  @field_mapper = field_mapper
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def batch_size
  @batch_size
end

#cursor_columnObject (readonly)

Returns the value of attribute cursor_column.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def cursor_column
  @cursor_column
end

#field_mapperObject (readonly)

Returns the value of attribute field_mapper.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def field_mapper
  @field_mapper
end

#limitObject (readonly)

Returns the value of attribute limit.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def limit
  @limit
end

#orderObject (readonly)

Returns the value of attribute order.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def order
  @order
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def primary_key
  @primary_key
end

#relationObject (readonly)

Returns the value of attribute relation.



10
11
12
# File 'lib/active_record_json_streamer/streamer.rb', line 10

def relation
  @relation
end

Instance Method Details

#enumeratorObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_record_json_streamer/streamer.rb', line 22

def enumerator
  Enumerator.new do |yielder|
    yielder << "["
    first = true
    emitted = 0

    col = relation.connection.quote_column_name(cursor_column)
    pk = relation.connection.quote_column_name(primary_key)
    tbl = relation.connection.quote_table_name(relation.table_name)
    col_ref = "#{tbl}.#{col}"
    pk_ref = "#{tbl}.#{pk}"

    current_relation = relation.order(cursor_column => order, primary_key => order)

    loop do
      remaining = limit - emitted if limit
      break if remaining && remaining <= 0

      fetch_size = remaining ? [ batch_size, remaining ].min : batch_size
      batch = current_relation.limit(fetch_size).to_a
      break if batch.empty?

      batch.each do |record|
        yielder << "," unless first
        first = false
        data = field_mapper ? field_mapper.call(record) : record.as_json
        yielder << JSON.generate(data)
        emitted += 1
      end

      break if batch.size < fetch_size

      last = batch.last
      last_cursor = last.public_send(cursor_column)
      last_pk = last.public_send(primary_key)

      if order == :desc
        current_relation = relation.where(
          "(#{col_ref} < :ca) OR (#{col_ref} = :ca AND #{pk_ref} < :pk)",
          ca: last_cursor, pk: last_pk
        ).order(cursor_column => :desc, primary_key => :desc)
      else
        current_relation = relation.where(
          "(#{col_ref} > :ca) OR (#{col_ref} = :ca AND #{pk_ref} > :pk)",
          ca: last_cursor, pk: last_pk
        ).order(cursor_column => :asc, primary_key => :asc)
      end
    end

    yielder << "]"
  end
end