def Write(self, stream: binaryStream): """ Writes this SNAPdb type to the stream. """ stream.WriteUInt64(self.Timestamp) stream.WriteUInt64(self.PointID) stream.WriteUInt64(self.EntryNumber)
def Read(self, stream: binaryStream): """ Reads this SNAPdb type from the stream. """ self.Value1 = stream.ReadUInt64() self.Value2 = stream.ReadUInt64() self.Value3 = stream.ReadUInt64()
def Write(self, stream: binaryStream): """ Writes this SNAPdb type to the stream. """ stream.WriteUInt64(self.Value1) stream.WriteUInt64(self.Value2) stream.WriteUInt64(self.Value3)
def Read(self, stream: binaryStream): """ Reads this SNAPdb type from the stream. """ self.Timestamp = stream.ReadUInt64() self.PointID = stream.ReadUInt64() self.EntryNumber = stream.ReadUInt64()
def Save(self, stream: binaryStream): if self.isKeyValueEncoded: stream.WriteByte(1) stream.WriteGuid(self.keyValueEncodingMethod) else: stream.WriteByte(2) stream.WriteGuid(self.keyEncodingMethod) stream.WriteGuid(self.valueEncodingMethod)
def WriteEndOfStream(self, stream: binaryStream) -> int: """ Writes the end of the stream symbol to the `stream`. Parameters ---------- stream: where to write the data """ if self.ContainsEndOfStreamSymbol: return stream.WriteByte(self.EndOfStreamSymbol) return stream.WriteByte(0)
def TryStreamDecode(self, stream: binaryStream, key: TKey, value: TValue) -> bool: """ Attempts to decode the next `key` and `value` from the `stream`. Parameters ---------- stream: where to read the data key: the target SNAPdb key type to hold decoded data value: the target SNAPdb value type to hold decoded data Returns ------- `True` if successful; otherwise, `False` if end of the stream has been reached. """ if not self.ContainsEndOfStreamSymbol: if stream.ReadByte() == 0: return False endOfStream = self.Decode(stream, self.prevKey, self.prevValue, key, value) key.CopyTo(self.prevKey) value.CopyTo(self.prevValue) return not endOfStream
def Save(self, stream: binaryStream): """ Serializes the filter to a stream. """ maxValue = max(self.pointIDs) targetUInt32 = maxValue <= Limits.MAXUINT32 stream.WriteByte(1 if targetUInt32 else 2) stream.WriteUInt64(maxValue) stream.WriteInt32(len(self.pointIDs)) if targetUInt32: for pointID in self.pointIDs: stream.WriteUInt32(pointID) else: for pointID in self.pointIDs: stream.WriteUInt64(pointID)
def ReadResponse(stream: binaryStream) -> int: response = stream.ReadByte() if response == ServerResponse.UNHANDLEDEXCEPTION: raise RuntimeError( f"Server unhandled exception: {stream.ReadString()}") return response
def __init__(self, stream: binaryStream): version = stream.ReadByte() if version != 1: raise RuntimeError(f"Unknown SNAPdb version: {version}") self.databaseName = stream.ReadString().strip().upper() self.keyTypeID = stream.ReadGuid() self.valueTypeID = stream.ReadGuid() count = stream.ReadInt32() definitions = list() for i in range(count): definitions.append(encodingDefinition(stream=stream)) self.encodingDefinitions = definitions self.keyTypeName = library.LookupTypeName(self.keyTypeID) self.valueTypeName = library.LookupTypeName(self.valueTypeID)
def __initFromStream(self, stream: binaryStream): """ Initializes an `encodingDefinition` from a stream. """ code = stream.ReadByte() if code == 1: self.keyEncodingMethod = Empty.GUID self.valueEncodingMethod = Empty.GUID self.keyValueEncodingMethod = stream.ReadGuid() self.isKeyValueEncoded = True elif code == 2: self.keyEncodingMethod = stream.ReadGuid() self.valueEncodingMethod = stream.ReadGuid() self.keyValueEncodingMethod = Empty.GUID self.isKeyValueEncoded = False self.isFixedSizeEncoding = ( self.keyValueEncodingMethod == encodingDefinition.FixedSizeIndividualGuid or self.keyEncodingMethod == encodingDefinition.FixedSizeIndividualGuid and self.valueEncodingMethod == encodingDefinition.FixedSizeIndividualGuid )
def ReadBytes(stream: binaryStream, length: int) -> bytes: buffer = bytearray(length) position = 0 while length > 0: bytesRead = stream.Read(buffer, position, length) if bytesRead == 0: raise RuntimeError("End of stream") length -= bytesRead position += bytesRead return bytes(buffer)
def StreamEncode(self, stream: binaryStream, key: TKey, value: TValue): """ Encodes the current `key` and `value` to the `stream`. Parameters ---------- stream: where to write the data key: the SNAPdb key type to encode value: the SNAPdb value type to encode """ if not self.ContainsEndOfStreamSymbol: stream.WriteByte(1) self.Encode(stream, self.prevKey, self.prevValue, key, value) key.CopyTo(self.prevKey) value.CopyTo(self.prevValue)
def Save(self, stream: binaryStream): """ Serializes the filter to a stream. """ targetFixedRange = self.mainInterval == 0 and self.subInterval == 0 and self.tolerance == 0 stream.WriteByte(1 if targetFixedRange else 2) stream.WriteUInt64(self.firstTime) stream.WriteUInt64(self.lastTime) if targetFixedRange: return stream.WriteUInt64(self.mainInterval) stream.WriteUInt64(self.subInterval) stream.WriteUInt64(self.tolerance)
def Decode(self, stream: binaryStream, prevKey: historianKey, prevValue: historianValue, key: historianKey, value: historianValue) -> bool: """ Decodes `key` and `value` from the provided `stream`. Parameters ---------- stream: where to read the data prevKey: the previous key if required by `UsesPreviousKey`; otherwise `None`. prevValue: the previous value if required by `UsesPreviousValue`; otherwise `None`. key: the target SNAPdb key type to hold decoded data value: the target SNAPdb value type to hold decoded data Returns ------- When `ContainsEndOfStreamSymbol` is `True`, value determines if the end of the stream symbol is detected; otherwise, if `ContainsEndOfStreamSymbol` is `False`, result is always `False` """ code = stream.ReadByte() if code == historianKeyValueEncoder.EndOfStream: return True if code < 128: if code < 64: key.Timestamp = prevKey.Timestamp key.PointID = np.uint64(prevKey.PointID) ^ np.uint64(code) key.EntryNumber = 0 value.Value1 = 0 value.Value2 = 0 value.Value3 = 0 else: key.Timestamp = prevKey.Timestamp key.PointID = np.uint64(prevKey.PointID) ^ np.uint64(code) ^ np.uint64(64) key.EntryNumber = 0 value.Value1 = stream.ReadUInt32() value.Value2 = 0 value.Value3 = 0 return False if (code & 64) != 0: # T is set key.Timestamp = np.uint64(prevKey.Timestamp) ^ stream.Read7BitUInt64() else: key.Timestamp = prevKey.Timestamp key.PointID = np.uint64(prevKey.PointID) ^ stream.Read7BitUInt64() if (code & 32) != 0: # E is set key.EntryNumber = stream.Read7BitUInt64() else: key.EntryNumber = 0 if (code & 16) != 0: # V1 High is set value.Value1 = stream.ReadUInt64() elif (code & 8) != 0: # V1 low is set value.Value1 = stream.ReadUInt32() else: value.Value1 = 0 if (code & 4) != 0: # V2 is set value.Value2 = stream.ReadUInt64() else: value.Value2 = 0 if (code & 2) != 0: # V1 High is set value.Value3 = stream.ReadUInt64() elif (code & 1) != 0: # V1 low is set value.Value3 = stream.ReadUInt32() else: value.Value3 = 0 return False
def Encode(self, stream: binaryStream, prevKey: historianKey, prevValue: historianValue, key: historianKey, value: historianValue): """ Encodes `key` and `value` to the provided `stream`. Parameters ---------- stream: where to write the data prevKey: the previous key if required by `UsesPreviousKey`; otherwise `None`. prevValue: the previous value if required by `UsesPreviousValue`; otherwise `None`. key: the SNAPdb key type to encode value: the SNAPdb value type to encode """ if (key.Timestamp == prevKey.Timestamp and (np.uint64(key.PointID) ^ np.uint64(prevKey.PointID)) < np.uint64(64) and key.EntryNumber == 0 and value.Value1 <= Limits.MAXUINT32 and value.Value2 == 0 and value.Value3 == 0): if value.Value1 == 0: stream.WriteByte(np.uint8(np.uint64(key.PointID) ^ np.uint64(prevKey.PointID))); else: stream.WriteByte(np.uint8((np.uint64(key.PointID) ^ np.uint64(prevKey.PointID)) | np.uint64(64))); stream.WriteUInt32(np.uint32(value.Value1)); return code = np.uint8(128) if key.Timestamp != prevKey.Timestamp: code |= np.uint8(64) if key.EntryNumber != 0: code |= np.uint8(32) if value.Value1 > Limits.MAXUINT32: code |= np.uint8(16) elif value.Value1 > 0: code |= np.uint8(8) if value.Value2 != 0: code |= np.uint8(4) if value.Value3 > Limits.MAXUINT32: code |= np.uint8(2) elif value.Value3 > 0: code |= np.uint8(1) stream.WriteByte(np.uint8(code)) if key.Timestamp != prevKey.Timestamp: stream.Write7BitUInt64(np.uint64(key.Timestamp) ^ np.uint64(prevKey.Timestamp)) stream.Write7BitUInt64(np.uint64(key.PointID) ^ np.uint64(prevKey.PointID)) if key.EntryNumber != 0: stream.Write7BitUInt64(key.EntryNumber) if value.Value1 > Limits.MAXUINT32: stream.WriteUInt64(value.Value1) elif value.Value1 > 0: stream.WriteUInt32(np.uint32(value.Value1)) if value.Value2 != 0: stream.WriteUInt64(value.Value2) if value.Value3 > Limits.MAXUINT32: stream.WriteUInt64(value.Value3) elif value.Value3 > 0: stream.WriteUInt32(np.uint32(value.Value3))
def Save(self, stream: binaryStream): stream.WriteByte(0) stream.WriteUInt64(Ticks.FromTimeDelta(self.timeout)) stream.WriteUInt64(self.maxReturnedCount) stream.WriteUInt64(self.maxScanCount) stream.WriteUInt64(self.maxSeekCount)