XRevRange returns the messages in the stream in reverse order. 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 in reverse order. Parameters: name: The name of the stream. st
(name string, start, stop int)
| 437 | // ErrAmountOfData, indicating that there is not enough data in the stream. |
| 438 | // - The returned slice of StreamMessage is reversed compared to the original order. |
| 439 | func (s *StreamStructure) XRevRange(name string, start, stop int) ([]StreamMessage, error) { |
| 440 | // Get the stream |
| 441 | encodedStreams, err := s.db.Get([]byte(name)) |
| 442 | if err != nil { |
| 443 | return nil, err |
| 444 | } |
| 445 | |
| 446 | // Decode the streams |
| 447 | if err = s.decodeStreams(encodedStreams, s.streams); err != nil { |
| 448 | return nil, err |
| 449 | } |
| 450 | |
| 451 | // Get the messages |
| 452 | messages := s.streams.Messages |
| 453 | |
| 454 | // Create a new slice of StreamMessage |
| 455 | var result []StreamMessage |
| 456 | |
| 457 | // Get the messages |
| 458 | if len(messages) >= stop { |
| 459 | messages = messages[start:stop] |
| 460 | // Convert []*StreamMessage to []StreamMessage |
| 461 | for _, msg := range messages { |
| 462 | result = append(result, *msg) |
| 463 | } |
| 464 | } else { |
| 465 | return nil, ErrAmountOfData |
| 466 | } |
| 467 | |
| 468 | // Reverse the slice |
| 469 | for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { |
| 470 | result[i], result[j] = result[j], result[i] |
| 471 | } |
| 472 | |
| 473 | return result, nil |
| 474 | } |
| 475 | |
| 476 | // XTrim trims the stream to a certain size. |
| 477 | // It takes the name of the stream and the maximum length as arguments, |