Class: FuseFS::SqliteMapperFS

Inherits:
PathMapperFS show all
Defined in:
lib/fusefs/sqlitemapper.rb

Constant Summary

Constants inherited from FuseDir

FuseDir::INIT_TIMES

Instance Attribute Summary collapse

Attributes inherited from PathMapperFS

#allow_write, #stats, #use_raw_file_access

Instance Method Summary collapse

Methods inherited from PathMapperFS

#cleanup, create, #map_directory, #map_file, #mkdir, #node, open_mode, #unmap

Methods inherited from FuseDir

#can_delete?, #can_mkdir?, #can_rmdir?, #can_write?, #contents, #delete, #directory?, #executable?, #file?, #mkdir, #raw_close, #raw_open, #raw_read, #raw_sync, #raw_truncate, #raw_write, #read_file, #rename, #rmdir, #scan_path, #sigint, #sigterm, #size, #split_path, #statistics, #times, #touch, #write_to, #xattr

Constructor Details

#initialize(db_path, sql, options = { }) {|row| ... } ⇒ SqliteMapperFS

Returns a new instance of SqliteMapperFS.

Parameters:

  • db_path (String)

    Path to Sqlite database

  • sql (String)

    query

  • options (Hash) (defaults to: { })

Yield Parameters:

  • row (Row)

    to map

Yield Returns:

  • (String, String, Hash<Symbol,Object>)

    newpath, realpath, options

    • newpath - the mapped path

    • realpath - path to the real file

    • options - additional information to store with this path



28
29
30
31
32
33
# File 'lib/fusefs/sqlitemapper.rb', line 28

def initialize(db_path,sql,options = { },&row_mapper)
    @db_path = db_path.to_s
    @sql = sql.to_s
    define_singleton_method(:map_row,row_mapper) if block_given?
    super(options)
end

Instance Attribute Details

#dbObject (readonly)

The database connection



13
14
15
# File 'lib/fusefs/sqlitemapper.rb', line 13

def db
  @db
end

#db_pathObject (readonly)

The database file



10
11
12
# File 'lib/fusefs/sqlitemapper.rb', line 10

def db_path
  @db_path
end

#scan_idObject (readonly)

Maintains a count of the number of times through the scan loop



16
17
18
# File 'lib/fusefs/sqlitemapper.rb', line 16

def scan_id
  @scan_id
end

Instance Method Details

#map_row(row) ⇒ String, Hash<Symbol,Object>

This method is abstract.

Maps a row into a new filepath

Parameters:

  • row (Hash)

    sqlite result hash for a row

Returns:

  • (String, String, Hash<Symbol,Object>)

    newpath, realpath, options

    • newpath - the mapped path

    • realpath - path to the real file

    • options - additional information to store with this path

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/fusefs/sqlitemapper.rb', line 43

def map_row(row)
    raise NotImplementedError, "abstract method #{__method__} not implemented"
end

#mountedObject

FuseFS callback when the filesystem is mounted Starts the scanning loop and performs the initial scan



50
51
52
53
54
55
# File 'lib/fusefs/sqlitemapper.rb', line 50

def mounted()
    @mounted = true
    @mutex = Mutex.new
    @cv = ConditionVariable.new
    @scan_thread = Thread.new() { scan_loop() }
end

#rescanObject

Trigger a rescan of the database



69
70
71
# File 'lib/fusefs/sqlitemapper.rb', line 69

def rescan()
    @mutex.synchronize { @cv.signal() }
end

#scanObject

Executes the sql query and passes each row to map_row (or the block passed in #initialize)

Subclasses can override this method for pre/post scan processing, calling super as required



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fusefs/sqlitemapper.rb', line 76

def scan()
    db.execute(@sql) do |row|
        new_path, real_path, options =  map_row(row)
        options ||= {}
        options[:sqlite_scan_id] = @scan_id
        begin
            map_file(new_path, real_path, options)
        rescue StandardError => e
            puts e
            puts e.backtrace.join("\n")
        end
    end
    cleanup() { |file_node| file_node.options[:sqlite_scan_id] != @scan_id }
end

#sighupObject

Rescan on HUP signal



92
93
94
# File 'lib/fusefs/sqlitemapper.rb', line 92

def sighup
  rescan()
end

#unmountedObject

FuseFS callback when filesystem is unmounted

Stops the database watching threads



61
62
63
64
65
# File 'lib/fusefs/sqlitemapper.rb', line 61

def unmounted()
    @mounted = false
    @mutex.synchronize { @cv.signal() }
    @scan_thread.join
end