From b92d1c952b829035cee682befd2f9a6151745ead Mon Sep 17 00:00:00 2001 From: Fedor Litau Date: Tue, 31 Aug 2021 20:11:49 +0200 Subject: [PATCH] Fix "slice bounds out of range" error in memoryMetric.selectPoints() --- memory_partition.go | 4 ++-- memory_partition_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/memory_partition.go b/memory_partition.go index a8a2af0..efde3aa 100644 --- a/memory_partition.go +++ b/memory_partition.go @@ -236,8 +236,8 @@ func (m *memoryMetric) selectPoints(start, end int64) []*DataPoint { } else { // Use binary search because points are in-order. endIdx = sort.Search(int(size), func(i int) bool { - return m.points[i].Timestamp < end - }) + 1 + return m.points[i].Timestamp >= end + }) } return m.points[startIdx:endIdx] } diff --git a/memory_partition_test.go b/memory_partition_test.go index 625d0bc..0e0701b 100644 --- a/memory_partition_test.go +++ b/memory_partition_test.go @@ -84,7 +84,43 @@ func Test_memoryPartition_SelectDataPoints(t *testing.T) { want: []*DataPoint{}, }, { - name: "select multiple points", + name: "select some points", + metric: "metric1", + start: 2, + end: 4, + memoryPartition: func() *memoryPartition { + m := newMemoryPartition(nil, 0, "").(*memoryPartition) + m.insertRows([]Row{ + { + Metric: "metric1", + DataPoint: DataPoint{Timestamp: 1, Value: 0.1}, + }, + { + Metric: "metric1", + DataPoint: DataPoint{Timestamp: 2, Value: 0.1}, + }, + { + Metric: "metric1", + DataPoint: DataPoint{Timestamp: 3, Value: 0.1}, + }, + { + Metric: "metric1", + DataPoint: DataPoint{Timestamp: 4, Value: 0.1}, + }, + { + Metric: "metric1", + DataPoint: DataPoint{Timestamp: 5, Value: 0.1}, + }, + }) + return m + }(), + want: []*DataPoint{ + {Timestamp: 2, Value: 0.1}, + {Timestamp: 3, Value: 0.1}, + }, + }, + { + name: "select all points", metric: "metric1", start: 1, end: 4,