Skip to content
Draft

Clean #193

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
70 changes: 63 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/db/native/modify/modify.zig
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,10 @@ fn modifyInternal(env: napi.c.napi_env, info: napi.c.napi_callback_info, resCoun
db.expire(&ctx);

const newDirtyRanges = ctx.dirtyRanges.values();
std.debug.print("newDirtyRanges.len {d} - {d}\n", .{ newDirtyRanges.len, dirtyRanges.len });
assert(newDirtyRanges.len < dirtyRanges.len);
_ = napi.c.memcpy(dirtyRanges.ptr, newDirtyRanges.ptr, newDirtyRanges.len * 8);
std.debug.print("newDirtyRanges.len {d} - {d}\n", .{ newDirtyRanges.len, dirtyRanges.len });
dirtyRanges[newDirtyRanges.len] = 0.0;
writeoutPrevNodeId(&ctx, resCount, ctx.id);
}
24 changes: 12 additions & 12 deletions packages/db/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
MigrateFns,
parse,
Schema,
StrictSchema,
SchemaChecksum,
type Schema,
type SchemaIn,
type SchemaMigrateFns,
type SchemaOut,
} from '@based/schema'
import { BasedDbQuery, QueryByAliasObj } from './query/BasedDbQuery.js'
import { debugMode } from '../utils.js'
Expand Down Expand Up @@ -38,7 +38,7 @@ export class DbClient extends DbShared {
this.hooks = hooks
this.maxModifySize = maxModifySize
this.modifyCtx = new Ctx(
0,
{ locales: {}, types: {}, hash: 0 },
new Uint8Array(
new ArrayBuffer(Math.min(1e3, maxModifySize), {
maxByteLength: maxModifySize,
Expand Down Expand Up @@ -75,21 +75,21 @@ export class DbClient extends DbShared {
}

async setSchema(
schema: Schema,
transformFns?: MigrateFns,
): Promise<SchemaChecksum> {
schema: SchemaIn,
transformFns?: SchemaMigrateFns,
): Promise<SchemaOut['hash']> {
const strictSchema = parse(schema).schema
await this.drain()
const schemaChecksum = await this.hooks.setSchema(
strictSchema as StrictSchema,
strictSchema as Schema<true>,
transformFns,
)
if (this.stopped) {
return this.schema.hash
return this.schema?.hash ?? 0
}
if (schemaChecksum !== this.schema?.hash) {
await this.once('schema')
return this.schema.hash
return this.schema?.hash ?? 0
}
return schemaChecksum
}
Expand Down Expand Up @@ -261,7 +261,7 @@ export class DbClient extends DbShared {

destroy() {
this.stop()
delete this.listeners
this.listeners = {}
}

stop() {
Expand Down
21 changes: 12 additions & 9 deletions packages/db/src/client/modify/Ctx.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { writeUint64 } from '@based/utils'
import type { SchemaTypeDef, PropDef } from '@based/schema/def'
import type { LangCode } from '@based/schema'
import type { LangCode, MainDef, SchemaOut, TypeDef } from '@based/schema'
import { type ModifyOp } from './types.js'
import type { Tmp } from './Tmp.js'

export class Ctx {
constructor(schemaChecksum: number, array: Uint8Array<ArrayBufferLike>) {
constructor(schema: SchemaOut, array: Uint8Array<ArrayBufferLike>) {
this.array = array
this.max = array.buffer.maxByteLength - 4 // dataLen
this.size = array.buffer.byteLength - 4
writeUint64(array, schemaChecksum, 0)
this.schema = schema
writeUint64(array, schema.hash, 0)
}
schema: SchemaOut
start: number
index: number = 8
schema: SchemaTypeDef
typeDef: TypeDef
array: Uint8Array<ArrayBufferLike>
max: number
size: number
unsafe?: boolean
operation: ModifyOp
main: Map<PropDef, any> = new Map()
main: Map<MainDef, any> = new Map()
draining: Promise<void>
scheduled: Promise<void>
scheduled?: Promise<void>
locale: LangCode
sort: number = 0
sortText: number = 0
defaults: number = 0
cursor: {
type?: number
prop?: number
main?: number
main: number
operation?: ModifyOp
upserting?: boolean
} = {}
} = {
main: 0,
}
batch: {
count?: number
promises?: Tmp[]
Expand Down
Loading