/// <summary> /// Sets the partition key to the provided value /// </summary> /// <param name="type"> The type in which the value is represented in Cassandra. </param> /// <param name="value"> The value of the partition key column. </param> public void Set(CqlType type, Object value) { if (value == null) { throw new ArgumentNullException("value"); } _key = ValueSerialization.Serialize(type, value); }
public BigInteger?ComputeToken(PartitionKey partitionKey) { object[] keys = partitionKey.Keys; if (1 == keys.Length) { ColumnType colType = keys[0].GetType().ToColumnType(); byte[] buffer = ValueSerialization.Serialize(colType, keys[0]); return(Hash(buffer, 0, buffer.Length)); } var rawValues = new byte[keys.Length][]; for (int i = 0; i < keys.Length; i++) { ColumnType colType = keys[i].GetType().ToColumnType(); rawValues[i] = ValueSerialization.Serialize(colType, keys[i]); } int length = keys.Length * 3 + rawValues.Sum(val => val.Length); using (var stream = new MemoryStream(length)) { foreach (var rawValue in rawValues) { //write length of composite key part as short var len = (short)rawValue.Length; stream.WriteByte((byte)(len >> 8)); stream.WriteByte((byte)(len)); //write value stream.Write(rawValue, 0, len); //write terminator byte stream.WriteByte(0); } byte[] buffer = stream.GetBuffer(); return(Hash(buffer, 0, length)); } }
/// <summary> /// Sets the partition key based on the provided values. Use this when composite partition keys are used /// </summary> /// <param name="types"> The types in which the values are represented in Cassandra. </param> /// <param name="values"> The values of the partition key columns. The values must be given in the same order as the partition key is defined. </param> public void Set(CqlType[] types, Object[] values) { if (types == null) { throw new ArgumentNullException("types"); } if (values == null) { throw new ArgumentNullException("values"); } if (types.Length != values.Length) { throw new ArgumentException("types and values are not of equal length"); } var rawValues = new byte[types.Length][]; for (int i = 0; i < types.Length; i++) { rawValues[i] = ValueSerialization.Serialize(types[i], values[i]); } int length = types.Length * 3 + rawValues.Sum(val => val.Length); using (var stream = new MemoryStream(length)) { foreach (var rawValue in rawValues) { stream.WriteShortByteArray(rawValue); stream.WriteByte(0); } _key = stream.ToArray(); } }