Sha256: afa19904db011f0fdadeeabdb5f188d826feae9fa59e4b2e3ef0eaebd4b2b807

Contents?: true

Size: 1.95 KB

Versions: 43

Compression:

Stored size: 1.95 KB

Contents

require 'spiderfw/utils/shared_store'
require 'fileutils'

module Spider; module Utils
    
    # Implementation of the SharedStore in the filesystem.
    # This is a persistent store, accessible by different threads and processes at once.
    class FileSharedStore < SharedStore
        attr_reader :path
        
        def initialize(config={})
            super
            @path = config[:path]
            if (!@path && config[:name])
                @path = Spider.conf.get('shared_store.file.base_path')+'/'+config[:name]
            end
            if (!@path)
                raise ArgumentError, "You must supply the FileSharedStore with a path, or a name a configured base path"
            end
            FileUtils.mkpath(@path)     
            @sync = Sync.new
        end
        
        def [](key)
            path = map_path(key)
            if (File.exist?(path))
                @sync.lock(Sync::SH)
                f = File.new(path, 'r')
                f.flock(File::LOCK_SH)
                data = Marshal.restore(f.read)
                f.flock(File::LOCK_UN)
                f.close
                @sync.lock(Sync::UN)
            end
            return data
        end
        
        def []=(key, value)
            path = map_path(key)
            @sync.lock(Sync::EX)
            f = File.new(path, 'w')
            f.flock(File::LOCK_EX)
            f.puts(Marshal.dump(value))
            f.flush
            f.flock(File::LOCK_UN)
            f.close
            @sync.lock(Sync::UN)
        end

        
        def delete(key)
            File.unlink(map_path(key))
        end
        
        def include?(key)
            File.exist?(map_path(key))
        end
        
        def map_path(key)
            "#{@path}/key"
        end
        
        def each_key
            Dir.new(@path).each do |key|
                next unless File.file?(@path+'/'+key)
                yield key
            end
        end
        
        
    end
    
end; end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
spiderfw-0.6.23 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.22 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.21 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.20 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.19 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.18 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.17 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.16 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.15 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.14 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.13 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.12 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.11 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.10 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.9 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.8 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.7 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.6 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.5 lib/spiderfw/utils/shared_store/file_shared_store.rb
spiderfw-0.6.4 lib/spiderfw/utils/shared_store/file_shared_store.rb