Class: LeveldbCurator

Inherits:
Object
  • Object
show all
Defined in:
lib/leveldb_curator.rb

Instance Method Summary collapse

Constructor Details

#initialize(databases = "my_leveldb_database") ⇒ LeveldbCurator

Returns a new instance of LeveldbCurator.



52
53
54
# File 'lib/leveldb_curator.rb', line 52

def initialize(databases = "my_leveldb_database")
  run_daemon(9068, databases)
end

Instance Method Details

#open_databases(databases) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/leveldb_curator.rb', line 14

def open_databases(databases)
  db_list = {}
  databases.each do |database|
    dbh = LevelDB::DB.new(database)
    db_list[database.to_sym] = dbh
  end
  db_list
end

#run_daemon(port, databases) ⇒ Object



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
# File 'lib/leveldb_curator.rb', line 23

def run_daemon(port, databases)
  unless databases.kind_of?(Array)
    databases = [databases]
  end
  db_handlers = open_databases(databases)
  Socket.udp_server_loop("localhost", port) { |msg, msg_src|
    proto = Protocol.new
    proto.read(msg)

    dbh = db_handlers[proto.database.to_sym]
    if proto.command_word == "put"
      begin
        dbh.put(proto.query_key, proto.query_value)
        msg_src.reply "true"
      rescue => exception
        msg_src.reply exception.to_s
      end
    elsif proto.command_word == "get"
      begin
        msg_src.reply dbh.get(proto.query_key)
      rescue
        msg_src.reply ""
      end
    else
      msg_src.reply "invalid command"
    end
  }
end