Skip to content

Commit

Permalink
chore: implements mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Mar 12, 2024
1 parent 6753baa commit efc44bc
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,40 @@ export interface MapperResult {
subId: string[]
}

export function mapper(_matches: MapperMatch[]): MapperResult {
return {
idMap: [],
subId: [],
function createIdMap(matches: MapperMatch[]): string[] {
const buffer: string[] = []

for (const [index, match] of matches.entries()) {
const { containerId, hostId } = match

if (index === 0) {
buffer.push(`0 100000 ${containerId}`)
} else if (matches[index].containerId !== matches[index - 1].containerId + 1) {
buffer.push(`${matches[index - 1].containerId + 1} ${matches[index - 1].containerId + 100001} ${containerId - matches[index - 1].containerId}`)
}

buffer.push(`${containerId} ${hostId} 1`)

if (index === matches.length - 1) {
buffer.push(`${containerId + 1} ${containerId + 100001} ${65535 - containerId}`)
}
}

return buffer
}

function createSubId(matches: MapperMatch[]): string[] {
return matches.map((match) => `root:${match.hostId}:1`)
}

function sortMatches(matches: MapperMatch[]): MapperMatch[] {
return matches.toSorted((a, b) => a.containerId - b.containerId)
}

export function mapper(matches: MapperMatch[]): MapperResult {
const sortedMatches = sortMatches(matches)
const idMap = createIdMap(sortedMatches)
const subId = createSubId(sortedMatches)

return { idMap, subId }
}

0 comments on commit efc44bc

Please sign in to comment.