Class: Arrow::Session::FileStore

Inherits:
Store show all
Defined in:
lib/arrow/session/filestore.rb

Overview

The Arrow::Session::FileStore class, a derivative of Arrow::Session::Store. Instances of this class store a session object as a marshalled hash on disk.

Authors

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary collapse

DefaultIoFlags =

The default flags to use when opening the backing store file

File::RDWR|File::CREAT

Constants inherited from Store

Store::DelegatedMethods, Store::RecommendedLocker

Instance Attribute Summary collapse

Attributes inherited from Store

#data

Instance Method Summary collapse

Methods inherited from Store

#[]=, #clear, create, #create_recommended_lock, #delete, derivativeDirs, #merge!, #modified?, #new?, #reject!, #replace

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(uri, idobj) ⇒ FileStore

Create a new Arrow::Session::FileStore object.



27
28
29
30
31
32
33
34
35
# File 'lib/arrow/session/filestore.rb', line 27

def initialize( uri, idobj )
	path = (uri.path || uri.opaque).dup
	path.untaint

	@dir = File.expand_path( path )
	@io = nil

	super
end

Instance Attribute Details

#dirObject (readonly)

The fully-qualified directory in which session files will be written.



43
44
45
# File 'lib/arrow/session/filestore.rb', line 43

def dir
  @dir
end

Instance Method Details

#closeObject

Close the output filehandle if it is opened.



76
77
78
# File 'lib/arrow/session/filestore.rb', line 76

def close
	@io.close unless @io.nil? || @io.closed?
end

#insertObject

Insert the specified data hash into whatever permanent storage the Store object is acting as an interface to.



83
84
85
86
87
88
# File 'lib/arrow/session/filestore.rb', line 83

def insert
	super {|data|
		self.log.debug "Inserting data into session file"
		self.open( DefaultIoFlags|File::EXCL ).print( data )
	}
end

#open(ioflags = DefaultIoFlags) ⇒ Object

Get the output filehandle for the session backing store file. Open it with the specified ioflags if it’s not already open.



63
64
65
66
67
68
69
70
71
72
# File 'lib/arrow/session/filestore.rb', line 63

def open( ioflags=DefaultIoFlags )
	if @io.nil? || @io.closed?
		file = self.session_file
		self.log.debug "Opening session file %s" % file
		@io = File.open( file, File::RDWR|File::CREAT )
		@io.sync = true
	end

	return @io
end

#removeObject

Permanently remove the data hash associated with the id used in the receiver’s creation from permanent storage.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/arrow/session/filestore.rb', line 117

def remove
	super
	self.close
	file = self.session_file
	if File.exists?( file )
		File.delete( file )
	else
		raise Arrow::SessionError,
			"Session file #{file} does not exist in the data store"
	end
end

#retrieveObject

Retrieve the data hash stored in permanent storage associated with the id the object was created with.



105
106
107
108
109
110
111
112
# File 'lib/arrow/session/filestore.rb', line 105

def retrieve
	super {
		self.log.debug "Reading data in session file"
		ofh = self.open( File::RDWR )
		ofh.seek( 0, File::SEEK_SET )
		ofh.read
	}
end

#saveObject

Close the file after saving to make sure it’s synched.



54
55
56
57
# File 'lib/arrow/session/filestore.rb', line 54

def save
	super
	@io = nil
end

#session_fileObject

Return the fully-qualified path to the session file for this store.



48
49
50
# File 'lib/arrow/session/filestore.rb', line 48

def session_file
	return File.join( @dir, @id.to_s )
end

#updateObject

Update the current data hash stored in permanent storage with the values contained in data.



93
94
95
96
97
98
99
100
# File 'lib/arrow/session/filestore.rb', line 93

def update
	super {|data|
		self.log.debug "Updating data in session file"
		ofh = self.open
		ofh.seek( 0, File::SEEK_SET )
		ofh.print( data )
	}
end