private Record ParseRecord(int opCount, int fieldCount, int generation, int expiration) { // 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; } } 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; if (bins == null) { bins = new Dictionary <string, object>(); } bins[name] = value; } return(new Record(bins, generation, expiration)); }
protected internal Record ParseRecord() { 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; if (bins == null) { bins = new Dictionary <string, object>(); } bins[name] = value; } return(new Record(bins, generation, expiration)); }
internal void ParsePrivileges(Role role) { int size = base.dataBuffer[base.dataOffset++]; role.privileges = new List <Privilege>(size); for (int i = 0; i < size; i++) { Privilege priv = new Privilege(); priv.code = (PrivilegeCode)base.dataBuffer[base.dataOffset++]; if (priv.CanScope()) { int len = base.dataBuffer[base.dataOffset++]; priv.ns = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len); base.dataOffset += len; len = base.dataBuffer[base.dataOffset++]; priv.setName = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len); base.dataOffset += len; } role.privileges.Add(priv); } }
/// <summary> /// Get name. /// </summary> public string GetName() { int len = nameEnd - nameBegin; return(ByteUtil.Utf8ToString(parent.buffer, nameBegin, len)); }
private Dictionary <string, string> ParseMultiResponse() { Dictionary <string, string> responses = new Dictionary <string, string>(); int offset = 0; int begin = 0; while (offset < length) { byte b = buffer[offset]; if (b == '\t') { string name = ByteUtil.Utf8ToString(buffer, begin, offset - begin); CheckError(name); begin = ++offset; // Parse field value. while (offset < length) { if (buffer[offset] == '\n') { break; } offset++; } if (offset > begin) { string value = ByteUtil.Utf8ToString(buffer, begin, offset - begin); responses[name] = value; } else { responses[name] = null; } begin = ++offset; } else if (b == '\n') { if (offset > begin) { string name = ByteUtil.Utf8ToString(buffer, begin, offset - begin); CheckError(name); responses[name] = null; } begin = ++offset; } else { offset++; } } if (offset > begin) { string name = ByteUtil.Utf8ToString(buffer, begin, offset - begin); CheckError(name); responses[name] = null; } return(responses); }
/// <summary> /// Return single value from response buffer. /// </summary> public string GetValue() { SkipToValue(); return(ByteUtil.Utf8ToString(buffer, offset, length - offset - 1)); }
private string GetTruncatedResponse() { int max = (length > 200) ? 200 : length; return(ByteUtil.Utf8ToString(buffer, 0, max)); }
private void ParseReplicasAll(Node node, string command) { // Use low-level info methods and parse byte array directly for maximum performance. // Receive format: replicas-all\t // <ns1>:[regime],<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...; // <ns2>:[regime],<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...;\n ExpectName(command); int begin = offset; int regime = 0; while (offset < length) { if (buffer[offset] == ':') { // Parse namespace. string ns = ByteUtil.Utf8ToString(buffer, begin, offset - begin).Trim(); if (ns.Length <= 0 || ns.Length >= 32) { string response = GetTruncatedResponse(); throw new AerospikeException.Parse("Invalid partition namespace " + ns + ". Response=" + response); } begin = ++offset; // Parse regime. if (command == Replicas) { while (offset < length) { byte b = buffer[offset]; if (b == ',') { break; } offset++; } regime = Convert.ToInt32(Encoding.UTF8.GetString(buffer, begin, offset - begin)); begin = ++offset; } // Parse replica count. while (offset < length) { byte b = buffer[offset]; if (b == ',') { break; } offset++; } int replicaCount = Convert.ToInt32(Encoding.UTF8.GetString(buffer, begin, offset - begin)); // Ensure replicaCount is uniform. Partitions partitions; if (!map.TryGetValue(ns, out partitions)) { // Create new replica array. partitions = new Partitions(partitionCount, replicaCount, regime != 0); CopyPartitionMap(); map[ns] = partitions; } else if (partitions.replicas.Length != replicaCount) { if (Log.InfoEnabled()) { Log.Info("Namespace " + ns + " replication factor changed from " + partitions.replicas.Length + " to " + replicaCount); } // Resize partition map. Partitions tmp = new Partitions(partitions, replicaCount); CopyPartitionMap(); partitions = tmp; map[ns] = partitions; } // Parse partition bitmaps. for (int i = 0; i < replicaCount; i++) { begin = ++offset; // Find bitmap endpoint while (offset < length) { byte b = buffer[offset]; if (b == ',' || b == ';') { break; } offset++; } if (offset == begin) { string response = GetTruncatedResponse(); throw new AerospikeException.Parse("Empty partition id for namespace " + ns + ". Response=" + response); } // Log.info("Map: " + namespace + '[' + i + "] " + node); DecodeBitmap(node, partitions, i, regime, begin); } begin = ++offset; } else { offset++; } } }
private void ParseReplicasAll(Node node) { // Use low-level info methods and parse byte array directly for maximum performance. // Receive format: replicas-all\t // <ns1>:<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...; // <ns2>:<count>,<base 64 encoded bitmap1>,<base 64 encoded bitmap2>...;\n ExpectName(ReplicasAll); int begin = offset; while (offset < length) { if (buffer[offset] == ':') { // Parse namespace. string ns = ByteUtil.Utf8ToString(buffer, begin, offset - begin).Trim(); if (ns.Length <= 0 || ns.Length >= 32) { string response = GetTruncatedResponse(); throw new AerospikeException.Parse("Invalid partition namespace " + ns + ". Response=" + response); } begin = ++offset; // Parse replica count. while (offset < length) { byte b = buffer[offset]; if (b == ',') { break; } offset++; } int replicaCount = Convert.ToInt32(Encoding.UTF8.GetString(buffer, begin, offset - begin)); // Ensure replicaArray is correct size. Node[][] replicaArray; if (!map.TryGetValue(ns, out replicaArray)) { // Create new replica array. replicaArray = new Node[replicaCount][]; for (int i = 0; i < replicaCount; i++) { replicaArray[i] = new Node[partitionCount]; } CopyPartitionMap(); map[ns] = replicaArray; } else if (replicaArray.Length != replicaCount) { if (Log.InfoEnabled()) { Log.Info("Namespace " + ns + " replication factor changed from " + replicaArray.Length + " to " + replicaCount); } // Resize replica array. Node[][] replicaTarget = new Node[replicaCount][]; if (replicaArray.Length < replicaCount) { int i = 0; // Copy existing entries. for (; i < replicaArray.Length; i++) { replicaTarget[i] = replicaArray[i]; } // Create new entries. for (; i < replicaCount; i++) { replicaTarget[i] = new Node[partitionCount]; } } else { // Copy existing entries. for (int i = 0; i < replicaCount; i++) { replicaTarget[i] = replicaArray[i]; } } CopyPartitionMap(); replicaArray = replicaTarget; map[ns] = replicaArray; } // Parse partition bitmaps. for (int i = 0; i < replicaCount; i++) { begin = ++offset; // Find bitmap endpoint while (offset < length) { byte b = buffer[offset]; if (b == ',' || b == ';') { break; } offset++; } if (offset == begin) { string response = GetTruncatedResponse(); throw new AerospikeException.Parse("Empty partition id for namespace " + ns + ". Response=" + response); } // Log.info("Map: " + namespace + '[' + i + "] " + node); DecodeBitmap(node, replicaArray[i], begin); } begin = ++offset; } else { offset++; } } }
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 if (id == WHITELIST) { role.whitelist = ParseWhitelist(len); } else { base.dataOffset += len; } } if (role.name == null) { throw new AerospikeException(ResultCode.INVALID_ROLE); } if (role.privileges == null) { role.privileges = new List <Privilege>(0); } if (role.whitelist == null) { role.whitelist = new List <string>(0); } list.Add(role); } return(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); }
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); }