Пример #1
0
        internal void ToTtbCell(OtpOutputStream os)
        {
            if (isNull)
            {
                os.WriteNil();
                return;
            }

            if (valueType == ColumnType.Varchar || valueType == ColumnType.Blob)
            {
                os.WriteBinary(varcharValue);
            }
            else if (valueType == ColumnType.SInt64)
            {
                os.WriteLong(sint64Value);
            }
            else if (valueType == ColumnType.Timestamp)
            {
                os.WriteLong(timestampValue);
            }
            else if (valueType == ColumnType.Boolean)
            {
                os.WriteBoolean(booleanValue);
            }
            else if (valueType == ColumnType.Double)
            {
                os.WriteDouble(doubleValue);
            }
            else
            {
                throw new InvalidOperationException("Could not convert to TTB value.");
            }
        }
        public void Write_List_And_Tuple()
        {
            byte[] want = new byte[]
            {
                131, 108, 0, 0, 0, 2, 104, 2, 100, 0, 4, 116, 114, 117, 101, 100, 0, 5,
                102, 97, 108, 115, 101, 108, 0, 0, 0, 2, 100, 0, 4, 116, 114, 117, 101,
                100, 0, 5, 102, 97, 108, 115, 101, 106, 106
            };
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.WriteByte(OtpExternal.VersionTag);
                os.WriteListHead(2);
                os.WriteTupleHead(2);
                os.WriteBoolean(true);
                os.WriteBoolean(false);
                os.WriteListHead(2);
                os.WriteBoolean(true);
                os.WriteBoolean(false);
                os.WriteNil();
                os.WriteNil();
                Assert.AreEqual(want.Length, os.Position);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
Пример #3
0
        /*
         * Convert this tuple to the equivalent Erlang external representation.
         *
         * @param buf an output stream to which the encoded tuple should be
         * written.
         **/
        public override void  encode(OtpOutputStream buf)
        {
            int arity = (int)(elems.Length);

            buf.write_tuple_head(arity);

            for (int i = 0; i < arity; i++)
            {
                buf.write_any(elems[i]);
            }
        }
        public void Write_2BE()
        {
            byte[] want = { 0xAB, 0xCD };
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.Write2BE(0xFFFFABCD);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
        public void Write_8BE()
        {
            byte[] want = { 0x12, 0xAD, 0xBE, 0xEF, 0x98, 0x76, 0xAB, 0xCD };
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.Write8BE(0x12ADBEEF9876ABCD);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
        public void Write_Byte()
        {
            byte want = 123;

            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.Write(want);
                got = os.ToArray();
            }

            Assert.AreEqual(want, got[0]);
        }
        public void Write_Long(long l, byte[] want)
        {
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.WriteByte(OtpExternal.VersionTag);
                os.WriteLong(l);
                Assert.AreEqual(want.Length, os.Position);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
        public void Can_Parse_Bare_RpbErrorResp()
        {
            byte[] b = null;
            using (var os = new OtpOutputStream())
            {
                os.WriteAtom(TtbErrorDecoder.RpbErrorRespAtom);
                os.Flush();
                b = os.ToArray();
            }

            var ex = Assert.Throws <RiakException>(() => new TsTtbResp(b));

            Assert.IsTrue(ex.Message.Contains(TtbErrorDecoder.RpbErrorRespEmpty));
        }
        public void Write_Atom()
        {
            byte[] want = { 131, 100, 0, 18, 102, 114, 97, 122, 122, 108, 101, 100, 97, 122, 122, 108, 101, 45, 49, 50, 51, 52 };
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.Write(OtpExternal.VersionTag);
                os.WriteAtom("frazzledazzle-1234");
                Assert.AreEqual(want.Length, os.Position);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
        public void Write_String_As_Binary(string s)
        {
            byte[] str  = Encoding.UTF8.GetBytes(s);
            byte[] want = BuildBinBuffer(str);
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.WriteStringAsBinary(s);
                Assert.AreEqual(want.Length, os.Position);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
Пример #11
0
        public void Read_Binary(string want)
        {
            byte[] buf = null;
            using (var os = new OtpOutputStream())
            {
                os.WriteStringAsBinary(want);
                buf = os.ToArray();
            }

            using (var s = new OtpInputStream(buf))
            {
                string got = s.ReadBinaryAsString();
                Assert.AreEqual(want, got);
            }
        }
        public void Can_Parse_RpbErrorResp_In_2_Tuple_With_Code()
        {
            byte[] b = null;
            using (var os = new OtpOutputStream())
            {
                os.WriteTupleHead(2);
                os.WriteAtom(TtbErrorDecoder.RpbErrorRespAtom);
                os.WriteLong(ErrCode);
                os.Flush();
                b = os.ToArray();
            }

            var ex = Assert.Throws <RiakException>(() => new TsTtbResp(b));

            Assert.IsTrue(ex.Message.Contains(ErrCode.ToString()));
        }
Пример #13
0
        /*
         * Convert this list to the equivalent Erlang external
         * representation. Note that this method never encodes lists as
         * strings, even when it is possible to do so.
         *
         * @param buf An output stream to which the encoded list should be
         * written.
         *
         **/
        public override void  encode(OtpOutputStream buf)
        {
            int _arity = arity();

            if (_arity > 0)
            {
                buf.write_list_head(_arity);

                for (int i = 0; i < _arity; i++)
                {
                    buf.write_any(elems[i]);
                }
            }

            buf.write_nil();
        }
        public void Write_Binary()
        {
            var r = new Random();

            byte[] rnd = new byte[65536];
            r.NextBytes(rnd);

            byte[] want = BuildBinBuffer(rnd);
            byte[] got;
            using (var os = new OtpOutputStream())
            {
                os.WriteBinary(rnd);
                Assert.AreEqual(want.Length, os.Position);
                got = os.ToArray();
            }

            CollectionAssert.AreEqual(want, got);
        }
Пример #15
0
        public override RiakReq ConstructRequest(bool useTtb)
        {
            if (useTtb)
            {
                expectedCode = MessageCode.TsTtbMsg;
                usingTtb     = true;

                using (var os = new OtpOutputStream())
                {
                    os.WriteByte(OtpExternal.VersionTag);

                    // TsQueryReq is a 4-tuple: {'tsqueryreq', TsInterpolation, boolIsStreaming, bytesCoverContext}
                    os.WriteTupleHead(4);
                    os.WriteAtom(TsQueryReqAtom);

                    // TsInterpolation is a 3-tuple
                    // {'tsinterpolation', query, []} empty list is interpolations
                    os.WriteTupleHead(3);
                    os.WriteAtom(TsInterpolationAtom);
                    os.WriteStringAsBinary(CommandOptions.Query);
                    os.WriteNil();

                    os.WriteBoolean(false);
                    os.WriteAtom(UndefinedAtom);
                    os.Flush();

                    return(new TsTtbMsg(os.ToArray()));
                }
            }
            else
            {
                var req = new TsQueryReq();

                req.query = new TsInterpolation
                {
                    @base = CommandOptions.Query
                };

                // NB: always stream, collect results unless callback is passed.
                req.stream = true;

                return(req);
            }
        }
Пример #16
0
 /*
  * Convert this PID to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded PID should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_pid(_node, _id, _serial, _creation);
 }
Пример #17
0
 /*
 * Convert this string to the equivalent Erlang external representation.
 *
 * @param buf an output stream to which the encoded string should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     throw new EncodeException("Cannot encode vars!");
 }
Пример #18
0
 /*
  * Convert this boolean to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded atom should be
  * written.
  **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_boolean(this.value);
 }
Пример #19
0
 /*
  * Convert this atom to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded atom should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_atom(this.atom);
 }
Пример #20
0
 /*
 * Convert this binary to the equivalent Erlang external representation.
 *
 * @param buf an output stream to which the encoded binary should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_binary(this.bin);
 }
Пример #21
0
 /*
  * Convert this binary to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded binary should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_binary(this.bin);
 }
Пример #22
0
 /*
 * Convert this port to the equivalent Erlang external representation.
 *
 * @param buf an output stream to which the encoded port should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_port(_node, _id, _creation);
 }
Пример #23
0
		/*
		* Convert this double to the equivalent Erlang external representation.
		*
		* @param buf an output stream to which the encoded value should be
		* written.
		**/
		public override void  encode(OtpOutputStream buf)
		{
			buf.write_double(this.d);
		}
Пример #24
0
 /*
  * Convert this string to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded string should be
  * written.
  **/
 public override void encode(OtpOutputStream buf)
 {
     throw new EncodeException("Cannot encode vars!");
 }
Пример #25
0
 /*
 * Convert this number to the equivalent Erlang external
 * representation.
 *
 * @param buf an output stream to which the encoded number should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_long(this.val);
 }
Пример #26
0
		/*
		* Convert this list to the equivalent Erlang external
		* representation. Note that this method never encodes lists as
		* strings, even when it is possible to do so.
		*
		* @param buf An output stream to which the encoded list should be
		* written.
		*
		**/
		public override void  encode(OtpOutputStream buf)
		{
			int _arity = arity();
			
			if (_arity > 0)
			{
				buf.write_list_head(_arity);
				
				 for (int i = 0; i < _arity; i++)
				{
					buf.write_any(elems[i]);
				}
			}
			
			buf.write_nil();
		}
Пример #27
0
		/*
		* Convert this boolean to the equivalent Erlang external representation.
		*
		* @param buf an output stream to which the encoded atom should be
		* written.
		**/
		public override void encode(OtpOutputStream buf)
		{
			buf.write_boolean(this.value);
		}
Пример #28
0
 /*
 * Convert this string to the equivalent Erlang external representation.
 *
 * @param buf an output stream to which the encoded string should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_string(str);
 }
Пример #29
0
 /*
  * Convert the object according to the rules of the Erlang external
  * format. This is mainly used for sending Erlang terms in messages,
  * however it can also be used for storing terms to disk.
  *
  * @param buf an output stream to which the encoded term should be
  * written.
  **/
 public abstract void  encode(OtpOutputStream buf);
Пример #30
0
 /*
 * Convert this atom to the equivalent Erlang external representation.
 *
 * @param buf an output stream to which the encoded atom should be
 * written.
 **/
 public override void encode(OtpOutputStream buf)
 {
     buf.write_atom(this.atom);
 }
Пример #31
0
 /*
  * Convert this number to the equivalent Erlang external
  * representation.
  *
  * @param buf an output stream to which the encoded number should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_long(this.val);
 }
Пример #32
0
 /*
  * Convert this string to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded string should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_string(str);
 }
Пример #33
0
        /*
        * Convert this tuple to the equivalent Erlang external representation.
        *
        * @param buf an output stream to which the encoded tuple should be
        * written.
        **/
        public override void encode(OtpOutputStream buf)
        {
            int arity = (int) (elems.Length);

            buf.write_tuple_head(arity);

             for (int i = 0; i < arity; i++)
            {
                buf.write_any(elems[i]);
            }
        }
Пример #34
0
        // TODO FUTURE this all really should be in a codec
        public override RiakReq ConstructRequest(bool useTtb)
        {
            RiakReq rv;

            if (useTtb)
            {
                usingTtb     = true;
                expectedCode = MessageCode.TsTtbMsg;

                byte[]            buffer;
                string            tableName = CommandOptions.Table;
                ICollection <Row> rows      = CommandOptions.Rows;

                using (var os = new OtpOutputStream())
                {
                    os.WriteByte(OtpExternal.VersionTag);

                    // {tsputreq, tableName, emptyList, rows}
                    os.WriteTupleHead(4);
                    os.WriteAtom(TsPutReqAtom);
                    os.WriteStringAsBinary(tableName);
                    os.WriteNil();

                    if (rows.Count > 0)
                    {
                        os.WriteListHead(rows.Count);

                        foreach (Row r in CommandOptions.Rows)
                        {
                            os.WriteTupleHead(r.Cells.Count);
                            foreach (Cell c in r.Cells)
                            {
                                c.ToTtbCell(os);
                            }
                        }

                        os.WriteNil();
                    }
                    else
                    {
                        os.WriteNil();
                    }

                    buffer = os.ToArray();
                }

                rv = new TsTtbMsg(buffer);
            }
            else
            {
                var req = new TsPutReq();

                req.table = CommandOptions.Table;

                if (EnumerableUtil.NotNullOrEmpty(CommandOptions.Columns))
                {
                    req.columns.AddRange(CommandOptions.Columns.Select(c => c.ToTsColumn()));
                }

                req.rows.AddRange(CommandOptions.Rows.Select(r => r.ToTsRow()));

                rv = req;
            }

            return(rv);
        }
Пример #35
0
 /*
  * Convert this double to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded value should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_double(this.d);
 }
Пример #36
0
 /*
 * Convert the object according to the rules of the Erlang external
 * format. This is mainly used for sending Erlang terms in messages,
 * however it can also be used for storing terms to disk.
 *
 * @param buf an output stream to which the encoded term should be
 * written.
 **/
 public abstract void encode(OtpOutputStream buf);
Пример #37
0
 /*
  * Convert this ref to the equivalent Erlang external representation.
  *
  * @param buf an output stream to which the encoded ref should be
  * written.
  **/
 public override void  encode(OtpOutputStream buf)
 {
     buf.write_ref(_node, _ids, _creation);
 }