Skip to content

Commit

Permalink
refactor(tools): move topological_sort from tools.utils to db.sort (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon authored Nov 15, 2023
1 parent 7e4c654 commit 12504c9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 73 deletions.
1 change: 1 addition & 0 deletions kong-3.6.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ build = {
["kong.workspaces"] = "kong/workspaces/init.lua",

["kong.db"] = "kong/db/init.lua",
["kong.db.utils"] = "kong/db/utils.lua",
["kong.db.errors"] = "kong/db/errors.lua",
["kong.db.iteration"] = "kong/db/iteration.lua",
["kong.db.dao"] = "kong/db/dao/init.lua",
Expand Down
2 changes: 1 addition & 1 deletion kong/db/schema/topological_sort.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local constants = require "kong.constants"
local utils = require "kong.tools.utils"
local utils = require "kong.db.utils"


local utils_toposort = utils.topological_sort
Expand Down
3 changes: 2 additions & 1 deletion kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local stringx = require "pl.stringx"
local semaphore = require "ngx.semaphore"
local kong_global = require "kong.global"
local constants = require "kong.constants"
local db_utils = require "kong.db.utils"


local setmetatable = setmetatable
Expand All @@ -28,7 +29,7 @@ local log = ngx.log
local match = string.match
local fmt = string.format
local sub = string.sub
local utils_toposort = utils.topological_sort
local utils_toposort = db_utils.topological_sort
local insert = table.insert
local table_merge = utils.table_merge

Expand Down
73 changes: 73 additions & 0 deletions kong/db/utils.lua
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
70 changes: 0 additions & 70 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local tostring = tostring
local tonumber = tonumber
local sort = table.sort
local concat = table.concat
local insert = table.insert
local fmt = string.format
local join = pl_stringx.join
local split = pl_stringx.split
Expand Down Expand Up @@ -590,75 +589,6 @@ _M.get_response_type = get_response_type
_M.get_error_template = get_error_template


local topological_sort do

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

topological_sort = function(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
end
_M.topological_sort = topological_sort


do
local modules = {
"kong.tools.table",
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ describe("Utils", function()

describe("topological_sort", function()
local get_neighbors = function(x) return x end
local ts = utils.topological_sort
local ts = require("kong.db.utils").topological_sort

it("it puts destinations first", function()
local a = { id = "a" }
Expand Down

1 comment on commit 12504c9

@khcp-gha-bot
Copy link

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

Please sign in to comment.