예제 #1
0
        protected internal override void writeVectorToOutputStream(ExtendedDataOutput @out)
        {
            int    indexCount   = rowIndices.Count;
            int    cols         = valueVec.rows();
            int    countBytes   = 1;
            UInt32 maxCount     = 255;
            int    indicesPos   = 0;
            int    valuesOffect = 0;

            while (indicesPos < indexCount)
            {
                int byteRequest = 4;
                int curRows     = 0;
                int indiceCount = 1;
                while (byteRequest < BUF_SIZE && indicesPos + indiceCount - 1 < indexCount && indiceCount < 65536)
                {
                    int curIndiceOffect = indicesPos + indiceCount - 1;
                    int index           = curIndiceOffect == 0 ? rowIndices[curIndiceOffect] : rowIndices[curIndiceOffect] - rowIndices[curIndiceOffect - 1];
                    while (index > maxCount)
                    {
                        byteRequest += (indiceCount - 1) * countBytes;
                        countBytes  *= 2;
                        maxCount     = Math.Min(UInt32.MaxValue, (UInt32)1 << (8 * countBytes) - 1);
                    }
                    curRows += index;
                    indiceCount++;
                    byteRequest += countBytes + baseUnitLength_ * index;
                }
                indiceCount--;
                @out.writeShort(indiceCount);
                @out.writeByte(countBytes);
                @out.writeByte(0);
                for (int i = 0; i < indiceCount; ++i)
                {
                    int index = indicesPos + i == 0 ? rowIndices[indicesPos + i] : rowIndices[indicesPos + i] - rowIndices[indicesPos + i - 1];
                    if (countBytes == 1)
                    {
                        @out.writeByte(index);
                    }
                    else if (countBytes == 2)
                    {
                        @out.writeShort(index);
                    }
                    else
                    {
                        @out.writeInt(index);
                    }
                }
                valueVec.serialize(valuesOffect, curRows, @out);
                indicesPos   += indiceCount;
                valuesOffect += curRows;
            }
        }
 protected internal override void writeVectorToOutputStream(ExtendedDataOutput @out)
 {
     foreach (byte value in values)
     {
         @out.writeByte(value);
     }
 }
예제 #3
0
 public override void serialize(int start, int count, ExtendedDataOutput @out)
 {
     for (int i = 0; i < count; ++i)
     {
         @out.writeByte(values[start + i]);
     }
 }
예제 #4
0
        public bool connect()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(hostName, port);
            socket.NoDelay = true;
            @out           = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));

            ExtendedDataInput @in  = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
            string            body = "connect\n";

            @out.writeBytes("API 0 ");
            @out.writeBytes(body.Length.ToString());
            @out.writeByte('\n');
            @out.writeBytes(body);
            @out.flush();


            string line   = @in.readLine();
            int    endPos = line.IndexOf(' ');

            if (endPos <= 0)
            {
                close();
                throw new IOException("Invalid ack msg : " + line);
                //return false;
            }
            sessionID = line.Substring(0, endPos);

            int startPos = endPos + 1;

            endPos = line.IndexOf(' ', startPos);
            if (endPos != line.Length - 2)
            {
                close();
                throw new IOException("Invalid ack msg : " + line);
                //return false;
            }

            if (line[endPos + 1] == '0')
            {
                remoteLittleEndian = false;
                @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
            }
            else
            {
                remoteLittleEndian = true;
            }

            if (this.userId.Length > 0 && this.password.Length > 0)
            {
                login();
            }
            if (this.initialScript != "")
            {
                run(initialScript);
            }
            return(true);
        }
예제 #5
0
        public virtual void write(ExtendedDataOutput output)
        {
            int length = 27 + AbstractExtendedDataOutputStream.getUTFlength(path, 0, 0) + sites.Count;

            foreach (string site in sites)
            {
                length += AbstractExtendedDataOutputStream.getUTFlength(site, 0, 0);
            }
            output.writeShort(length);
            output.writeString(path);
            output.write(id);
            output.writeInt(version);
            output.writeInt(size_Renamed);
            output.writeByte(flag);
            output.writeByte(sites.Count);
            foreach (string site in sites)
            {
                output.writeUTF(site);
            }
        }
예제 #6
0
        public virtual bool tryReconnect()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(hostName, port);
            @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));

            @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
            string body = "connect\n";

            @out.writeBytes("API 0 ");
            @out.writeBytes(body.Length.ToString());
            @out.writeByte('\n');
            @out.writeBytes(body);
            @out.flush();


            string line   = @in.readLine();
            int    endPos = line.IndexOf(' ');

            if (endPos <= 0)
            {
                close();
                return(false);
            }
            sessionID = line.Substring(0, endPos);

            int startPos = endPos + 1;

            endPos = line.IndexOf(' ', startPos);
            if (endPos != line.Length - 2)
            {
                close();
                return(false);
            }

            if (line[endPos + 1] == '0')
            {
                remoteLittleEndian = false;
                @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
            }
            else
            {
                remoteLittleEndian = true;
            }
            @in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));

            return(true);
        }
예제 #7
0
        public virtual void write(ExtendedDataOutput @out)
        {
            int flag = ((int)DATA_FORM.DF_MATRIX << 8) + (int)getDataType();

            @out.writeShort(flag);
            byte labelFlag = (byte)((hasRowLabel() ? 1 : 0) + (hasColumnLabel() ? 2 : 0));

            @out.writeByte(labelFlag);
            if (hasRowLabel())
            {
                rowLabels.write(@out);
            }
            if (hasColumnLabel())
            {
                columnLabels.write(@out);
            }
            @out.writeShort(flag);
            @out.writeInt(rows());
            @out.writeInt(columns());
            writeVectorToOutputStream(@out);
        }
예제 #8
0
 protected override void writeScalarToOutputStream(ExtendedDataOutput @out)
 {
     @out.writeByte(value);
 }
예제 #9
0
        public virtual void upload(IDictionary <string, IEntity> variableObjectMap)
        {
            if (variableObjectMap == null || variableObjectMap.Count == 0)
            {
                return;
            }

            lock (threadLock)
            {
                try
                {
                    bool reconnect = false;
                    if (socket == null || !socket.Connected)
                    {
                        if (sessionID.Length == 0)
                        {
                            throw new IOException("Database connection is not established yet.");
                        }
                        else
                        {
                            reconnect = true;
                            socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            socket.Connect(hostName, port);

                            @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                        }
                    }

                    IList <IEntity> objects = new List <IEntity>();

                    string body = "variable\n";
                    foreach (string key in variableObjectMap.Keys)
                    {
                        if (!isVariableCandidate(key))
                        {
                            throw new System.ArgumentException("'" + key + "' is not a good variable name.");
                        }
                        body += key + ",";
                        objects.Add(variableObjectMap[key]);
                    }
                    body  = body.Substring(0, body.Length - 1);
                    body += ("\n" + objects.Count + "\n");
                    body += remoteLittleEndian ? "1" : "0";

                    try
                    {
                        @out.writeBytes("API " + sessionID + " ");
                        @out.writeBytes(body.Length.ToString());
                        @out.writeByte('\n');
                        @out.writeBytes(body);
                        for (int i = 0; i < objects.Count; ++i)
                        {
                            objects[i].write(@out);
                        }
                        @out.flush();
                    }
                    catch (IOException ex)
                    {
                        if (reconnect)
                        {
                            socket = null;
                            throw ex;
                        }

                        try
                        {
                            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            socket.Connect(hostName, port);
                            @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                            @out.writeBytes("API " + sessionID + " ");
                            @out.writeBytes(body.Length.ToString());
                            @out.writeByte('\n');
                            @out.writeBytes(body);
                            for (int i = 0; i < objects.Count; ++i)
                            {
                                objects[i].write(@out);
                            }
                            @out.flush();
                            reconnect = true;
                        }
                        catch (Exception e)
                        {
                            socket = null;
                            throw e;
                        }
                    }

                    ExtendedDataInput @in = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));

                    string[] headers = @in.readLine().Split(' ');
                    if (headers.Length != 3)
                    {
                        socket = null;
                        throw new IOException("Received invalid header.");
                    }

                    if (reconnect)
                    {
                        sessionID = headers[0];
                        if (this.userId.Length > 0 && this.password.Length > 0)
                        {
                            login();
                        }
                        if (this.initialScript != "")
                        {
                            run(initialScript);
                        }
                    }
                    string msg = @in.readLine();
                    if (!msg.Equals("OK"))
                    {
                        throw new IOException(msg);
                    }
                }
                finally
                {
                }
            }
        }
예제 #10
0
        public virtual IEntity run(string function, IList <IEntity> arguments)
        {
            lock (threadLock)
            {
                try
                {
                    bool reconnect = false;
                    if (socket == null || !socket.Connected)
                    {
                        if (sessionID.Length == 0)
                        {
                            throw new IOException("Database connection is not established yet.");
                        }
                        else
                        {
                            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            socket.Connect(hostName, port);
                            @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                        }
                    }

                    string body = "function\n" + function;
                    body += ("\n" + arguments.Count + "\n");
                    body += remoteLittleEndian ? "1" : "0";

                    ExtendedDataInput @in     = null;
                    string[]          headers = null;
                    try
                    {
                        @out.writeBytes("API " + sessionID + " ");
                        @out.writeBytes(body.Length.ToString());
                        @out.writeByte('\n');
                        @out.writeBytes(body);
                        for (int i = 0; i < arguments.Count; ++i)
                        {
                            arguments[i].write(@out);
                        }
                        @out.flush();

                        @in     = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
                        headers = @in.readLine().Split(' ');
                    }
                    catch (IOException ex)
                    {
                        if (reconnect)
                        {
                            socket = null;
                            throw ex;
                        }

                        try
                        {
                            tryReconnect();
                            @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                            @out.writeBytes("API " + sessionID + " ");
                            @out.writeBytes(body.Length.ToString());
                            @out.writeByte('\n');
                            @out.writeBytes(body);
                            for (int i = 0; i < arguments.Count; ++i)
                            {
                                arguments[i].write(@out);
                            }
                            @out.flush();

                            @in       = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
                            headers   = @in.readLine().Split(' ');
                            reconnect = true;
                        }
                        catch (Exception e)
                        {
                            socket = null;
                            throw e;
                        }
                    }

                    if (headers.Length != 3)
                    {
                        socket = null;
                        throw new IOException("Received invalid header.");
                    }

                    if (reconnect)
                    {
                        sessionID = headers[0];
                        if (this.userId.Length > 0 && this.password.Length > 0)
                        {
                            login();
                        }
                        if (this.initialScript != "")
                        {
                            run(initialScript);
                        }
                    }
                    int numObject = int.Parse(headers[1]);

                    string msg = @in.readLine();
                    if (!msg.Equals("OK"))
                    {
                        throw new IOException(msg);
                    }

                    if (numObject == 0)
                    {
                        return(new data.Void());
                    }

                    try
                    {
                        short flag = @in.readShort();
                        int   form = flag >> 8;
                        int   type = flag & 0xff;

                        if (form < 0 || form > MAX_FORM_VALUE)
                        {
                            throw new IOException("Invalid form value: " + form);
                        }
                        if (type < 0 || type > MAX_TYPE_VALUE)
                        {
                            throw new IOException("Invalid type value: " + type);
                        }

                        DATA_FORM df = (DATA_FORM)Enum.GetValues(typeof(DATA_FORM)).GetValue(form);
                        DATA_TYPE dt = (DATA_TYPE)Enum.GetValues(typeof(DATA_TYPE)).GetValue(type);

                        return(factory.createEntity(df, dt, @in));
                    }
                    catch (IOException ex)
                    {
                        socket = null;
                        throw ex;
                    }
                }
                finally
                {
                }
            }
        }
예제 #11
0
        public virtual IEntity run(string script, ProgressListener listener)
        {
            lock (threadLock)
            {
                bool reconnect = false;
                if (socket == null || !socket.Connected)
                {
                    if (sessionID.Length == 0)
                    {
                        throw new IOException("Database connection is not established yet.");
                    }
                    else
                    {
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        socket.Connect(hostName, port);

                        @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                    }
                }

                string            body   = "script\n" + script;
                ExtendedDataInput @in    = null;
                string            header = null;
                try
                {
                    @out.writeBytes((listener != null ? "API2 " : "API ") + sessionID + " ");
                    @out.writeBytes(AbstractExtendedDataOutputStream.getUTFlength(body, 0, 0).ToString());
                    @out.writeByte('\n');
                    @out.writeBytes(body);
                    @out.flush();

                    @in = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));

                    header = @in.readLine();
                }
                catch (IOException ex)
                {
                    if (reconnect)
                    {
                        socket = null;
                        throw ex;
                    }

                    try
                    {
                        tryReconnect();
                        @out.writeBytes((listener != null ? "API2 " : "API ") + sessionID + " ");
                        @out.writeBytes(AbstractExtendedDataOutputStream.getUTFlength(body, 0, 0).ToString());
                        @out.writeByte('\n');
                        @out.writeBytes(body);
                        @out.flush();

                        @in       = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
                        header    = @in.readLine();
                        reconnect = true;
                    }
                    catch (Exception e)
                    {
                        socket = null;
                        throw e;
                    }
                }
                string msg;

                while (header.Equals("MSG"))
                {
                    //read intermediate message to indicate the progress
                    msg = @in.readString();
                    if (listener != null)
                    {
                        listener.progress(msg);
                    }
                    header = @in.readLine();
                }

                string[] headers = header.Split(' ');
                if (headers.Length != 3)
                {
                    socket = null;
                    throw new IOException("Received invalid header: " + header);
                }

                if (reconnect)
                {
                    sessionID = headers[0];
                    if (this.userId.Length > 0 && this.password.Length > 0)
                    {
                        login();
                    }
                    if (this.initialScript != "")
                    {
                        run(initialScript);
                    }
                }
                int numObject = int.Parse(headers[1]);

                msg = @in.readLine();
                if (!msg.Equals("OK"))
                {
                    throw new IOException(msg);
                }

                if (numObject == 0)
                {
                    return(new data.Void());
                }
                try
                {
                    short flag = @in.readShort();
                    int   form = flag >> 8;
                    int   type = flag & 0xff;

                    if (form < 0 || form > MAX_FORM_VALUE)
                    {
                        throw new IOException("Invalid form value: " + form);
                    }
                    if (type < 0 || type > MAX_TYPE_VALUE)
                    {
                        throw new IOException("Invalid type value: " + type);
                    }

                    DATA_FORM df = (DATA_FORM)Enum.GetValues(typeof(DATA_FORM)).GetValue(form);
                    DATA_TYPE dt = (DATA_TYPE)Enum.GetValues(typeof(DATA_TYPE)).GetValue(type);

                    return(factory.createEntity(df, dt, @in));
                }
                catch (IOException ex)
                {
                    socket = null;
                    throw ex;
                }
            }
        }
예제 #12
0
        public bool connect()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(hostName, port);
            socket.NoDelay = true;
            @out           = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));

            @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
            string body = "connect\n";

            @out.writeBytes("API 0 ");
            @out.writeBytes(body.Length.ToString());
            @out.writeByte('\n');
            @out.writeBytes(body);
            @out.flush();


            string line   = @in.readLine();
            int    endPos = line.IndexOf(' ');

            if (endPos <= 0)
            {
                close();
                throw new IOException("Invalid ack msg : " + line);
            }
            sessionID = line.Substring(0, endPos);

            int startPos = endPos + 1;

            endPos = line.IndexOf(' ', startPos);
            if (endPos != line.Length - 2)
            {
                close();
                throw new IOException("Invalid ack msg : " + line);
                //return false;
            }

            if (line[endPos + 1] == '0')
            {
                remoteLittleEndian = false;
                @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
            }
            else
            {
                remoteLittleEndian = true;
            }
            @in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));

            if (userId.Length > 0 && password.Length > 0)
            {
                login();
            }
            if (startup != "")
            {
                run(startup);
            }
            if (highAvailability && controllerHost == null)
            {
                try
                {
                    controllerHost = ((BasicString)run("rpc(getControllerAlias(),getNodeHost)")).getString();
                    controllerPort = ((BasicInt)run("rpc(getControllerAlias(),getNodePort)")).getValue();
                }
                catch (Exception) { }
            }
            return(true);
        }