Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for protocol version 12 #5

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/orient_db_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def initialize(socket, protocol_version, options = {})
end

def close
@socket.close
@socket.close
end

def config_get(session, config_name)
@protocol.config_get(@socket, session, config_name)
end

def close_database(session)
Expand Down Expand Up @@ -93,6 +97,7 @@ def load_record(session, rid)

result = @protocol.record_load(@socket, session, rid)


if result[:message_content]
result[:message_content].tap do |r|
r[:cluster_id] = rid.cluster_id
Expand All @@ -115,7 +120,7 @@ def open_database(database, options = {})
response = @protocol.db_open(@socket, database, options)
session = response[:session]
message_content = response[:message_content]

@sessions[session] = DatabaseSession.new(message_content[:session], self, message_content[:clusters])
end

Expand All @@ -126,6 +131,14 @@ def query(session, text, options = {})

result[:message_content]
end

def command(session, text, options = {})
options[:query_class_name] = :command

result = @protocol.command(@socket, session, text, options)

result[:message_content]
end

def reload(session)
result = @protocol.db_reload(@socket, session)
Expand Down
7 changes: 2 additions & 5 deletions lib/orient_db_client/protocol_factory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require File.join(File.dirname(__FILE__), 'protocols', 'protocol7')
require File.join(File.dirname(__FILE__), 'protocols', 'protocol9')
require File.join(File.dirname(__FILE__), 'protocols', 'protocol12')

module OrientDbClient
class ProtocolFactory
Expand All @@ -10,10 +10,7 @@ class ProtocolFactory

PROTOCOLS = {
'7' => Protocols::Protocol7,
'9' => Protocols::Protocol9,
'10' => Protocols::Protocol9,
'11' => Protocols::Protocol9,
'12' => Protocols::Protocol9
'12' => Protocols::Protocol12
}

def self.get_protocol(version)
Expand Down
226 changes: 226 additions & 0 deletions lib/orient_db_client/protocols/protocol12.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
require 'orient_db_client/network_message'
require 'orient_db_client/version'
require 'bindata'

module OrientDbClient
module Protocols
class Protocol12 < Protocol7
VERSION = 12

module Commands
class ConfigGet < BinData::Record
endian :big
int8 :operation, :value => Protocol7::Operations::CONFIG_GET
int32 :session
protocol_string :config_name
end

class DbCreate < BinData::Record
endian :big

int8 :operation, :value => Protocol7::Operations::DB_CREATE
int32 :session

protocol_string :database
protocol_string :database_type
protocol_string :storage_type
end

class DbOpen < BinData::Record
endian :big

int8 :operation, :value => Protocol7::Operations::DB_OPEN
int32 :session, :value => Protocol7::NEW_SESSION

protocol_string :driver_name, :value => Protocol7::DRIVER_NAME
protocol_string :driver_version, :value => Protocol7::DRIVER_VERSION
int16 :protocol_version
protocol_string :client_id
protocol_string :database_name
protocol_string :database_type
protocol_string :user_name
protocol_string :user_password
end

class RecordLoad12 < BinData::Record
endian :big

int8 :operation, :value => Protocol7::Operations::RECORD_LOAD
int32 :session

int16 :cluster_id
int64 :cluster_position
protocol_string :fetch_plan
int8 :ignore_cache, :initial_value => 1
end

class RecordCreate12 < BinData::Record
endian :big

int8 :operation, :value => Protocol7::Operations::RECORD_CREATE
int32 :session
int32 :datasegment_id, :value => -1
int16 :cluster_id
protocol_string :record_content
int8 :record_type, :value => Protocol7::RecordTypes::DOCUMENT
int8 :mode, :value => Protocol7::SyncModes::SYNC
end

end

def self.command(socket, session, command, options = {})
options[:query_class_name].tap do |qcn|
if qcn.is_a?(Symbol)
qcn = case qcn
when :query then 'q'
when :command then 'c'
end
end

if qcn.nil? || qcn == 'com.orientechnologies.orient.core.sql.query.OSQLSynchQuery'
qcn = 'q'
end

options[:query_class_name] = qcn
end

super socket, session, command, options
end

def self.db_create(socket, session, database, options = {})
if options.is_a?(String)
options = { :storage_type => options }
end

options = {
:database_type => 'document'
}.merge(options)

super
end

def self.read_clusters(socket)
clusters = []

num_clusters = read_short(socket)
(num_clusters).times do |x|
cluster =
{
:name => read_string(socket),
:id => read_short(socket),
:type => read_string(socket),
:data_segment => read_short(socket)
}
clusters << cluster

end

clusters
end

def self.read_record_load(socket)
result = nil

status = read_byte(socket)

while (status != PayloadStatuses::NO_RECORDS)
case status
when PayloadStatuses::RESULTSET
record = record || read_record(socket)
case record[:record_type]
when 'd'.ord
result = result || record
result[:document] = deserializer.deserialize(record[:bytes])[:document]
else
raise "Unsupported record type: #{record[:record_type]}"
end
else
raise "Unsupported payload status: #{status}"
end
status = read_byte(socket)
end

result
end

def self.read_db_open(socket)
session = read_integer(socket)
clusters = read_clusters(socket)
{ :session => session,
:clusters => clusters,
:cluster_config => read_string(socket) }
end

def self.record_create(socket, session, cluster_id, record)
command = Commands::RecordCreate12.new :session => session,
:cluster_id => cluster_id,
:record_content => serializer.serialize(record)
command.write(socket)

read_response(socket)

{ :session => read_integer(socket),
:message_content => read_record_create(socket).merge({ :cluster_id => cluster_id }) }
end

def self.db_open(socket, database, options = {})
command = Commands::DbOpen.new :protocol_version => self.version,
:database_name => database,
:database_type => options[:database_type] || 'document',
:user_name => options[:user],
:user_password => options[:password]
command.write(socket)

read_response(socket)

{ :session => read_integer(socket),
:message_content => read_db_open(socket) }
end

def self.config_get(socket, session, config_name)
config = Commands::ConfigGet.new :session => session,
:config_name => config_name

config.write(socket)

response = read_response(socket)
{ :session => read_integer(socket),
:value => read_string(socket) }

end

def self.record_load(socket, session, rid, options = {})
command = Commands::RecordLoad12.new :session => session,
:cluster_id => rid.cluster_id,
:cluster_position => rid.cluster_position
# :ignore_cache => options[:ignore_cache] === true ? 1 : 0

command.write(socket)

read_response(socket)

{ :session => read_integer(socket),
:message_content => read_record_load(socket) }
end

def self.read_record_create(socket)
{ :cluster_position => read_long(socket),
:record_version => read_integer(socket) }
end

private

def self.make_db_create_command(*args)
session = args.shift
database = args.shift
options = args.shift

Commands::DbCreate.new :session => session,
:database => database,
:database_type => options[:database_type].to_s,
:storage_type => options[:storage_type]
end

end
end
end
29 changes: 23 additions & 6 deletions lib/orient_db_client/protocols/protocol7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module Operations
RECORD_DELETE = 33
RECORD_LOAD = 30
RECORD_UPDATE = 32
CONFIG_GET = 70
CONFIG_SET = 71
CONFIG_LIST = 72
end

module RecordTypes
Expand Down Expand Up @@ -398,6 +401,7 @@ def self.datacluster_remove(socket, session, cluster_id)
end

def self.db_close(socket, session = NEW_SESSION)
return true if socket.closed?
command = Commands::DbClose.new :session => session
command.write(socket)

Expand Down Expand Up @@ -463,6 +467,8 @@ def self.db_open(socket, database, options = {})
{ :session => read_integer(socket),
:message_content => read_db_open(socket) }
end



def self.db_reload(socket, session)
command = Commands::DbReload.new :session => session
Expand All @@ -483,6 +489,8 @@ def self.db_size(socket, session)
{ :session => read_integer(socket),
:message_content => read_db_size(socket) }
end



def self.record_create(socket, session, cluster_id, record)
command = Commands::RecordCreate.new :session => session,
Expand Down Expand Up @@ -578,12 +586,17 @@ def self.read_count(socket)
def self.read_clusters(socket)
clusters = []

read_short(socket).times do
clusters << {
num_clusters = read_short(socket)
(num_clusters).times do |x|
cluster =
{
:name => read_string(socket),
:id => read_short(socket),
:type => read_string(socket)
:type => read_string(socket),
:other => read_short(socket)
}
clusters << cluster

end

clusters
Expand Down Expand Up @@ -616,6 +629,8 @@ def self.read_command(socket)
collection = read_record_collection(socket)
result.concat collection
break
when PayloadStatuses::SERIALIZED
result.push(status)
else
raise "Unsupported payload status: #{status}"
end
Expand Down Expand Up @@ -650,8 +665,10 @@ def self.read_db_exist(socket)
end

def self.read_db_open(socket)
{ :session => read_integer(socket),
:clusters => read_clusters(socket),
session = read_integer(socket)
clusters = read_clusters(socket)
{ :session => session,
:clusters => clusters,
:cluster_config => read_string(socket) }
end

Expand Down Expand Up @@ -765,4 +782,4 @@ def self.read_string(socket)
end
end
end
end
end
Loading