//------------------------------------------------------- // Private methods. //------------------------------------------------------- /// <summary> /// Issue request and set results buffer. This method is used internally. /// The static request methods should be used instead. /// </summary> /// <param name="conn">socket connection to server node</param> /// <exception cref="AerospikeException">if socket send or receive fails</exception> private void SendCommand(Connection conn) { try { // Write size field. ulong size = ((ulong)offset - 8L) | (2L << 56) | (1L << 48); ByteUtil.LongToBytes(size, buffer, 0); // Write. conn.Write(buffer, offset); // Read - reuse input buffer. conn.ReadFully(buffer, 8); size = (ulong)ByteUtil.BytesToLong(buffer, 0); length = (int)(size & 0xFFFFFFFFFFFFL); ResizeBuffer(length); conn.ReadFully(buffer, length); offset = 0; } catch (SocketException se) { throw new AerospikeException(se); } }
private void WriteSize() { // Write total size of message which is the current offset. ulong size = (ulong)(dataOffset - dataBegin - 8) | (MSG_VERSION << 56) | (MSG_TYPE << 48); ByteUtil.LongToBytes(size, dataBuffer, dataBegin); }
protected internal sealed override void End() { // Write total size of message. ulong size = ((ulong)dataOffset - 8) | (CL_MSG_VERSION << 56) | (AS_MSG_TYPE << 48); ByteUtil.LongToBytes(size, dataBuffer, 0); }
protected internal void End() { // Write total size of message which is the current offset. ulong size = ((ulong)dataOffset - 8) | (CL_MSG_VERSION << 56) | (AS_MSG_TYPE << 48); ByteUtil.LongToBytes(size, dataBuffer, 0); }
private void PackLong(int type, ulong val) { if (offset + 9 > buffer.Length) { Resize(9); } buffer[offset++] = (byte)type; ByteUtil.LongToBytes(val, buffer, offset); offset += 8; }
public bool SetBigInt64(ulong value, int offset) { try { int capacity = offset + 8; EnsureCapacity(capacity); ByteUtil.LongToBytes(value, bytes, offset); ResetSize(capacity); return(true); } catch (Exception) { return(false); } }
public override int Write(byte[] buf, int offset) { // Write value type ByteUtil.ShortToBytes(type, buf, offset); offset += 2; // Write length ByteUtil.IntToBytes(8, buf, offset); offset += 4; // Write value ByteUtil.LongToBytes((ulong)value, buf, offset); offset += 8; return(offset); }
protected internal void EndInfo() { // Write total size of message. int length = dataOffset - segment.offset; if (length > dataLength) { throw new AerospikeException("Actual buffer length " + length + " is greater than estimated length " + dataLength); } // Switch dataLength from length to buffer end offset. dataLength = dataOffset; dataOffset = segment.offset; ulong size = ((ulong)length - 8) | (2UL << 56) | (1UL << 48); ByteUtil.LongToBytes(size, dataBuffer, segment.offset); }
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(); }
public void SetScan(ScanPolicy policy, string ns, string setName, string[] binNames, ulong taskId) { Begin(); int fieldCount = 0; if (ns != null) { dataOffset += ByteUtil.EstimateSizeUtf8(ns) + FIELD_HEADER_SIZE; fieldCount++; } if (setName != null) { dataOffset += ByteUtil.EstimateSizeUtf8(setName) + FIELD_HEADER_SIZE; fieldCount++; } // Estimate scan options size. dataOffset += 2 + FIELD_HEADER_SIZE; fieldCount++; // Estimate scan timeout size. dataOffset += 4 + FIELD_HEADER_SIZE; fieldCount++; // Estimate taskId size. dataOffset += 8 + FIELD_HEADER_SIZE; fieldCount++; if (binNames != null) { foreach (String binName in binNames) { EstimateOperationSize(binName); } } SizeBuffer(); byte readAttr = (byte)Command.INFO1_READ; if (!policy.includeBinData) { readAttr |= (byte)Command.INFO1_NOBINDATA; } int operationCount = (binNames == null) ? 0 : binNames.Length; WriteHeader(policy, readAttr, 0, fieldCount, operationCount); if (ns != null) { WriteField(ns, FieldType.NAMESPACE); } if (setName != null) { WriteField(setName, FieldType.TABLE); } WriteFieldHeader(2, FieldType.SCAN_OPTIONS); byte priority = (byte)policy.priority; priority <<= 4; if (policy.failOnClusterChange) { priority |= 0x08; } dataBuffer[dataOffset++] = priority; dataBuffer[dataOffset++] = (byte)policy.scanPercent; // Write scan timeout WriteFieldHeader(4, FieldType.SCAN_TIMEOUT); dataOffset += ByteUtil.IntToBytes((uint)policy.socketTimeout, dataBuffer, dataOffset); // Write taskId field WriteFieldHeader(8, FieldType.TRAN_ID); dataOffset += ByteUtil.LongToBytes(taskId, dataBuffer, dataOffset); if (binNames != null) { foreach (String binName in binNames) { WriteOperation(binName, Operation.Type.READ); } } End(); }
//------------------------------------------------------- // 64 bit floating point conversions. //------------------------------------------------------- public static int DoubleToBytes(double v, byte[] buf, int offset) { return(ByteUtil.LongToBytes((ulong)BitConverter.DoubleToInt64Bits(v), buf, offset)); }
private void WriteField(ulong val, int type) { WriteFieldHeader(8, type); dataOffset += ByteUtil.LongToBytes(val, dataBuffer, dataOffset); }
public override int Write(byte[] buffer, int offset) { return(ByteUtil.LongToBytes(value, buffer, offset)); }