Sha256: 9c7dd3742084bcb0431084f89d401005df7f6ad269b4aab2c6365ef2b4ee1ea8

Contents?: true

Size: 1.91 KB

Versions: 216

Compression:

Stored size: 1.91 KB

Contents

require 'rbbt/tsv'
module Persist
  module TSVAdapter
    attr_accessor :persistence_path, :closed, :writable, :mutex

    MAX_CHAR = 255.chr

    def mutex
      @mutex ||= Mutex.new
    end

    def prefix(key)
      range(key, 1, key + MAX_CHAR, 1)
    end

    def get_prefix(key)
      keys = prefix(key)
      select(:key => keys)
    end

    def closed?
      @closed
    end

    def close
      @closed = true
      super
      self
    end

    def write?
      @writable
    end

    def read?
      ! write?
    end

    def collect
      res = []
      each do |key, value|
        res << if block_given?
                 yield key, value
        else
          [key, value]
        end
      end
      res
    end

    def delete(key)
      out(key)
    end

    def write_and_read
      lock_filename = Persist.persistence_path(persistence_path + '.write', {:dir => TSV.lock_dir})
      #mutex.synchronize do
      Misc.lock(lock_filename) do
        write if closed? or not write?
        res = begin
                yield
              ensure
                read
              end
        res
      end
      #end
    end

    def write_and_close
      lock_filename = Persist.persistence_path(persistence_path + '.write', {:dir => TSV.lock_dir})
      #mutex.synchronize do
      Misc.lock(lock_filename, true) do
        write if closed? or not write?
        res = begin
                yield
              rescue Exception
                Log.exception $!
                raise $!
              ensure
                close
              end
        res
      #end
      end
    end

    def read_and_close
      read if closed? or not read?
      res = begin
              yield
            ensure
              close
            end
      res
    end


    def merge!(hash)
      hash.each do |key,values|
        self[key] = values
      end
    end


    def range(*args)
      super(*args) #- TSV::ENTRY_KEYS.to_a
    end
  end
end

Version data entries

216 entries across 216 versions & 1 rubygems

Version Path
rbbt-util-5.26.148 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.147 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.146 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.145 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.144 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.143 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.142 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.141 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.140 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.139 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.138 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.137 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.136 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.135 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.134 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.133 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.132 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.131 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.130 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.129 lib/rbbt/persist/tsv/adapter.rb