class Angradb::Driver

Constants

BUFFER_SIZE

Public Class Methods

new(ip_address, ip_port) click to toggle source
# File lib/angradb.rb, line 12
def initialize(ip_address, ip_port)
  @ip_address = ip_address
  @ip_port = ip_port
  open_tcp_connection
end

Public Instance Methods

connect(db_name) click to toggle source

Connects to a specific database on Angradb Params:

db_name

name of the database

Returns:

response

response of the server

# File lib/angradb.rb, line 23
def connect(db_name)
  request = 'connect ' + db_name
  response = send_to_server request
  raise response if response.include? 'does not exist'
  response
end
create_db(db_name) click to toggle source

Creates a specific database on Angradb Params:

db_name

name of the database

Returns:

response

response of the server

# File lib/angradb.rb, line 35
def create_db(db_name)
  request = 'create_db ' + db_name
  send_to_server request
end
delete(key) click to toggle source

Deletes a document on the connected database on Angradb Params:

key

the key for the document

# Returns:

response

the server response

# File lib/angradb.rb, line 89
def delete(key)
  raise 'A key should be provided' unless key
  request = 'delete ' + key
  response = send_to_server request
  raise 'Error deleting the document: ' + response unless response == "ok"
  # returns the key without the quotes
  response
end
look_up(key) click to toggle source

Looks up a document on the connected database on Angradb Params:

key

the key for the document

# Returns:

response

the requested document

# File lib/angradb.rb, line 76
def look_up(key)
  request = 'lookup ' + key
  response = send_to_server request
  raise 'Error on lookup of the document: ' + response if response == "not_found"
  # returns the key without the quotes
  response
end
save(doc) click to toggle source

Creates a document on the connected database of Angradb Params:

doc

document to be saved

Returns:

key

returns the key for the saved document

# File lib/angradb.rb, line 45
def save(doc)
  request = 'save ' + doc
  begin
    response = send_to_server request
  rescue
    raise 'Error on saving the document: ' + response.to_s
  end

  # returns the key without the quotes
  response.delete '"'
end
update(key, doc) click to toggle source

Updates a document on the connected database of Angradb Params:

key

the key for the document

doc

document to be saved

# Returns:

response

response of the server

# File lib/angradb.rb, line 63
def update(key, doc)
  request = 'update ' + key + ' ' + doc
  response = send_to_server request
  raise 'Error on updating the document: ' + response unless response == "ok"
  # returns the key without the quotes
  response
end

Private Instance Methods

open_tcp_connection() click to toggle source
# File lib/angradb.rb, line 100
def open_tcp_connection
  begin
    @session = TCPSocket.new @ip_address, @ip_port
  rescue
    raise 'Couldnt connect with the socket-server'
  end
end
send_to_server(request) click to toggle source
# File lib/angradb.rb, line 108
def send_to_server(request)
  @session.write(request)
  treat_response @session.gets
end
treat_response(string) click to toggle source
# File lib/angradb.rb, line 113
def treat_response(string)
  string.chomp # removes \n
  # JSON.parse(string)
end