Skip to content

Commit 86e50b1

Browse files
Merge pull request #145 from sat-utils/visitedMemoryReduce
Fixes bug where nodes with no children were marked as visited increasing memory.
2 parents dc27c01 + e7efbdc commit 86e50b1

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

packages/api-lib/libs/ingest.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ const limiter = new Bottleneck({
1717
const limitedRequest = limiter.wrap(request)
1818
const limitedRead = limiter.wrap(util.promisify(fs.readFile))
1919

20-
async function fetchChildren(node, basePath) {
20+
function getSelfRef(node) {
21+
let ref
2122
const self = node.links.find((link) => (link.rel === 'self'))
23+
if (self && self.href) {
24+
ref = self.href
25+
}
26+
return ref
27+
}
28+
29+
function getChildLinks(node) {
2230
const links =
2331
node.links.filter((link) => (link.rel === 'child' || link.rel === 'item'))
32+
return links
33+
}
34+
35+
async function fetchChildren(node, links, basePath) {
36+
const selfHref = getSelfRef(node)
2437
const linkPromises = links.map((link) => {
2538
let urlPath
2639
let returnPromise
27-
if (!self || !self.href || !link.href) {
40+
if (!selfHref || !link.href) {
2841
return Promise.reject(new Error(`${node.id} has invalid links`))
2942
}
3043
if (path.isAbsolute(link.href)) {
@@ -34,7 +47,7 @@ async function fetchChildren(node, basePath) {
3447
if (basePath) {
3548
urlPath = `${path.dirname(basePath)}/${link.href}`
3649
} else {
37-
urlPath = `${path.dirname(self.href)}/${link.href}`
50+
urlPath = `${path.dirname(selfHref)}/${link.href}`
3851
}
3952
}
4053
if (isUrl(urlPath)) {
@@ -61,31 +74,26 @@ async function fetchChildren(node, basePath) {
6174
return children
6275
}
6376

64-
function getSelfRef(node) {
65-
let ref
66-
const self = node.links.find((link) => (link.rel === 'self'))
67-
if (self && self.href) {
68-
ref = self.href
69-
}
70-
return ref
71-
}
72-
7377
// Mutates stack and visited
7478
async function visitChildren(node, stack, visited, basePath) {
7579
let children
80+
const nodeLinks = getChildLinks(node)
7681
try {
77-
children = await fetchChildren(node, basePath)
82+
children = await fetchChildren(node, nodeLinks, basePath)
7883
} catch (error) {
7984
logger.error(error)
8085
}
8186
if (children) {
8287
// eslint-disable-next-line
8388
for (const child of children) {
8489
const key = getSelfRef(child)
90+
const childLinks = getChildLinks(child)
8591
if (key) {
8692
if (!visited[key]) {
87-
visited[key] = true
8893
stack.push(child)
94+
if (childLinks.length) {
95+
visited[key] = true
96+
}
8997
}
9098
} else {
9199
logger.error(`${node.id} has invalid self link`)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22
docker-compose up & while ! nc -z $DOCKER_NAME 4571; do sleep 1; done;
3-
sleep 10;
3+
sleep 20;
44
node ./ingestData.js && yarn ava ./tests/integration/test_api.js

0 commit comments

Comments
 (0)