| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build cgo |
| 4 | |
| 5 | package db2 |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "fmt" |
| 10 | "strconv" |
| 11 | ) |
| 12 | |
| 13 | func (c *Collector) collectIndexInstances(ctx context.Context) error { |
| 14 | if c.MaxIndexes <= 0 { |
| 15 | return nil |
| 16 | } |
| 17 | |
| 18 | var currentSchema, currentIndex, key string |
| 19 | err := c.doQuery(ctx, queryIndexInstances, func(column, value string, lineEnd bool) { |
| 20 | switch column { |
| 21 | case "INDSCHEMA": |
| 22 | currentSchema = value |
| 23 | case "INDNAME": |
| 24 | currentIndex = value |
| 25 | key = fmt.Sprintf("%s.%s", currentSchema, currentIndex) |
| 26 | |
| 27 | if !c.allowIndex(key) { |
| 28 | key = "" |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | if _, exists := c.indexes[key]; !exists { |
| 33 | c.indexes[key] = &indexMetrics{name: key} |
| 34 | } |
| 35 | c.mx.indexes[key] = indexInstanceMetrics{} |
| 36 | case "NLEAF": |
| 37 | if key != "" { |
| 38 | if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 39 | metrics := c.mx.indexes[key] |
| 40 | metrics.LeafNodes = v |
| 41 | c.mx.indexes[key] = metrics |
| 42 | } |
| 43 | } |
| 44 | case "INDEX_SCANS": |
| 45 | if key != "" { |
| 46 | if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 47 | metrics := c.mx.indexes[key] |
| 48 | metrics.IndexScans = v |
| 49 | c.mx.indexes[key] = metrics |
| 50 | } |
| 51 | } |
| 52 | case "FULL_SCANS": |
| 53 | if key != "" { |
| 54 | if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 55 | metrics := c.mx.indexes[key] |
| 56 | metrics.FullScans = v |
| 57 | c.mx.indexes[key] = metrics |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if lineEnd { |
| 63 | key = "" |
| 64 | currentIndex = "" |
| 65 | currentSchema = "" |
| 66 | } |
| 67 | }) |
| 68 | |
| 69 | return err |
| 70 | } |