Sha256: da6090282c1ffeacac0019cb124707f8ab561d63acd2150157c4ff9b4d2b1578

Contents?: true

Size: 1.99 KB

Versions: 154

Compression:

Stored size: 1.99 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
      #mutex.synchronize do
      read if closed? or not read?
      res = begin
              yield
            ensure
              close
            end
      res
      #end
    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

154 entries across 154 versions & 1 rubygems

Version Path
rbbt-util-5.19.25 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.24 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.23 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.22 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.21 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.20 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.19 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.18 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.17 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.16 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.15 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.14 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.13 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.12 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.11 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.10 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.9 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.8 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.7 lib/rbbt/persist/tsv/adapter.rb
rbbt-util-5.19.6 lib/rbbt/persist/tsv/adapter.rb