forked from fireproof-storage/fireproof
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MMKV & friends, store-native implementation
- Loading branch information
Showing
10 changed files
with
215 additions
and
11 deletions.
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
import { ConfigOpts, Database, useFireproof as useFireproofReact } from 'use-fireproof'; | ||
import { StoreOpts } from '@fireproof/encrypted-blockstore' | ||
import * as crypto from '@fireproof/encrypted-blockstore/crypto-web' | ||
|
||
import { | ||
makeDataStore, | ||
makeMetaStore, | ||
makeRemoteWAL | ||
} from './store-native' | ||
|
||
const store = { | ||
makeDataStore, | ||
makeMetaStore, | ||
makeRemoteWAL | ||
} as unknown as StoreOpts | ||
|
||
|
||
// Fireproof React exports | ||
export * from 'use-fireproof'; | ||
|
||
// export (override with) a new 'useFireproof' for React Native | ||
export const useFireproof = ( | ||
name?: string | Database | undefined, | ||
config?: ConfigOpts | undefined | ||
) => { | ||
return useFireproofReact(name, { | ||
...config, | ||
store, | ||
crypto, | ||
}) | ||
}; | ||
|
File renamed without changes.
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,131 @@ | ||
/* eslint-disable import/first */ | ||
import { format, parse, ToString } from '@ipld/dag-json' | ||
import { MMKV } from 'react-native-mmkv' | ||
import { AnyBlock, AnyLink, DbMeta } from '@fireproof/encrypted-blockstore/src/types' | ||
import { DataStore as DataStoreBase, MetaStore as MetaStoreBase } from '@fireproof/encrypted-blockstore/src/store' | ||
import { RemoteWAL as RemoteWALBase, WALState } from '@fireproof/encrypted-blockstore/src/remote-wal' | ||
import type { Loadable, Loader } from '@fireproof/encrypted-blockstore/src/loader' | ||
|
||
export const makeDataStore = (name: string) => new DataStore(name) | ||
export const makeMetaStore = (loader: Loader) => new MetaStore(loader.name) | ||
export const makeRemoteWAL = (loader: Loadable) => new RemoteWAL(loader) | ||
|
||
export class DataStore extends DataStoreBase { | ||
tag: string = 'car-native-mmkv' | ||
rndb: MMKV | null = null | ||
|
||
async _withDB(dbWorkFun: (arg0: any) => any) { | ||
if (!this.rndb) { | ||
const dbName = `fp.${this.STORAGE_VERSION}.${this.name}` | ||
this.rndb = new MMKV({ | ||
id: dbName, | ||
}) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await dbWorkFun(this.rndb) | ||
} | ||
|
||
async load(cid: AnyLink): Promise<AnyBlock> { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await this._withDB(async (db: MMKV) => { | ||
const bytes = db.getBuffer(cid.toString()) | ||
if (!bytes) throw new Error(`missing db block ${cid.toString()}`) | ||
return { cid, bytes } | ||
}) | ||
} | ||
|
||
async save(car: AnyBlock): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await this._withDB(async (db: MMKV) => { | ||
db.set(car.cid.toString(), car.bytes) | ||
}) | ||
} | ||
|
||
async remove(cid: AnyLink): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await this._withDB(async (db: MMKV) => { | ||
db.delete(cid.toString()) | ||
}) | ||
} | ||
} | ||
|
||
export class RemoteWAL extends RemoteWALBase { | ||
tag: string = 'wal-native-mmkv' | ||
wal: MMKV | null = null | ||
|
||
headerKey(branch: string) { | ||
// remove 'public' on next storage version bump | ||
return `fp.${this.STORAGE_VERSION}.wal.${this.loader.name}.${branch}` | ||
} | ||
|
||
async _withDB(dbWorkFun: (arg0: any) => any) { | ||
if (!this.wal) { | ||
const dbName = `fp.${this.STORAGE_VERSION}.wal` | ||
this.wal = new MMKV({ | ||
id: dbName, | ||
}) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await dbWorkFun(this.wal) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async load(branch = 'main'): Promise<WALState | null> { | ||
return await this._withDB(async (db: MMKV) => { | ||
const doc = db.getString(this.headerKey(branch)) | ||
if (!doc) return null | ||
return parse<WALState>(doc) | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async save(state: WALState, branch = 'main'): Promise<void> { | ||
return await this._withDB(async (db: MMKV) => { | ||
const encoded: ToString<WALState> = format(state) | ||
db.set(this.headerKey(branch), encoded) | ||
}) | ||
} | ||
} | ||
|
||
export class MetaStore extends MetaStoreBase { | ||
tag: string = 'header-native-mmkv' | ||
meta: MMKV | null = null | ||
|
||
headerKey(branch: string) { | ||
return `fp.${this.STORAGE_VERSION}.meta.${this.name}.${branch}` | ||
} | ||
|
||
async _withDB(dbWorkFun: (arg0: any) => any) { | ||
if (!this.meta) { | ||
const dbName = `fp.${this.STORAGE_VERSION}.meta` | ||
this.meta = new MMKV({ | ||
id: dbName, | ||
}) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await dbWorkFun(this.meta) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async load(branch: string = 'main'): Promise<DbMeta[] | null> { | ||
return await this._withDB(async (db: MMKV) => { | ||
const doc = db.getString(this.headerKey(branch)) | ||
if (!doc) return null | ||
// TODO: react native wrt below? | ||
// browser assumes a single writer process | ||
// to support concurrent updates to the same database across multiple tabs | ||
// we need to implement the same kind of mvcc.crdt solution as in store-fs and connect-s3 | ||
return [this.parseHeader(doc)] | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async save(meta: DbMeta, branch: string = 'main') { | ||
return await this._withDB(async (db: MMKV) => { | ||
const headerKey = this.headerKey(branch) | ||
const bytes = this.makeHeader(meta) | ||
db.set(headerKey, bytes) | ||
return null | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.