Module: Twilic::Core::Model

Defined in:
lib/twilic/core/model.rb

Defined Under Namespace

Classes: BaseRef, BaseSnapshotMessage, Column, ColumnBatchMessage, ControlMessage, ControlOpcode, ControlStreamCodec, ControlStreamMessage, ElementType, ExtMessage, KeyRef, MapEntry, Message, MessageKind, MessageMapEntry, NullStrategy, PatchOpcode, PatchOperation, PromoteEnumControl, RegisterShapeControl, RowBatchMessage, Schema, SchemaField, SchemaObjectMessage, ShapedObjectMessage, StatePatchMessage, StringMode, StringValue, TemplateBatchMessage, TemplateDescriptor, TypedVector, TypedVectorData, Value, ValueKind, VectorCodec

Constant Summary collapse

EMPTY_MESSAGE_FIELDS =
{
  scalar: nil, array: nil, map: nil, shaped_object: nil, schema_object: nil,
  typed_vector: nil, row_batch: nil, column_batch: nil, control: nil, ext: nil,
  state_patch: nil, template_batch: nil, control_stream: nil, base_snapshot: nil
}.freeze

Class Method Summary collapse

Class Method Details

.array_value(items) ⇒ Object



381
382
383
384
# File 'lib/twilic/core/model.rb', line 381

def array_value(items)
  Value.new(kind: ValueKind::ARRAY, bool: false, i64: 0, u64: 0, f64: 0.0,
            str: "", bin: +"", arr: items.map(&:clone_value), map: [])
end

.binary_value(b) ⇒ Object



376
377
378
379
# File 'lib/twilic/core/model.rb', line 376

def binary_value(b)
  Value.new(kind: ValueKind::BINARY, bool: false, i64: 0, u64: 0, f64: 0.0,
            str: "", bin: b.b.dup, arr: [], map: [])
end

.bool_value(b) ⇒ Object



351
352
353
354
# File 'lib/twilic/core/model.rb', line 351

def bool_value(b)
  Value.new(kind: ValueKind::BOOL, bool: b, i64: 0, u64: 0, f64: 0.0,
            str: "", bin: +"", arr: [], map: [])
end

.clone_column(c) ⇒ Object



464
465
466
467
468
469
470
471
472
# File 'lib/twilic/core/model.rb', line 464

def clone_column(c)
  pres = c.has_presence ? c.presence.dup : nil
  dict_id = c.dictionary_id
  Column.new(
    field_id: c.field_id, null_strategy: c.null_strategy, presence: pres,
    has_presence: c.has_presence, codec: c.codec, dictionary_id: dict_id,
    values: clone_typed_vector_data(c.values)
  )
end

.clone_control(c) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/twilic/core/model.rb', line 482

def clone_control(c)
  return nil unless c

  rs = if c.register_shape
         RegisterShapeControl.new(
           shape_id: c.register_shape.shape_id,
           keys: c.register_shape.keys.dup
         )
       end
  pe = if c.promote_string_field_to_enum
         PromoteEnumControl.new(
           field_identity: c.promote_string_field_to_enum.field_identity,
           values: c.promote_string_field_to_enum.values.dup
         )
       end
  ControlMessage.new(
    register_keys: c.register_keys.dup, register_shape: rs,
    register_strings: c.register_strings.dup,
    promote_string_field_to_enum: pe, reset_tables: c.reset_tables,
    reset_state: c.reset_state, opcode: c.opcode
  )
end

.clone_typed_vector(tv) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/twilic/core/model.rb', line 428

def clone_typed_vector(tv)
  return nil unless tv

  data = tv.data
  out_data = case tv.element_type
             when ElementType::BOOL
               TypedVectorData.new(bools: data.bools.dup, i64s: [], u64s: [], f64s: [],
                                   strings: [], binary: [], values: [], kind: tv.element_type)
             when ElementType::I64
               TypedVectorData.new(bools: [], i64s: data.i64s.dup, u64s: [], f64s: [],
                                   strings: [], binary: [], values: [], kind: tv.element_type)
             when ElementType::U64
               TypedVectorData.new(bools: [], i64s: [], u64s: data.u64s.dup, f64s: [],
                                   strings: [], binary: [], values: [], kind: tv.element_type)
             when ElementType::F64
               TypedVectorData.new(bools: [], i64s: [], u64s: [], f64s: data.f64s.dup,
                                   strings: [], binary: [], values: [], kind: tv.element_type)
             when ElementType::STRING
               TypedVectorData.new(bools: [], i64s: [], u64s: [], f64s: [],
                                   strings: data.strings.dup, binary: [], values: [],
                                   kind: tv.element_type)
             when ElementType::BINARY
               TypedVectorData.new(bools: [], i64s: [], u64s: [], f64s: [],
                                   strings: [], binary: data.binary.map(&:b), values: [],
                                   kind: tv.element_type)
             when ElementType::VALUE
               TypedVectorData.new(bools: [], i64s: [], u64s: [], f64s: [],
                                   strings: [], binary: [],
                                   values: data.values.map(&:clone_value), kind: tv.element_type)
             else
               TypedVectorData.new(bools: [], i64s: [], u64s: [], f64s: [],
                                   strings: [], binary: [], values: [], kind: tv.element_type)
             end
  TypedVector.new(element_type: tv.element_type, codec: tv.codec, data: out_data)
end

.clone_typed_vector_data(d) ⇒ Object



474
475
476
477
478
479
480
# File 'lib/twilic/core/model.rb', line 474

def clone_typed_vector_data(d)
  TypedVectorData.new(
    bools: d.bools.dup, i64s: d.i64s.dup, u64s: d.u64s.dup, f64s: d.f64s.dup,
    strings: d.strings.dup, binary: d.binary.map(&:b), values: d.values.map(&:clone_value),
    kind: d.kind
  )
end

.entry(key, value) ⇒ Object



386
387
388
# File 'lib/twilic/core/model.rb', line 386

def entry(key, value)
  MapEntry.new(key, value)
end

.equal(a, b) ⇒ Object



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 'lib/twilic/core/model.rb', line 402

def equal(a, b)
  return false unless a.kind == b.kind

  case a.kind
  when ValueKind::NULL then true
  when ValueKind::BOOL then a.bool == b.bool
  when ValueKind::I64 then a.i64 == b.i64
  when ValueKind::U64 then a.u64 == b.u64
  when ValueKind::F64 then a.f64 == b.f64
  when ValueKind::STRING then a.str == b.str
  when ValueKind::BINARY then a.bin == b.bin
  when ValueKind::ARRAY
    return false unless a.arr.length == b.arr.length

    a.arr.each_with_index.all? { |v, i| equal(v, b.arr[i]) }
  when ValueKind::MAP
    return false unless a.map.length == b.map.length

    a.map.each_with_index.all? do |e, i|
      e.key == b.map[i].key && equal(e.value, b.map[i].value)
    end
  else
    false
  end
end

.f64_value(n) ⇒ Object



366
367
368
369
# File 'lib/twilic/core/model.rb', line 366

def f64_value(n)
  Value.new(kind: ValueKind::F64, bool: false, i64: 0, u64: 0, f64: n,
            str: "", bin: +"", arr: [], map: [])
end

.i64_value(n) ⇒ Object



356
357
358
359
# File 'lib/twilic/core/model.rb', line 356

def i64_value(n)
  Value.new(kind: ValueKind::I64, bool: false, i64: n, u64: 0, f64: 0.0,
            str: "", bin: +"", arr: [], map: [])
end

.map_value(entries = nil, **kwargs) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
# File 'lib/twilic/core/model.rb', line 390

def map_value(entries = nil, **kwargs)
  if kwargs.any?
    entries = kwargs.map { |k, v| MapEntry.new(k.to_s, v) }
  elsif entries.is_a?(Hash)
    entries = entries.map { |k, v| MapEntry.new(k.to_s, v) }
  end
  entries ||= []
  Value.new(kind: ValueKind::MAP, bool: false, i64: 0, u64: 0, f64: 0.0,
            str: "", bin: +"", arr: [],
            map: entries.map { |e| MapEntry.new(e.key, e.value.clone_value) })
end

.message(kind:, **kwargs) ⇒ Object



340
341
342
# File 'lib/twilic/core/model.rb', line 340

def self.message(kind:, **kwargs)
  Message.new(**EMPTY_MESSAGE_FIELDS, kind: kind, **kwargs)
end

.null_valueObject



346
347
348
349
# File 'lib/twilic/core/model.rb', line 346

def null_value
  Value.new(kind: ValueKind::NULL, bool: false, i64: 0, u64: 0, f64: 0.0,
            str: "", bin: +"", arr: [], map: [])
end

.string_value(s) ⇒ Object



371
372
373
374
# File 'lib/twilic/core/model.rb', line 371

def string_value(s)
  Value.new(kind: ValueKind::STRING, bool: false, i64: 0, u64: 0, f64: 0.0,
            str: s, bin: +"", arr: [], map: [])
end

.u64_value(n) ⇒ Object



361
362
363
364
# File 'lib/twilic/core/model.rb', line 361

def u64_value(n)
  Value.new(kind: ValueKind::U64, bool: false, i64: 0, u64: n, f64: 0.0,
            str: "", bin: +"", arr: [], map: [])
end