private static Column[] ParseColumns(OtpInputStream s)
        {
            int colNameCount = s.ReadListHead();

            string[] columnNames = new string[colNameCount];
            for (int i = 0; i < colNameCount; i++)
            {
                columnNames[i] = s.ReadBinaryAsString();
            }

            if (colNameCount > 0)
            {
                s.ReadNil();
            }

            int colTypeCount = s.ReadListHead();

            ColumnType[] columnTypes = new ColumnType[colTypeCount];
            for (int i = 0; i < colTypeCount; i++)
            {
                string a = s.ReadAtom();
                columnTypes[i] = (ColumnType)Enum.Parse(typeof(ColumnType), a, true);
            }

            if (colTypeCount > 0)
            {
                s.ReadNil();
            }

            return(columnNames.Zip(columnTypes, (cname, ctype) => new Column(cname, ctype)).ToArray());
        }
        public ResponseDecoder(TsTtbResp response)
        {
            using (var s = new OtpInputStream(response.Response))
            {
                byte tag = s.Read1();
                if (tag != OtpExternal.VersionTag)
                {
                    string msg = string.Format(
                        "Expected OTP input stream to start with {0}, got {1}",
                        OtpExternal.VersionTag,
                        tag);
                    throw new InvalidOperationException(msg);
                }

                tag = s.Peek();
                switch (tag)
                {
                case OtpExternal.AtomTag:
                    string atom = s.ReadAtom();
                    decodedResponse = ParseAtomResult(atom);
                    break;

                case OtpExternal.SmallTupleTag:
                case OtpExternal.LargeTupleTag:
                    decodedResponse = ParseTupleResult(s);
                    break;

                default:
                    throw new InvalidOperationException("Expected an atom or tuple.");
                }
            }
        }
        public ResponseDecoder(TsTtbResp response)
        {
            using (var s = new OtpInputStream(response.Response))
            {
                byte tag = s.Read1();
                if (tag != OtpExternal.VersionTag)
                {
                    string msg = string.Format(
                        "Expected OTP input stream to start with {0}, got {1}",
                        OtpExternal.VersionTag,
                        tag);
                    throw new InvalidOperationException(msg);
                }

                tag = s.Peek();
                switch (tag)
                {
                    case OtpExternal.AtomTag:
                        string atom = s.ReadAtom();
                        decodedResponse = ParseAtomResult(atom);
                        break;
                    case OtpExternal.SmallTupleTag:
                    case OtpExternal.LargeTupleTag:
                        decodedResponse = ParseTupleResult(s);
                        break;
                    default:
                        throw new InvalidOperationException("Expected an atom or tuple.");
                }
            }
        }
示例#4
0
 public void Read_Atom(byte[] buf, string want)
 {
     using (var s = new OtpInputStream(buf))
     {
         string atom = s.ReadAtom();
         Assert.AreEqual(want, atom);
     }
 }
示例#5
0
 public void Can_Peek(byte[] buf, bool want)
 {
     using (var s = new OtpInputStream(buf))
     {
         byte b = s.Read1();
         Assert.AreEqual(OtpExternal.VersionTag, b);
         b = s.Peek();
         Assert.AreEqual(OtpExternal.AtomTag, b);
         string atom = s.ReadAtom();
         Assert.AreEqual(want.ToString().ToLowerInvariant(), atom);
     }
 }
示例#6
0
 public override void OnSuccess(RiakResp response)
 {
     if (usingTtb)
     {
         var ttbresp = (TsTtbResp)response;
         using (var s = new OtpInputStream(ttbresp.Response))
         {
             s.ReadTupleHead();
             string atom = s.ReadAtom();
             if (atom.Equals(TsPutRespAtom) == false)
             {
                 throw new InvalidDataException(
                           string.Format("Expected {0}, got {1}", TsPutRespAtom, atom));
             }
         }
     }
     else
     {
         Response = new StoreResponse();
     }
 }
        private static DecodedResponse ParseTupleResult(OtpInputStream s)
        {
            // Response is:
            // {'tsgetresp', {ColNames, ColTypes, Rows}}
            // {'tsqueryresp', {ColNames, ColTypes, Rows}}
            int arity = s.ReadTupleHead();

            if (arity != 2)
            {
                throw new InvalidOperationException("Expected response to be a 2-tuple");
            }

            string msg;
            string atom = s.ReadAtom();

            switch (atom)
            {
            case TsGetRespAtom:
            case TsQueryRespAtom:
                arity = s.ReadTupleHead();
                if (arity != 3)
                {
                    msg = string.Format(
                        "Second item in {0} response tuple must be a 3-tuple.",
                        atom);
                    throw new InvalidOperationException(msg);
                }

                Column[] cols = ParseColumns(s);
                Row[]    rows = ParseRows(s, cols);
                return(new DecodedResponse(cols, rows));

            default:
                msg = string.Format(
                    "Expected tsgetresp or tsqueryresp atom, got {0}",
                    atom);
                throw new InvalidOperationException(msg);
            }
        }
        private static DecodedResponse ParseTupleResult(OtpInputStream s)
        {
            // Response is:
            // {'tsgetresp', {ColNames, ColTypes, Rows}}
            // {'tsqueryresp', {ColNames, ColTypes, Rows}}
            int arity = s.ReadTupleHead();
            if (arity != 2)
            {
                throw new InvalidOperationException("Expected response to be a 2-tuple");
            }

            string msg;
            string atom = s.ReadAtom();
            switch (atom)
            {
                case TsGetRespAtom:
                case TsQueryRespAtom:
                    arity = s.ReadTupleHead();
                    if (arity != 3)
                    {
                        msg = string.Format(
                            "Second item in {0} response tuple must be a 3-tuple.",
                            atom);
                        throw new InvalidOperationException(msg);
                    }

                    Column[] cols = ParseColumns(s);
                    Row[] rows = ParseRows(s, cols);
                    return new DecodedResponse(cols, rows);
                default:
                    msg = string.Format(
                        "Expected tsgetresp or tsqueryresp atom, got {0}",
                        atom);
                    throw new InvalidOperationException(msg);
            }
        }
        private static Column[] ParseColumns(OtpInputStream s)
        {
            int colNameCount = s.ReadListHead();
            string[] columnNames = new string[colNameCount];
            for (int i = 0; i < colNameCount; i++)
            {
                columnNames[i] = s.ReadBinaryAsString(); 
            }

            if (colNameCount > 0)
            {
                s.ReadNil();
            }

            int colTypeCount = s.ReadListHead();
            ColumnType[] columnTypes = new ColumnType[colTypeCount];
            for (int i = 0; i < colTypeCount; i++)
            {
                string a = s.ReadAtom();
                columnTypes[i] = (ColumnType)Enum.Parse(typeof(ColumnType), a, true);
            }

            if (colTypeCount > 0)
            {
                s.ReadNil();
            }

            return columnNames.Zip(columnTypes, (cname, ctype) => new Column(cname, ctype)).ToArray();
        }