Example #1
0
        private void WriteField(string str, int type)
        {
            int len = ByteUtil.StringToUtf8(str, dataBuffer, dataOffset + FIELD_HEADER_SIZE);

            WriteFieldHeader(len, type);
            dataOffset += len;
        }
Example #2
0
        public void PackString(string val)
        {
            int size = ByteUtil.EstimateSizeUtf8(val) + 1;

            if (size < 32)
            {
                PackByte((byte)(0xa0 | size));
            }
            // TODO: Enable this code after all servers/clients
            // have been upgraded to handle 8 bit string length format.

            /*
             * else if (size < 256)
             * {
             *      PackByte(0xd9, (byte)size);
             * }
             */
            else if (size < 65536)
            {
                PackShort(0xda, (ushort)size);
            }
            else
            {
                PackInt(0xdb, (uint)size);
            }

            if (offset + size > buffer.Length)
            {
                Resize(size);
            }
            buffer[offset++] = (byte)ParticleType.STRING;
            offset          += ByteUtil.StringToUtf8(val, buffer, offset);
        }
        public Cluster(ClientPolicy policy, Host[] hosts)
        {
            this.seeds = hosts;

            if (policy.user != null && policy.user.Length > 0)
            {
                this.user = ByteUtil.StringToUtf8(policy.user);

                string pass = policy.password;

                if (pass == null)
                {
                    pass = "";
                }

                if (!(pass.Length == 60 && pass.StartsWith("$2a$")))
                {
                    pass = AdminCommand.HashPassword(pass);
                }
                this.password = ByteUtil.StringToUtf8(pass);
            }

            connectionQueueSize = policy.maxConnsPerNode;
            connectionTimeout   = policy.timeout;
            maxSocketIdleMillis = 1000 * ((policy.maxSocketIdle <= MaxSocketIdleSecondLimit) ? policy.maxSocketIdle : MaxSocketIdleSecondLimit);
            tendInterval        = policy.tendInterval;
            ipMap = policy.ipMap;
            requestProleReplicas = policy.requestProleReplicas;
            useServicesAlternate = policy.useServicesAlternate;

            aliases      = new Dictionary <Host, Node>();
            nodes        = new Node[0];
            partitionMap = new Dictionary <string, Node[][]>();
        }
 protected internal void ChangePassword(byte[] user, string password)
 {
     if (this.user != null && Util.ByteArrayEquals(user, this.user))
     {
         this.password = ByteUtil.StringToUtf8(password);
     }
 }
Example #5
0
 public void PackGeoJSON(string val)
 {
     byte[] buffer = ByteUtil.StringToUtf8(val);
     PackByteArrayBegin(buffer.Length + 1);
     PackByte(ParticleType.GEOJSON);
     PackByteArray(buffer, 0, buffer.Length);
 }
Example #6
0
        /// <summary>
        /// Send multiple commands to server and store results.
        /// This constructor is used internally.
        /// The static request methods should be used instead.
        /// </summary>
        /// <param name="conn">connection to server node</param>
        /// <param name="commands">commands sent to server</param>
        public Info(Connection conn, params string[] commands)
        {
            buffer = ThreadLocalData.GetBuffer();

            // First, do quick conservative buffer size estimate.
            offset = 8;

            foreach (string command in commands)
            {
                offset += command.Length * 2 + 1;
            }

            // If conservative estimate may be exceeded, get exact estimate
            // to preserve memory and resize buffer.
            if (offset > buffer.Length)
            {
                offset = 8;

                foreach (string command in commands)
                {
                    offset += ByteUtil.EstimateSizeUtf8(command) + 1;
                }
                ResizeBuffer(offset);
            }
            offset = 8;             // Skip size field.

            // The command format is: <name1>\n<name2>\n...
            foreach (string command in commands)
            {
                offset          += ByteUtil.StringToUtf8(command, buffer, offset);
                buffer[offset++] = (byte)'\n';
            }
            SendCommand(conn);
        }
        public Cluster(ClientPolicy policy, Host[] hosts)
        {
            this.seeds = hosts;

            if (policy.user != null && policy.user.Length > 0)
            {
                this.user = ByteUtil.StringToUtf8(policy.user);

                string pass = policy.password;

                if (pass == null)
                {
                    pass = "";
                }

                if (!(pass.Length == 60 && pass.StartsWith("$2a$")))
                {
                    pass = AdminCommand.HashPassword(pass);
                }
                this.password = ByteUtil.StringToUtf8(pass);
            }

            connectionQueueSize = policy.maxThreads + 1;             // Add one connection for tend thread.
            connectionTimeout   = policy.timeout;
            maxSocketIdle       = policy.maxSocketIdle;
            tendInterval        = policy.tendInterval;
            ipMap = policy.ipMap;
            requestProleReplicas = policy.requestProleReplicas;

            aliases      = new Dictionary <Host, Node>();
            nodes        = new Node[0];
            partitionMap = new Dictionary <string, Node[][]>();
        }
Example #8
0
        private void WriteField(byte id, string str)
        {
            int len = ByteUtil.StringToUtf8(str, dataBuffer, dataOffset + FIELD_HEADER_SIZE);

            WriteFieldHeader(id, len);
            dataOffset += len;
        }
Example #9
0
        public Cluster(ClientPolicy policy, Host[] hosts)
        {
            this.clusterName = policy.clusterName;

            // Default TLS names when TLS enabled.
            if (policy.tlsPolicy != null)
            {
                bool useClusterName = HasClusterName;

                for (int i = 0; i < hosts.Length; i++)
                {
                    Host host = hosts[i];

                    if (host.tlsName == null)
                    {
                        string tlsName = useClusterName ? clusterName : host.name;
                        hosts[i] = new Host(host.name, tlsName, host.port);
                    }
                }
            }
            this.seeds = hosts;

            if (policy.user != null && policy.user.Length > 0)
            {
                this.user = ByteUtil.StringToUtf8(policy.user);

                string pass = policy.password;

                if (pass == null)
                {
                    pass = "";
                }

                if (!(pass.Length == 60 && pass.StartsWith("$2a$")))
                {
                    pass = AdminCommand.HashPassword(pass);
                }
                this.password = ByteUtil.StringToUtf8(pass);
            }

            tlsPolicy           = policy.tlsPolicy;
            connectionQueueSize = policy.maxConnsPerNode;
            connPoolsPerNode    = policy.connPoolsPerNode;
            connectionTimeout   = policy.timeout;
            maxSocketIdleMillis = 1000 * ((policy.maxSocketIdle <= MaxSocketIdleSecondLimit) ? policy.maxSocketIdle : MaxSocketIdleSecondLimit);
            tendInterval        = policy.tendInterval;
            ipMap = policy.ipMap;
            requestProleReplicas = policy.requestProleReplicas;
            useServicesAlternate = policy.useServicesAlternate;

            aliases      = new Dictionary <Host, Node>();
            nodesMap     = new Dictionary <string, Node>();
            nodes        = new Node[0];
            partitionMap = new Dictionary <string, Partitions>();
            cancel       = new CancellationTokenSource();
            cancelToken  = cancel.Token;
        }
Example #10
0
        /// <summary>
        /// Generate unique server hash value from set name, key type and user defined key.
        /// The hash function is RIPEMD-160 (a 160 bit hash).
        /// </summary>
        /// <param name="setName">optional set name, enter null when set does not exist</param>
        /// <param name="key">record identifier, unique within set</param>
        /// <returns>unique server hash value</returns>
        /// <exception cref="AerospikeException">if digest computation fails</exception>
        public static byte[] ComputeDigest(string setName, Value key)
        {
            byte[] buffer = ThreadLocalData.GetBuffer();
            int    offset = ByteUtil.StringToUtf8(setName, buffer, 0);

            buffer[offset++] = (byte)key.Type;
            offset          += key.Write(buffer, offset);

            return(Hash.ComputeHash(buffer, offset));
        }
Example #11
0
        public void PackString(string val)
        {
            int size = ByteUtil.EstimateSizeUtf8(val);

            PackStringBegin(size);

            if (offset + size > buffer.Length)
            {
                Resize(size);
            }
            offset += ByteUtil.StringToUtf8(val, buffer, offset);
        }
Example #12
0
        private void WriteOperation(string name, Operation.Type operationType)
        {
            int nameLength = ByteUtil.StringToUtf8(name, dataBuffer, dataOffset + OPERATION_HEADER_SIZE);

            ByteUtil.IntToBytes((uint)(nameLength + 4), dataBuffer, dataOffset);
            dataOffset += 4;
            dataBuffer[dataOffset++] = Operation.GetProtocolType(operationType);
            dataBuffer[dataOffset++] = (byte)0;
            dataBuffer[dataOffset++] = (byte)0;
            dataBuffer[dataOffset++] = (byte)nameLength;
            dataOffset += nameLength;
        }
Example #13
0
        private void WriteOperation(Operation operation)
        {
            int nameLength  = ByteUtil.StringToUtf8(operation.binName, dataBuffer, dataOffset + OPERATION_HEADER_SIZE);
            int valueLength = operation.value.Write(dataBuffer, dataOffset + OPERATION_HEADER_SIZE + nameLength);

            ByteUtil.IntToBytes((uint)(nameLength + valueLength + 4), dataBuffer, dataOffset);
            dataOffset += 4;
            dataBuffer[dataOffset++] = Operation.GetProtocolType(operation.type);
            dataBuffer[dataOffset++] = (byte)operation.value.Type;
            dataBuffer[dataOffset++] = (byte)0;
            dataBuffer[dataOffset++] = (byte)nameLength;
            dataOffset += nameLength + valueLength;
        }
Example #14
0
        public void PackParticleString(string val)
        {
            int size = ByteUtil.EstimateSizeUtf8(val) + 1;

            PackStringBegin(size);

            if (offset + size > buffer.Length)
            {
                Resize(size);
            }
            buffer[offset++] = (byte)ParticleType.STRING;
            offset          += ByteUtil.StringToUtf8(val, buffer, offset);
        }
            public override int Write(byte[] buf, int offset)
            {
                // Write value type
                ByteUtil.ShortToBytes(type, buf, offset);
                offset += 2;

                // Write value
                int len = ByteUtil.StringToUtf8(value, buf, offset + 4);

                ByteUtil.IntToBytes((uint)len, buf, offset);
                offset += 4 + len;
                return(offset);
            }
 public bool SetString(string value, int offset)
 {
     try
     {
         int len = ByteUtil.EstimateSizeUtf8(value);
         EnsureCapacity(offset + len);
         len = ByteUtil.StringToUtf8(value, bytes, offset);
         ResetSize(offset + len);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Example #17
0
        private void WriteRoles(IList <string> roles)
        {
            int offset = dataOffset + FIELD_HEADER_SIZE;

            dataBuffer[offset++] = (byte)roles.Count;

            foreach (string role in roles)
            {
                int len = ByteUtil.StringToUtf8(role, dataBuffer, offset + 1);
                dataBuffer[offset] = (byte)len;
                offset            += len + 1;
            }

            int size = offset - dataOffset - FIELD_HEADER_SIZE;

            WriteFieldHeader(ROLES, size);
            dataOffset = offset;
        }
Example #18
0
        private void WritePrivileges(IList <Privilege> privileges)
        {
            int offset = dataOffset + FIELD_HEADER_SIZE;

            dataBuffer[offset++] = (byte)privileges.Count;

            foreach (Privilege privilege in privileges)
            {
                dataBuffer[offset++] = (byte)privilege.code;

                if (privilege.CanScope())
                {
                    if (!(privilege.setName == null || privilege.setName.Length == 0) &&
                        (privilege.ns == null || privilege.ns.Length == 0))
                    {
                        throw new AerospikeException(ResultCode.INVALID_PRIVILEGE, "Admin privilege '" +
                                                     privilege.PrivilegeCodeToString() + "' has a set scope with an empty namespace.");
                    }

                    int len = ByteUtil.StringToUtf8(privilege.ns, dataBuffer, offset + 1);
                    dataBuffer[offset] = (byte)len;
                    offset            += len + 1;

                    len = ByteUtil.StringToUtf8(privilege.setName, dataBuffer, offset + 1);
                    dataBuffer[offset] = (byte)len;
                    offset            += len + 1;
                }
                else
                {
                    if (!(privilege.ns == null || privilege.ns.Length == 0) ||
                        !(privilege.setName == null || privilege.setName.Length == 0))
                    {
                        throw new AerospikeException(ResultCode.INVALID_PRIVILEGE, "Admin global privilege '" +
                                                     privilege.PrivilegeCodeToString() + "' has namespace/set scope which is invalid.");
                    }
                }
            }

            int size = offset - dataOffset - FIELD_HEADER_SIZE;

            WriteFieldHeader(PRIVILEGES, size);
            dataOffset = offset;
        }
Example #19
0
        //-------------------------------------------------------
        // Constructor
        //-------------------------------------------------------

        /// <summary>
        /// Send single command to server and store results.
        /// This constructor is used internally.
        /// The static request methods should be used instead.
        /// </summary>
        /// <param name="conn">connection to server node</param>
        /// <param name="command">command sent to server</param>
        public Info(Connection conn, string command)
        {
            buffer = ThreadLocalData.GetBuffer();

            // If conservative estimate may be exceeded, get exact estimate
            // to preserve memory and resize buffer.
            if ((command.Length * 2 + 9) > buffer.Length)
            {
                offset = ByteUtil.EstimateSizeUtf8(command) + 9;
                ResizeBuffer(offset);
            }
            offset = 8;             // Skip size field.

            // The command format is: <name1>\n<name2>\n...
            offset          += ByteUtil.StringToUtf8(command, buffer, offset);
            buffer[offset++] = (byte)'\n';

            SendCommand(conn);
        }
Example #20
0
        protected internal override void WriteBuffer()
        {
            dataOffset = 8;

            foreach (string command in commands)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(command) + 1;
            }

            SizeBuffer();
            dataOffset += 8;             // Skip size field.

            // The command format is: <name1>\n<name2>\n...
            foreach (string command in commands)
            {
                dataOffset += ByteUtil.StringToUtf8(command, dataBuffer, dataOffset);
                dataBuffer[dataOffset++] = (byte)'\n';
            }
            EndInfo();
        }
            public override int Write(byte[] buf, int offset)
            {
                // Write value type
                ByteUtil.ShortToBytes(type, buf, offset);
                offset += 2;

                // Write value
                int len = ByteUtil.StringToUtf8(value, buf, offset + 4 + 1 + 2);

                ByteUtil.IntToBytes((uint)(len + 1 + 2), buf, offset);
                offset += 4;

                buf[offset] = 0;                 // flags
                offset     += 1;

                ByteUtil.ShortToBytes(0, buf, offset);                 // ncells
                offset += 2;

                offset += len;
                return(offset);
            }
        private void WriteWhitelist(IList <string> whitelist)
        {
            int  offset = dataOffset + FIELD_HEADER_SIZE;
            bool comma  = false;

            foreach (string address in whitelist)
            {
                if (comma)
                {
                    dataBuffer[offset++] = (byte)',';
                }
                else
                {
                    comma = true;
                }
                offset += ByteUtil.StringToUtf8(address, dataBuffer, offset);
            }

            int size = offset - dataOffset - FIELD_HEADER_SIZE;

            WriteFieldHeader(WHITELIST, size);
            dataOffset = offset;
        }
 public override int Write(byte[] buffer, int offset)
 {
     return(ByteUtil.StringToUtf8(value, buffer, offset));
 }
Example #24
0
        protected internal void SetQuery(Policy policy, Statement statement, bool write)
        {
            byte[] functionArgBuffer = null;
            int    fieldCount        = 0;
            int    filterSize        = 0;
            int    binNameSize       = 0;

            Begin();

            if (statement.ns != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.ns) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.indexName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.indexName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.setName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.setName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            // Allocate space for TaskId field.
            dataOffset += 8 + FIELD_HEADER_SIZE;
            fieldCount++;

            if (statement.filter != null)
            {
                IndexCollectionType type = statement.filter.CollectionType;

                if (type != IndexCollectionType.DEFAULT)
                {
                    dataOffset += FIELD_HEADER_SIZE + 1;
                    fieldCount++;
                }

                dataOffset += FIELD_HEADER_SIZE;
                filterSize++;                 // num filters
                filterSize += statement.filter.EstimateSize();
                dataOffset += filterSize;
                fieldCount++;

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null)
                {
                    dataOffset += FIELD_HEADER_SIZE;
                    binNameSize++;                     // num bin names

                    foreach (string binName in statement.binNames)
                    {
                        binNameSize += ByteUtil.EstimateSizeUtf8(binName) + 1;
                    }
                    dataOffset += binNameSize;
                    fieldCount++;
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                // Estimate scan options size.
                dataOffset += 2 + FIELD_HEADER_SIZE;
                fieldCount++;

                // Estimate scan timeout size.
                dataOffset += 4 + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            PredExp[] predExp  = statement.PredExp;
            int       predSize = 0;

            if (predExp != null)
            {
                dataOffset += FIELD_HEADER_SIZE;
                predSize    = PredExp.EstimateSize(predExp);
                dataOffset += predSize;
                fieldCount++;
            }

            if (statement.functionName != null)
            {
                dataOffset += FIELD_HEADER_SIZE + 1;                 // udf type
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.packageName) + FIELD_HEADER_SIZE;
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.functionName) + FIELD_HEADER_SIZE;

                if (statement.functionArgs.Length > 0)
                {
                    functionArgBuffer = Packer.Pack(statement.functionArgs);
                }
                else
                {
                    functionArgBuffer = new byte[0];
                }
                dataOffset += FIELD_HEADER_SIZE + functionArgBuffer.Length;
                fieldCount += 4;
            }

            if (statement.filter == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        EstimateOperationSize(binName);
                    }
                }
            }

            SizeBuffer();
            int operationCount = (statement.filter == null && statement.binNames != null) ? statement.binNames.Length : 0;

            if (write)
            {
                WriteHeader((WritePolicy)policy, Command.INFO1_READ, Command.INFO2_WRITE, fieldCount, operationCount);
            }
            else
            {
                QueryPolicy qp       = (QueryPolicy)policy;
                int         readAttr = qp.includeBinData ? Command.INFO1_READ : Command.INFO1_READ | Command.INFO1_NOBINDATA;
                WriteHeader(policy, readAttr, 0, fieldCount, operationCount);
            }

            if (statement.ns != null)
            {
                WriteField(statement.ns, FieldType.NAMESPACE);
            }

            if (statement.indexName != null)
            {
                WriteField(statement.indexName, FieldType.INDEX_NAME);
            }

            if (statement.setName != null)
            {
                WriteField(statement.setName, FieldType.TABLE);
            }

            // Write taskId field
            WriteFieldHeader(8, FieldType.TRAN_ID);
            ByteUtil.LongToBytes(statement.taskId, dataBuffer, dataOffset);
            dataOffset += 8;

            if (statement.filter != null)
            {
                IndexCollectionType type = statement.filter.CollectionType;

                if (type != IndexCollectionType.DEFAULT)
                {
                    WriteFieldHeader(1, FieldType.INDEX_TYPE);
                    dataBuffer[dataOffset++] = (byte)type;
                }

                WriteFieldHeader(filterSize, FieldType.INDEX_RANGE);
                dataBuffer[dataOffset++] = (byte)1;
                dataOffset = statement.filter.Write(dataBuffer, dataOffset);

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null)
                {
                    WriteFieldHeader(binNameSize, FieldType.QUERY_BINLIST);
                    dataBuffer[dataOffset++] = (byte)statement.binNames.Length;

                    foreach (string binName in statement.binNames)
                    {
                        int len = ByteUtil.StringToUtf8(binName, dataBuffer, dataOffset + 1);
                        dataBuffer[dataOffset] = (byte)len;
                        dataOffset            += len + 1;
                    }
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                WriteFieldHeader(2, FieldType.SCAN_OPTIONS);
                byte priority = (byte)policy.priority;
                priority <<= 4;
                dataBuffer[dataOffset++] = priority;
                dataBuffer[dataOffset++] = (byte)100;

                // Write scan timeout
                WriteFieldHeader(4, FieldType.SCAN_TIMEOUT);
                dataOffset += ByteUtil.IntToBytes((uint)policy.socketTimeout, dataBuffer, dataOffset);
            }

            if (predExp != null)
            {
                WriteFieldHeader(predSize, FieldType.PREDEXP);
                dataOffset = PredExp.Write(predExp, dataBuffer, dataOffset);
            }

            if (statement.functionName != null)
            {
                WriteFieldHeader(1, FieldType.UDF_OP);
                dataBuffer[dataOffset++] = (statement.returnData) ? (byte)1 : (byte)2;
                WriteField(statement.packageName, FieldType.UDF_PACKAGE_NAME);
                WriteField(statement.functionName, FieldType.UDF_FUNCTION);
                WriteField(functionArgBuffer, FieldType.UDF_ARGLIST);
            }

            // Scan bin names are specified after all fields.
            if (statement.filter == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        WriteOperation(binName, Operation.Type.READ);
                    }
                }
            }
            End();
        }
Example #25
0
        public Cluster(ClientPolicy policy, Host[] hosts)
        {
            this.clusterName = policy.clusterName;
            tlsPolicy        = policy.tlsPolicy;
            this.authMode    = policy.authMode;

            // Default TLS names when TLS enabled.
            if (tlsPolicy != null)
            {
                bool useClusterName = HasClusterName;

                for (int i = 0; i < hosts.Length; i++)
                {
                    Host host = hosts[i];

                    if (host.tlsName == null)
                    {
                        string tlsName = useClusterName ? clusterName : host.name;
                        hosts[i] = new Host(host.name, tlsName, host.port);
                    }
                }
            }
            else
            {
                if (authMode == AuthMode.EXTERNAL)
                {
                    throw new AerospikeException("TLS is required for authentication mode: " + authMode);
                }
            }

            this.seeds = hosts;

            if (policy.user != null && policy.user.Length > 0)
            {
                this.user = ByteUtil.StringToUtf8(policy.user);

                // Only store clear text password if external authentication is used.
                if (authMode != AuthMode.INTERNAL)
                {
                    this.password = ByteUtil.StringToUtf8(policy.password);
                }

                string pass = policy.password;

                if (pass == null)
                {
                    pass = "";
                }

                if (!(pass.Length == 60 && pass.StartsWith("$2a$")))
                {
                    pass = AdminCommand.HashPassword(pass);
                }
                this.passwordHash = ByteUtil.StringToUtf8(pass);
            }

            connectionQueueSize = policy.maxConnsPerNode;
            connPoolsPerNode    = policy.connPoolsPerNode;
            connectionTimeout   = policy.timeout;
            loginTimeout        = policy.loginTimeout;
            maxSocketIdleMillis = 1000 * ((policy.maxSocketIdle <= MaxSocketIdleSecondLimit) ? policy.maxSocketIdle : MaxSocketIdleSecondLimit);
            tendInterval        = policy.tendInterval;
            ipMap = policy.ipMap;
            useServicesAlternate = policy.useServicesAlternate;
            rackAware            = policy.rackAware;
            rackId = policy.rackId;

            aliases      = new Dictionary <Host, Node>();
            nodesMap     = new Dictionary <string, Node>();
            nodes        = new Node[0];
            partitionMap = new Dictionary <string, Partitions>();
            cancel       = new CancellationTokenSource();
            cancelToken  = cancel.Token;
        }