-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gatsby): Add cache-lmdb implementation #32373
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as path from "path" | ||
import fs from "fs-extra" | ||
import { describeWhenLMDB } from "../worker/__tests__/test-helpers" | ||
|
||
const complexObject = { | ||
key: `value`, | ||
another_key: 2, | ||
nested: { hello: `world`, foo: `bar`, nested: { super: `duper` } }, | ||
} | ||
|
||
describeWhenLMDB(`cache-lmdb`, () => { | ||
let cache | ||
|
||
beforeAll(async () => { | ||
const { default: GatsbyCacheLmdb } = await import(`../cache-lmdb`) | ||
cache = new GatsbyCacheLmdb({ name: `__test__` }) | ||
const fileDir = path.join( | ||
process.cwd(), | ||
`.cache/caches-lmdb-${process.env.JEST_WORKER_ID}` | ||
) | ||
await fs.emptyDir(fileDir) | ||
}) | ||
|
||
it(`it can be instantiated`, () => { | ||
if (!cache) fail(`cache not instantiated`) | ||
}) | ||
it(`returns cache instance with get/set methods`, () => { | ||
expect(cache.get).toEqual(expect.any(Function)) | ||
expect(cache.set).toEqual(expect.any(Function)) | ||
}) | ||
describe(`set`, () => { | ||
it(`resolves to the value it cached (string)`, () => | ||
expect(cache.set(`string`, `I'm a simple string`)).resolves.toBe( | ||
`I'm a simple string` | ||
)) | ||
it(`resolves to the value it cached (object)`, () => | ||
expect(cache.set(`object`, complexObject)).resolves.toStrictEqual( | ||
complexObject | ||
)) | ||
}) | ||
describe(`get`, () => { | ||
it(`resolves to the found value (string)`, () => | ||
expect(cache.get(`string`)).resolves.toBe(`I'm a simple string`)) | ||
it(`resolves to the found value (object)`, () => | ||
expect(cache.get(`object`)).resolves.toStrictEqual(complexObject)) | ||
}) | ||
}) |
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,53 @@ | ||
import { open, RootDatabase, Database } from "lmdb-store" | ||
import path from "path" | ||
|
||
// Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb" | ||
const cacheDbFile = | ||
process.env.NODE_ENV === `test` | ||
? `caches-lmdb-${ | ||
// FORCE_TEST_DATABASE_ID will be set if this gets executed in worker context | ||
// when running jest tests. JEST_WORKER_ID will be set when this gets executed directly | ||
// in test context (jest will use jest-worker internally). | ||
process.env.FORCE_TEST_DATABASE_ID ?? process.env.JEST_WORKER_ID | ||
}` | ||
: `caches-lmdb` | ||
|
||
export default class GatsbyCacheLmdb { | ||
private static store | ||
private db: Database | undefined | ||
public readonly name: string | ||
|
||
constructor({ name = `db` }: { name: string }) { | ||
this.name = name | ||
} | ||
|
||
private static getStore(): RootDatabase { | ||
if (!GatsbyCacheLmdb.store) { | ||
GatsbyCacheLmdb.store = open({ | ||
name: `root`, | ||
path: path.join(process.cwd(), `.cache/${cacheDbFile}`), | ||
compression: true, | ||
maxDbs: 200, | ||
}) | ||
} | ||
return GatsbyCacheLmdb.store | ||
} | ||
|
||
private getDb(): Database { | ||
if (!this.db) { | ||
this.db = GatsbyCacheLmdb.getStore().openDB({ | ||
name: this.name, | ||
}) | ||
} | ||
return this.db | ||
} | ||
|
||
async get<T = unknown>(key): Promise<T | undefined> { | ||
return this.getDb().get(key) | ||
} | ||
|
||
async set<T>(key: string, value: T): Promise<T | undefined> { | ||
await this.getDb().put(key, value) | ||
return value | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Curious why you need to postfix it with worker id? Jest runs tests on multiple workers per file not test.
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.
Mostly did that because that's how the class uses itself: https://github.com/gatsbyjs/gatsby/pull/32373/files#diff-efecbacd93ffd4230b01b2b38d8e9ab6b06f723b31146e8a3f08e747123a885aR11
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.
I think this just clears the folder with the previous version of db. The actual path is set in
cache-lmdb.ts
and we need it in case if multiple tests use cache.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.
That's a little bit weird but I don't have enough context so 👍
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.
This is not for this test alone. Other tests running in other processes may be writing/cleaning the same db.