Skip to content

Commit

Permalink
Merge pull request #114 from oliver-oloughlin/feature/collection-unif…
Browse files Browse the repository at this point in the history
…ication

Feature/collection unification
  • Loading branch information
oliver-oloughlin authored Nov 20, 2023
2 parents fd7e912 + fe6847c commit 5ca0499
Show file tree
Hide file tree
Showing 67 changed files with 3,388 additions and 1,723 deletions.
5 changes: 5 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"fmt": {
"semiColons": false
},
"lint": {
"rules": {
"exclude": ["no-explicit-any"]
}
},
"test": {
"include": ["./tests"]
}
Expand Down
5 changes: 0 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ export { kvdex } from "./src/kvdex.ts"
export { model } from "./src/model.ts"
export { KvDex } from "./src/kvdex.ts"
export { Collection, collection } from "./src/collection.ts"
export {
IndexableCollection,
indexableCollection,
} from "./src/indexable_collection.ts"
export { LargeCollection, largeCollection } from "./src/large_collection.ts"
export { AtomicBuilder } from "./src/atomic_builder.ts"
export { Document } from "./src/document.ts"

Expand Down
62 changes: 25 additions & 37 deletions src/atomic_builder.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { Collection } from "./collection.ts"
import { InvalidAtomicBuilderCollectionError } from "./errors.ts"
import { IndexableCollection } from "./indexable_collection.ts"
import { LargeCollection } from "./large_collection.ts"
import { InvalidCollectionError } from "./errors.ts"
import type {
AtomicCheck,
AtomicMutation,
AtomicSetOptions,
CollectionOptions,
CollectionSelector,
EnqueueOptions,
IndexableCollectionOptions,
KvId,
KvObject,
KvValue,
Expand Down Expand Up @@ -60,9 +57,9 @@ export class AtomicBuilder<
operations?: Operations,
) {
// Check for large collection
if (collection instanceof LargeCollection) {
throw new InvalidAtomicBuilderCollectionError(
"Atomic operations are not supported for LargeCollection",
if (collection._isSerialized) {
throw new InvalidCollectionError(
"Atomic operations are not supported for serialized collections",
)
}

Expand Down Expand Up @@ -156,7 +153,7 @@ export class AtomicBuilder<
const collection = this.collection
const parsed = collection._model.parse(value as TInput)
const docId = id ?? collection._idGenerator(parsed)
const idKey = extendKey(collection._keys.idKey, docId)
const idKey = extendKey(collection._keys.id, docId)

// Add set operation
this.operations.atomic.check({ key: idKey, versionstamp: null }).set(
Expand All @@ -165,23 +162,20 @@ export class AtomicBuilder<
options,
)

if (collection instanceof IndexableCollection) {
if (collection._isIndexable) {
// Set data as KvObject type
const _data = parsed as KvObject

// Add collection id key for collision detection
this.operations.indexAddCollectionKeys.push(collection._keys.baseKey)
this.operations.indexAddCollectionKeys.push(collection._keys.base)

// Add indexing operations
setIndices(
docId,
_data,
_data,
this.operations.atomic,
this.collection as unknown as IndexableCollection<
KvObject,
KvObject,
IndexableCollectionOptions<KvObject>
>,
this.collection,
options,
)
}
Expand All @@ -206,15 +200,15 @@ export class AtomicBuilder<
delete(id: KvId) {
// Create id key from id and collection id key
const collection = this.collection
const idKey = extendKey(collection._keys.idKey, id)
const idKey = extendKey(collection._keys.id, id)

// Add delete operation
this.operations.atomic.delete(idKey)

// If collection is indexable, handle indexing
if (this.collection instanceof IndexableCollection) {
if (this.collection._isIndexable) {
// Add collection key for collision detection
this.operations.indexDeleteCollectionKeys.push(collection._keys.baseKey)
this.operations.indexDeleteCollectionKeys.push(collection._keys.base)

// Add delete preperation function to prepeare delete functions list
this.operations.prepareDeleteFns.push(async (kv) => {
Expand Down Expand Up @@ -251,7 +245,7 @@ export class AtomicBuilder<
// Create Deno atomic checks from atomci checks input list
const checks: Deno.AtomicCheck[] = atomicChecks.map(
({ id, versionstamp }) => {
const key = extendKey(this.collection._keys.idKey, id)
const key = extendKey(this.collection._keys.id, id)
return {
key,
versionstamp,
Expand Down Expand Up @@ -283,7 +277,7 @@ export class AtomicBuilder<
*/
sum(id: KvId, value: TOutput extends Deno.KvU64 ? bigint : never) {
// Create id key from id and collection id key
const idKey = extendKey(this.collection._keys.idKey, id)
const idKey = extendKey(this.collection._keys.id, id)

// Add sum operation to atomic ops list
this.operations.atomic.sum(idKey, value)
Expand All @@ -310,7 +304,7 @@ export class AtomicBuilder<
*/
min(id: KvId, value: TOutput extends Deno.KvU64 ? bigint : never) {
// Create id key from id and collection id key
const idKey = extendKey(this.collection._keys.idKey, id)
const idKey = extendKey(this.collection._keys.id, id)

// Add min operation to atomic ops list
this.operations.atomic.min(idKey, value)
Expand All @@ -337,7 +331,7 @@ export class AtomicBuilder<
*/
max(id: KvId, value: TOutput extends Deno.KvU64 ? bigint : never) {
// Create id key from id and collection id key
const idKey = extendKey(this.collection._keys.idKey, id)
const idKey = extendKey(this.collection._keys.id, id)

// Add max operation to atomic ops list
this.operations.atomic.max(idKey, value)
Expand Down Expand Up @@ -375,7 +369,7 @@ export class AtomicBuilder<

// Map from atomic mutations to kv mutations
const kvMutations: Deno.KvMutation[] = mutations.map(({ id, ...rest }) => {
const idKey = extendKey(collection._keys.idKey, id)
const idKey = extendKey(collection._keys.id, id)

if (rest.type === "delete") {
return {
Expand Down Expand Up @@ -409,7 +403,7 @@ export class AtomicBuilder<
}

// If collection is indexable, handle indexing
if (collection instanceof IndexableCollection) {
if (collection._isIndexable) {
// Get document id from mutation key
const id = getDocumentId(mut.key)

Expand All @@ -421,18 +415,15 @@ export class AtomicBuilder<
// If mutation type is "set", handle setting of indices
if (mut.type === "set") {
// Add collection key for collision detection
this.operations.indexAddCollectionKeys.push(collection._keys.baseKey)
this.operations.indexAddCollectionKeys.push(collection._keys.base)

// Add indexing operations to atomic ops list
setIndices(
id,
mut.value as KvObject,
mut.value as KvObject,
this.operations.atomic,
this.collection as unknown as IndexableCollection<
KvObject,
KvObject,
IndexableCollectionOptions<KvObject>
>,
this.collection,
{
...mut,
},
Expand All @@ -443,7 +434,7 @@ export class AtomicBuilder<
if (mut.type === "delete") {
// Add collection key for collision detection
this.operations.indexDeleteCollectionKeys.push(
collection._keys.baseKey,
collection._keys.base,
)

// Add delete preperation function to delete preperation functions list
Expand Down Expand Up @@ -493,7 +484,8 @@ export class AtomicBuilder<
enqueue(data: QueueValue, options?: EnqueueOptions) {
// Prepare and add enqueue operation
const prep = prepareEnqueue(
this.collection._keys.baseKey,
this.collection._keys.base,
this.collection._keys.undelivered,
data,
options,
)
Expand Down Expand Up @@ -551,11 +543,7 @@ export class AtomicBuilder<
id,
data,
atomic,
this.collection as unknown as IndexableCollection<
KvObject,
KvObject,
IndexableCollectionOptions<KvObject>
>,
this.collection,
)

// Execute atomic operation
Expand Down
2 changes: 1 addition & 1 deletion src/atomic_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class AtomicWrapper implements Deno.AtomicOperation {
)

// Check status of all commits
const success = settled.every((v) => v.status === "fulfilled")
const success = settled.every((v) => v.status === "fulfilled" && v.value.ok)

// If successful, return commit result
if (success) {
Expand Down
Loading

0 comments on commit 5ca0499

Please sign in to comment.