mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Always release lock when patching
This commit is contained in:
parent
74415cf49b
commit
3362eee2c2
@ -65,10 +65,25 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
|
||||
throw new NotImplementedHttpError('WHERE statements are not supported');
|
||||
}
|
||||
|
||||
// Read the quads of the current representation
|
||||
const lock = await this.locker.acquire(identifier);
|
||||
const quads = await this.source.getRepresentation(identifier,
|
||||
{ type: [{ value: INTERNAL_QUADS, weight: 1 }]});
|
||||
try {
|
||||
await this.applyPatch(identifier, deletes, inserts);
|
||||
} finally {
|
||||
await lock.release();
|
||||
}
|
||||
}
|
||||
|
||||
private isDeleteInsert(op: Algebra.Operation): op is Algebra.DeleteInsert {
|
||||
return op.type === Algebra.types.DELETE_INSERT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the given deletes and inserts to the resource.
|
||||
*/
|
||||
private async applyPatch(identifier: ResourceIdentifier, deletes: Algebra.Pattern[], inserts: Algebra.Pattern[]):
|
||||
Promise<void> {
|
||||
// Read the quads of the current representation
|
||||
const quads = await this.source.getRepresentation(identifier, { type: [{ value: INTERNAL_QUADS, weight: 1 }]});
|
||||
const store = new Store<BaseQuad>();
|
||||
const importEmitter = store.import(quads.data);
|
||||
await new Promise((resolve, reject): void => {
|
||||
@ -91,11 +106,5 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
|
||||
metadata,
|
||||
};
|
||||
await this.source.setRepresentation(identifier, representation);
|
||||
|
||||
await lock.release();
|
||||
}
|
||||
|
||||
private isDeleteInsert(op: Algebra.Operation): op is Algebra.DeleteInsert {
|
||||
return op.type === Algebra.types.DELETE_INSERT;
|
||||
}
|
||||
}
|
||||
|
@ -199,4 +199,20 @@ describe('A SparqlUpdatePatchHandler', (): void => {
|
||||
await expect(handle).rejects.toThrow('Only DELETE/INSERT SPARQL update operations are supported');
|
||||
expect(order).toEqual([]);
|
||||
});
|
||||
|
||||
it('releases the lock if an error occurs while patching.', async(): Promise<void> => {
|
||||
source.getRepresentation = jest.fn(async(): Promise<any> => {
|
||||
order.push('getRepresentation');
|
||||
throw new Error('error');
|
||||
});
|
||||
|
||||
const input = { identifier: { path: 'path' },
|
||||
patch: { algebra: translate(
|
||||
'INSERT DATA { <http://test.com/s1> <http://test.com/p1> <http://test.com/o1>. ' +
|
||||
'<http://test.com/s2> <http://test.com/p2> <http://test.com/o2> }',
|
||||
{ quads: true },
|
||||
) } as SparqlUpdatePatch };
|
||||
await expect(handler.handle(input)).rejects.toThrow('error');
|
||||
expect(order).toEqual([ 'acquire', 'getRepresentation', 'release' ]);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user