in src/blob/persistence/SqlBlobMetadataStore.ts [1427:1517]
public async stageBlock(
context: Context,
block: BlockModel,
leaseAccessConditions?: Models.LeaseAccessConditions
): Promise<void> {
await this.sequelize.transaction(async (t) => {
await this.assertContainerExists(
context,
block.accountName,
block.containerName,
t
);
const blobFindResult = await BlobsModel.findOne({
where: {
accountName: block.accountName,
containerName: block.containerName,
blobName: block.blobName,
snapshot: "",
deleting: 0
},
transaction: t
});
if (blobFindResult !== null && blobFindResult !== undefined) {
const blobModel: BlobModel = this.convertDbModelToBlobModel(
blobFindResult
);
if (blobModel.isCommitted === true) {
LeaseFactory.createLeaseState(
new BlobLeaseAdapter(blobModel),
context
).validate(new BlobWriteLeaseValidator(leaseAccessConditions));
}
// If the new block ID does not have same length with before uncommitted block ID, return failure.
const existBlock = await BlocksModel.findOne({
attributes: ["blockName"],
where: {
accountName: block.accountName,
containerName: block.containerName,
blobName: block.blobName,
deleting: 0
},
order: [["id", "ASC"]],
transaction: t
});
if (
existBlock &&
Buffer.from(
this.getModelValue<string>(existBlock, "blockName", true),
"base64"
).length !== Buffer.from(block.name, "base64").length
) {
throw StorageErrorFactory.getInvalidBlobOrBlock(context.contextId);
}
} else {
const newBlob = {
deleted: false,
accountName: block.accountName,
containerName: block.containerName,
name: block.blobName,
properties: {
creationTime: context.startTime!,
lastModified: context.startTime!,
etag: newEtag(),
contentLength: 0,
blobType: Models.BlobType.BlockBlob
},
snapshot: "",
isCommitted: false
};
await BlobsModel.upsert(this.convertBlobModelToDbModel(newBlob), {
transaction: t
});
}
await BlocksModel.upsert(
{
accountName: block.accountName,
containerName: block.containerName,
blobName: block.blobName,
blockName: block.name,
size: block.size,
persistency: this.serializeModelValue(block.persistency)
},
{ transaction: t }
);
});
}