コード例 #1
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, 14);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
                record = new Record(null, generation, expiration);
            }
            else
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    record = null;
                }
                else
                {
                    throw new AerospikeException(resultCode);
                }
            }
            EmptySocket(conn);
        }
コード例 #2
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);
            conn.UpdateLastUsed();

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, 14);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
                record = new Record(null, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #3
0
        public object UnpackList()
        {
            if (length <= 0)
            {
                return(new List <object>(0));
            }

            int type = buffer[offset++];
            int count;

            if ((type & 0xf0) == 0x90)
            {
                count = type & 0x0f;
            }
            else if (type == 0xdc)
            {
                count   = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
            }
            else if (type == 0xdd)
            {
                count   = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
            }
            else
            {
                return(new List <object>(0));
            }
            return(UnpackList(count));
        }
コード例 #4
0
        public object UnpackMap()
        {
            if (length <= 0)
            {
                return(new Dictionary <object, object>(0));
            }

            int type = buffer[offset++];
            int count;

            if ((type & 0xf0) == 0x80)
            {
                count = type & 0x0f;
            }
            else if (type == 0xde)
            {
                count   = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
            }
            else if (type == 0xdf)
            {
                count   = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
            }
            else
            {
                return(new Dictionary <object, object>(0));
            }
            return(UnpackMap(count));
        }
コード例 #5
0
        protected internal Record ParseRecord()
        {
            Dictionary <string, object> bins = null;

            for (int i = 0; i < opCount; i++)
            {
                int opSize = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 5;
                byte particleType = dataBuffer[dataOffset];
                dataOffset += 2;
                byte   nameSize = dataBuffer[dataOffset++];
                string name     = ByteUtil.Utf8ToString(dataBuffer, dataOffset, nameSize);
                dataOffset += nameSize;

                int    particleBytesSize = opSize - (4 + nameSize);
                object value             = ByteUtil.BytesToParticle(particleType, dataBuffer, dataOffset, particleBytesSize);
                dataOffset += particleBytesSize;

                if (bins == null)
                {
                    bins = new Dictionary <string, object>();
                }
                bins[name] = value;
            }
            return(new Record(bins, generation, expiration));
        }
コード例 #6
0
        protected internal Record ParseRecord()
        {
            Dictionary <string, object> bins = null;

            for (int i = 0; i < opCount; i++)
            {
                ReadBytes(8);
                int  opSize       = ByteUtil.BytesToInt(dataBuffer, 0);
                byte particleType = dataBuffer[5];
                byte nameSize     = dataBuffer[7];

                ReadBytes(nameSize);
                string name = ByteUtil.Utf8ToString(dataBuffer, 0, nameSize);

                int particleBytesSize = (int)(opSize - (4 + nameSize));
                ReadBytes(particleBytesSize);
                object value = ByteUtil.BytesToParticle(particleType, dataBuffer, 0, particleBytesSize);

                if (bins == null)
                {
                    bins = new Dictionary <string, object>();
                }
                bins[name] = value;
            }
            return(new Record(bins, generation, expiration));
        }
コード例 #7
0
        protected internal Record ParseRecordBatch()
        {
            Dictionary <string, object> bins = null;

            for (int i = 0; i < opCount; i++)
            {
                int    opSize       = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                byte   particleType = dataBuffer[dataOffset + 5];
                byte   nameSize     = dataBuffer[dataOffset + 7];
                string name         = ByteUtil.Utf8ToString(dataBuffer, dataOffset + 8, nameSize);
                dataOffset += 4 + 4 + nameSize;

                int    particleBytesSize = (int)(opSize - (4 + nameSize));
                object value             = ByteUtil.BytesToParticle(particleType, dataBuffer, dataOffset, particleBytesSize);
                dataOffset += particleBytesSize;

                // Currently, the batch command returns all the bins even if a subset of
                // the bins are requested. We have to filter it on the client side.
                // TODO: Filter batch bins on server!
                if (binNames == null || binNames.Contains(name))
                {
                    if (bins == null)
                    {
                        bins = new Dictionary <string, object>();
                    }
                    bins[name] = value;
                }
            }
            return(new Record(bins, generation, expiration));
        }
コード例 #8
0
        protected internal sealed override void ParseResult()
        {
            int resultCode = dataBuffer[5];
            int generation = ByteUtil.BytesToInt(dataBuffer, 6);
            int expiration = ByteUtil.BytesToInt(dataBuffer, 10);
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, 18);
            int opCount    = ByteUtil.BytesToShort(dataBuffer, 20);

            dataOffset = Command.MSG_REMAINING_HEADER_SIZE;

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                }
                else
                {
                    record = ParseRecord(opCount, fieldCount, generation, expiration);
                }
            }
            else
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    record = null;
                }
                else
                {
                    throw new AerospikeException(resultCode);
                }
            }
        }
コード例 #9
0
        protected internal override void ParseResult()
        {
            int resultCode = dataBuffer[dataOffset + 5];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset + 6);
                int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset + 10);

                record = new Record(null, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #10
0
 public int GetIntBig(int offset)
 {
     if (offset < 0 || offset + 4 > this.length)
     {
         return(0);
     }
     return(ByteUtil.BytesToInt(bytes, offset));
 }
コード例 #11
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            long sz           = ByteUtil.BytesToLong(dataBuffer, 0);
            byte headerLength = dataBuffer[8];
            int  resultCode   = dataBuffer[13];
            int  generation   = ByteUtil.BytesToInt(dataBuffer, 14);
            int  expiration   = ByteUtil.BytesToInt(dataBuffer, 18);
            int  fieldCount   = ByteUtil.BytesToShort(dataBuffer, 26);          // almost certainly 0
            int  opCount      = ByteUtil.BytesToShort(dataBuffer, 28);
            int  receiveSize  = ((int)(sz & 0xFFFFFFFFFFFFL)) - headerLength;

            // Read remaining message bytes.
            if (receiveSize > 0)
            {
                SizeBuffer(receiveSize);
                conn.ReadFully(dataBuffer, receiveSize);
            }

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #12
0
            internal override int ParseBlock(int receiveSize)
            {
                base.dataOffset = 0;

                while (base.dataOffset < receiveSize)
                {
                    int resultCode = base.dataBuffer[base.dataOffset + 1];

                    if (resultCode != 0)
                    {
                        return(resultCode);
                    }

                    Role role       = new Role();
                    int  fieldCount = base.dataBuffer[base.dataOffset + 3];
                    base.dataOffset += HEADER_REMAINING;

                    for (int i = 0; i < fieldCount; i++)
                    {
                        int len = ByteUtil.BytesToInt(base.dataBuffer, base.dataOffset);
                        base.dataOffset += 4;
                        int id = base.dataBuffer[base.dataOffset++];
                        len--;

                        if (id == ROLE)
                        {
                            role.name        = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len);
                            base.dataOffset += len;
                        }
                        else if (id == PRIVILEGES)
                        {
                            ParsePrivileges(role);
                        }
                        else
                        {
                            base.dataOffset += len;
                        }
                    }

                    if (role.name == null && role.privileges == null)
                    {
                        continue;
                    }

                    if (role.privileges == null)
                    {
                        role.privileges = new List <Privilege>(0);
                    }
                    list.Add(role);
                }
                return(0);
            }
コード例 #13
0
            internal override int ParseBlock(int receiveSize)
            {
                base.dataOffset = 0;

                while (base.dataOffset < receiveSize)
                {
                    int resultCode = base.dataBuffer[base.dataOffset + 1];

                    if (resultCode != 0)
                    {
                        return(resultCode);
                    }

                    User user       = new User();
                    int  fieldCount = base.dataBuffer[base.dataOffset + 3];
                    base.dataOffset += HEADER_REMAINING;

                    for (int i = 0; i < fieldCount; i++)
                    {
                        int len = ByteUtil.BytesToInt(base.dataBuffer, base.dataOffset);
                        base.dataOffset += 4;
                        int id = base.dataBuffer[base.dataOffset++];
                        len--;

                        if (id == USER)
                        {
                            user.name        = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len);
                            base.dataOffset += len;
                        }
                        else if (id == ROLES)
                        {
                            ParseRoles(user);
                        }
                        else
                        {
                            base.dataOffset += len;
                        }
                    }

                    if (user.name == null && user.roles == null)
                    {
                        continue;
                    }

                    if (user.roles == null)
                    {
                        user.roles = new List <string>(0);
                    }
                    list.Add(user);
                }
                return(0);
            }
コード例 #14
0
        private bool ParseGroup()
        {
            // Parse each message response and add it to the result array
            while (dataOffset < dataLength)
            {
                dataOffset += 3;
                info3       = dataBuffer[dataOffset];
                dataOffset += 2;
                resultCode  = dataBuffer[dataOffset];

                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR || resultCode == ResultCode.FILTERED_OUT)
                    {
                        if (stopOnNotFound)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        throw new AerospikeException(resultCode);
                    }
                }

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) != 0)
                {
                    return(true);
                }
                dataOffset++;
                generation  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                expiration  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                batchIndex  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                fieldCount  = ByteUtil.BytesToShort(dataBuffer, dataOffset);
                dataOffset += 2;
                opCount     = ByteUtil.BytesToShort(dataBuffer, dataOffset);
                dataOffset += 2;

                if (!valid)
                {
                    throw new AerospikeException.QueryTerminated();
                }

                Key key = ParseKey(fieldCount);
                ParseRow(key);
            }
            return(false);
        }
コード例 #15
0
        protected internal override void ParseRow(Key key)
        {
#if NETFRAMEWORK
            if (opCount != 1)
            {
                throw new AerospikeException("Query aggregate expected exactly one bin.  Received " + opCount);
            }

            // Parse aggregateValue.
            int opSize = ByteUtil.BytesToInt(dataBuffer, dataOffset);
            dataOffset += 5;
            byte particleType = dataBuffer[dataOffset];
            dataOffset += 2;
            byte   nameSize = dataBuffer[dataOffset++];
            string name     = ByteUtil.Utf8ToString(dataBuffer, dataOffset, nameSize);
            dataOffset += nameSize;

            int particleBytesSize = (int)(opSize - (4 + nameSize));

            if (!name.Equals("SUCCESS"))
            {
                if (name.Equals("FAILURE"))
                {
                    object value = ByteUtil.BytesToParticle(particleType, dataBuffer, dataOffset, particleBytesSize);
                    throw new AerospikeException(ResultCode.QUERY_GENERIC, value.ToString());
                }
                else
                {
                    throw new AerospikeException(ResultCode.PARSE_ERROR, "Query aggregate expected bin name SUCCESS.  Received " + name);
                }
            }

            object aggregateValue = LuaInstance.BytesToLua(particleType, dataBuffer, dataOffset, particleBytesSize);
            dataOffset += particleBytesSize;

            if (!valid)
            {
                throw new AerospikeException.QueryTerminated();
            }

            if (aggregateValue != null)
            {
                try
                {
                    inputQueue.Add(aggregateValue, cancelToken);
                }
                catch (OperationCanceledException)
                {
                }
            }
#endif
        }
コード例 #16
0
        private bool ParseGroup(int receiveSize)
        {
            while (dataOffset < receiveSize)
            {
                dataOffset += 3;
                info3       = dataBuffer[dataOffset];
                dataOffset += 2;
                resultCode  = dataBuffer[dataOffset];

                // The only valid server return codes are "ok" and "not found".
                // If other return codes are received, then abort the batch.
                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR || resultCode == ResultCode.FILTERED_OUT)
                    {
                        if (stopOnNotFound)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        throw new AerospikeException(resultCode);
                    }
                }

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) != 0)
                {
                    return(false);
                }

                dataOffset++;
                generation  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                expiration  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                batchIndex  = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;
                fieldCount  = ByteUtil.BytesToShort(dataBuffer, dataOffset);
                dataOffset += 2;
                opCount     = ByteUtil.BytesToShort(dataBuffer, dataOffset);
                dataOffset += 2;

                Key key = ParseKey(fieldCount);
                ParseRow(key);
            }
            return(true);
        }
コード例 #17
0
        /// <summary>
        /// Parse all results in the batch.  Add records to shared list.
        /// If the record was not found, the bins will be null.
        /// </summary>
        protected internal override bool ParseRecordResults(int receiveSize)
        {
            //Parse each message response and add it to the result array
            dataOffset = 0;

            while (dataOffset < receiveSize)
            {
                ReadBytes(MSG_REMAINING_HEADER_SIZE);
                int resultCode = dataBuffer[5];

                // The only valid server return codes are "ok" and "not found".
                // If other return codes are received, then abort the batch.
                if (resultCode != 0 && resultCode != ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    throw new AerospikeException(resultCode);
                }

                byte info3 = dataBuffer[3];

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST)
                {
                    return(false);
                }

                int generation = ByteUtil.BytesToInt(dataBuffer, 6);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 10);
                int fieldCount = ByteUtil.BytesToShort(dataBuffer, 18);
                int opCount    = ByteUtil.BytesToShort(dataBuffer, 20);

                Key key    = ParseKey(fieldCount);
                int offset = batch.offsets[index++];

                if (Util.ByteArrayEquals(key.digest, keys[offset].digest))
                {
                    if (resultCode == 0)
                    {
                        records[offset] = ParseRecord(opCount, generation, expiration);
                    }
                }
                else
                {
                    throw new AerospikeException.Parse("Unexpected batch key returned: " + key.ns + ',' + ByteUtil.BytesToHexString(key.digest) + ',' + index + ',' + offset);
                }
            }
            return(true);
        }
コード例 #18
0
        private bool ParseGroup()
        {
            // Parse each message response and add it to the result array
            dataOffset = 0;

            while (dataOffset < dataLength)
            {
                resultCode = dataBuffer[dataOffset + 5];

                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                    {
                        if (stopOnNotFound)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        throw new AerospikeException(resultCode);
                    }
                }

                // If this is the end marker of the response, do not proceed further
                if ((dataBuffer[dataOffset + 3] & Command.INFO3_LAST) != 0)
                {
                    return(true);
                }
                generation = ByteUtil.BytesToInt(dataBuffer, dataOffset + 6);
                expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset + 10);
                batchIndex = ByteUtil.BytesToInt(dataBuffer, dataOffset + 14);
                fieldCount = ByteUtil.BytesToShort(dataBuffer, dataOffset + 18);
                opCount    = ByteUtil.BytesToShort(dataBuffer, dataOffset + 20);

                dataOffset += Command.MSG_REMAINING_HEADER_SIZE;

                if (!valid)
                {
                    throw new AerospikeException.QueryTerminated();
                }

                Key key = ParseKey();
                ParseRow(key);
            }
            return(false);
        }
コード例 #19
0
        protected internal sealed override void ParseResult()
        {
            int resultCode = dataBuffer[dataOffset + 5];
            int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset + 6);
            int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset + 10);
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, dataOffset + 18);
            int opCount    = ByteUtil.BytesToShort(dataBuffer, dataOffset + 20);

            dataOffset += Command.MSG_REMAINING_HEADER_SIZE;

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #20
0
        private bool ParseGroup(int receiveSize)
        {
            //Parse each message response and add it to the result array
            dataOffset = 0;

            while (dataOffset < receiveSize)
            {
                ReadBytes(MSG_REMAINING_HEADER_SIZE);
                resultCode = dataBuffer[5];

                // The only valid server return codes are "ok" and "not found".
                // If other return codes are received, then abort the batch.
                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                    {
                        if (stopOnNotFound)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        throw new AerospikeException(resultCode);
                    }
                }

                byte info3 = dataBuffer[3];

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST)
                {
                    return(false);
                }

                generation = ByteUtil.BytesToInt(dataBuffer, 6);
                expiration = ByteUtil.BytesToInt(dataBuffer, 10);
                batchIndex = ByteUtil.BytesToInt(dataBuffer, 14);
                fieldCount = ByteUtil.BytesToShort(dataBuffer, 18);
                opCount    = ByteUtil.BytesToShort(dataBuffer, 20);

                Key key = ParseKey(fieldCount);
                ParseRow(key);
            }
            return(true);
        }
コード例 #21
0
        protected internal Key ParseKey()
        {
            byte[] digest  = null;
            string ns      = null;
            string setName = null;
            Value  userKey = null;

            for (int i = 0; i < fieldCount; i++)
            {
                int fieldlen = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 4;

                int fieldtype = dataBuffer[dataOffset++];
                int size      = fieldlen - 1;

                switch (fieldtype)
                {
                case FieldType.DIGEST_RIPE:
                    digest = new byte[size];
                    Array.Copy(dataBuffer, dataOffset, digest, 0, size);
                    dataOffset += size;
                    break;

                case FieldType.NAMESPACE:
                    ns          = ByteUtil.Utf8ToString(dataBuffer, dataOffset, size);
                    dataOffset += size;
                    break;

                case FieldType.TABLE:
                    setName     = ByteUtil.Utf8ToString(dataBuffer, dataOffset, size);
                    dataOffset += size;
                    break;

                case FieldType.KEY:
                    int type = dataBuffer[dataOffset++];
                    size--;
                    userKey     = ByteUtil.BytesToKeyValue(type, dataBuffer, dataOffset, size);
                    dataOffset += size;
                    break;
                }
            }
            return(new Key(ns, digest, setName, userKey));
        }
コード例 #22
0
        private Record ParseRecord(int opCount, int fieldCount, int generation, int expiration)
        {
            Dictionary <string, object> bins = null;

            // There can be fields in the response (setname etc).
            // But for now, ignore them. Expose them to the API if needed in the future.
            if (fieldCount != 0)
            {
                // Just skip over all the fields
                for (int i = 0; i < fieldCount; i++)
                {
                    int fieldSize = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                    dataOffset += 4 + fieldSize;
                }
            }

            for (int i = 0; i < opCount; i++)
            {
                int opSize = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                dataOffset += 5;
                byte particleType = dataBuffer[dataOffset];
                dataOffset += 2;
                byte   nameSize = dataBuffer[dataOffset++];
                string name     = ByteUtil.Utf8ToString(dataBuffer, dataOffset, nameSize);
                dataOffset += nameSize;

                int    particleBytesSize = (int)(opSize - (4 + nameSize));
                object value             = ByteUtil.BytesToParticle(particleType, dataBuffer, dataOffset, particleBytesSize);
                dataOffset += particleBytesSize;

                if (bins == null)
                {
                    bins = new Dictionary <string, object>();
                }
                AddBin(bins, name, value);
            }
            return(new Record(bins, generation, expiration));
        }
コード例 #23
0
        protected internal override void ParseResult()
        {
            int resultCode = dataBuffer[dataOffset + 5];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset + 6);
                int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset + 10);

                record = new Record(null, generation, expiration);
            }
            else
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    record = null;
                }
                else
                {
                    throw new AerospikeException(resultCode);
                }
            }
        }
コード例 #24
0
        /// <summary>
        /// Parses the given byte buffer and populate the result object.
        /// Returns the number of bytes that were parsed from the given buffer.
        /// </summary>
        private Record ParseRecord(int opCount, int generation, int expiration)
        {
            Dictionary <string, object> bins = null;

            for (int i = 0; i < opCount; i++)
            {
                if (!valid)
                {
                    throw new AerospikeException.QueryTerminated();
                }

                ReadBytes(8);
                int  opSize       = ByteUtil.BytesToInt(dataBuffer, 0);
                byte particleType = dataBuffer[5];
                byte nameSize     = dataBuffer[7];

                ReadBytes(nameSize);
                string name = ByteUtil.Utf8ToString(dataBuffer, 0, nameSize);

                int particleBytesSize = (int)(opSize - (4 + nameSize));
                ReadBytes(particleBytesSize);
                object value = ByteUtil.BytesToParticle(particleType, dataBuffer, 0, particleBytesSize);

                // Currently, the batch command returns all the bins even if a subset of
                // the bins are requested. We have to filter it on the client side.
                // TODO: Filter batch bins on server!
                if (binNames == null || binNames.Contains(name))
                {
                    if (bins == null)
                    {
                        bins = new Dictionary <string, object>();
                    }
                    bins[name] = value;
                }
            }
            return(new Record(bins, generation, expiration));
        }
コード例 #25
0
        protected internal Key ParseKey(int fieldCount)
        {
            byte[] digest  = null;
            string ns      = null;
            string setName = null;
            Value  userKey = null;

            for (int i = 0; i < fieldCount; i++)
            {
                ReadBytes(4);
                int fieldlen = ByteUtil.BytesToInt(dataBuffer, 0);
                ReadBytes(fieldlen);
                int fieldtype = dataBuffer[0];
                int size      = fieldlen - 1;

                switch (fieldtype)
                {
                case FieldType.DIGEST_RIPE:
                    digest = new byte[size];
                    Array.Copy(dataBuffer, 1, digest, 0, size);
                    break;

                case FieldType.NAMESPACE:
                    ns = ByteUtil.Utf8ToString(dataBuffer, 1, size);
                    break;

                case FieldType.TABLE:
                    setName = ByteUtil.Utf8ToString(dataBuffer, 1, size);
                    break;

                case FieldType.KEY:
                    userKey = ByteUtil.BytesToKeyValue(dataBuffer[1], dataBuffer, 2, size - 1);
                    break;
                }
            }
            return(new Key(ns, digest, setName, userKey));
        }
コード例 #26
0
        protected internal override bool ParseRecordResults(int receiveSize)
        {
            // Read/parse remaining message bytes one record at a time.
            dataOffset = 0;

            while (dataOffset < receiveSize)
            {
                ReadBytes(MSG_REMAINING_HEADER_SIZE);
                int resultCode = dataBuffer[5];

                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                    {
                        return(false);
                    }
                    throw new AerospikeException(resultCode);
                }

                byte info3 = dataBuffer[3];

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST)
                {
                    return(false);
                }

                int generation = ByteUtil.BytesToInt(dataBuffer, 6);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 10);
                int fieldCount = ByteUtil.BytesToShort(dataBuffer, 18);
                int opCount    = ByteUtil.BytesToShort(dataBuffer, 20);

                Key key = ParseKey(fieldCount);

                // Parse bins.
                Dictionary <string, object> bins = null;

                for (int i = 0; i < opCount; i++)
                {
                    ReadBytes(8);
                    int  opSize       = ByteUtil.BytesToInt(dataBuffer, 0);
                    byte particleType = dataBuffer[5];
                    byte nameSize     = dataBuffer[7];

                    ReadBytes(nameSize);
                    string name = ByteUtil.Utf8ToString(dataBuffer, 0, nameSize);

                    int particleBytesSize = (int)(opSize - (4 + nameSize));
                    ReadBytes(particleBytesSize);
                    object value = ByteUtil.BytesToParticle(particleType, dataBuffer, 0, particleBytesSize);

                    if (bins == null)
                    {
                        bins = new Dictionary <string, object>();
                    }
                    bins[name] = value;
                }

                if (!valid)
                {
                    throw new AerospikeException.ScanTerminated();
                }

                // Call the callback function.
                callback(key, new Record(bins, generation, expiration));
            }
            return(true);
        }
コード例 #27
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, 8);

            long sz          = ByteUtil.BytesToLong(dataBuffer, 0);
            int  receiveSize = (int)(sz & 0xFFFFFFFFFFFFL);

            if (receiveSize <= 0)
            {
                throw new AerospikeException("Invalid receive size: " + receiveSize);
            }

            SizeBuffer(receiveSize);
            conn.ReadFully(dataBuffer, receiveSize);
            conn.UpdateLastUsed();

            ulong type = (ulong)((sz >> 48) & 0xff);

            if (type == Command.AS_MSG_TYPE)
            {
                dataOffset = 5;
            }
            else if (type == Command.MSG_TYPE_COMPRESSED)
            {
                int    usize = (int)ByteUtil.BytesToLong(dataBuffer, 0);
                byte[] ubuf  = new byte[usize];

                ByteUtil.Decompress(dataBuffer, 8, receiveSize, ubuf, usize);
                dataBuffer = ubuf;
                dataOffset = 13;
            }
            else
            {
                throw new AerospikeException("Invalid proto type: " + type + " Expected: " + Command.AS_MSG_TYPE);
            }

            int resultCode = dataBuffer[dataOffset];

            dataOffset++;
            int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 4;
            int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 8;
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;
            int opCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #28
0
        protected internal override bool ParseRecordResults(int receiveSize)
        {
            // Read/parse remaining message bytes one record at a time.
            dataOffset = 0;

            while (dataOffset < receiveSize)
            {
                ReadBytes(MSG_REMAINING_HEADER_SIZE);
                int resultCode = dataBuffer[5];

                if (resultCode != 0)
                {
                    if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                    {
                        return(false);
                    }
                    throw new AerospikeException(resultCode);
                }

                byte info3 = dataBuffer[3];

                // If this is the end marker of the response, do not proceed further
                if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST)
                {
                    return(false);
                }

                int fieldCount = ByteUtil.BytesToShort(dataBuffer, 18);
                int opCount    = ByteUtil.BytesToShort(dataBuffer, 20);

                ParseKey(fieldCount);

                if (opCount != 1)
                {
                    throw new AerospikeException("Query aggregate expected exactly one bin.  Received " + opCount);
                }

                // Parse aggregateValue.
                ReadBytes(8);
                int  opSize       = ByteUtil.BytesToInt(dataBuffer, 0);
                byte particleType = dataBuffer[5];
                byte nameSize     = dataBuffer[7];

                ReadBytes(nameSize);
                string name = ByteUtil.Utf8ToString(dataBuffer, 0, nameSize);

                int particleBytesSize = (int)(opSize - (4 + nameSize));
                ReadBytes(particleBytesSize);

                if (!name.Equals("SUCCESS"))
                {
                    if (name.Equals("FAILURE"))
                    {
                        object value = ByteUtil.BytesToParticle(particleType, dataBuffer, 0, particleBytesSize);
                        throw new AerospikeException(ResultCode.QUERY_GENERIC, value.ToString());
                    }
                    else
                    {
                        throw new AerospikeException(ResultCode.QUERY_GENERIC, "Query aggregate expected bin name SUCCESS.  Received " + name);
                    }
                }

                object aggregateValue = LuaInstance.BytesToLua(particleType, dataBuffer, 0, particleBytesSize);

                if (!valid)
                {
                    throw new AerospikeException.QueryTerminated();
                }

                if (aggregateValue != null)
                {
                    try
                    {
                        inputQueue.Add(aggregateValue, cancelToken);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                }
            }
            return(true);
        }
コード例 #29
0
        public void Login(Cluster cluster, Connection conn, out byte[] sessionToken, out DateTime?sessionExpiration)
        {
            sessionToken      = null;
            sessionExpiration = null;
            dataOffset        = 8;

            conn.SetTimeout(cluster.loginTimeout);

            try
            {
                if (cluster.authMode == AuthMode.INTERNAL)
                {
                    WriteHeader(LOGIN, 2);
                    WriteField(USER, cluster.user);
                    WriteField(CREDENTIAL, cluster.passwordHash);
                }
                else
                {
                    WriteHeader(LOGIN, 3);
                    WriteField(USER, cluster.user);
                    WriteField(CREDENTIAL, cluster.passwordHash);
                    WriteField(CLEAR_PASSWORD, cluster.password);
                }
                WriteSize();
                conn.Write(dataBuffer, dataOffset);
                conn.ReadFully(dataBuffer, HEADER_SIZE);

                int result = dataBuffer[RESULT_CODE];

                if (result != 0)
                {
                    if (result == INVALID_COMMAND)
                    {
                        // New login not supported.  Try old authentication.
                        AuthenticateOld(cluster, conn);
                        return;
                    }

                    // login failed.
                    throw new AerospikeException(result, "Login failed");
                }

                // Read session token.
                long size        = ByteUtil.BytesToLong(dataBuffer, 0);
                int  receiveSize = ((int)(size & 0xFFFFFFFFFFFFL)) - HEADER_REMAINING;
                int  fieldCount  = dataBuffer[11];

                if (receiveSize <= 0 || receiveSize > dataBuffer.Length || fieldCount <= 0)
                {
                    throw new AerospikeException(result, "Failed to retrieve session token");
                }

                conn.ReadFully(dataBuffer, receiveSize);
                dataOffset = 0;

                for (int i = 0; i < fieldCount; i++)
                {
                    int len = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                    dataOffset += 4;
                    int id = dataBuffer[dataOffset++];
                    len--;

                    if (id == SESSION_TOKEN)
                    {
                        sessionToken = new byte[len];
                        Array.Copy(dataBuffer, dataOffset, sessionToken, 0, len);
                    }
                    else if (id == SESSION_TTL)
                    {
                        // Subtract 60 seconds from ttl so client session expires before server session.
                        long seconds = ByteUtil.BytesToUInt(dataBuffer, dataOffset) - 60;

                        if (seconds > 0)
                        {
                            sessionExpiration = DateTime.UtcNow.AddSeconds(seconds);
                        }
                        else
                        {
                            Log.Warn("Invalid session TTL: " + seconds);
                        }
                    }
                    dataOffset += len;
                }

                if (sessionToken == null)
                {
                    throw new AerospikeException(result, "Failed to retrieve session token");
                }
            }
            finally
            {
                conn.SetTimeout(cluster.connectionTimeout);
            }
        }
コード例 #30
0
        private object UnpackObject()
        {
            int type = buffer[offset++];

            switch (type)
            {
            case 0xc0:                     // nil
            {
                return(null);
            }

            case 0xc3:                     // boolean true
            {
                return(true);
            }

            case 0xc2:                     // boolean false
            {
                return(false);
            }

            case 0xca:                     // float
            {
                float val = ByteUtil.BytesToFloat(buffer, offset);
                offset += 4;
                return(val);
            }

            case 0xcb:                     // double
            {
                double val = ByteUtil.BytesToDouble(buffer, offset);
                offset += 8;
                return(val);
            }

            case 0xd0:                     // signed 8 bit integer
            {
                return((long)(sbyte)(buffer[offset++]));
            }

            case 0xcc:                     // unsigned 8 bit integer
            {
                return((long)(buffer[offset++]));
            }

            case 0xd1:                     // signed 16 bit integer
            {
                int val = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
                return((long)(short)val);
            }

            case 0xcd:                     // unsigned 16 bit integer
            {
                int val = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
                return((long)val);
            }

            case 0xd2:                     // signed 32 bit integer
            {
                int val = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
                return((long)val);
            }

            case 0xce:                     // unsigned 32 bit integer
            {
                uint val = ByteUtil.BytesToUInt(buffer, offset);
                offset += 4;
                return((long)val);
            }

            case 0xd3:                     // signed 64 bit integer
            {
                long val = ByteUtil.BytesToLong(buffer, offset);
                offset += 8;
                return(val);
            }

            case 0xcf:                     // unsigned 64 bit integer
            {
                // The contract is to always return long.
                // The caller can always cast back to ulong.
                long val = ByteUtil.BytesToLong(buffer, offset);
                offset += 8;
                return(val);
            }

            case 0xda:                     // raw bytes with 16 bit header
            {
                int count = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
                return(UnpackBlob(count));
            }

            case 0xdb:                     // raw bytes with 32 bit header
            {
                // Array length is restricted to positive int values (0 - int.MAX_VALUE).
                int count = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
                return(UnpackBlob(count));
            }

            case 0xdc:                     // list with 16 bit header
            {
                int count = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
                return(UnpackList(count));
            }

            case 0xdd:                     // list with 32 bit header
            {
                // List size is restricted to positive int values (0 - int.MAX_VALUE).
                int count = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
                return(UnpackList(count));
            }

            case 0xde:                     // map with 16 bit header
            {
                int count = ByteUtil.BytesToShort(buffer, offset);
                offset += 2;
                return(UnpackMap(count));
            }

            case 0xdf:                     // map with 32 bit header
            {
                // Map size is restricted to positive int values (0 - int.MAX_VALUE).
                int count = ByteUtil.BytesToInt(buffer, offset);
                offset += 4;
                return(UnpackMap(count));
            }

            default:
            {
                if ((type & 0xe0) == 0xa0)                         // raw bytes with 8 bit combined header
                {
                    return(UnpackBlob(type & 0x1f));
                }

                if ((type & 0xf0) == 0x80)                         // map with 8 bit combined header
                {
                    return(UnpackMap(type & 0x0f));
                }

                if ((type & 0xf0) == 0x90)                         // list with 8 bit combined header
                {
                    return(UnpackList(type & 0x0f));
                }

                if (type < 0x80)                         // 8 bit combined unsigned integer
                {
                    return((long)type);
                }

                if (type >= 0xe0)                         // 8 bit combined signed integer
                {
                    return((long)(type - 0xe0 - 32));
                }
                throw new IOException("Unknown unpack type: " + type);
            }
            }
        }