Module: Persistence::Cursor::CursorInterface

Includes:
Enumerable
Included in:
Persistence::Cursor
Defined in:
lib/persistence/cursor/cursor_interface.rb

Overview

Interface implementation for Cursor class instances.

Instance Method Summary collapse

Instance Method Details

#any(any_count = 1) ⇒ Object+

Persist any object in cursor context.

Parameters:

  • any_count (Integer) (defaults to: 1)

    How many objects to persist from cursor context.

Returns:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/persistence/cursor/cursor_interface.rb', line 247

def any( any_count = 1 )
  
  object = nil
  
  if total_count = count
    begin
      @adapter_cursor.first
      objects = [ ]
      any_count.times do
        skip_records = rand( total_count - ( any_count + 1 ) )
        skip_records.times do
          @adapter_cursor.next
        end
        objects.push( self.next )
      end
      objects = objects[ 0 ] if objects.count <= 1
      # reset to first
      @adapter_cursor.first
    rescue StopIteration
    end      
  end
  
  return objects
  
end

#atomizeObject

Enable cursor as atomic cursor, causing objects to be loaded with atomic properties,

regardless how they are configured.

Returns:

  • self



59
60
61
62
63
64
65
# File 'lib/persistence/cursor/cursor_interface.rb', line 59

def atomize
  
  extend( ::Persistence::Cursor::Atomic )
  
  return self
  
end

#closeObject

Declare cursor use complete.

Returns:

  • self



41
42
43
44
45
46
47
# File 'lib/persistence/cursor/cursor_interface.rb', line 41

def close
  
  @adapter_cursor.close
  
  return self
  
end

#currentObject+

Persist current object in cursor context.

Returns:



282
283
284
285
286
287
288
# File 'lib/persistence/cursor/cursor_interface.rb', line 282

def current

  first unless @has_position

  return get_object( @adapter_cursor.current )

end

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

Iterate objects in current cursor context.

Yields:

  • (object)

    Current object for cursor context.

Yield Parameters:

  • object

    Object stored in cursor context.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/persistence/cursor/cursor_interface.rb', line 140

def each

  # we have to set position if it's not already set before we can iterate
  first unless @has_position

  return to_enum unless block_given?

  # support for Enumerator#feed - permit a return value
  feed_value = nil

  begin
    while yield_object = self.next
      feed_value = yield( yield_object )
    end
  rescue StopIteration
  end

  return feed_value
  
end

#first(first_count = 1) ⇒ Object+

Persist first object in cursor context.

Parameters:

  • first_count (Integer) (defaults to: 1)

    How many objects to persist from start of cursor context.

Returns:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/persistence/cursor/cursor_interface.rb', line 172

def first( first_count = 1 )

  objects = nil

  if @has_position = persisted?
    begin
      if first_count == 1
        objects = current
      else
        objects = [ ]
        first_count.times do
          objects.push( self.next )
        end
      end
      # reset to first
      @adapter_cursor.first
    rescue StopIteration
    end
  end

  return objects

end

#get_object(global_id) ⇒ Hash{String,Symbol=>Object}

Get persistence hash for object with specified persistence ID.

Parameters:

  • global_id

    Object persistence ID for retrieval.

Returns:

  • (Hash{String,Symbol=>Object})

    Persistence hash of object properties for persistence ID.



331
332
333
334
335
# File 'lib/persistence/cursor/cursor_interface.rb', line 331

def get_object( global_id )

  return @persistence_bucket.get_object( global_id )

end

#initialize(bucket_instance, index_instance = nil) ⇒ Object

Parameters:

  • bucket_instance

    Bucket to use for cursor context.

  • index_instance (defaults to: nil)

    Index to use for cursor context.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/persistence/cursor/cursor_interface.rb', line 18

def initialize( bucket_instance, index_instance = nil )

  @persistence_bucket = bucket_instance
  @parent_index = index_instance
  
  # use persistence port, bucket, index, value to instantiate adapter cursor
  if index_instance
    @adapter_cursor = index_instance.adapter_index.cursor
  else
    @adapter_cursor = bucket_instance.adapter_bucket.cursor
  end
  
end

#last(last_count = 1) ⇒ Object+

Persist last object in cursor context.

Parameters:

  • last_count (Integer) (defaults to: 1)

    How many objects to persist from end of cursor context.

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/persistence/cursor/cursor_interface.rb', line 207

def last( last_count = 1 )
  
  objects = nil
  
  if total_count = count
    begin
      first
      skip_records = total_count - last_count
      skip_records.times do
        @adapter_cursor.next
      end
      if last_count == 1
        objects = self.next
      else
        objects = [ ]
        last_count.times do
          objects.push( self.next )
        end
      end
      # reset to first
      @adapter_cursor.first
    rescue StopIteration
    end
  end
  
  return objects
  
end

#next(count = 1) ⇒ Object

Return the next object in cursor’s current context.

Returns:

  • (Object)

    Next object in cursor’s current context.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/persistence/cursor/cursor_interface.rb', line 299

def next( count = 1 )

  objects = nil

  if count > 1

    objects = [ ]
    count.times do
      objects.push( get_object( @adapter_cursor.next ) )
    end

  else

    objects = get_object( @adapter_cursor.next )

  end

  return objects

end

#persist(global_id) ⇒ Object

Load object with specified persistence ID.

Parameters:

  • global_id

    Object persistence ID for retrieval.

Returns:

  • (Object)

    Object for persistence ID.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/persistence/cursor/cursor_interface.rb', line 117

def persist( global_id )

  object = nil

  if @has_position = @adapter_cursor.persisted?( global_id )
    object = get_object( @adapter_cursor.current )
  end

  return object

end

#persisted?(key, ...) ⇒ true, false

Query whether keys are persisted in cursor’s current context.

Parameters:

  • Key (Object)

    to look up.

Returns:

  • (true, false)

    Whether key(s) exist in cursor’s current context.



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
# File 'lib/persistence/cursor/cursor_interface.rb', line 80

def persisted?( *args )

  # [ global_id, ... ]

  persisted = false

  # args => global_id, ...
  # positions on last id or false if one of ids is not persisted
  if args.count > 0

    args.each do |this_global_id|
      break unless persisted = @adapter_cursor.persisted?( this_global_id )
    end

  # no args
  # positions on first if keys or false if no keys
  else

    persisted = @adapter_cursor.persisted?

  end

  return @has_position = persisted

end