Skip to content

Commit

Permalink
Simplified impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus committed Aug 30, 2023
1 parent f3f27f0 commit 22f1507
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
16 changes: 3 additions & 13 deletions htap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,22 @@ func (qcc *queryContextCache) init() *queryContextCache {
func (qcc *queryContextCache) add(qces ...queryContextEntry) {
qcc.mutex.Lock()
defer qcc.mutex.Unlock()
var newQcesBasedOnPriorities []queryContextEntry
for _, newQce := range qces {
logger.Debugf("adding query context: %v", newQce)
newQceProcessed := false
for existingQceIdx, existingQce := range qcc.entries {
if newQce.ID == existingQce.ID {
newQceProcessed = true
if newQce.Timestamp > existingQce.Timestamp {
if newQce.Priority == existingQce.Priority {
qcc.entries[existingQceIdx].Timestamp = newQce.Timestamp
qcc.entries[existingQceIdx].Context = newQce.Context
} else {
qcc.entries[existingQceIdx] = newQce
}
qcc.entries[existingQceIdx] = newQce
} else if newQce.Timestamp == existingQce.Timestamp {
if newQce.Priority != existingQce.Priority {
qcc.entries[existingQceIdx].Priority = newQce.Priority
qcc.entries[existingQceIdx].Context = newQce.Context
qcc.entries[existingQceIdx] = newQce
}
}
} else {
if newQce.Priority == existingQce.Priority {
qcc.entries[existingQceIdx].ID = newQce.ID
qcc.entries[existingQceIdx].Timestamp = newQce.Timestamp
qcc.entries[existingQceIdx].Context = newQce.Context
qcc.entries[existingQceIdx] = newQce
newQceProcessed = true
}
}
Expand All @@ -58,7 +49,6 @@ func (qcc *queryContextCache) add(qces ...queryContextEntry) {
qcc.entries = append(qcc.entries, newQce)
}
}
qcc.entries = append(qcc.entries, newQcesBasedOnPriorities...)
sort.Slice(qcc.entries, func(idx1, idx2 int) bool {
return qcc.entries[idx1].Priority < qcc.entries[idx2].Priority
})
Expand Down
30 changes: 15 additions & 15 deletions htap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ func TestSortingByPriority(t *testing.T) {
t.Run("Add another entry with different id, timestamp and priority - greater priority", func(t *testing.T) {
qcc.add(qceB)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceA, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with different id, timestamp and priority - lesser priority", func(t *testing.T) {
qcc.add(qceC)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceA, qceB}) {
t.Fatalf("entry with lesser priority should be added at the beginninig. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with different id, timestamp and priority - priority in the middle", func(t *testing.T) {
qcc.add(qceD)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceA, qceD, qceB}) {
t.Fatalf("entry with priority in the middle should be added at the middle. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
}
Expand All @@ -153,13 +153,13 @@ func TestAddingQcesWithTheSameIdAndLaterTimestamp(t *testing.T) {
t.Run("Add another entry with different priority", func(t *testing.T) {
qcc.add(qceC)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with same priority", func(t *testing.T) {
qcc.add(qceD)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceD, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
}
Expand All @@ -182,13 +182,13 @@ func TestAddingQcesWithTheSameIdAndSameTimestamp(t *testing.T) {
t.Run("Add another entry with different priority", func(t *testing.T) {
qcc.add(qceC)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with same priority", func(t *testing.T) { // TODO should anything happen here?
t.Run("Add another entry with same priority", func(t *testing.T) {
qcc.add(qceD)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
}
Expand All @@ -205,19 +205,19 @@ func TestAddingQcesWithTheSameIdAndEarlierTimestamp(t *testing.T) {
qcc.add(qceA)
qcc.add(qceB)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceA, qceB}) {
t.Fatalf("no entries added to cache. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with different priority", func(t *testing.T) {
qcc.add(qceC)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceA, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with same priority", func(t *testing.T) { // TODO should anything happen here?
t.Run("Add another entry with same priority", func(t *testing.T) {
qcc.add(qceD)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceA, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
}
Expand All @@ -234,19 +234,19 @@ func TestAddingQcesWithDifferentId(t *testing.T) {
qcc.add(qceA)
qcc.add(qceB)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceA, qceB}) {
t.Fatalf("no entries added to cache. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with same priority", func(t *testing.T) {
qcc.add(qceC)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceC, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
t.Run("Add another entry with different priority", func(t *testing.T) {
qcc.add(qceD)
if !reflect.DeepEqual(qcc.entries, []queryContextEntry{qceD, qceC, qceB}) {
t.Fatalf("entry with greater priority should be added at the end. %v", qcc.entries)
t.Fatalf("unexpected qcc entries. %v", qcc.entries)
}
})
}
Expand Down

0 comments on commit 22f1507

Please sign in to comment.