Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 4bd2355

Browse files
committed
make "public org" explicit + optimize find in case orgIdPublic passed
1 parent d215f5b commit 4bd2355

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

idx/cassandra/cassandra_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestGetAddKey(t *testing.T) {
125125
ix := New()
126126
initForTests(ix)
127127

128-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public")
128+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public")
129129
org1Series := getMetricData(1, 2, 5, 10, "metric.org1")
130130
org2Series := getMetricData(2, 2, 5, 10, "metric.org2")
131131

@@ -138,7 +138,7 @@ func TestGetAddKey(t *testing.T) {
138138
Convey(fmt.Sprintf("Then listing metrics for OrgId %d", orgId), func() {
139139
defs := ix.List(orgId)
140140
numSeries := len(series)
141-
if orgId != -1 {
141+
if orgId != idx.OrgIdPublic {
142142
numSeries += 5
143143
}
144144
So(defs, ShouldHaveLength, numSeries)
@@ -258,7 +258,7 @@ func TestAddToWriteQueue(t *testing.T) {
258258
func TestFind(t *testing.T) {
259259
ix := New()
260260
initForTests(ix)
261-
for _, s := range getMetricData(-1, 2, 5, 10, "metric.demo") {
261+
for _, s := range getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.demo") {
262262
ix.AddOrUpdate(s, 1)
263263
}
264264
for _, s := range getMetricData(1, 2, 5, 10, "metric.demo") {

idx/idx.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
schema "gopkg.in/raintank/schema.v1"
1414
)
1515

16+
const OrgIdPublic = -1
17+
1618
var (
1719
BothBranchAndLeaf = errors.New("node can't be both branch and leaf")
1820
BranchUnderLeaf = errors.New("can't add branch under leaf")
@@ -118,7 +120,7 @@ type MetricIndex interface {
118120

119121
// Find searches the index. The method is passed an OrgId, a query
120122
// pattern and a unix timestamp. Searches should return all nodes that match for
121-
// the given OrgId and OrgId -1. The pattern should be handled in the same way
123+
// the given OrgId and OrgIdPublic. The pattern should be handled in the same way
122124
// Graphite would. see https://graphite.readthedocs.io/en/latest/render_api.html#paths-and-wildcards
123125
// And the unix stimestamp is used to ignore series that have been stale since
124126
// the timestamp.

idx/memory/memory.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -836,15 +836,17 @@ func (m *MemoryIdx) Find(orgId int, pattern string, from int64) ([]idx.Node, err
836836
if err != nil {
837837
return nil, err
838838
}
839-
publicNodes, err := m.find(-1, pattern)
840-
if err != nil {
841-
return nil, err
839+
if orgId != idx.OrgIdPublic {
840+
publicNodes, err := m.find(idx.OrgIdPublic, pattern)
841+
if err != nil {
842+
return nil, err
843+
}
844+
matchedNodes = append(matchedNodes, publicNodes...)
842845
}
843-
matchedNodes = append(matchedNodes, publicNodes...)
844846
log.Debug("memory-idx: %d nodes matching pattern %s found", len(matchedNodes), pattern)
845847
results := make([]idx.Node, 0)
846848
seen := make(map[string]struct{})
847-
// if there are public (orgId -1) and private leaf nodes with the same series
849+
// if there are public (orgId OrgIdPublic) and private leaf nodes with the same series
848850
// path, then the public metricDefs will be excluded.
849851
for _, n := range matchedNodes {
850852
if _, ok := seen[n.Path]; !ok {

idx/memory/memory_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func testGetAddKey(t *testing.T) {
8686
ix := New()
8787
ix.Init()
8888

89-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", false)
89+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", false)
9090
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", false)
9191
org2Series := getMetricData(2, 2, 5, 10, "metric.org2", false)
9292

@@ -99,7 +99,7 @@ func testGetAddKey(t *testing.T) {
9999
Convey(fmt.Sprintf("Then listing metrics for OrgId %d", orgId), func() {
100100
defs := ix.List(orgId)
101101
numSeries := len(series)
102-
if orgId != -1 {
102+
if orgId != idx.OrgIdPublic {
103103
numSeries += 5
104104
}
105105
So(defs, ShouldHaveLength, numSeries)
@@ -128,7 +128,7 @@ func TestFind(t *testing.T) {
128128
func testFind(t *testing.T) {
129129
ix := New()
130130
ix.Init()
131-
for _, s := range getMetricData(-1, 2, 5, 10, "metric.demo", false) {
131+
for _, s := range getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.demo", false) {
132132
s.Time = 10 * 86400
133133
ix.AddOrUpdate(s, 1)
134134
}
@@ -256,7 +256,7 @@ func testDelete(t *testing.T) {
256256
ix := New()
257257
ix.Init()
258258

259-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", false)
259+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", false)
260260
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", false)
261261

262262
for _, s := range publicSeries {
@@ -271,7 +271,7 @@ func TestDeleteTagged(t *testing.T) {
271271
ix := New()
272272
ix.Init()
273273

274-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", true)
274+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", true)
275275
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", true)
276276

277277
for _, s := range publicSeries {

0 commit comments

Comments
 (0)