Fix descending cache cursor

pull/4980/head
Philip O'Toole 2015-12-03 14:08:22 -08:00
parent b73b21f062
commit 2d79d7e35f
2 changed files with 37 additions and 20 deletions

View File

@ -434,26 +434,36 @@ type devCursor struct {
// SeekTo positions the cursor at the timestamp specified by seek and returns the // SeekTo positions the cursor at the timestamp specified by seek and returns the
// timestamp and value. // timestamp and value.
func (c *devCursor) SeekTo(seek int64) (int64, interface{}) { func (c *devCursor) SeekTo(seek int64) (int64, interface{}) {
// Seek to position in cache.
c.cacheKeyBuf, c.cacheValueBuf = func() (int64, interface{}) {
// Seek to position in cache index. // Seek to position in cache index.
c.cachePos = sort.Search(len(c.cache), func(i int) bool { c.cachePos = sort.Search(len(c.cache), func(i int) bool {
if c.ascending {
return c.cache[i].Time().UnixNano() >= seek return c.cache[i].Time().UnixNano() >= seek
}
return c.cache[i].Time().UnixNano() <= seek
}) })
if len(c.cache) == 0 {
c.cacheKeyBuf = tsdb.EOF
}
if c.cachePos < len(c.cache) { if c.cachePos < len(c.cache) {
c.cacheKeyBuf = c.cache[c.cachePos].Time().UnixNano() v := c.cache[c.cachePos]
c.cacheValueBuf = c.cache[c.cachePos].Value() if v.UnixNano() == seek || c.ascending {
} else { // Exact seek found or, if ascending, next one is good.
c.cacheKeyBuf = tsdb.EOF return v.UnixNano(), v.Value()
}
// Nothing available if descending.
return tsdb.EOF, nil
} }
// TODO: Get the first block from tsm files for the given 'seek' // Ascending cursor, no match in the cache.
if c.ascending {
return tsdb.EOF, nil
}
// Descending cursor, go to previous value in cache, and return if it exists.
c.cachePos--
if c.cachePos < 0 {
return tsdb.EOF, nil
}
return c.cache[c.cachePos].UnixNano(), c.cache[c.cachePos].Value()
}()
// Seek to position to tsm block. // Seek to position to tsm block.
if c.ascending { if c.ascending {
c.tsmValues, _ = c.tsm.Scan(SeriesFieldKey(c.series, c.fields[0]), time.Unix(0, seek-1), c.ascending) c.tsmValues, _ = c.tsm.Scan(SeriesFieldKey(c.series, c.fields[0]), time.Unix(0, seek-1), c.ascending)
@ -525,11 +535,19 @@ func (c *devCursor) read() (int64, interface{}) {
// nextCache returns the next value from the cache. // nextCache returns the next value from the cache.
func (c *devCursor) nextCache() (int64, interface{}) { func (c *devCursor) nextCache() (int64, interface{}) {
if c.ascending {
c.cachePos++ c.cachePos++
if c.cachePos >= len(c.cache) { if c.cachePos >= len(c.cache) {
return tsdb.EOF, nil return tsdb.EOF, nil
} }
return c.cache[c.cachePos].UnixNano(), c.cache[c.cachePos].Value() return c.cache[c.cachePos].UnixNano(), c.cache[c.cachePos].Value()
} else {
c.cachePos--
if c.cachePos < 0 {
return tsdb.EOF, nil
}
return c.cache[c.cachePos].UnixNano(), c.cache[c.cachePos].Value()
}
} }
// nextTSM returns the next value from the TSM files. // nextTSM returns the next value from the TSM files.

View File

@ -131,7 +131,6 @@ func TestDevEngine_QueryTSM_Ascending(t *testing.T) {
// Ensure an engine containing cached values responds correctly to queries. // Ensure an engine containing cached values responds correctly to queries.
func TestDevEngine_QueryCache_Descending(t *testing.T) { func TestDevEngine_QueryCache_Descending(t *testing.T) {
t.Skip("fixme")
// Generate temporary file. // Generate temporary file.
f, _ := ioutil.TempFile("", "tsm1dev") f, _ := ioutil.TempFile("", "tsm1dev")
f.Close() f.Close()