Module: Persistence::Adapter::FlatFile::Bucket::Index::IndexInterface

Includes:
PathHelpers, Serialization
Included in:
Persistence::Adapter::FlatFile::Bucket::Index
Defined in:
lib/persistence/adapter/flat_file/bucket/index/index_interface.rb

Instance Method Summary collapse

Instance Method Details

#adapter_classObject

adapter_class #



25
26
27
28
29
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 25

def adapter_class
  
  return @parent_bucket.parent_adapter.class
  
end

#countObject

count #



35
36
37
38
39
40
41
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 35

def count
  
  glob_list = Dir.glob( File.join( directory__index, '*' ) )

  return glob_list.count
  
end

#cursorObject

cursor #



71
72
73
74
75
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 71

def cursor

  return ::Persistence::Adapter::FlatFile::Cursor.new( @parent_bucket, self )

end

#deleteObject

delete #



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 47

def delete

  directories = [ directory__index,
                  directory__reverse_index ]

  # delete index data
  directories.each do |this_directory|

    # delete all indexed contents
    Dir[ File.join( this_directory, '*' ) ].each do |this_file|
      File.delete( this_file )
    end

    # delete index
    Dir.delete( this_directory )

  end

end

#delete_keys_for_object_id!(global_id) ⇒ Object

delete_keys_for_object_id! #



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 163

def delete_keys_for_object_id!( global_id )

  key = open_read_unserialize_and_close( file__reverse_index_keys_for_global_id( @parent_bucket.name, @name, global_id ) )

  files = [ file__reverse_index_keys_for_global_id( @parent_bucket.name, @name, global_id ),
            file__index( key ) ]

  files.each do |this_file|
    File.delete( this_file )
  end

  return self

end

#directory__indexObject

directory__index #



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 182

def directory__index

  directory__index = File.join( @parent_bucket.parent_adapter.home_directory,
                                'indexes',
                                @parent_bucket.name.to_s,
                                @name.to_s )

  ensure_directory_path_exists( directory__index )

  return directory__index

end

#directory__reverse_indexObject

Bucket Index IDs: <home_directory>/global_ids/bucket/



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 200

def directory__reverse_index

  directory__reverse_index = File.join( @parent_bucket.parent_adapter.home_directory,
                                        'indexes',
                                        'reverse_index',
                                        @parent_bucket.name.to_s,
                                        @name.to_s )

  ensure_directory_path_exists( directory__reverse_index )

  return directory__reverse_index

end

#file__index(key) ⇒ Object

Global ID: <home_directory>/indexes/bucket/file__path_encoded_name_from_key.ruby_serialize.txt



231
232
233
234
235
236
237
238
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 231

def file__index( key )

  key = key.to_s if key.is_a?( Class )

  return File.join( directory__index,
                    file__path_encoded_name_from_key( key ) + adapter_class::SerializationExtension )

end

#file__reverse_index_keys_for_global_id(bucket_name, index_name, global_id) ⇒ Object

Global IDs: <home_directory>/global_ids/bucket/file__path_encoded_name_from_key.ruby_serialize.txt



219
220
221
222
223
224
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 219

def file__reverse_index_keys_for_global_id( bucket_name, index_name, global_id )

  return File.join( directory__reverse_index,
                    global_id.to_s + adapter_class::SerializationExtension )

end

#get_object_id(key) ⇒ Object

get_object_id #



91
92
93
94
95
96
97
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 91

def get_object_id( key )

  file__id_for_key = file__index( key )
  
  return open_read_unserialize_and_close( file__id_for_key )

end

#index_object_id(global_id, value) ⇒ Object

index_object_id #



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 103

def index_object_id( global_id, value )

  file_path = file__index( value )

  if permits_duplicates?

    if File.exists?( file_path )
      
      open_read_unserialize_perform_action_serialize_write_and_close( file_path ) do |ids|
        ids.push( global_id ).sort.uniq
      end
      
    else

      # index value => ID
      create_or_update_value_serialize_and_write( file_path, [ global_id ] )

    end
          
  else
    
    if File.exists?( file_path )
      
      # check to make sure that we do not already have an index value with a different ID
      open_read_unserialize_perform_action_serialize_write_and_close( file_path ) do |id|

        # If we already have a file for this index value and it holds this object's id,
        # then we don't need to do anything.
        # Otherwise we have a duplicate key in a unique index, which is a problem.
        unless id == global_id
          raise ::Persistence::Exception::DuplicateViolatesUniqueIndex.new( 
                'Attempt to create entry in index named :' + @name.to_s + 
                ' would create duplicates in a unique index.' )
        end

      end

    else

      # index value => ID
      create_or_update_value_serialize_and_write( file_path, global_id )

    end
    
  end

  # index ID => value for updating keys if they change
  object_index_location = file__reverse_index_keys_for_global_id( @parent_bucket.name, 
                                                                  @name, 
                                                                  global_id )
  create_or_update_value_serialize_and_write( object_index_location, value )
  
  return self

end

#initialize(index_name, parent_bucket, permits_duplicates) ⇒ Object

initialize #



11
12
13
14
15
16
17
18
19
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 11

def initialize( index_name, parent_bucket, permits_duplicates )

  @name = index_name

  @parent_bucket = parent_bucket

  @permits_duplicates = permits_duplicates
  
end

#permits_duplicates?Boolean

permits_duplicates? #

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/persistence/adapter/flat_file/bucket/index/index_interface.rb', line 81

def permits_duplicates?
  
  return @permits_duplicates

end