XRange returns the messages in the stream. It takes the name of the stream, the start index, and the stop index as arguments, and returns a slice of StreamMessage containing the messages within the specified range. Parameters: name: The name of the stream. start: The start index of the range (in
(name string, start, stop int)
| 374 | // - If the number of messages in the stream is less than stop, it returns |
| 375 | // ErrAmountOfData, indicating that there is not enough data in the stream. |
| 376 | func (s *StreamStructure) XRange(name string, start, stop int) ([]StreamMessage, error) { |
| 377 | // Get the stream |
| 378 | encodedStreams, err := s.db.Get([]byte(name)) |
| 379 | if err != nil { |
| 380 | return nil, err |
| 381 | } |
| 382 | |
| 383 | // Decode the streams |
| 384 | if err = s.decodeStreams(encodedStreams, s.streams); err != nil { |
| 385 | return nil, err |
| 386 | } |
| 387 | |
| 388 | // Get the messages |
| 389 | messages := s.streams.Messages |
| 390 | |
| 391 | // Create a new slice of StreamMessage |
| 392 | var result []StreamMessage |
| 393 | |
| 394 | // Get the messages |
| 395 | if len(messages) >= stop { |
| 396 | messages = messages[start:stop] |
| 397 | |
| 398 | // Convert []*StreamMessage to []StreamMessage |
| 399 | for _, msg := range messages { |
| 400 | result = append(result, *msg) |
| 401 | } |
| 402 | } else { |
| 403 | return nil, ErrAmountOfData |
| 404 | } |
| 405 | |
| 406 | return result, nil |
| 407 | } |
| 408 | |
| 409 | // XRevRange returns the messages in the stream in reverse order. |
| 410 | // It takes the name of the stream, the start index, and the stop index as arguments, |