Class: StreamStorage

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

Constant Summary collapse

STREAMS =
"streams"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, basedir, buffer_size = 8192) ⇒ StreamStorage

Returns a new instance of StreamStorage.



4
5
6
7
8
9
10
11
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 4

def initialize name, basedir, buffer_size = 8192
	@buffer_size = buffer_size
	raise "Invalid directory '#{basedir}'!" unless File.exist? basedir           
	@path = File.join(basedir.gsub("\\", '/'), name.to_s)
	
	Dir.mkdir @path unless File.exist? @path						
	Dir.mkdir File.join(@path, STREAMS) unless File.exist? File.join(@path, STREAMS)                
end

Class Method Details

.delete(name, basedir) ⇒ Object



117
118
119
120
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 117

def self.delete name, basedir
	path = File.join(basedir, name.to_s)
	FileUtils.rm_rf path if File.exist? path
end

Instance Method Details

#clearObject



13
14
15
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 13

def clear
	FileUtils.rm Dir.glob(File.join(@path, STREAMS, "*.dat"))		
end

#delete(id) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 104

def delete id
	id = strip_id(id)
	name = File.join(@path, STREAMS, "#{id.to_s}.dat")		
	File.delete name if File.exist? name
	
	name = File.join(@path, STREAMS, "#{id.to_s}.met")		
	File.delete name if File.exist? name
end

#list_of_idsObject



113
114
115
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 113

def list_of_ids
	Dir.glob(File.join(@path, STREAMS, "*.dat")).collect{|name| File.basename(name, ".dat")}
end

#metadata_put(id, metadata) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 21

def  id, 
	id = strip_id(id)
	metafile_name = File.join(@path, STREAMS, "#{id.to_s}.met")
	File.open(metafile_name, "wb") do |datafile|			
		datafile.write YAML.dump()
	end						
end

#metadata_read(id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 29

def  id
	id = strip_id(id)
	begin
		File.open(File.join(@path, STREAMS, "#{id.to_s}.met"), "rb") do |file|
			return YAML.load(file.read)
		end
	rescue Exception => e
		if e.message =~ /No such file or directory/
			raise RuntimeError, "The Metadata with id '#{id}' not exist!", caller
		else
			raise e
		end
	end
end

#sizeObject



100
101
102
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 100

def size
	Dir.glob(File.join(@path, STREAMS, "*.dat")).size 
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 44

def stream_put id, data = nil, &block		
	id = strip_id(id)
	datafile_name = File.join(@path, STREAMS, "#{id.to_s}.dat")
	File.open(datafile_name, "wb") do |datafile|
		if block
			block.call datafile
		elsif data
			datafile.write data
		else
			raise "Data is not supplied!"
		end				
	end		
end

#stream_put_each(id, stream) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 58

def stream_put_each id, stream
	stream_put id do |out|
		while part = stream.read(@buffer_size)
			out.write part
		end
	end		
end

#stream_read(id, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 66

def stream_read id, &block
	id = strip_id(id)
	begin
		File.open(File.join(@path, STREAMS, "#{id.to_s}.dat"), "rb") do |file|
			if block
				block.call file
			else
				return file.read
			end
		end
	rescue Exception => e
		if e.message =~ /No such file or directory/
			raise RuntimeError, "The Stream with id '#{id}' has been deleted!", caller
		else
			raise e
		end
	end
end

#stream_read_each(id, &block) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 85

def stream_read_each id, &block
	stream_read id do |f|
		while part = f.read(@buffer_size)
			block.call part
		end
	end
end

#stream_size(id) ⇒ Object

Raises:

  • (RuntimeError)


93
94
95
96
97
98
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 93

def stream_size id
	id = strip_id(id)
	name = File.join(@path, STREAMS, "#{id.to_s}.dat")		
	raise RuntimeError, "The Stream with id '#{id}' has been deleted!", caller unless File.exist? name
	File.size(name)
end

#strip_id(id) ⇒ Object



17
18
19
# File 'lib/ObjectModel/Repository/StreamStorage.rb', line 17

def strip_id id
	id.sid
end