Compare commits
6 Commits
50a822aa0c
...
43b8b9db90
Author | SHA1 | Date | |
---|---|---|---|
43b8b9db90 | |||
ba528e477f | |||
276d2feddb | |||
3888bac91e | |||
9e3884d90e | |||
2044efa3cf |
@ -1,4 +1,3 @@
|
||||
import * as _RhizomeImports from "../src";
|
||||
/**
|
||||
* Tests for lossless view compose() and decompose() bidirectional conversion
|
||||
* Ensures that deltas can be composed into lossless views and decomposed back
|
||||
|
@ -58,7 +58,7 @@ describe('Concurrent Write Scenarios', () => {
|
||||
const result = resolver.resolve();
|
||||
|
||||
expect(result).toBeDefined();
|
||||
// Should resolve deterministically (likely based on delta processing order)
|
||||
// Should resolve deterministically using the LastWriteWins resolver's tie-breaking algorithm
|
||||
expect(typeof result!['entity1'].properties.score).toBe('number');
|
||||
expect([100, 200]).toContain(result!['entity1'].properties.score);
|
||||
});
|
||||
|
@ -670,7 +670,11 @@ describe('Custom Resolvers', () => {
|
||||
|
||||
const result = resolver.resolve();
|
||||
expect(result).toBeDefined();
|
||||
expect(result!['entity1'].properties.score).toBe(0); // Default value
|
||||
// The entity might not be present in the result if no properties were resolved
|
||||
if (result!['entity1']) {
|
||||
expect(result!['entity1'].properties).toBeDefined();
|
||||
expect(result!['entity1'].properties).not.toHaveProperty('score');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
@ -1,4 +1,3 @@
|
||||
import * as _RhizomeImports from "../src";
|
||||
import { Delta } from '../src/core';
|
||||
import { NegationHelper } from '../src/features';
|
||||
import { RhizomeNode } from '../src/node';
|
||||
|
@ -251,7 +251,7 @@ describe('Nested Object Resolution Performance', () => {
|
||||
while (currentView.nestedObjects.next && currentView.nestedObjects.next.length > 0) {
|
||||
currentView = currentView.nestedObjects.next[0];
|
||||
depth++;
|
||||
if (depth >= 5) break; // Prevent infinite loop
|
||||
if (depth >= 10) break; // Prevent infinite loop
|
||||
}
|
||||
|
||||
expect(depth).toBeLessThanOrEqual(5);
|
||||
|
@ -12,7 +12,8 @@
|
||||
import { RhizomeNode } from '../src/node';
|
||||
import { Delta } from '../src/core';
|
||||
import { DefaultSchemaRegistry } from '../src/schema';
|
||||
import { CommonSchemas, SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema';
|
||||
import { SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema';
|
||||
import { CommonSchemas } from '../src/test-utils/schemas';
|
||||
import { TypedCollectionImpl } from '../src/collections';
|
||||
|
||||
describe('Nested Object Resolution', () => {
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { QueryEngine } from '../src/query';
|
||||
import { Lossless } from '../src/views';
|
||||
import { DefaultSchemaRegistry } from '../src/schema';
|
||||
import { CommonSchemas, SchemaBuilder, PrimitiveSchemas } from '../src/schema';
|
||||
import { SchemaBuilder, PrimitiveSchemas } from '../src/schema';
|
||||
import { CommonSchemas } from '../src/test-utils/schemas';
|
||||
import { Delta } from '../src/core';
|
||||
import { RhizomeNode } from '../src/node';
|
||||
|
||||
|
@ -3,10 +3,11 @@ import {
|
||||
PrimitiveSchemas,
|
||||
ReferenceSchemas,
|
||||
ArraySchemas,
|
||||
CommonSchemas,
|
||||
// CommonSchemas has been moved to ./test-utils/schemas
|
||||
ObjectSchema
|
||||
} from '../src/schema';
|
||||
import { DefaultSchemaRegistry } from '../src/schema';
|
||||
import { CommonSchemas } from '../src/test-utils/schemas';
|
||||
import { TypedCollectionImpl, SchemaValidationError } from '../src/collections';
|
||||
import { RhizomeNode } from '../src/node';
|
||||
import { Delta } from '../src/core';
|
||||
|
@ -1,4 +1,3 @@
|
||||
import * as _RhizomeImports from "../src";
|
||||
import { Delta } from '../src/core';
|
||||
import { Lossless } from '../src/views';
|
||||
import { RhizomeNode } from '../src/node';
|
||||
|
@ -198,46 +198,8 @@ export class SchemaBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// Common schema patterns
|
||||
export const CommonSchemas = {
|
||||
// User schema with friends references
|
||||
User: () => SchemaBuilder
|
||||
.create('user')
|
||||
.name('User')
|
||||
.description('A user entity with profile information')
|
||||
.property('name', PrimitiveSchemas.requiredString())
|
||||
.property('email', PrimitiveSchemas.string())
|
||||
.property('age', PrimitiveSchemas.number())
|
||||
.property('active', PrimitiveSchemas.boolean())
|
||||
.property('friends', ArraySchemas.of(ReferenceSchemas.to('user-summary', 2)))
|
||||
.required('name')
|
||||
.build(),
|
||||
|
||||
// User summary schema for references to prevent infinite recursion
|
||||
UserSummary: () => SchemaBuilder
|
||||
.create('user-summary')
|
||||
.name('User Summary')
|
||||
.description('Abbreviated user information for references')
|
||||
.property('name', PrimitiveSchemas.requiredString())
|
||||
.property('email', PrimitiveSchemas.string())
|
||||
.required('name')
|
||||
.additionalProperties(false)
|
||||
.build(),
|
||||
|
||||
// Document schema
|
||||
Document: () => SchemaBuilder
|
||||
.create('document')
|
||||
.name('Document')
|
||||
.description('A document with metadata')
|
||||
.property('title', PrimitiveSchemas.requiredString())
|
||||
.property('content', PrimitiveSchemas.string())
|
||||
.property('author', ReferenceSchemas.required('user-summary'))
|
||||
.property('tags', ArraySchemas.of(PrimitiveSchemas.string()))
|
||||
.property('created', PrimitiveSchemas.requiredNumber())
|
||||
.property('published', PrimitiveSchemas.boolean())
|
||||
.required('title', 'author', 'created')
|
||||
.build()
|
||||
} as const;
|
||||
// Common schema patterns have been moved to __tests__/test-utils/schemas.ts
|
||||
// since they are only used for testing purposes.
|
||||
|
||||
/**
|
||||
* Context for tracking resolution state during nested object resolution
|
||||
|
59
src/test-utils/schemas.ts
Normal file
59
src/test-utils/schemas.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { SchemaBuilder } from '../../src/schema';
|
||||
|
||||
/**
|
||||
* Common schemas used for testing purposes.
|
||||
* These schemas are not part of the main application code
|
||||
* and are only used in test files.
|
||||
*/
|
||||
export const CommonSchemas = {
|
||||
// User schema with friends references
|
||||
User: () => SchemaBuilder
|
||||
.create('user')
|
||||
.name('User')
|
||||
.description('A user entity with profile information')
|
||||
.property('name', { type: 'primitive', primitiveType: 'string', required: true })
|
||||
.property('email', { type: 'primitive', primitiveType: 'string' })
|
||||
.property('age', { type: 'primitive', primitiveType: 'number' })
|
||||
.property('active', { type: 'primitive', primitiveType: 'boolean' })
|
||||
.property('friends', {
|
||||
type: 'array',
|
||||
itemSchema: {
|
||||
type: 'reference',
|
||||
targetSchema: 'user-summary',
|
||||
maxDepth: 2
|
||||
}
|
||||
})
|
||||
.required('name')
|
||||
.build(),
|
||||
|
||||
// User summary schema for references to prevent infinite recursion
|
||||
UserSummary: () => SchemaBuilder
|
||||
.create('user-summary')
|
||||
.name('User Summary')
|
||||
.description('Abbreviated user information for references')
|
||||
.property('name', { type: 'primitive', primitiveType: 'string', required: true })
|
||||
.property('email', { type: 'primitive', primitiveType: 'string' })
|
||||
.build(),
|
||||
|
||||
// Document schema
|
||||
Document: () => SchemaBuilder
|
||||
.create('document')
|
||||
.name('Document')
|
||||
.description('A document with title, content, and author')
|
||||
.property('title', { type: 'primitive', primitiveType: 'string', required: true })
|
||||
.property('content', { type: 'primitive', primitiveType: 'string' })
|
||||
.property('author', {
|
||||
type: 'reference',
|
||||
targetSchema: 'user-summary',
|
||||
maxDepth: 1,
|
||||
required: true
|
||||
})
|
||||
.property('tags', {
|
||||
type: 'array',
|
||||
itemSchema: { type: 'primitive', primitiveType: 'string' }
|
||||
})
|
||||
.property('created', { type: 'primitive', primitiveType: 'number', required: true })
|
||||
.property('published', { type: 'primitive', primitiveType: 'boolean' })
|
||||
.required('title', 'author', 'created')
|
||||
.build()
|
||||
} as const;
|
@ -41,11 +41,32 @@ export abstract class Lossy<Accumulator, Result> {
|
||||
// apply a filter to the deltas composing that lossless view,
|
||||
// and then apply a supplied resolver function which receives
|
||||
// the filtered lossless view as input.
|
||||
// Resolve the current state of the view
|
||||
resolve(entityIds?: DomainEntityID[]): Result | undefined {
|
||||
if (!entityIds) {
|
||||
entityIds = Array.from(this.lossless.domainEntities.keys());
|
||||
}
|
||||
|
||||
// If we don't have an accumulator, build it from the lossless view
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = {} as Accumulator;
|
||||
|
||||
// Use the general view method to get the full view
|
||||
const fullView = this.lossless.view(entityIds, this.deltaFilter);
|
||||
|
||||
// Build the accumulator by reducing each entity's view
|
||||
for (const entityId of entityIds) {
|
||||
const losslessViewOne = fullView[entityId];
|
||||
if (losslessViewOne) {
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = this.initializer(losslessViewOne);
|
||||
} else {
|
||||
this.accumulator = this.reducer(this.accumulator, losslessViewOne);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.accumulator) return undefined;
|
||||
|
||||
return this.resolver(this.accumulator);
|
||||
|
@ -64,8 +64,10 @@ export class AggregationResolver extends Lossy<Accumulator, Result> {
|
||||
super(lossless);
|
||||
}
|
||||
|
||||
initializer(): Accumulator {
|
||||
return {};
|
||||
initializer(view: LosslessViewOne): Accumulator {
|
||||
return {
|
||||
[view.id]: { id: view.id, properties: {} }
|
||||
};
|
||||
}
|
||||
|
||||
reducer(acc: Accumulator, cur: LosslessViewOne): Accumulator {
|
||||
@ -120,31 +122,7 @@ export class AggregationResolver extends Lossy<Accumulator, Result> {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Override resolve to build accumulator on-demand if needed
|
||||
resolve(entityIds?: DomainEntityID[]): Result | undefined {
|
||||
if (!entityIds) {
|
||||
entityIds = Array.from(this.lossless.domainEntities.keys());
|
||||
}
|
||||
|
||||
// If we don't have an accumulator, build it from the lossless view
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = this.initializer();
|
||||
|
||||
// Use the general view method instead of viewSpecific
|
||||
const fullView = this.lossless.view(entityIds, this.deltaFilter);
|
||||
|
||||
for (const entityId of entityIds) {
|
||||
const losslessViewOne = fullView[entityId];
|
||||
if (losslessViewOne) {
|
||||
this.accumulator = this.reducer(this.accumulator, losslessViewOne);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.accumulator) return undefined;
|
||||
|
||||
return this.resolver(this.accumulator);
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience classes for common aggregation types
|
||||
|
@ -14,7 +14,8 @@ export interface ResolverPlugin<T = unknown> {
|
||||
update(currentState: T, newValue: PropertyTypes, delta: CollapsedDelta): T;
|
||||
|
||||
// Resolve the final value from the accumulated state
|
||||
resolve(state: T): PropertyTypes;
|
||||
// Returns undefined if no valid value could be resolved
|
||||
resolve(state: T): PropertyTypes | undefined;
|
||||
}
|
||||
|
||||
// Configuration for custom resolver
|
||||
@ -63,8 +64,10 @@ export class CustomResolver extends Lossy<CustomResolverAccumulator, CustomResol
|
||||
super(lossless);
|
||||
}
|
||||
|
||||
initializer(): CustomResolverAccumulator {
|
||||
return {};
|
||||
initializer(view: LosslessViewOne): CustomResolverAccumulator {
|
||||
return {
|
||||
[view.id]: { id: view.id, properties: {} }
|
||||
};
|
||||
}
|
||||
|
||||
reducer(acc: CustomResolverAccumulator, cur: LosslessViewOne): CustomResolverAccumulator {
|
||||
@ -106,7 +109,10 @@ export class CustomResolver extends Lossy<CustomResolverAccumulator, CustomResol
|
||||
|
||||
for (const [propertyId, propertyState] of Object.entries(entity.properties)) {
|
||||
const resolvedValue = propertyState.plugin.resolve(propertyState.state);
|
||||
entityResult.properties[propertyId] = resolvedValue;
|
||||
// Only add the property if the resolved value is not undefined
|
||||
if (resolvedValue !== undefined) {
|
||||
entityResult.properties[propertyId] = resolvedValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Only include entities that have at least one resolved property
|
||||
@ -118,30 +124,7 @@ export class CustomResolver extends Lossy<CustomResolverAccumulator, CustomResol
|
||||
return res;
|
||||
}
|
||||
|
||||
// Override resolve to build accumulator on-demand if needed
|
||||
resolve(entityIds?: DomainEntityID[]): CustomResolverResult | undefined {
|
||||
if (!entityIds) {
|
||||
entityIds = Array.from(this.lossless.domainEntities.keys());
|
||||
}
|
||||
|
||||
// If we don't have an accumulator, build it from the lossless view
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = this.initializer();
|
||||
|
||||
const fullView = this.lossless.view(entityIds, this.deltaFilter);
|
||||
|
||||
for (const entityId of entityIds) {
|
||||
const losslessViewOne = fullView[entityId];
|
||||
if (losslessViewOne) {
|
||||
this.accumulator = this.reducer(this.accumulator, losslessViewOne);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.accumulator) return undefined;
|
||||
|
||||
return this.resolver(this.accumulator);
|
||||
}
|
||||
}
|
||||
|
||||
// Built-in plugin implementations
|
||||
@ -269,8 +252,8 @@ export class MinPlugin implements ResolverPlugin<{ min?: number }> {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
resolve(state: { min?: number }): PropertyTypes {
|
||||
return state.min || 0;
|
||||
resolve(state: { min?: number }): PropertyTypes | undefined {
|
||||
return state.min;
|
||||
}
|
||||
}
|
||||
|
||||
@ -290,7 +273,7 @@ export class MaxPlugin implements ResolverPlugin<{ max?: number }> {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
resolve(state: { max?: number }): PropertyTypes {
|
||||
return state.max || 0;
|
||||
resolve(state: { max?: number }): PropertyTypes | undefined {
|
||||
return state.max;
|
||||
}
|
||||
}
|
@ -70,66 +70,44 @@ export function lastValueFromDeltas(
|
||||
}
|
||||
|
||||
export class LastWriteWins extends Lossy<Accumulator, Result> {
|
||||
initializer(): Accumulator {
|
||||
return {};
|
||||
initializer(view: LosslessViewOne): Accumulator {
|
||||
return {
|
||||
[view.id]: { id: view.id, properties: {} }
|
||||
};
|
||||
}
|
||||
|
||||
reducer(acc: Accumulator, cur: LosslessViewOne): Accumulator {
|
||||
if (!acc[cur.id]) {
|
||||
acc[cur.id] = {id: cur.id, properties: {}};
|
||||
acc[cur.id] = { id: cur.id, properties: {} };
|
||||
}
|
||||
|
||||
for (const [key, deltas] of Object.entries(cur.propertyDeltas)) {
|
||||
const {value, timeUpdated} = lastValueFromDeltas(key, deltas) || {};
|
||||
if (!value || !timeUpdated) continue;
|
||||
const { value, timeUpdated } = lastValueFromDeltas(key, deltas) || {};
|
||||
if (!value || timeUpdated === undefined) continue;
|
||||
|
||||
if (timeUpdated > (acc[cur.id].properties[key]?.timeUpdated || 0)) {
|
||||
acc[cur.id].properties[key] = {
|
||||
value,
|
||||
timeUpdated
|
||||
};
|
||||
const currentTime = acc[cur.id].properties[key]?.timeUpdated || 0;
|
||||
if (timeUpdated > currentTime) {
|
||||
acc[cur.id].properties[key] = { value, timeUpdated };
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
};
|
||||
}
|
||||
|
||||
resolver(cur: Accumulator): Result {
|
||||
const res: Result = {};
|
||||
const result: Result = {};
|
||||
|
||||
for (const [id, ent] of Object.entries(cur)) {
|
||||
res[id] = {id, properties: {}};
|
||||
for (const [key, {value}] of Object.entries(ent.properties)) {
|
||||
res[id].properties[key] = value;
|
||||
}
|
||||
for (const [id, entity] of Object.entries(cur)) {
|
||||
result[id] = {
|
||||
id,
|
||||
properties: Object.fromEntries(
|
||||
Object.entries(entity.properties)
|
||||
.map(([key, { value }]) => [key, value])
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
// Override resolve to build accumulator on-demand if needed
|
||||
resolve(entityIds?: DomainEntityID[]): Result | undefined {
|
||||
if (!entityIds) {
|
||||
entityIds = Array.from(this.lossless.domainEntities.keys());
|
||||
}
|
||||
|
||||
// If we don't have an accumulator, build it from the lossless view
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = this.initializer();
|
||||
|
||||
// Use the general view method
|
||||
const fullView = this.lossless.view(entityIds, this.deltaFilter);
|
||||
|
||||
for (const entityId of entityIds) {
|
||||
const losslessViewOne = fullView[entityId];
|
||||
if (losslessViewOne) {
|
||||
this.accumulator = this.reducer(this.accumulator, losslessViewOne);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.accumulator) return undefined;
|
||||
|
||||
return this.resolver(this.accumulator);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,8 +72,10 @@ export class TimestampResolver extends Lossy<Accumulator, Result> {
|
||||
super(lossless);
|
||||
}
|
||||
|
||||
initializer(): Accumulator {
|
||||
return {};
|
||||
initializer(view: LosslessViewOne): Accumulator {
|
||||
return {
|
||||
[view.id]: { id: view.id, properties: {} }
|
||||
};
|
||||
}
|
||||
|
||||
reducer(acc: Accumulator, cur: LosslessViewOne): Accumulator {
|
||||
@ -124,31 +126,7 @@ export class TimestampResolver extends Lossy<Accumulator, Result> {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Override resolve to build accumulator on-demand if needed
|
||||
resolve(entityIds?: DomainEntityID[]): Result | undefined {
|
||||
if (!entityIds) {
|
||||
entityIds = Array.from(this.lossless.domainEntities.keys());
|
||||
}
|
||||
|
||||
// If we don't have an accumulator, build it from the lossless view
|
||||
if (!this.accumulator) {
|
||||
this.accumulator = this.initializer();
|
||||
|
||||
// Use the general view method instead of viewSpecific
|
||||
const fullView = this.lossless.view(entityIds, this.deltaFilter);
|
||||
|
||||
for (const entityId of entityIds) {
|
||||
const losslessViewOne = fullView[entityId];
|
||||
if (losslessViewOne) {
|
||||
this.accumulator = this.reducer(this.accumulator, losslessViewOne);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.accumulator) return undefined;
|
||||
|
||||
return this.resolver(this.accumulator);
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience classes for different tie-breaking strategies
|
||||
|
Loading…
x
Reference in New Issue
Block a user