Speedup a backup a bit (#4362)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2024-01-16 12:15:59 +07:00 committed by GitHub
parent 7ba9e53639
commit 8f173069c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,22 +14,26 @@
// //
import core, { import core, {
DOMAIN_MODEL,
DOMAIN_TX,
SortingOrder,
TxProcessor,
escapeLikeForRegexp,
getTypeOf,
isOperator,
toFindResult,
type AttachedDoc, type AttachedDoc,
type Class, type Class,
type Doc, type Doc,
type DocumentQuery, type DocumentQuery,
type DocumentUpdate, type DocumentUpdate,
type Domain, type Domain,
DOMAIN_MODEL,
DOMAIN_TX,
type Enum, type Enum,
type EnumOf, type EnumOf,
escapeLikeForRegexp,
type FindOptions, type FindOptions,
type FindResult, type FindResult,
type Hierarchy, type Hierarchy,
type IndexingConfiguration, type IndexingConfiguration,
isOperator,
type Lookup, type Lookup,
type Mixin, type Mixin,
type ModelDb, type ModelDb,
@ -37,24 +41,20 @@ import core, {
type QueryUpdate, type QueryUpdate,
type Ref, type Ref,
type ReverseLookups, type ReverseLookups,
SortingOrder, type SortQuerySelector,
type SortingQuery, type SortingQuery,
type SortingRules, type SortingRules,
type SortQuerySelector,
type StorageIterator, type StorageIterator,
toFindResult,
type Tx, type Tx,
type TxCUD,
type TxCollectionCUD, type TxCollectionCUD,
type TxCreateDoc, type TxCreateDoc,
type TxCUD,
type TxMixin, type TxMixin,
TxProcessor,
type TxRemoveDoc, type TxRemoveDoc,
type TxResult, type TxResult,
type TxUpdateDoc, type TxUpdateDoc,
type WithLookup, type WithLookup,
type WorkspaceId, type WorkspaceId
getTypeOf
} from '@hcengineering/core' } from '@hcengineering/core'
import type { DbAdapter, TxAdapter } from '@hcengineering/server-core' import type { DbAdapter, TxAdapter } from '@hcengineering/server-core'
import serverCore from '@hcengineering/server-core' import serverCore from '@hcengineering/server-core'
@ -646,24 +646,32 @@ abstract class MongoAdapterBase implements DbAdapter {
if (d === null) { if (d === null) {
return undefined return undefined
} }
let digest = (d as any)['%hash%'] let digest: string | null = (d as any)['%hash%']
if ('%hash%' in d) { if ('%hash%' in d) {
delete d['%hash%'] delete d['%hash%']
} }
if (digest == null || digest === '') { const pos = (digest ?? '').indexOf('|')
if (digest == null || digest === '' || pos === -1) {
const doc = JSON.stringify(d) const doc = JSON.stringify(d)
const hash = createHash('sha256') const hash = createHash('sha256')
hash.update(doc) hash.update(doc)
const size = doc.length
digest = hash.digest('base64') digest = hash.digest('base64')
bulkUpdate.set(d._id, digest) bulkUpdate.set(d._id, `${digest}|${size.toString(16)}`)
// await coll.updateOne({ _id: d._id }, { $set: { '%hash%': digest } })
await flush() await flush()
}
return { return {
id: d._id, id: d._id,
hash: digest, hash: digest,
size: this.calcSize(d) // Some approx size for document. size
}
} else {
return {
id: d._id,
hash: digest.slice(0, pos),
size: parseInt(digest.slice(pos + 1), 16)
}
} }
}, },
close: async () => { close: async () => {