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.169 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.168 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.167 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.166 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.164 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.163 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.162 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.161 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.160 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.159 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.158 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.157 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.156 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.155 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.154 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.153 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.152 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.151 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.150 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.26.149 lib/rbbt/persist/tsv/adapter.rb