Module: Persistence::Port::Bucket::BucketInterface

Includes:
Enumerable, Object::Flat::File::FilePersistence
Included in:
Persistence::Port::Bucket
Defined in:
lib/persistence/port/bucket/bucket_interface.rb

Overview

Interface for Bucket implementation. Provided separately for easy overriding.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Object::Flat::File::FilePersistence

#persist_files_by_content!, #persist_files_by_path!

Instance Attribute Details

#nameSymbol, String

Returns Name.

Returns:

  • (Symbol, String)

    Name.



98
99
100
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 98

def name
  @name
end

#Name of bucket(ofbucket) ⇒ Symbol, String (readonly)

Returns Name.

Returns:

  • (Symbol, String)

    Name.



98
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 98

attr_accessor :name

#parent_portObject

parent_port #



104
105
106
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 104

def parent_port
  @parent_port
end

Instance Method Details

#adapter_bucketObject

Retrieve parallel adapter bucket instance.

Returns:

  • (Object)

    Adapter bucket instance.



79
80
81
82
83
84
85
86
87
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 79

def adapter_bucket
  
  unless @adapter_bucket
    raise 'Persistence port must be enabled first.'
  end
  
  return @adapter_bucket
  
end

#atomic_cursor(*args) ⇒ Persistence::Cursor

Create and return cursor instance for this index enabled to load all object properties as atomic.

See Persistence::Cursor#atomize.

Returns:



593
594
595
596
597
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 593

def atomic_cursor( *args )
  
  return cursor( *args ).atomize

end

#count(*args, &count_block) ⇒ Integer

Get the number of objects in index. See Enumerable.

Returns:

  • (Integer)

    Number of objects in bucket.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 231

def count( *args, & count_block ) 

  return_value = 0
  
  if block_given?
    return_value = super( & count_block )
  elsif args.empty?
    return_value = adapter_bucket.count
  else
    return_value = super( *args )
  end

  return return_value

end

#create_index(index_name, sort_by_proc = nil, &indexing_block) ⇒ Object

Create a bucket index, which is a block index that runs on each inserted object, regardless of type.



134
135
136
137
138
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 134

def create_index( index_name, sort_by_proc = nil, & indexing_block )
  
  return create_bucket_index( index_name, false, sort_by_proc, & indexing_block )
  
end

#create_index_with_duplicates(index_name, sort_by_proc = nil, sort_duplicates_by_proc = nil, &indexing_block) ⇒ Object

Create a bucket index that permits duplicates.



147
148
149
150
151
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 147

def create_index_with_duplicates( index_name, sort_by_proc = nil, sort_duplicates_by_proc = nil, & indexing_block )

  return create_bucket_index( index_name, true, sort_by_proc, sort_duplicates_by_proc, & indexing_block )
  
end

#cursor(*args, &block) ⇒ Persistence::Cursor

Create and return cursor instance for this bucket.

Returns:



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 566

def cursor( *args, & block )
  
  cursor_instance = ::Persistence::Cursor.new( self )
  
  if args.count > 0
    cursor_instance.persisted?( *args )
  end
  
  if block_given?
    cursor_instance = cursor_instance.instance_eval( & block )
    cursor_instance.close
  end
  
  return cursor_instance

end

#delete_attribute!(object, attribute_name) ⇒ Object

Delete attribute for object from port.

Parameters:

  • object

    Object that owns indexed entries.

  • attribute_name

    Name of attribute.



474
475
476
477
478
479
480
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 474

def delete_attribute!( object, attribute_name )

  primary_key = primary_key_for_attribute_name( object, attribute_name )

  return adapter_bucket.delete_attribute!( object, primary_key )

end

#delete_index_for_object(object, index_name) ⇒ Object

Delete indexed entries for object.

Parameters:

  • object

    Object that owns indexed entries.

  • index_name

    Name of index.



260
261
262
263
264
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 260

def delete_index_for_object( object, index_name )

  return delete_index_for_object_id( object.persistence_id )

end

#delete_index_for_object_id(global_id, index_name) ⇒ Object

Delete indexed entries for object.

Parameters:

  • global_id

    Object persistence ID that owns indexed entries.

  • index_name

    Name of index.



279
280
281
282
283
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 279

def delete_index_for_object_id( global_id, index_name )

  return adapter_bucket.delete_index_for_object_id( global_id )

end

#delete_object!(global_id) ⇒ Hash

Delete object from persistence port.

Parameters:

  • global_id

    Object persistence ID to delete from port.

Returns:

  • (Hash)

    Persistence hash from port.



455
456
457
458
459
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 455

def delete_object!( global_id )
  
  return adapter_bucket.delete_object!( global_id )

end

#disableObject

Disable bucket when port disables. Internal helper method necessary to ensure that old references don’t stick around.



116
117
118
119
120
121
122
123
124
125
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 116

def disable

  @adapter_bucket = nil
  @parent_port = nil
  
  @indexes.each do |this_index_name, this_index|
    this_index.disable
  end
  
end

#each {|object| ... } ⇒ Object

Iterate objects in current bucket.

Yields:

  • (object)

    Current object.

Yield Parameters:

  • object

    Object stored in bucket.



610
611
612
613
614
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 610

def each( & block )
  
  return atomic_cursor.each( & block )

end

#get_attribute(object, attribute_name) ⇒ Object

Get attribute for object from port.

Parameters:

  • object

    Object that owns indexed entries.

  • attribute_name

    Name of attribute.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 298

def get_attribute( object, attribute_name )
  
  primary_key = primary_key_for_attribute_name( object, attribute_name )

  attribute_value = adapter_bucket.get_attribute( object, primary_key )

  if attribute_value.is_a?( ::Persistence::Object::Complex::ComplexObject )
    attribute_value.persistence_port = object.persistence_port
    attribute_value = attribute_value.persist
  end
  
  return attribute_value

end

#get_flat_object(global_id) ⇒ Object

Get flat object from persistence port.

Parameters:

  • global_id

    Object persistence ID to persist from port.

Returns:

  • (Object)

    Persisted object instance.



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 426

def get_flat_object( global_id )

  flat_object = nil

  if persistence_value_hash = adapter_bucket.get_object( global_id )

    if persistence_value_key_data = persistence_value_hash.first
      flat_object = persistence_value_key_data[ 1 ]
      flat_object.persistence_id = global_id
    end
  end

  return flat_object
  
end

#get_object(global_id) ⇒ Object

Get object from persistence port.

Parameters:

  • global_id

    Object persistence ID to persist from port.

Returns:

  • (Object)

    Persisted object instance.



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 366

def get_object( global_id )
  
  object = nil
  
  # get the class
  if klass = @parent_port.get_class_for_object_id( global_id )

    object = klass.new
    
    object.persistence_id = global_id

    # if we have non-atomic attributes, load them
    unless klass.non_atomic_attribute_readers.empty?

      if persistence_hash_from_port = get_object_hash( global_id )
        
        object.load_persistence_hash( object.persistence_port, persistence_hash_from_port )

      end

    end

  end
  
  return object

end

#get_object_hash(global_id) ⇒ Hash

Get object properties from persistence port.

Parameters:

  • global_id

    Object persistence ID to persist from port.

Returns:

  • (Hash)

    Persistence hash from port.



407
408
409
410
411
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 407

def get_object_hash( global_id )

  return adapter_bucket.get_object( global_id )

end

#has_attribute_index?(index_name, ...) ⇒ true, false

Query whether attribute index(es) exist for object.

Parameters:

  • index_name

    Name of requested index.

Returns:

  • (true, false)

    Whether index(es) exist.



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 210

def has_index?( *indexes )
  
  has_index = false
  
  indexes.each do |this_index|
    break unless has_index = @indexes.has_key?( index_name )
  end
  
  return has_index
  
end

#index(index_name, ensure_exists = false) ⇒ Persistence::Object::Index

Retrieve index.

Parameters:

  • index_name (Symbol, String)

    Name of index

  • ensure_exists (true, false) (defaults to: false)

    Whether exception should be thrown if index does not exist.

Returns:



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 183

def index( index_name, ensure_exists = false )
  
  index_instance = nil
  
  unless index_instance = @indexes[ index_name ]
    if ensure_exists
      raise ::ArgumentError, 'No index found by name ' << index_name.to_s + '.'
    end
  end
  
  return index_instance
  
end

#initialize(parent_port, bucket_name) ⇒ Object

Parameters:

  • parent_port

    Port where this bucket is active. Can be nil if no port is yet enabled.

  • bucket_name

    Name of the bucket to open in port.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 21

def initialize( parent_port, bucket_name )

  @name = bucket_name

  @indexes = { }
  @pending_indexes = [ ]

  if parent_port
    initialize_for_port( parent_port )
  end
  
end

#initialize_for_port(port) ⇒ Object

Initialize bucket for a port that has been enabled.

Parameters:

  • port

    Parent port in which to initialize bucket.



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
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 43

def initialize_for_port( port )

  if port = ::Persistence.port_for_name_or_port( port ) and port.enabled?
  
    @parent_port = ::Persistence.port_for_name_or_port( port )
  
    if @parent_port.enabled?
      @adapter_bucket = @parent_port.adapter.persistence_bucket( @name )
    end

    @indexes.each do |this_index_name, this_index_instance|
      this_index_instance.initialize_for_bucket( self )
    end
  
    @pending_indexes.delete_if do |this_pending_index|
      this_pending_index.initialize_for_bucket( self )
      true
    end
  
  else
    
    disable
    
  end
  
end

#pend_index(index_instance) ⇒ Object

Pend index instance for port to be enabled.

Parameters:

  • index_instance

    Instance of index that is waiting for port to be enabled.



164
165
166
167
168
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 164

def pend_index( index_instance )
  
  @pending_indexes.push( index_instance )
  
end

#persists_file_paths_as_strings?Boolean

persists_file_paths_as_strings? #

Returns:

  • (Boolean)


543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 543

def persists_file_paths_as_strings?
  
  persists_file_paths_as_strings = nil
  
  persists_file_paths_as_strings = super
  
  if persists_file_paths_as_strings.nil?
    persists_file_paths_as_strings = parent_port.persists_file_paths_as_strings?
  end
  
  return persists_file_paths_as_strings
  
end

#persists_files_by_content?Boolean

persists_files_by_content? #

Returns:

  • (Boolean)


507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 507

def persists_files_by_content?
  
  persists_files_by_content = nil
  
  persists_files_by_content = super
  
  if persists_files_by_content.nil?
    persists_files_by_content = parent_port.persists_files_by_content?
  end
  
  return persists_files_by_content
  
end

#persists_files_by_path?Boolean

persists_files_by_path? #

Returns:

  • (Boolean)


525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 525

def persists_files_by_path?
  
  persists_files_by_path = nil
  
  persists_files_by_path = super
  
  if persists_files_by_path.nil?
    persists_files_by_path = parent_port.persists_files_by_path?
  end
  
  return persists_files_by_path
  
end

#primary_key_for_attribute_name(object, attribute_name) ⇒ String

Generate primary key for storage for attribute name on object.

Parameters:

  • object

    Object for which attribute is being stored.

  • attribute_name

    Name of attribute.

Returns:

  • (String)

    Primary key.



497
498
499
500
501
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 497

def primary_key_for_attribute_name( object, attribute_name )

  return adapter_bucket.primary_key_for_attribute_name( object, attribute_name )

end

#put_attribute!(object, attribute_name, attribute_value) ⇒ Object

Put attribute for object to port.

Parameters:

  • object

    Object that owns indexed entries.

  • attribute_name

    Name of attribute.

  • attribute_value

    Value to put.



328
329
330
331
332
333
334
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 328

def put_attribute!( object, attribute_name, attribute_value )
  
  primary_key = primary_key_for_attribute_name( object, attribute_name )
  
  return adapter_bucket.put_attribute!( object, primary_key, attribute_value )
  
end

#put_object!(object) ⇒ Object

Put object properties to persistence port.

Parameters:

  • object

    Object to persist to port.



347
348
349
350
351
# File 'lib/persistence/port/bucket/bucket_interface.rb', line 347

def put_object!( object )    

  return adapter_bucket.put_object!( object )

end