metropolis.git  about / heads / tags
key-value store for Rack
blob 56e588d3699394fc024c2fc4200f275b0ee20383 1576 bytes (raw)
$ git show HEAD:lib/metropolis/tdb.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
# -*- encoding: binary -*-

require 'tdb'

module Metropolis::TDB
  include Metropolis::Common
  autoload :Single, 'metropolis/tdb/single'
  autoload :Multi, 'metropolis/tdb/multi'

  def setup(opts)
    super
    @rbuf = ""
    @tdb_opts = { :tdb_flags => 0 }
    if @readonly
      @tdb_opts[:open_flags] = IO::RDONLY
      extend Metropolis::Common::RO
    end
    if @query
      size = @query['hash_size'] and @tdb_opts[:hash_size] = size.to_i
      hash = @query['hash'] and @tdb_opts[:hash] = hash.to_sym

      case @query['volatile']
      when 'true'; @tdb_opts[:tdb_flags] |= TDB::VOLATILE
      when 'false', nil
      else
        raise ArgumentError, "'volatile' must be 'true' or 'false'"
      end

      case @query['sync']
      when 'true', nil
      when 'false'; @tdb_opts[:tdb_flags] |= TDB::NOSYNC
      else
        raise ArgumentError, "'sync' must be 'true' or 'false'"
      end
    end
    extend(@path_pattern ? Metropolis::TDB::Multi : Metropolis::TDB::Single)
  end

  def put(key, env)
    value = env[Rack_Input].read
    db(key) do |tdb|
      case env[HTTP_X_TT_PDMODE]
      when "1"
        # TODO: make this atomic
        return r(409) if tdb.include?(key)
      when "2"
        value = (tdb.get(key) || "") << value
      end
      tdb.store(key, value)
    end
    r(201)
  end

  def delete(key)
    r(db(key) { |tdb| tdb.nuke!(key) } ? 200 : 404)
  end

  def get(key, env)
    value = db(key) { |tdb| tdb.fetch(key, @rbuf) } or return r(404)
    [ 200, { Content_Length => value.size.to_s }.merge!(@headers), [ value ] ]
  end
end

git clone https://yhbt.net/metropolis.git