Class: Foobara::Persistence::EntityBase::TransactionTable

Inherits:
Object
  • Object
show all
Includes:
Concerns::Queries, Concerns::RecordTracking
Defined in:
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/queries.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb

Overview

TODO: move this under Transaction

Defined Under Namespace

Modules: Concerns Classes: NoRecordFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction, entity_class) ⇒ TransactionTable

Returns a new instance of TransactionTable.



25
26
27
28
29
30
31
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 25

def initialize(transaction, entity_class)
  self.transaction = transaction
  self.entity_class = entity_class
  self.entity_attributes_crud_driver_table = transaction.entity_attributes_crud_driver.table_for(entity_class)

  super()
end

Instance Attribute Details

#entity_attributes_crud_driver_tableObject

Returns the value of attribute entity_attributes_crud_driver_table.



21
22
23
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 21

def entity_attributes_crud_driver_table
  @entity_attributes_crud_driver_table
end

#entity_classObject

Returns the value of attribute entity_class.



21
22
23
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 21

def entity_class
  @entity_class
end

#transactionObject

Returns the value of attribute transaction.



21
22
23
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 21

def transaction
  @transaction
end

Instance Method Details

#all_exist?(record_ids) ⇒ Boolean

Returns:

  • (Boolean)


470
471
472
473
474
475
476
477
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 470

def all_exist?(record_ids)
  to_check_ids = record_ids.reject do |record_id|
    record = tracked_records.find_by_key(record_id)
    record && !record.hard_deleted? && (!record.persisted? || record.loaded?)
  end

  to_check_ids.empty? || entity_attributes_crud_driver_table.all_exist?(to_check_ids)
end

#countObject



456
457
458
459
460
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 456

def count
  persisted_count = entity_attributes_crud_driver_table.count

  persisted_count + marked_created.count - marked_hard_deleted.count
end

#exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


462
463
464
465
466
467
468
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 462

def exists?(record_id)
  record = tracked_records.find_by_key(record_id)
  # TODO: stamp the fact that it exists on the record somehow or in the record_tracking concern.

  (record && !record.hard_deleted? && (!record.persisted? || record.loaded?)) ||
    entity_attributes_crud_driver_table.exists?(record_id)
end

#find_all_by_attribute(attribute_name_or_path, value) ⇒ Object



228
229
230
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 228

def find_all_by_attribute(attribute_name_or_path, value)
  find_many_by(attribute_name_or_path => value)
end

#find_all_by_attribute_any_of(attribute_name, values) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 384

def find_all_by_attribute_any_of(attribute_name, values)
  values = Util.array(values)
  return [] if values.empty?

  value_type = entity_class.attributes_type.element_types[attribute_name]

  values = values.map do |value|
    value_type.process_value!(value)
  end

  yielded_ids = Set.new

  Enumerator.new do |yielder|
    tracked_records.each do |record|
      next if hard_deleted?(record)

      record_value = record.read_attribute(attribute_name)

      # TODO: what if there are multiple??
      if values.include?(record_value)
        yielded_ids << record.primary_key
        yielder << record
      end
    end

    values = values.map do |value|
      to_persistable(value, false)
    end

    entity_attributes_crud_driver_table.find_all_by_attribute_any_of(
      attribute_name, values
    ).each do |found_attributes|
      found_attributes = normalize_attributes(found_attributes)
      record_id = primary_key_for_attributes(found_attributes)

      next if yielded_ids.include?(record_id)

      record = find_tracked(record_id) || entity_class.loaded(found_attributes)

      yielder << record
    end
  end
end

#find_all_by_attribute_containing_any_of(attribute_name, values) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 336

def find_all_by_attribute_containing_any_of(attribute_name, values)
  values = Util.array(values)
  return [] if values.empty?

  value_type = entity_class.attributes_type.element_types[attribute_name].element_type

  values = values.map do |value|
    value_type.process_value!(value)
  end

  yielded_ids = Set.new

  Enumerator.new do |yielder|
    tracked_records.each do |record|
      next if hard_deleted?(record)

      record_values = record.read_attribute(attribute_name)
      next unless record_values
      next if record_values.empty?

      # TODO: what if there are multiple??
      values.each do |value|
        if record_values.include?(value)
          yielded_ids << record.primary_key
          yielder << record
        end
      end
    end

    values = values.map do |value|
      to_persistable(value, false)
    end

    entity_attributes_crud_driver_table.find_all_by_attribute_containing_any_of(
      attribute_name, values
    ).each do |found_attributes|
      found_attributes = normalize_attributes(found_attributes)
      record_id = primary_key_for_attributes(found_attributes)

      next if yielded_ids.include?(record_id)

      record = find_tracked(record_id) || entity_class.loaded(found_attributes)

      yielder << record
    end
  end
end

#find_by(attributes_filter) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 254

def find_by(attributes_filter)
  element_types = entity_class.attributes_type.element_types

  attributes_filter = attributes_filter.to_h do |attribute_name, value|
    [attribute_name,  element_types[attribute_name].process_value!(value)]
  end

  tracked_records.each do |record|
    next if hard_deleted?(record)

    if entity_attributes_crud_driver_table.matches_attributes_filter?(record.attributes, attributes_filter)
      return record
    end
  end

  found_attributes = entity_attributes_crud_driver_table.find_by(attributes_filter)

  if found_attributes
    found_attributes = normalize_attributes(found_attributes)
    record_id = primary_key_for_attributes(found_attributes)

    record = find_tracked(record_id)
    return record if record

    entity_class.loaded(found_attributes)
  end
end

#find_by_attribute(attribute_name, value) ⇒ Object



224
225
226
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 224

def find_by_attribute(attribute_name, value)
  find_by(attribute_name => value)
end

#find_by_attribute_containing(attribute_name, value) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 232

def find_by_attribute_containing(attribute_name, value)
  value = entity_class.attributes_type.element_types[attribute_name].element_type.process_value!(value)

  tracked_records.each do |record|
    next if hard_deleted?(record)

    # TODO: what if there are multiple??
    if record.read_attribute(attribute_name).include?(value)
      return record
    end
  end

  found_attributes = entity_attributes_crud_driver_table.find_by_attribute_containing(
    attribute_name,
    to_persistable(value, false)
  )

  if found_attributes
    entity_class.loaded(normalize_attributes(found_attributes))
  end
end

#find_many_by(attributes_filter) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 282

def find_many_by(attributes_filter)
  find_by_type = entity_class.domain.foobara_type_from_declaration(entity_class.attributes_for_find_by)

  path_filters = {}
  regular_filters = {}

  attributes_filter.each_pair do |attribute_name_or_path, value|
    case attribute_name_or_path
    when ::Symbol, ::String
      regular_filters[attribute_name_or_path] = value
    when ::Array, Value::DataPath
      path_filters[attribute_name_or_path] = value
    else
      # :nocov:
      raise "Unexpected filter type: #{attribute_name_or_path.class}"
      # :nocov:
    end
  end

  regular_filters = find_by_type.process_value!(regular_filters)

  path_filters.keys.each do |path|
    type = entity_class.deep_associations[DataPath.for(path).to_s]
    path_filters[path] = type.process_value!(path_filters[path])
  end

  attributes_filter = regular_filters.merge(path_filters)

  yielded_ids = Set.new

  Enumerator.new do |yielder|
    tracked_records.each do |record|
      next if hard_deleted?(record)
      next unless record.loaded? || record.built? || record.created?

      if entity_attributes_crud_driver_table.matches_attributes_filter?(record.attributes, attributes_filter)
        yielded_ids << record.primary_key
        yielder << record
      end
    end

    entity_attributes_crud_driver_table.find_many_by(attributes_filter).each do |found_attributes|
      found_attributes = normalize_attributes(found_attributes)
      record_id = primary_key_for_attributes(found_attributes)

      next if yielded_ids.include?(record_id)

      record = find_tracked(record_id) || entity_class.loaded(found_attributes)

      yielder << record
    end
  end
end

#find_tracked(record_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 33

def find_tracked(record_id)
  unless record_id
    # :nocov:
    raise ArgumentError, "Cannot use a blank primary key value"
    # :nocov:
  end

  if record_id.is_a?(::String) && record_id.empty?
    # :nocov:
    raise ArgumentError, "Cannot use a blank primary key value"
    # :nocov:
  end

  if record_id.is_a?(::Symbol) && record_id.to_s.empty?
    # :nocov:
    raise ArgumentError, "Cannot use a blank primary key value"
    # :nocov:
  end

  tracked_records.find_by_key(record_id)
end

#firstObject



55
56
57
58
59
60
61
62
63
64
65
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 55

def first
  found_attributes = normalize_attributes(entity_attributes_crud_driver_table.first)

  if found_attributes
    record_id = primary_key_for_attributes(found_attributes)

    record = tracked_records.find_by_key(record_id) || transaction.loaded(entity_class, found_attributes)

    record || marked_created.first
  end
end

#flush_created!Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 520

def flush_created!
  marked_created.each do |record|
    tracked_records.delete(record)

    flush_created_associations!(record)

    # TODO: do this in bulk
    attributes = entity_attributes_crud_driver_table.insert(to_persistable(record))
    record.write_attributes_without_callbacks(attributes)

    # we need to update finding the tracked object by key and removing/reading it seems to be the simplest
    # way to accomplish that at the moment
    tracked_records << record

    record.is_persisted = record.is_loaded = true
    record.is_created = false
    record.save_persisted_attributes
  end

  marked_created.clear
end

#flush_created_associations!(record) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 561

def flush_created_associations!(record)
  entity_class.associations.each_key do |association_data_path|
    DataPath.values_at(association_data_path, record).each do |associated_record|
      next unless associated_record.created?

      transaction = Persistence::EntityBase::Transaction.open_transaction_for(associated_record)

      unless transaction
        # :nocov:
        raise "No open transaction for #{associated_record}"
        # :nocov:
      end

      if transaction.created?(associated_record)
        transaction.flush_created_record!(associated_record)
      end
    end
  end
end

#flush_created_record!(record) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 542

def flush_created_record!(record)
  tracked_records.delete(record)

  flush_created_associations!(record)

  unmark_created(record)

  attributes = entity_attributes_crud_driver_table.insert(to_persistable(record))
  record.write_attributes_without_callbacks(attributes)

  # we need to update finding the tracked object by key and removing/reading it seems to be the simplest
  # way to accomplish that at the moment
  tracked_records << record

  record.is_persisted = record.is_loaded = true
  record.is_created = false
  record.save_persisted_attributes
end

#flush_updated_and_hard_deleted!Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 581

def flush_updated_and_hard_deleted!
  # TODO: use bulk operations to improve performance...
  marked_updated.each do |record|
    attributes = entity_attributes_crud_driver_table.update(to_persistable(record))
    record.write_attributes_without_callbacks(attributes)
    record.save_persisted_attributes
  end

  marked_updated.clear

  # TODO: use bulk operations to improve performance...
  marked_hard_deleted.each do |record|
    entity_attributes_crud_driver_table.hard_delete(record.primary_key)
    record.save_persisted_attributes
  end

  marked_hard_deleted.clear
end

#hard_delete_all!Object



446
447
448
449
450
451
452
453
454
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 446

def hard_delete_all!
  tracked_records.each do |record|
    record.hard_delete! unless record.hard_deleted?
  end

  entity_attributes_crud_driver_table.hard_delete_all

  all_hard_deleted
end

#load(entity_or_record_id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 67

def load(entity_or_record_id)
  if entity_or_record_id.is_a?(Entity)
    if entity_or_record_id.loaded?
      # :nocov:
      raise "#{entity_or_record_id} is already loaded!"
      # :nocov:
    end

    entity = tracked_records[entity_or_record_id]

    if entity && !entity.equal?(entity_or_record_id)
      # :nocov:
      raise "This transaction is already tracking a different entity with the same primary key." \
            "Try passing in the primary key instead of constructing an unloaded entity to pass in."
      # :nocov:
    end

    record_id = entity.primary_key
  else
    record_id = entity_or_record_id

    if record_id.is_a?(::Hash)
      # :nocov:
      raise ArgumentError, "Unlikely that you meant to use a hash as a primary key"
      # :nocov:
    end

    unless record_id
      # :nocov:
      raise ArgumentError, "Cannot use a blank primary key value"
      # :nocov:
    end

    if record_id.is_a?(::String) && record_id.empty?
      # :nocov:
      raise ArgumentError, "Cannot use a blank primary key value"
      # :nocov:
    end

    if record_id.is_a?(::Symbol) && record_id.to_s.empty?
      # :nocov:
      raise ArgumentError, "Cannot use a blank primary key value"
      # :nocov:
    end

    entity = tracked_records.find_by_key(record_id)

    if entity&.loaded?
      return entity
    end
  end

  if entity
    loading(entity) do
      attributes = normalize_attributes(entity_attributes_crud_driver_table.find!(record_id))

      unless attributes
        # :nocov:
        raise NoRecordFound, "could not find record for #{entity_class.full_entity_name}:#{record_id}"
        # :nocov:
      end

      entity.successfully_loaded(attributes)
    end
  else
    attributes = normalize_attributes(entity_attributes_crud_driver_table.find!(record_id))

    unless attributes
      # :nocov:
      raise NoRecordFound, "could not find record for #{entity_class.full_entity_name}:#{record_id}"
      # :nocov:
    end

    transaction.loaded(entity_class, attributes)
  end
end

#load_many(record_ids_or_entities) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 144

def load_many(record_ids_or_entities)
  to_load_record_ids = []
  entities = {}

  record_ids_or_entities.each do |entity_or_record_id|
    if entity_or_record_id.is_a?(Entity)
      if entity_or_record_id.loaded?
        next
      end

      entity = tracked_records[entity_or_record_id]

      if entity && !entity.equal?(entity_or_record_id)
        # :nocov:
        raise "This transaction is already tracking a different entity with the same primary key." \
              "Try passing in the primary key instead of constructing an unloaded entity to pass in."
        # :nocov:
      end

      record_id = entity.primary_key
      to_load_record_ids << record_id
      entities[record_id] = entity
    else
      record_id = entity_or_record_id

      if record_id.is_a?(::Hash)
        # :nocov:
        raise ArgumentError, "Unlikely that you meant to use a hash as a primary key"
        # :nocov:
      end

      # TODO: encapsulate this record_id verification
      unless record_id
        # :nocov:
        raise ArgumentError, "Cannot use a blank primary key value"
        # :nocov:
      end

      if record_id.is_a?(::String) && record_id.empty?
        # :nocov:
        raise ArgumentError, "Cannot use a blank primary key value"
        # :nocov:
      end

      if record_id.is_a?(::Symbol) && record_id.to_s.empty?
        # :nocov:
        raise ArgumentError, "Cannot use a blank primary key value"
        # :nocov:
      end

      entity = tracked_records.find_by_key(record_id)

      if entity
        if entity.loaded?
          entities[record_id] = entity
        end
      else
        entities[record_id] = entity_class.thunk(record_id)
        to_load_record_ids << record_id
      end
    end
  end

  entity_attributes_crud_driver_table.find_many!(to_load_record_ids).each do |attributes|
    attributes = normalize_attributes(attributes)
    record_id = primary_key_for_attributes(attributes)
    entity = entities[record_id]
    entity.successfully_loaded(attributes)
    entities[record_id] = entity
  end

  record_ids_or_entities.map do |record_id_or_entity|
    if record_id_or_entity.is_a?(Entity)
      record_id_or_entity
    else
      entities[record_id_or_entity]
    end
  end
end

#primary_key_for_attributes(attributes) ⇒ Object



479
480
481
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 479

def primary_key_for_attributes(attributes)
  attributes[entity_class.primary_key_attribute]
end

#revert!Object



612
613
614
615
616
617
618
619
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 612

def revert!
  # TODO: could pause record tracking while doing this as a performance boost
  marked_updated.each(&:restore_without_callbacks!)
  marked_hard_deleted.each(&:restore_without_callbacks!)
  marked_created.each(&:restore_without_callbacks!)

  reverted
end

#rollback!Object



600
601
602
603
604
605
606
607
608
609
610
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 600

def rollback!
  # TODO: could pause record tracking while doing this as a performance boost
  # is it really safe to do this without callbacks?? What about other systems listening
  # to those callbacks? This feels wrong.
  # TODO: fix this
  marked_updated.each(&:restore_without_callbacks!)
  marked_hard_deleted.each(&:restore_without_callbacks!)
  marked_created.each(&:hard_delete_without_callbacks!)

  rolled_back
end

#to_persistable(object, initial = true) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 483

def to_persistable(object, initial = true)
  case object
  when Entity
    if initial
      to_persistable(object.attributes, false)
    else
      object.primary_key
    end
  when Model
    to_persistable(object.attributes, false)
  when ::Hash
    object.transform_values do |value|
      to_persistable(value, false)
    end
  when ::Array
    object.map do |element|
      to_persistable(element, false)
    end
  else
    object
  end
end

#track_created(entity) ⇒ Object



432
433
434
435
436
437
438
439
440
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 432

def track_created(entity)
  if entity.persisted?
    # :nocov:
    raise "Cannot insert #{entity} because it's already persisted."
    # :nocov:
  end

  created(entity)
end

#track_loaded(entity) ⇒ Object



442
443
444
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 442

def track_loaded(entity)
  tracked_records << entity
end

#track_unloaded_thunk(record) ⇒ Object



428
429
430
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 428

def track_unloaded_thunk(record)
  tracked(record)
end

#tracking?(record) ⇒ Boolean

Returns:

  • (Boolean)


621
622
623
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 621

def tracking?(record)
  tracked_records.include?(record)
end

#validate!Object



506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb', line 506

def validate!
  marked_created.each do |record|
    unless record.valid?
      raise InvalidRecordError, record
    end
  end

  marked_updated.each do |record|
    unless record.valid?
      raise InvalidRecordError, record
    end
  end
end