MCPcopy Create free account
hub / github.com/scality/cloudserver / processBytesToWrite

Function processBytesToWrite

lib/api/apiUtils/quotas/quotaUtils.js:22–57  ·  view source on GitHub ↗

* Process the bytes to write based on the request and object metadata * @param {string} apiMethod - api method * @param {BucketInfo} bucket - bucket info * @param {string} versionId - version id of the object * @param {number} contentLength - content length of the object * @param {object} objMD

(apiMethod, bucket, versionId, contentLength, objMD, destObjMD = null)

Source from the content-addressed store, hash-verified

20 * @return {number} processed content length
21 */
22function processBytesToWrite(apiMethod, bucket, versionId, contentLength, objMD, destObjMD = null) {
23 // Get the size of "hot" data. Object being restored are _considered_ hot, as we "reserve" the
24 // space for them as soon as restore is requested: therefore we must 'free' it immediately on
25 // delete as well.
26 const isCold = objMD => objMD.archive && !objMD.archive.restoreRequestedAt;
27 const getHotContentLength = objMD => {
28 if (isCold(objMD)) {
29 return 0;
30 }
31 return Number.parseInt(objMD['content-length'], 10);
32 };
33
34 let bytes = contentLength;
35 if (apiMethod === 'objectRestore') {
36 // object is being restored
37 bytes = Number.parseInt(objMD['content-length'], 10);
38 } else if (!bytes && objMD?.['content-length']) {
39 if (apiMethod === 'objectCopy' || apiMethod === 'objectPutCopyPart') {
40 // object is being copied, increases the storage...
41 bytes = Number.parseInt(objMD['content-length'], 10);
42
43 if (destObjMD && !bucket.isVersioningEnabled()) {
44 // but it also replaces the target, which decreases storage
45 bytes -= getHotContentLength(destObjMD);
46 }
47 } else if (!bucket.isVersioningEnabled() || bucket.isVersioningEnabled() && versionId) {
48 // object is being deleted (non versioned) or hard-deleted (versioned, as indicated by
49 // the `versionId` field)
50 bytes = -getHotContentLength(objMD);
51 }
52 } else if (bytes && objMD?.['content-length'] && !bucket.isVersioningEnabled()) {
53 // object is being replaced: store the diff, if the bucket is not versioned
54 bytes = bytes - getHotContentLength(objMD);
55 }
56 return bytes || 0;
57}
58
59/**
60 * Checks if a metric is stale based on the provided parameters.

Callers 4

getObjMetadataAndDeleteFunction · 0.85
objectRestoreFunction · 0.85
quotaUtils.jsFile · 0.85

Calls 1

getHotContentLengthFunction · 0.85

Tested by

no test coverage detected