XRead reads messages from a stream. Returns a slice of StreamMessage. Parameters: name: The name of the stream. count: The maximum number of messages to read. Returns: []StreamMessage: A slice of StreamMessage containing the read messages. error: An error if any occurred during the operation
(name string, count int)
| 196 | // - If the number of messages in the stream is less than count, it returns |
| 197 | // ErrAmountOfData, indicating that there is not enough data in the stream. |
| 198 | func (s *StreamStructure) XRead(name string, count int) ([]StreamMessage, error) { |
| 199 | if count <= 0 { |
| 200 | return nil, ErrInvalidCount |
| 201 | } |
| 202 | |
| 203 | // Get the stream |
| 204 | encodedStreams, err := s.db.Get([]byte(name)) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | |
| 209 | // Decode the streams |
| 210 | if err = s.decodeStreams(encodedStreams, s.streams); err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | |
| 214 | // Get the messages |
| 215 | messages := s.streams.Messages |
| 216 | |
| 217 | // Create a new slice of StreamMessage |
| 218 | var result []StreamMessage |
| 219 | |
| 220 | // Get the messages |
| 221 | if len(messages) >= count { |
| 222 | messages = messages[:count] |
| 223 | // Convert []*StreamMessage to []StreamMessage |
| 224 | for _, msg := range messages { |
| 225 | result = append(result, *msg) |
| 226 | } |
| 227 | } else { |
| 228 | return nil, ErrAmountOfData |
| 229 | } |
| 230 | |
| 231 | return result, nil |
| 232 | } |
| 233 | |
| 234 | // XDel deletes a message from a stream. |
| 235 | // Returns true if the message was deleted. |