-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tools): move topological_sort from tools.utils to db.sort (#…
- Loading branch information
1 parent
7e4c654
commit 12504c9
Showing
6 changed files
with
78 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
local insert = table.insert | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
local function visit(current, neighbors_map, visited, marked, sorted) | ||
if visited[current] then | ||
return true | ||
end | ||
|
||
if marked[current] then | ||
return nil, "Cycle detected, cannot sort topologically" | ||
end | ||
|
||
marked[current] = true | ||
|
||
local schemas_pointing_to_current = neighbors_map[current] | ||
if schemas_pointing_to_current then | ||
local neighbor, ok, err | ||
for i = 1, #schemas_pointing_to_current do | ||
neighbor = schemas_pointing_to_current[i] | ||
ok, err = visit(neighbor, neighbors_map, visited, marked, sorted) | ||
if not ok then | ||
return nil, err | ||
end | ||
end | ||
end | ||
|
||
marked[current] = false | ||
|
||
visited[current] = true | ||
|
||
insert(sorted, 1, current) | ||
|
||
return true | ||
end | ||
|
||
|
||
function _M.topological_sort(items, get_neighbors) | ||
local neighbors_map = {} | ||
local source, destination | ||
local neighbors | ||
for i = 1, #items do | ||
source = items[i] -- services | ||
neighbors = get_neighbors(source) | ||
for j = 1, #neighbors do | ||
destination = neighbors[j] --routes | ||
neighbors_map[destination] = neighbors_map[destination] or {} | ||
insert(neighbors_map[destination], source) | ||
end | ||
end | ||
|
||
local sorted = {} | ||
local visited = {} | ||
local marked = {} | ||
|
||
local current, ok, err | ||
for i = 1, #items do | ||
current = items[i] | ||
if not visited[current] and not marked[current] then | ||
ok, err = visit(current, neighbors_map, visited, marked, sorted) | ||
if not ok then | ||
return nil, err | ||
end | ||
end | ||
end | ||
|
||
return sorted | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12504c9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bazel Build
Docker image available
kong/kong:12504c9fad0620e90c3e778b2bcac032c7374a0f
Artifacts available https://github.com/Kong/kong/actions/runs/6875976252