Class: Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjectModel/Repository.rb,
lib/ObjectModel/Repository.rb

Overview

Stream

Constant Summary collapse

INITIALIZATION_SYNC =
Monitor.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = {}) ⇒ Repository

Returns a new instance of Repository.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ObjectModel/Repository.rb', line 7

def initialize name, params = {}
	INITIALIZATION_SYNC.synchronize do								
		super()
		
		config = Dir.getwd + "/config/object_model.yaml"
		
		@name = name.to_s; @name.should_not! :be_nil
		@params = params
		@dir = params[:directory] || CONFIG[:directory]
		@dir.should_not! :be_nil						
		
		check_the_same						
		
		# Disk Storages
		@storage = ObjectStorage.new @name, @dir
		@indexes_storage = Indexes::IndexStorage.new @name, @dir
		@stream_storage = StreamStorage.new @name, @dir, CONFIG[:buffer_size]																							
		
		# Synchronization
		@sync, @entity_loading_sync, @stream_sync = Sync.new, Monitor.new, Monitor.new												
		
		# Listeners
		@entity_listeners = []
		
		# Transaction Strategy
		if params.include? :transaction_strategy
			klass = params[:transaction_strategy].should! :be_a, Class
			@transaction_strategy = klass.new self
		else
			@transaction_strategy = Tools::DefaultTransactionStrategy.new self
		end												
		
		# Cache
		if params.include? :cache
			@entities_cache = params[:cache]
		else
			cache_class_name = CONFIG[:cache]; cache_class_name.should_not! :be_nil
			cache_class = eval cache_class_name, TOPLEVEL_BINDING, __FILE__, __LINE__
			cache_parameters = CONFIG[:cache_parameters]
			@entities_cache = cache_parameters ? cache_class.new(self, cache_parameters) : cache_class.new
		end			
		
		# Indexes
		index_def = params[:indexes] || []
		index_def.should! :be_a, Array
		index_def << Indexes::HashIndex.new(:path){|e| e.path}
		
		@index_manager = Indexes::Manager.new self			
		@indexes_storage.index_manager = @index_manager
		index_def.each{|index| @index_manager.add index}
		build_indexes			
	end
end

Instance Attribute Details

#_index_managerObject (readonly)

Returns the value of attribute _index_manager.



3
4
5
# File 'lib/ObjectModel/Repository.rb', line 3

def _index_manager
  @_index_manager
end

#entity_listenersObject (readonly)

Returns the value of attribute entity_listeners.



3
4
5
# File 'lib/ObjectModel/Repository.rb', line 3

def entity_listeners
  @entity_listeners
end

#indexes_storageObject (readonly)

Returns the value of attribute indexes_storage.



3
4
5
# File 'lib/ObjectModel/Repository.rb', line 3

def indexes_storage
  @indexes_storage
end

#storageObject (readonly)

Returns the value of attribute storage.



3
4
5
# File 'lib/ObjectModel/Repository.rb', line 3

def storage
  @storage
end

#stream_storageObject (readonly)

Returns the value of attribute stream_storage.



3
4
5
# File 'lib/ObjectModel/Repository.rb', line 3

def stream_storage
  @stream_storage
end

Class Method Details

.delete(name, directory = ) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/ObjectModel/Repository.rb', line 234

def delete name, directory = CONFIG[:directory]; 		
	INITIALIZATION_SYNC.synchronize do
		name = name.to_s
		path = File.join(directory, name)
		FileUtils.rm_rf path if File.exist? path
	end				
end

Instance Method Details

#[](path) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/ObjectModel/Repository.rb', line 210

def [] path		
	path = path.to_s if path.is_a? Path
	path.should! :be_a, String
	
	entity_id = index(:path).get_entity_id path
	raise_without_self NotFoundError, "Entity with Path '#{path}' not found!", ObjectModel if entity_id == nil
	
	entity_id.should! :be_a, String
	return by_id entity_id
end

#_commit(tr) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ObjectModel/Repository.rb', line 114

def _commit tr
	processor = TransactionProcessor.new self, tr
	processor.check_outdated			
	tr.event_processor.fire_before_commit
	
	begin				
		@sync.synchronize(:EX) do 
			processor.write_back
			@storage.transaction do
				processor.persist					
				@index_manager.update tr			
			end
			@entities_cache.update tr
		end															
	rescue Exception => e
		close
		log.error "Fatal Error, Repository '#{@name}' can't be used!"
		raise e				
	end						
	
	tr.commited!
	return tr
end

#add_index(index) ⇒ Object



185
186
187
188
189
190
# File 'lib/ObjectModel/Repository.rb', line 185

def add_index index
	@sync.synchronize(:EX) do
		@index_manager.add index
		@index_manager.build_indexes
	end
end

#build_indexesObject



177
178
179
# File 'lib/ObjectModel/Repository.rb', line 177

def build_indexes
	@sync.synchronize(:EX){@index_manager.build_indexes}
end

#by_id(entity_id) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ObjectModel/Repository.rb', line 154

def by_id entity_id		
	@entity_loading_sync.synchronize do
		entity = @entities_cache[entity_id]							
		unless entity
			entity = AnEntity::EntityType.load entity_id, self, @storage
			entity.should_not! :be_nil
			@entities_cache[entity_id] = entity
		end			
		return entity
	end
end

#clearObject



148
149
150
151
152
# File 'lib/ObjectModel/Repository.rb', line 148

def clear
	close
	Repository.delete @name, @dir
	initialize @name, @params
end

#clear_indexesObject



181
182
183
# File 'lib/ObjectModel/Repository.rb', line 181

def clear_indexes
	@sync.synchronize(:EX){@index_manager.clear_indexes}
end

#closeObject



138
139
140
141
142
143
144
145
146
# File 'lib/ObjectModel/Repository.rb', line 138

def close		
	INITIALIZATION_SYNC.synchronize do
		@sync.synchronize(:EX) do
			@storage.close
			@indexes_storage.close
			@@runing.delete @name + @dir
		end
	end
end

#commit(tr) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/ObjectModel/Repository.rb', line 97

def commit tr
	if t = Thread.current[:om_transaction]
		t.should! :equal?, tr
		_commit tr
	else
		transaction(tr){_commit tr}
	end										
end

#delete_index(index_name) ⇒ Object



192
193
194
# File 'lib/ObjectModel/Repository.rb', line 192

def delete_index index_name
	@sync.synchronize(:EX){@index_manager.delete index_name}
end

#each(&b) ⇒ Object



278
279
280
# File 'lib/ObjectModel/Repository.rb', line 278

def each &b
	AnEntity::EntityType.each_entity_in_repository self, &b
end

#get(key) ⇒ Object



110
111
112
# File 'lib/ObjectModel/Repository.rb', line 110

def get key
	storage.get key
end

#include?(path) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
# File 'lib/ObjectModel/Repository.rb', line 200

def include? path
	path = path.to_s if path.is_a? Path
	path.should! :be_a, String
	
	entity_id = index(:path).get_entity_id path
	entity_id.should! :be_a, [String, NilClass]
	
	return entity_id != nil
end

#include_id?(entity_id) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
# File 'lib/ObjectModel/Repository.rb', line 166

def include_id? entity_id
	@entity_loading_sync.synchronize do			
		entity = @entities_cache[entity_id]
		if entity
			return true
		else
			return AnEntity::EntityType.storage_include? entity_id, @storage
		end			
	end
end

#index(name) ⇒ Object



196
197
198
# File 'lib/ObjectModel/Repository.rb', line 196

def index name
	@index_manager[name]
end

#inspectObject



225
226
227
# File 'lib/ObjectModel/Repository.rb', line 225

def inspect
	to_s
end

#isolate(&b) ⇒ Object



61
62
63
64
65
# File 'lib/ObjectModel/Repository.rb', line 61

def isolate &b		
	@sync.synchronize :SH do 
		b.call
	end
end


229
230
231
# File 'lib/ObjectModel/Repository.rb', line 229

def print name = nil
	@storage.print name
end

#put(key, value) ⇒ Object



106
107
108
# File 'lib/ObjectModel/Repository.rb', line 106

def put key, value
	storage.put key, value
end

#sizeObject



274
275
276
# File 'lib/ObjectModel/Repository.rb', line 274

def size
	@storage.size		
end

#stream_collect_garbage(object_space = ObjectSpace) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/ObjectModel/Repository.rb', line 316

def stream_collect_garbage object_space = ObjectSpace 
	@stream_sync.synchronize do			
		ObjectSpace.garbage_collect
		memory = Set.new
		object_space.each_object(StreamID){|s| memory.add s.sid.to_s}			
		@stream_storage.list_of_ids.each do |id|
			@stream_storage.delete StreamID.new(id) unless memory.include?(id)
		end
	end
end

#stream_metadata_put(id, metadata) ⇒ Object



266
267
268
# File 'lib/ObjectModel/Repository.rb', line 266

def  id, 
	@stream_storage. id, 
end

#stream_metadata_read(id) ⇒ Object



262
263
264
# File 'lib/ObjectModel/Repository.rb', line 262

def  id
	@stream_storage. id
end

#stream_put(data = nil, &block) ⇒ Object



282
283
284
285
286
287
# File 'lib/ObjectModel/Repository.rb', line 282

def stream_put data = nil, &block; 	
	id = nil
	@stream_sync.synchronize{id = StreamID.new(storage.generate(:stream_id))}		
	@stream_storage.stream_put id, data, &block		
	return id
end

#stream_put_each(stream) ⇒ Object



289
290
291
292
293
294
# File 'lib/ObjectModel/Repository.rb', line 289

def stream_put_each stream
	id = nil
	@stream_sync.synchronize{id = StreamID.new(storage.generate(:stream_id))}
	@stream_storage.stream_put_each id, stream
	return id
end

#stream_put_from_file(file_name) ⇒ Object



296
297
298
299
300
# File 'lib/ObjectModel/Repository.rb', line 296

def stream_put_from_file file_name
	File.open file_name, "r" do |f|
		stream_put_each f
	end
end

#stream_read(id, &block) ⇒ Object



308
309
310
# File 'lib/ObjectModel/Repository.rb', line 308

def stream_read id, &block; 
	@stream_storage.stream_read id, &block
end

#stream_read_each(id, &block) ⇒ Object



312
313
314
# File 'lib/ObjectModel/Repository.rb', line 312

def stream_read_each id, &block
	@stream_storage.stream_read_each id, &block
end

#stream_read_to_file(id, file_name) ⇒ Object



302
303
304
305
306
# File 'lib/ObjectModel/Repository.rb', line 302

def stream_read_to_file id, file_name
	File.open file_name, "w" do |to|
		stream_read_each(id){|part| to.write part}
	end
end

#stream_size(id) ⇒ Object



270
271
272
# File 'lib/ObjectModel/Repository.rb', line 270

def stream_size id
	@stream_storage.stream_size id
end

#to_sObject



221
222
223
# File 'lib/ObjectModel/Repository.rb', line 221

def to_s
	"#<Repository: #{@name}>" 
end

#transaction(transaction_or_name = nil, &b) ⇒ Object



67
68
69
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
# File 'lib/ObjectModel/Repository.rb', line 67

def transaction transaction_or_name = nil, &b
	Thread.current[:om_transaction].should! :be_nil
	
	tr = if transaction_or_name.is_a? Transaction
		transaction_or_name
	else 			
		t = @transaction_strategy.create_new
		t.name = transaction_or_name if transaction_or_name
		t
	end
	tr.repository = self
	
	@sync.synchronize :SH do 
		begin
			Thread.current[:om_transaction] = tr
			b.call tr
		ensure
			Thread.current[:om_transaction] = nil
		end			
		if tr.commited?
			begin
				tr.event_processor.fire_after_commit
			ensure
				@transaction_strategy.after_commit tr if tr.managed
			end
		end
	end
	return tr
end