Sha256: d1551d75abdcdea0c00b50c59b33b24418aecd9280ef427dc458e132ec34c51e

Contents?: true

Size: 985 Bytes

Versions: 2

Compression:

Stored size: 985 Bytes

Contents

require "volatiledb/version"
require 'digest/sha1'

module Volatile
  
  class DB
    def initialize
      @raw ||= Volatile::Raw.new
      @db  ||= {}
    end 

    def put(key, &action)
      @db[key] = action
      @raw.put(key, action.call)
      key
    end

    def get(key)
      data = @raw.get key
      unless data.nil?
        data
      else
        @raw.put(key, @db[key].call)
        @raw.get(key)
      end
    end
  end
  
  class Raw
    def initialize
      @db ||= {}
    end

    def put(key, data)
      handle = handle_from key
      File.open("/tmp/#{handle}", "w") {|f| f << data}
      @db[key] = handle
      key
    end

    def get(key)
      handle = @db[key]
      f = "/tmp/#{handle}"
      if File.exists?(f)
        File.read f
      else
        nil
      end
    end
    
    private
    def handle_from(key)
      timestamp = Time.now.to_i
      guid = Digest::SHA1.hexdigest("#{key}#{timestamp}")
      "#{guid}.volatiledb"
    end
  end
    
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volatiledb-0.0.2 lib/volatiledb.rb
volatiledb-0.0.1 lib/volatiledb.rb