Module: Persistence::Adapter::FlatFile::Cursor::CursorInterface

Includes:
PathHelpers, Serialization
Included in:
Persistence::Adapter::FlatFile::Cursor
Defined in:
lib/persistence/adapter/flat_file/cursor/cursor_interface.rb

Instance Method Summary collapse

Instance Method Details

#adapter_classObject

adapter_class #



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

def adapter_class
  
  return @parent_bucket.parent_adapter.class
  
end

#closeObject

close #



49
50
51
52
53
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 49

def close
  
  # nothing required

end

#currentObject

current should return the current ID or object hash



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 132

def current

  current = nil

  if @current_position
    current = open_read_unserialize_and_close( File.join( @key_to_id_directory, 
                                                          @current_position.peek ) )
  end

  return current

end

#current_keyObject

current should return the current ID or object hash



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 150

def current_key

  current_key = nil
  
  if @parent_index
    encoded_key_as_file_name = @current_position.peek
    encoded_key = encoded_key_as_file_name.split( '.' )[ 0 ]
    current_key = file__key_from_path_encoded_name( encoded_key )
  else
    current_key = current
  end

  return current_key
  
end

#firstObject

first should set the cursor position and return the first ID or object hash



119
120
121
122
123
124
125
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 119

def first
  
  @current_position.rewind

  return current
  
end

#initialize(parent_bucket_instance, parent_index_instance = nil) ⇒ Object

initialize #



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 11

def initialize( parent_bucket_instance, parent_index_instance = nil )

  @parent_bucket = parent_bucket_instance
  @parent_index = parent_index_instance

  # we're iterating a directory of files named after their key_digest and containing their global ID
  @key_to_id_directory = ( @parent_index ? @parent_index.instance_eval { directory__index }
                                         : @parent_bucket.instance_eval { directory__ids_in_bucket } )
  
  # instantiate enumerator to track our current position
  init_current_position
  
end

#nextObject

next #



170
171
172
173
174
175
176
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 170

def next

  @current_position.next

  return current

end

#persisted?(*args) ⇒ Boolean

persisted? is responsible for setting the cursor position

Returns:

  • (Boolean)


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

def persisted?( *args )

  has_key = false
  no_key  = false
  case args.count
  when 1
    key = args[0]
  when 0
    no_key = true
  end
  
  # if we have no args we are asking whether any keys exist
  if no_key

    init_current_position

    has_key = true unless ::Dir[ @key_to_id_directory ].empty?
    
  else

    # check if we have a position that currently points to key - if so we are done
    if has_key = ( current_key == key )
      return has_key
    end

    init_current_position

    # Find location of key in Directory enumerator.
    # It seems we can't use Dir#seek because there is no direct way to find the index from a file path.
    # Please prove me wrong if you can!
    begin

      until has_key = ( current_key == key )
        self.next
      end
    rescue StopIteration
    end

  end
  
  return has_key

end

#supports_bucket_order?Boolean

supports_bucket_order? #

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 29

def supports_bucket_order?
  
  return true
  
end

#supports_index_order?Boolean

supports_index_order? #

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/persistence/adapter/flat_file/cursor/cursor_interface.rb', line 39

def supports_index_order?
  
  return false

end