fix: Always release lock when patching

This commit is contained in:
Joachim Van Herwegen 2020-12-01 16:53:25 +01:00
parent 74415cf49b
commit 3362eee2c2
2 changed files with 34 additions and 9 deletions

View File

@ -65,10 +65,25 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
throw new NotImplementedHttpError('WHERE statements are not supported'); throw new NotImplementedHttpError('WHERE statements are not supported');
} }
// Read the quads of the current representation
const lock = await this.locker.acquire(identifier); const lock = await this.locker.acquire(identifier);
const quads = await this.source.getRepresentation(identifier, try {
{ type: [{ value: INTERNAL_QUADS, weight: 1 }]}); 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 store = new Store<BaseQuad>();
const importEmitter = store.import(quads.data); const importEmitter = store.import(quads.data);
await new Promise((resolve, reject): void => { await new Promise((resolve, reject): void => {
@ -91,11 +106,5 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
metadata, metadata,
}; };
await this.source.setRepresentation(identifier, representation); 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;
} }
} }

View File

@ -199,4 +199,20 @@ describe('A SparqlUpdatePatchHandler', (): void => {
await expect(handle).rejects.toThrow('Only DELETE/INSERT SPARQL update operations are supported'); await expect(handle).rejects.toThrow('Only DELETE/INSERT SPARQL update operations are supported');
expect(order).toEqual([]); 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' ]);
});
}); });