/// <summary> /// Generic header write. /// </summary> private void WriteHeader(Policy policy, int readAttr, int writeAttr, int fieldCount, int operationCount) { int infoAttr = 0; if (policy.linearizeRead) { infoAttr |= Command.INFO3_LINEARIZE_READ; } if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } dataOffset += 8; // Write all header data except total size which must be written last. dataBuffer[dataOffset++] = MSG_REMAINING_HEADER_SIZE; // Message header length. dataBuffer[dataOffset++] = (byte)readAttr; dataBuffer[dataOffset++] = (byte)writeAttr; dataBuffer[dataOffset++] = (byte)infoAttr; for (int i = 0; i < 10; i++) { dataBuffer[dataOffset++] = 0; } dataOffset += ByteUtil.IntToBytes((uint)policy.totalTimeout, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, dataOffset); }
private void PackShort(int type, ushort val) { if (offset + 3 > buffer.Length) { Resize(3); } buffer[offset++] = (byte)type; ByteUtil.ShortToBytes(val, buffer, offset); offset += 2; }
public void PackRawShort(int val) { // WARNING. This method is not compatible with message pack standard. if (offset + 2 > buffer.Length) { Resize(2); } ByteUtil.ShortToBytes((ushort)val, buffer, offset); offset += 2; }
public override int Write(byte[] buf, int offset) { // Write op type ByteUtil.ShortToBytes(op, buf, offset); offset += 2; // Write zero length ByteUtil.IntToBytes(0, buf, offset); offset += 4; return(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 SetBigInt16(ushort value, int offset) { try { int capacity = offset + 2; EnsureCapacity(capacity); ByteUtil.ShortToBytes(value, bytes, offset); ResetSize(capacity); return(true); } catch (Exception) { return(false); } }
public override int Write(byte[] buf, int offset) { // Write op type ByteUtil.ShortToBytes(op, buf, offset); offset += 2; // Write length ByteUtil.IntToBytes(4, buf, offset); offset += 4; // Write predicate count ByteUtil.IntToBytes(flags, buf, offset); offset += 4; return(offset); }
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); }
/// <summary> /// Generic header write. /// </summary> private void WriteHeader(Policy policy, int readAttr, int writeAttr, int fieldCount, int operationCount) { int infoAttr = 0; switch (policy.readModeSC) { case ReadModeSC.SESSION: break; case ReadModeSC.LINEARIZE: infoAttr |= Command.INFO3_SC_READ_TYPE; break; case ReadModeSC.ALLOW_REPLICA: infoAttr |= Command.INFO3_SC_READ_RELAX; break; case ReadModeSC.ALLOW_UNAVAILABLE: infoAttr |= Command.INFO3_SC_READ_TYPE | Command.INFO3_SC_READ_RELAX; break; } if (policy.readModeAP == ReadModeAP.ALL) { readAttr |= Command.INFO1_READ_MODE_AP_ALL; } dataOffset += 8; // Write all header data except total size which must be written last. dataBuffer[dataOffset++] = MSG_REMAINING_HEADER_SIZE; // Message header length. dataBuffer[dataOffset++] = (byte)readAttr; dataBuffer[dataOffset++] = (byte)writeAttr; dataBuffer[dataOffset++] = (byte)infoAttr; for (int i = 0; i < 10; i++) { dataBuffer[dataOffset++] = 0; } dataOffset += ByteUtil.IntToBytes((uint)policy.totalTimeout, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, dataOffset); }
/// <summary> /// Generic header write. /// </summary> protected internal void WriteHeader(Policy policy, int readAttr, int writeAttr, int fieldCount, int operationCount) { if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } // Write all header data except total size which must be written last. dataBuffer[8] = MSG_REMAINING_HEADER_SIZE; // Message header length. dataBuffer[9] = (byte)readAttr; dataBuffer[10] = (byte)writeAttr; for (int i = 11; i < 26; i++) { dataBuffer[i] = 0; } ByteUtil.ShortToBytes((ushort)fieldCount, dataBuffer, 26); ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, 28); dataOffset = MSG_TOTAL_HEADER_SIZE; }
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); }
/// <summary> /// Header write for write operations. /// </summary> private void WriteHeader(WritePolicy policy, int readAttr, int writeAttr, int fieldCount, int operationCount) { // Set flags. int generation = 0; int infoAttr = 0; switch (policy.recordExistsAction) { case RecordExistsAction.UPDATE: break; case RecordExistsAction.UPDATE_ONLY: infoAttr |= Command.INFO3_UPDATE_ONLY; break; case RecordExistsAction.REPLACE: infoAttr |= Command.INFO3_CREATE_OR_REPLACE; break; case RecordExistsAction.REPLACE_ONLY: infoAttr |= Command.INFO3_REPLACE_ONLY; break; case RecordExistsAction.CREATE_ONLY: writeAttr |= Command.INFO2_CREATE_ONLY; break; } switch (policy.generationPolicy) { case GenerationPolicy.NONE: break; case GenerationPolicy.EXPECT_GEN_EQUAL: generation = policy.generation; writeAttr |= Command.INFO2_GENERATION; break; case GenerationPolicy.EXPECT_GEN_GT: generation = policy.generation; writeAttr |= Command.INFO2_GENERATION_GT; break; } if (policy.commitLevel == CommitLevel.COMMIT_MASTER) { infoAttr |= Command.INFO3_COMMIT_MASTER; } if (policy.linearizeRead) { infoAttr |= Command.INFO3_LINEARIZE_READ; } if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } if (policy.durableDelete) { writeAttr |= Command.INFO2_DURABLE_DELETE; } dataOffset += 8; // Write all header data except total size which must be written last. dataBuffer[dataOffset++] = MSG_REMAINING_HEADER_SIZE; // Message header length. dataBuffer[dataOffset++] = (byte)readAttr; dataBuffer[dataOffset++] = (byte)writeAttr; dataBuffer[dataOffset++] = (byte)infoAttr; dataBuffer[dataOffset++] = 0; // unused dataBuffer[dataOffset++] = 0; // clear the result code dataOffset += ByteUtil.IntToBytes((uint)generation, dataBuffer, dataOffset); dataOffset += ByteUtil.IntToBytes((uint)policy.expiration, dataBuffer, dataOffset); dataOffset += ByteUtil.IntToBytes((uint)policy.totalTimeout, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, dataOffset); }
public void SetBatchRead(BatchPolicy policy, Key[] keys, BatchNode batch, string[] binNames, int readAttr) { // Estimate full row size int[] offsets = batch.offsets; int max = batch.offsetsSize; ushort fieldCount = policy.sendSetName ? (ushort)2 : (ushort)1; // Calculate size of bin names. int binNameSize = 0; int operationCount = 0; if (binNames != null) { foreach (string binName in binNames) { binNameSize += ByteUtil.EstimateSizeUtf8(binName) + OPERATION_HEADER_SIZE; } operationCount = binNames.Length; } // Estimate buffer size. Begin(); dataOffset += FIELD_HEADER_SIZE + 5; Key prev = null; for (int i = 0; i < max; i++) { Key key = keys[offsets[i]]; dataOffset += key.digest.Length + 4; // Try reference equality in hope that namespace for all keys is set from a fixed variable. if (prev != null && prev.ns == key.ns && (!policy.sendSetName || prev.setName == key.setName)) { // Can set repeat previous namespace/bin names to save space. dataOffset++; } else { // Estimate full header, namespace and bin names. dataOffset += ByteUtil.EstimateSizeUtf8(key.ns) + FIELD_HEADER_SIZE + 6; if (policy.sendSetName) { dataOffset += ByteUtil.EstimateSizeUtf8(key.setName) + FIELD_HEADER_SIZE; } dataOffset += binNameSize; prev = key; } } SizeBuffer(); if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } WriteHeader(policy, readAttr | Command.INFO1_BATCH, 0, 1, 0); int fieldSizeOffset = dataOffset; WriteFieldHeader(0, policy.sendSetName ? FieldType.BATCH_INDEX_WITH_SET : FieldType.BATCH_INDEX); // Need to update size at end ByteUtil.IntToBytes((uint)max, dataBuffer, dataOffset); dataOffset += 4; dataBuffer[dataOffset++] = (policy.allowInline) ? (byte)1 : (byte)0; prev = null; for (int i = 0; i < max; i++) { int index = offsets[i]; ByteUtil.IntToBytes((uint)index, dataBuffer, dataOffset); dataOffset += 4; Key key = keys[index]; byte[] digest = key.digest; Array.Copy(digest, 0, dataBuffer, dataOffset, digest.Length); dataOffset += digest.Length; // Try reference equality in hope that namespace for all keys is set from a fixed variable. if (prev != null && prev.ns == key.ns && (!policy.sendSetName || prev.setName == key.setName)) { // Can set repeat previous namespace/bin names to save space. dataBuffer[dataOffset++] = 1; // repeat } else { // Write full header, namespace and bin names. dataBuffer[dataOffset++] = 0; // do not repeat dataBuffer[dataOffset++] = (byte)readAttr; dataOffset += ByteUtil.ShortToBytes(fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, dataOffset); WriteField(key.ns, FieldType.NAMESPACE); if (policy.sendSetName) { WriteField(key.setName, FieldType.TABLE); } if (binNames != null) { foreach (string binName in binNames) { WriteOperation(binName, Operation.Type.READ); } } prev = key; } } // Write real field size. ByteUtil.IntToBytes((uint)(dataOffset - MSG_TOTAL_HEADER_SIZE - 4), dataBuffer, fieldSizeOffset); End(); }
public void SetBatchRead(BatchPolicy policy, List <BatchRead> records, BatchNode batch) { // Estimate full row size int[] offsets = batch.offsets; int max = batch.offsetsSize; ushort fieldCount = policy.sendSetName ? (ushort)2 : (ushort)1; BatchRead prev = null; Begin(); dataOffset += FIELD_HEADER_SIZE + 5; for (int i = 0; i < max; i++) { BatchRead record = records[offsets[i]]; Key key = record.key; string[] binNames = record.binNames; dataOffset += key.digest.Length + 4; // Avoid relatively expensive full equality checks for performance reasons. // Use reference equality only in hope that common namespaces/bin names are set from // fixed variables. It's fine if equality not determined correctly because it just // results in more space used. The batch will still be correct. if (prev != null && prev.key.ns == key.ns && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins) { // Can set repeat previous namespace/bin names to save space. dataOffset++; } else { // Estimate full header, namespace and bin names. dataOffset += ByteUtil.EstimateSizeUtf8(key.ns) + FIELD_HEADER_SIZE + 6; if (policy.sendSetName) { dataOffset += ByteUtil.EstimateSizeUtf8(key.setName) + FIELD_HEADER_SIZE; } if (binNames != null) { foreach (string binName in binNames) { EstimateOperationSize(binName); } } prev = record; } } SizeBuffer(); int readAttr = Command.INFO1_READ; if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } WriteHeader(policy, readAttr | Command.INFO1_BATCH, 0, 1, 0); int fieldSizeOffset = dataOffset; WriteFieldHeader(0, policy.sendSetName ? FieldType.BATCH_INDEX_WITH_SET : FieldType.BATCH_INDEX); // Need to update size at end ByteUtil.IntToBytes((uint)max, dataBuffer, dataOffset); dataOffset += 4; dataBuffer[dataOffset++] = (policy.allowInline) ? (byte)1 : (byte)0; prev = null; for (int i = 0; i < max; i++) { int index = offsets[i]; ByteUtil.IntToBytes((uint)index, dataBuffer, dataOffset); dataOffset += 4; BatchRead record = records[index]; Key key = record.key; string[] binNames = record.binNames; byte[] digest = key.digest; Array.Copy(digest, 0, dataBuffer, dataOffset, digest.Length); dataOffset += digest.Length; // Avoid relatively expensive full equality checks for performance reasons. // Use reference equality only in hope that common namespaces/bin names are set from // fixed variables. It's fine if equality not determined correctly because it just // results in more space used. The batch will still be correct. if (prev != null && prev.key.ns == key.ns && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins) { // Can set repeat previous namespace/bin names to save space. dataBuffer[dataOffset++] = 1; // repeat } else { // Write full header, namespace and bin names. dataBuffer[dataOffset++] = 0; // do not repeat if (binNames != null && binNames.Length != 0) { dataBuffer[dataOffset++] = (byte)readAttr; dataOffset += ByteUtil.ShortToBytes(fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes((ushort)binNames.Length, dataBuffer, dataOffset); WriteField(key.ns, FieldType.NAMESPACE); if (policy.sendSetName) { WriteField(key.setName, FieldType.TABLE); } foreach (string binName in binNames) { WriteOperation(binName, Operation.Type.READ); } } else { dataBuffer[dataOffset++] = (byte)(readAttr | (record.readAllBins ? Command.INFO1_GET_ALL : Command.INFO1_NOBINDATA)); dataOffset += ByteUtil.ShortToBytes(fieldCount, dataBuffer, dataOffset); dataOffset += ByteUtil.ShortToBytes(0, dataBuffer, dataOffset); WriteField(key.ns, FieldType.NAMESPACE); if (policy.sendSetName) { WriteField(key.setName, FieldType.TABLE); } } prev = record; } } // Write real field size. ByteUtil.IntToBytes((uint)(dataOffset - MSG_TOTAL_HEADER_SIZE - 4), dataBuffer, fieldSizeOffset); End(); }
/// <summary> /// Header write for write operations. /// </summary> protected internal void WriteHeader(WritePolicy policy, int readAttr, int writeAttr, int fieldCount, int operationCount) { // Set flags. int generation = 0; int infoAttr = 0; switch (policy.recordExistsAction) { case RecordExistsAction.UPDATE: break; case RecordExistsAction.UPDATE_ONLY: infoAttr |= Command.INFO3_UPDATE_ONLY; break; case RecordExistsAction.REPLACE: infoAttr |= Command.INFO3_CREATE_OR_REPLACE; break; case RecordExistsAction.REPLACE_ONLY: infoAttr |= Command.INFO3_REPLACE_ONLY; break; case RecordExistsAction.CREATE_ONLY: writeAttr |= Command.INFO2_CREATE_ONLY; break; } switch (policy.generationPolicy) { case GenerationPolicy.NONE: break; case GenerationPolicy.EXPECT_GEN_EQUAL: generation = policy.generation; writeAttr |= Command.INFO2_GENERATION; break; case GenerationPolicy.EXPECT_GEN_GT: generation = policy.generation; writeAttr |= Command.INFO2_GENERATION_GT; break; } if (policy.commitLevel == CommitLevel.COMMIT_MASTER) { infoAttr |= Command.INFO3_COMMIT_MASTER; } if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) { readAttr |= Command.INFO1_CONSISTENCY_ALL; } // Write all header data except total size which must be written last. dataBuffer[8] = MSG_REMAINING_HEADER_SIZE; // Message header length. dataBuffer[9] = (byte)readAttr; dataBuffer[10] = (byte)writeAttr; dataBuffer[11] = (byte)infoAttr; dataBuffer[12] = 0; // unused dataBuffer[13] = 0; // clear the result code ByteUtil.IntToBytes((uint)generation, dataBuffer, 14); ByteUtil.IntToBytes((uint)policy.expiration, dataBuffer, 18); // Initialize timeout. It will be written later. dataBuffer[22] = 0; dataBuffer[23] = 0; dataBuffer[24] = 0; dataBuffer[25] = 0; ByteUtil.ShortToBytes((ushort)fieldCount, dataBuffer, 26); ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, 28); dataOffset = MSG_TOTAL_HEADER_SIZE; }
public void SetBatchRead(BatchPolicy policy, Key[] keys, BatchNode batch, string[] binNames, int readAttr) { // Estimate full row size int[] offsets = batch.offsets; int max = batch.offsetsSize; int rowSize = 30 + FIELD_HEADER_SIZE + 31; // Row's header(30) + max namespace(31). int operationCount = 0; if (binNames != null) { foreach (string binName in binNames) { EstimateOperationSize(binName); } rowSize += dataOffset; operationCount = binNames.Length; } // Estimate buffer size. Begin(); dataOffset += FIELD_HEADER_SIZE + 5; string prevNamespace = null; for (int i = 0; i < max; i++) { Key key = keys[offsets[i]]; // Try reference equality in hope that namespace for all keys is set from a fixed variable. if (key.ns == prevNamespace || (prevNamespace != null && prevNamespace.Equals(key.ns))) { // Can set repeat previous namespace/bin names to save space. dataOffset += 25; } else { // Must write full header and namespace/bin names. dataOffset += rowSize; prevNamespace = key.ns; } } SizeBuffer(); WriteHeader(policy, readAttr | Command.INFO1_BATCH, 0, 1, 0); int fieldSizeOffset = dataOffset; WriteFieldHeader(0, FieldType.BATCH_INDEX); // Need to update size at end ByteUtil.IntToBytes((uint)max, dataBuffer, dataOffset); dataOffset += 4; dataBuffer[dataOffset++] = (policy.allowInline) ? (byte)1 : (byte)0; prevNamespace = null; for (int i = 0; i < max; i++) { int index = offsets[i]; ByteUtil.IntToBytes((uint)index, dataBuffer, dataOffset); dataOffset += 4; Key key = keys[index]; byte[] digest = key.digest; Array.Copy(digest, 0, dataBuffer, dataOffset, digest.Length); dataOffset += digest.Length; // Try reference equality in hope that namespace for all keys is set from a fixed variable. if (key.ns == prevNamespace || (prevNamespace != null && prevNamespace.Equals(key.ns))) { // Can set repeat previous namespace/bin names to save space. dataBuffer[dataOffset++] = 1; // repeat } else { // Write full header, namespace and bin names. dataBuffer[dataOffset++] = 0; // do not repeat dataBuffer[dataOffset++] = (byte)readAttr; dataBuffer[dataOffset++] = 0; // pad dataBuffer[dataOffset++] = 0; // pad ByteUtil.ShortToBytes((ushort)operationCount, dataBuffer, dataOffset); dataOffset += 2; WriteField(key.ns, FieldType.NAMESPACE); if (binNames != null) { foreach (string binName in binNames) { WriteOperation(binName, Operation.Type.READ); } } prevNamespace = key.ns; } } // Write real field size. ByteUtil.IntToBytes((uint)(dataOffset - MSG_TOTAL_HEADER_SIZE - 4), dataBuffer, fieldSizeOffset); End(); }