Exemplo n.º 1
0
        public Request Request(int sessionId)
        {
            Request request = new Request();
            
            // standard request fields
            request.DataItems.Add(new RequestDataItem() { Type = "byte", Data = BinarySerializer.ToArray((byte)OperationType.COMMAND) });
            request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(sessionId) });
            // operation specific fields
            request.DataItems.Add(new RequestDataItem() { Type = "byte", Data = BinarySerializer.ToArray((byte)OperationMode) });

            // class name field
            string className = "x";
            switch (ClassType)
            {
                // idempotent command (e.g. select)
                case CommandClassType.Idempotent:
                    className = "com.orientechnologies.orient.core.sql.query.OSQLSynchQuery";
                    break;
                // non-idempotent command (e.g. insert)
                case CommandClassType.NonIdempotent:
                    className = "com.orientechnologies.orient.core.sql.OCommandSQL";
                    break;
                // script command
                case CommandClassType.Script:
                    className = "com.orientechnologies.orient.core.command.script.OCommandScript";
                    break;
                default:
                    break;
            }

            // TODO: sql script case length
            request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(
                //4 + // this int
                4 + // class name int length
                BinarySerializer.Length(className) + 
                4 + // limit int length
                4 + // text int length
                BinarySerializer.Length(CommandPayload.Text) + 
                4 + // fetch plant int length
                BinarySerializer.Length(CommandPayload.FetchPlan) +
                4 // serialized params int (disable)
            ) });
            request.DataItems.Add(new RequestDataItem() { Type = "string", Data = BinarySerializer.ToArray(className) });

            if (CommandPayload.Type == CommandPayloadType.SqlScript)
            {
                request.DataItems.Add(new RequestDataItem() { Type = "string", Data = BinarySerializer.ToArray(CommandPayload.Language) });
            }

            request.DataItems.Add(new RequestDataItem() { Type = "string", Data = BinarySerializer.ToArray(CommandPayload.Text) });
            request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(CommandPayload.NonTextLimit) });
            request.DataItems.Add(new RequestDataItem() { Type = "string", Data = BinarySerializer.ToArray(CommandPayload.FetchPlan) });
            //request.DataItems.Add(new RequestDataItem() { Type = "bytes", Data = CommandPayload.SerializedParams });
            // HACK: 0:int means disable
            request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(0) });
            
            return request;
        }
Exemplo n.º 2
0
        private ODocument ExecuteOperationInternal(IOperation operation)
        {
            try
            {
                if (_networkStream == null)
                    Reconnect();

                var req = new Request(this);
                req.SetSessionId(SessionId);

                Request request = operation.Request(req);
                byte[] buffer;

                foreach (RequestDataItem item in request.DataItems)
                {
                    switch (item.Type)
                    {
                        case "byte":
                        case "short":
                        case "int":
                        case "long":
                            Send(item.Data);
                            break;
                        case "record":
                            buffer = new byte[2 + item.Data.Length];
                            Buffer.BlockCopy(BinarySerializer.ToArray(item.Data.Length), 0, buffer, 0, 2);
                            Buffer.BlockCopy(item.Data, 0, buffer, 2, item.Data.Length);
                            Send(buffer);
                            break;
                        case "bytes":
                        case "string":
                        case "strings":
                            Send(BinarySerializer.ToArray(item.Data.Length));
                            Send(item.Data);
                            break;
                        default:
                            break;
                    }
                }

                _networkStream.Flush();

                if (request.OperationMode != OperationMode.Synchronous)
                    return null;

                Response response = new Response(this);
                response.Receive();
                return ((IOperation)operation).Response(response);
            }
            catch (IOException)
            {
                Destroy();
                throw;
            }
        }
Exemplo n.º 3
0
        public override Request Request(Request request)
        {

            base.Request(request);

            // operation specific fields
            request.AddDataItem((byte)OperationMode);

            // idempotent command (e.g. select)
            var queryPayload = CommandPayload as CommandPayloadQuery;
            if (queryPayload != null)
            {
                // Write command payload length
                request.AddDataItem(queryPayload.PayLoadLength);
                request.AddDataItem(queryPayload.ClassName);
                //(text:string)(non-text-limit:int)[(fetch-plan:string)](serialized-params:bytes[])
                request.AddDataItem(queryPayload.Text);
                request.AddDataItem(queryPayload.NonTextLimit);
                request.AddDataItem(queryPayload.FetchPlan);

                if (queryPayload.SerializedParams == null || queryPayload.SerializedParams.Length == 0)
                {
                    request.AddDataItem((int)0);
                }
                else
                {
                    request.AddDataItem(queryPayload.SerializedParams);
                }
                return request;
            }
            // non-idempotent command (e.g. insert)
            var scriptPayload = CommandPayload as CommandPayloadScript;
            if (scriptPayload != null)
            {
                // Write command payload length
                request.AddDataItem(scriptPayload.PayLoadLength);
                request.AddDataItem(scriptPayload.ClassName);
                if (scriptPayload.Language != "gremlin")
                    request.AddDataItem(scriptPayload.Language);
                request.AddDataItem(scriptPayload.Text);
                if (scriptPayload.SimpleParams == null)
                    request.AddDataItem((byte)0); // 0 - false, 1 - true
                else
                {
                    request.AddDataItem((byte)1);
                    request.AddDataItem(scriptPayload.SimpleParams);
                }
                request.AddDataItem((byte)0);

                return request;
            }
            var commandPayload = CommandPayload as CommandPayloadCommand;
            if (commandPayload != null)
            {
                // Write command payload length
                request.AddDataItem(commandPayload.PayLoadLength);
                request.AddDataItem(commandPayload.ClassName);
                // (text:string)(has-simple-parameters:boolean)(simple-paremeters:bytes[])(has-complex-parameters:boolean)(complex-parameters:bytes[])
                request.AddDataItem(commandPayload.Text);
                // has-simple-parameters boolean
                if (commandPayload.SimpleParams == null)
                    request.AddDataItem((byte)0); // 0 - false, 1 - true
                else
                {
                    request.AddDataItem((byte)1);
                    request.AddDataItem(commandPayload.SimpleParams);
                }
                //request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(0) });
                // has-complex-parameters
                request.AddDataItem((byte)0); // 0 - false, 1 - true
                //request.DataItems.Add(new RequestDataItem() { Type = "int", Data = BinarySerializer.ToArray(0) });
                return request;
            }
            throw new OException(OExceptionType.Operation, "Invalid payload");
        }
Exemplo n.º 4
0
        internal ODocument ExecuteOperation(IOperation operation)
        {
            Request request = operation.Request(SessionId);

            byte[] buffer;

            foreach (RequestDataItem item in request.DataItems)
            {
                switch (item.Type)
                {
                case "byte":
                case "short":
                case "int":
                case "long":
                    Send(item.Data);
                    break;

                case "record":
                    buffer = new byte[2 + item.Data.Length];
                    Buffer.BlockCopy(BinarySerializer.ToArray(item.Data.Length), 0, buffer, 0, 2);
                    Buffer.BlockCopy(item.Data, 0, buffer, 2, item.Data.Length);
                    Send(buffer);
                    break;

                case "bytes":
                case "string":
                case "strings":
                    //buffer = new byte[4 + item.Data.Length];
                    //Buffer.BlockCopy(BinarySerializer.ToArray(item.Data.Length), 0, buffer, 0, 4);
                    //Buffer.BlockCopy(item.Data, 0, buffer, 4, item.Data.Length);
                    //Send(buffer);

                    Send(BinarySerializer.ToArray(item.Data.Length));
                    Send(item.Data);
                    break;

                default:
                    break;
                }
            }

            _networkStream.Flush();

            if (request.OperationMode == OperationMode.Synchronous)
            {
                try
                {
                    Response response = new Response(this);

                    response.Receive();

                    return(((IOperation)operation).Response(response));
                }
                catch (Exception exception)
                {
                    //reset connection as the socket may contains unread data and is considered unstable
                    Reconnect();
                    throw;
                }
            }
            else
            {
                return(null);
            }
        }