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

  • Michael Granger

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

Constant Summary

DefaultIoFlags =

The default flags to use when opening the backing store file

File::RDWR|File::CREAT

Constants inherited from Store

DelegatedMethods, RecommendedLocker

Instance Attribute Summary

Instance Method Summary

Methods inherited from Store

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

Methods inherited from Arrow::Object

deprecate_class_method, deprecate_method, inherited

Methods included from Arrow::Loggable

#log

Constructor Details

- (FileStore) initialize(uri, idobj)

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

- (Object) dir (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

- (Object) close

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

- (Object) insert

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

- (Object) open(ioflags = DefaultIoFlags)

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

- (Object) remove

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

- (Object) retrieve

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

- (Object) save

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

- (Object) session_file

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

- (Object) update

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