2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-06-06 10:06:32 +00:00
|
|
|
import { Account, AnyAttribute, Class, Doc, DocData, DocIndexState, IndexKind, Obj, Ref, Space } from './classes'
|
2023-01-04 17:58:54 +00:00
|
|
|
import core from './component'
|
2023-03-21 16:08:45 +00:00
|
|
|
import { Hierarchy } from './hierarchy'
|
|
|
|
import { FindResult } from './storage'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
|
|
|
function toHex (value: number, chars: number): string {
|
|
|
|
const result = value.toString(16)
|
|
|
|
if (result.length < chars) {
|
|
|
|
return '0'.repeat(chars - result.length) + result
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = (Math.random() * (1 << 24)) | 0
|
|
|
|
const random = toHex((Math.random() * (1 << 24)) | 0, 6) + toHex((Math.random() * (1 << 16)) | 0, 4)
|
|
|
|
|
|
|
|
function timestamp (): string {
|
|
|
|
const time = (Date.now() / 1000) | 0
|
|
|
|
return toHex(time, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
function count (): string {
|
|
|
|
const val = counter++ & 0xffffff
|
|
|
|
return toHex(val, 6)
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export function generateId<T extends Doc> (): Ref<T> {
|
|
|
|
return (timestamp() + random + count()) as Ref<T>
|
|
|
|
}
|
2021-09-10 15:54:13 +00:00
|
|
|
|
|
|
|
let currentAccount: Account
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-04-08 03:06:38 +00:00
|
|
|
export function getCurrentAccount (): Account {
|
|
|
|
return currentAccount
|
|
|
|
}
|
2021-09-10 15:54:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @param account -
|
|
|
|
*/
|
|
|
|
export function setCurrentAccount (account: Account): void {
|
|
|
|
currentAccount = account
|
|
|
|
}
|
2022-02-16 09:02:31 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function escapeLikeForRegexp (value: string): string {
|
|
|
|
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|
2022-04-08 03:06:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toFindResult<T extends Doc> (docs: T[], total?: number): FindResult<T> {
|
|
|
|
const length = total ?? docs.length
|
|
|
|
return Object.assign(docs, { total: length })
|
|
|
|
}
|
2022-11-16 15:03:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface WorkspaceId {
|
|
|
|
name: string
|
|
|
|
productId: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* Combine workspace with productId, if not equal ''
|
|
|
|
*/
|
|
|
|
export function getWorkspaceId (workspace: string, productId: string = ''): WorkspaceId {
|
|
|
|
return {
|
|
|
|
name: workspace,
|
|
|
|
productId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toWorkspaceString (id: WorkspaceId, sep = '@'): string {
|
|
|
|
return id.name + (id.productId === '' ? '' : sep + id.productId)
|
|
|
|
}
|
2023-01-04 17:58:54 +00:00
|
|
|
|
|
|
|
const attributesPrefix = 'attributes.'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface IndexKeyOptions {
|
|
|
|
_class?: Ref<Class<Obj>>
|
|
|
|
docId?: Ref<DocIndexState>
|
|
|
|
extra?: string[]
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function docUpdKey (name: string, opt?: IndexKeyOptions): string {
|
|
|
|
return attributesPrefix + docKey(name, opt)
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function docKey (name: string, opt?: IndexKeyOptions): string {
|
|
|
|
const extra = opt?.extra !== undefined && opt?.extra?.length > 0 ? `#${opt.extra?.join('#') ?? ''}` : ''
|
|
|
|
return (
|
|
|
|
(opt?.docId !== undefined ? opt.docId.split('.').join('_') + '|' : '') +
|
|
|
|
(opt?._class === undefined ? name : `${opt?._class}%${name}${extra}`)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function extractDocKey (key: string): {
|
|
|
|
_class?: Ref<Class<Doc>>
|
|
|
|
attr: string
|
|
|
|
docId?: Ref<DocIndexState>
|
|
|
|
extra: string[]
|
|
|
|
} {
|
|
|
|
let k = key
|
|
|
|
if (k.startsWith(attributesPrefix)) {
|
|
|
|
k = k.slice(attributesPrefix.length)
|
|
|
|
}
|
|
|
|
let docId: Ref<DocIndexState> | undefined
|
|
|
|
let _class: Ref<Class<Doc>> | undefined
|
|
|
|
let attr = ''
|
|
|
|
const docSepPos = k.indexOf('|')
|
|
|
|
if (docSepPos !== -1) {
|
|
|
|
docId = k.substring(0, docSepPos).replace('_', '.') as Ref<DocIndexState>
|
|
|
|
k = k.substring(docSepPos + 1)
|
|
|
|
}
|
|
|
|
const clPos = k.indexOf('%')
|
|
|
|
if (clPos !== -1) {
|
|
|
|
_class = k.substring(0, clPos) as Ref<Class<Doc>>
|
|
|
|
attr = k.substring(clPos + 1)
|
|
|
|
} else {
|
|
|
|
attr = k
|
|
|
|
}
|
|
|
|
const extra = attr.split('#')
|
|
|
|
attr = extra.splice(0, 1)[0]
|
|
|
|
|
|
|
|
return { docId, attr, _class, extra }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isFullTextAttribute (attr: AnyAttribute): boolean {
|
2023-04-20 10:11:22 +00:00
|
|
|
return (
|
|
|
|
attr.index === IndexKind.FullText ||
|
|
|
|
attr.type._class === core.class.TypeAttachment ||
|
|
|
|
attr.type._class === core.class.EnumOf
|
|
|
|
)
|
2023-01-04 17:58:54 +00:00
|
|
|
}
|
2023-01-18 09:57:13 +00:00
|
|
|
|
2023-10-17 08:21:59 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isIndexedAttribute (attr: AnyAttribute): boolean {
|
|
|
|
return attr.index === IndexKind.Indexed
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:57:13 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface IdMap<T extends Doc> extends Map<Ref<T>, T> {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toIdMap<T extends Doc> (arr: T[]): IdMap<T> {
|
|
|
|
return new Map(arr.map((p) => [p._id, p]))
|
|
|
|
}
|
2023-02-01 11:27:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function concatLink (host: string, path: string): string {
|
|
|
|
if (!host.endsWith('/') && !path.startsWith('/')) {
|
|
|
|
return `${host}/${path}`
|
|
|
|
} else if (host.endsWith('/') && path.startsWith('/')) {
|
|
|
|
const newPath = path.slice(1)
|
|
|
|
return `${host}${newPath}`
|
|
|
|
} else {
|
|
|
|
return `${host}${path}`
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 16:08:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function fillDefaults<T extends Doc> (
|
|
|
|
hierarchy: Hierarchy,
|
|
|
|
object: DocData<T> | T,
|
|
|
|
_class: Ref<Class<T>>
|
|
|
|
): DocData<T> | T {
|
|
|
|
const baseClass = hierarchy.isDerived(_class, core.class.AttachedDoc) ? core.class.AttachedDoc : core.class.Doc
|
|
|
|
const attributes = hierarchy.getAllAttributes(_class, baseClass)
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (attribute[1].defaultValue !== undefined) {
|
|
|
|
if ((object as any)[attribute[0]] === undefined) {
|
|
|
|
;(object as any)[attribute[0]] = attribute[1].defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object
|
|
|
|
}
|
2023-06-06 10:06:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class AggregateValueData {
|
|
|
|
constructor (
|
|
|
|
readonly name: string,
|
|
|
|
readonly _id: Ref<Doc>,
|
|
|
|
readonly space: Ref<Space>,
|
|
|
|
readonly rank?: string,
|
|
|
|
readonly category?: Ref<Doc>
|
|
|
|
) {}
|
|
|
|
|
|
|
|
getRank (): string {
|
|
|
|
return this.rank ?? ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class AggregateValue {
|
|
|
|
constructor (readonly name: string | undefined, readonly values: AggregateValueData[]) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type CategoryType = number | string | undefined | Ref<Doc> | AggregateValue
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class DocManager {
|
|
|
|
protected readonly byId: IdMap<Doc>
|
|
|
|
|
|
|
|
constructor (protected readonly docs: Doc[]) {
|
|
|
|
this.byId = toIdMap(docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
get (ref: Ref<Doc>): Doc | undefined {
|
|
|
|
return this.byId.get(ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
getDocs (): Doc[] {
|
|
|
|
return this.docs
|
|
|
|
}
|
|
|
|
|
|
|
|
getIdMap (): IdMap<Doc> {
|
|
|
|
return this.byId
|
|
|
|
}
|
|
|
|
|
|
|
|
filter (predicate: (value: Doc) => boolean): Doc[] {
|
|
|
|
return this.docs.filter(predicate)
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 11:17:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class RateLimitter {
|
|
|
|
idCounter: number = 0
|
|
|
|
processingQueue = new Map<string, Promise<void>>()
|
|
|
|
last: number = 0
|
|
|
|
|
|
|
|
queue: (() => Promise<void>)[] = []
|
|
|
|
|
|
|
|
constructor (readonly config: () => { rate: number, perSecond?: number }) {}
|
|
|
|
|
|
|
|
async exec<T, B extends Record<string, any> = {}>(op: (args?: B) => Promise<T>, args?: B): Promise<T> {
|
|
|
|
const processingId = `${this.idCounter++}`
|
|
|
|
const cfg = this.config()
|
|
|
|
|
|
|
|
while (this.processingQueue.size > cfg.rate) {
|
|
|
|
await Promise.race(this.processingQueue.values())
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const p = op(args)
|
|
|
|
this.processingQueue.set(processingId, p as Promise<void>)
|
|
|
|
return await p
|
|
|
|
} finally {
|
|
|
|
this.processingQueue.delete(processingId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async add<T, B extends Record<string, any> = {}>(op: (args?: B) => Promise<T>, args?: B): Promise<void> {
|
|
|
|
const cfg = this.config()
|
|
|
|
|
|
|
|
if (this.processingQueue.size < cfg.rate) {
|
|
|
|
void this.exec(op, args)
|
|
|
|
} else {
|
|
|
|
await this.exec(op, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitProcessing (): Promise<void> {
|
|
|
|
await await Promise.race(this.processingQueue.values())
|
|
|
|
}
|
|
|
|
}
|