Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions changelog.d/20250915_144507_markiewicz_cache_json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
A new scriv changelog fragment.

Uncomment the section that is right (remove the HTML comment wrapper).
For top level release notes, leave all the headers commented out.
-->

<!--
### Added

- A bullet item for the Added category.

-->
### Changed

- Parsed JSON files are now cached to reduce I/O and parsing costs.

<!--
### Fixed

- A bullet item for the Fixed category.

-->
<!--
### Deprecated

- A bullet item for the Deprecated category.

-->
<!--
### Removed

- A bullet item for the Removed category.

-->
<!--
### Security

- A bullet item for the Security category.

-->
<!--
### Infrastructure

- A bullet item for the Infrastructure category.

-->
15 changes: 10 additions & 5 deletions src/files/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BIDSFile } from '../types/filetree.ts'
import type { FileIgnoreRules } from './ignore.ts'
import { testAsyncFileAccess } from './access.test.ts'

import { pathsToTree } from '../files/filetree.ts'
import { loadJSON } from './json.ts'

function encodeUTF16(text: string) {
Expand All @@ -18,9 +19,12 @@ function encodeUTF16(text: string) {
return buffer
}

function makeFile(text: string, encoding: string): BIDSFile {
function makeFile(path: string, text: string, encoding: string): BIDSFile {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised we got away with not having path here for so long.

const bytes = encoding === 'utf-8' ? new TextEncoder().encode(text) : encodeUTF16(text)
const file = pathsToTree([path]).get(path) as BIDSFile
return {
path: file.path,
parent: file.parent,
readBytes: async (size: number) => {
return new Uint8Array(bytes)
},
Expand All @@ -30,13 +34,13 @@ function makeFile(text: string, encoding: string): BIDSFile {

Deno.test('Test JSON error conditions', async (t) => {
await t.step('Load valid JSON', async () => {
const JSONfile = makeFile('{"a": 1}', 'utf-8')
const JSONfile = makeFile('/valid-contents.json', '{"a": 1}', 'utf-8')
const result = await loadJSON(JSONfile)
assertObjectMatch(result, { a: 1 })
})

await t.step('Error on BOM', async () => {
const BOMfile = makeFile('\uFEFF{"a": 1}', 'utf-8')
const BOMfile = makeFile('/BOM.json', '\uFEFF{"a": 1}', 'utf-8')
let error: any = undefined
await loadJSON(BOMfile).catch((e) => {
error = e
Expand All @@ -45,7 +49,7 @@ Deno.test('Test JSON error conditions', async (t) => {
})

await t.step('Error on UTF-16', async () => {
const UTF16file = makeFile('{"a": 1}', 'utf-16')
const UTF16file = makeFile('/utf16.json', '{"a": 1}', 'utf-16')
let error: any = undefined
await loadJSON(UTF16file).catch((e) => {
error = e
Expand All @@ -54,13 +58,14 @@ Deno.test('Test JSON error conditions', async (t) => {
})

await t.step('Error on invalid JSON syntax', async () => {
const badJSON = makeFile('{"a": 1]', 'utf-8')
const badJSON = makeFile('/bad-syntax.json', '{"a": 1]', 'utf-8')
let error: any = undefined
await loadJSON(badJSON).catch((e) => {
error = e
})
assertObjectMatch(error, { code: 'JSON_INVALID' })
})
loadJSON.cache.clear()
})

testAsyncFileAccess('Test file access errors for loadJSON', loadJSON)
5 changes: 4 additions & 1 deletion src/files/json.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { filememoizeAsync } from '../utils/memoize.ts'
import type { BIDSFile } from '../types/filetree.ts'
import { readBytes } from './access.ts'

Expand All @@ -21,7 +22,7 @@ async function readJSONText(file: BIDSFile): Promise<string> {
}
}

export async function loadJSON(file: BIDSFile): Promise<Record<string, unknown>> {
async function _loadJSON(file: BIDSFile): Promise<Record<string, unknown>> {
const text = await readJSONText(file) // Raise encoding errors
let parsedText
try {
Expand All @@ -37,3 +38,5 @@ export async function loadJSON(file: BIDSFile): Promise<Record<string, unknown>>
}
return parsedText
}

export const loadJSON = filememoizeAsync(_loadJSON)
2 changes: 2 additions & 0 deletions src/schema/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BIDSContext, type BIDSContextDataset } from './context.ts'
import type { BIDSFile, FileTree } from '../types/filetree.ts'
import type { DatasetIssues } from '../issues/datasetIssues.ts'
import { loadTSV } from '../files/tsv.ts'
import { loadJSON } from '../files/json.ts'

function* quickWalk(dir: FileTree): Generator<BIDSFile> {
for (const file of dir.files) {
Expand Down Expand Up @@ -49,6 +50,7 @@ async function* _walkFileTree(
}
}
loadTSV.cache.delete(fileTree.path)
loadJSON.cache.delete(fileTree.path)
}

/** Walk all files in the dataset and construct a context for each one */
Expand Down
Loading