2022-04-14 05:30:30 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2022-04-23 03:45:55 +00:00
|
|
|
import {
|
2024-03-22 07:12:41 +00:00
|
|
|
type Class,
|
|
|
|
type Doc,
|
|
|
|
type DocumentQuery,
|
|
|
|
type Domain,
|
|
|
|
type FindOptions,
|
|
|
|
type FindResult,
|
|
|
|
type MeasureContext,
|
|
|
|
type ModelDb,
|
|
|
|
type Ref,
|
|
|
|
type SearchOptions,
|
|
|
|
type SearchQuery,
|
|
|
|
type SearchResult,
|
|
|
|
type StorageIterator,
|
|
|
|
type Tx,
|
|
|
|
type TxResult
|
2022-09-21 08:08:25 +00:00
|
|
|
} from '@hcengineering/core'
|
2024-03-22 07:12:41 +00:00
|
|
|
import { type DbConfiguration } from './configuration'
|
2024-04-06 08:14:06 +00:00
|
|
|
import { createServerStorage } from './server'
|
2024-03-22 07:12:41 +00:00
|
|
|
import {
|
|
|
|
type BroadcastFunc,
|
|
|
|
type Middleware,
|
|
|
|
type MiddlewareCreator,
|
|
|
|
type Pipeline,
|
2024-05-27 07:25:57 +00:00
|
|
|
type ServerStorage,
|
2024-03-22 07:12:41 +00:00
|
|
|
type SessionContext
|
|
|
|
} from './types'
|
2022-04-14 05:30:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-01-04 17:58:54 +00:00
|
|
|
export async function createPipeline (
|
2023-03-02 03:31:47 +00:00
|
|
|
ctx: MeasureContext,
|
2023-01-04 17:58:54 +00:00
|
|
|
conf: DbConfiguration,
|
|
|
|
constructors: MiddlewareCreator[],
|
2023-02-15 03:14:20 +00:00
|
|
|
upgrade: boolean,
|
2023-05-16 07:05:47 +00:00
|
|
|
broadcast: BroadcastFunc
|
2023-01-04 17:58:54 +00:00
|
|
|
): Promise<Pipeline> {
|
2024-01-30 11:07:34 +00:00
|
|
|
const storage = await ctx.with(
|
|
|
|
'create-server-storage',
|
|
|
|
{},
|
|
|
|
async (ctx) =>
|
|
|
|
await createServerStorage(ctx, conf, {
|
|
|
|
upgrade,
|
2024-05-09 09:06:28 +00:00
|
|
|
broadcast
|
2024-01-30 11:07:34 +00:00
|
|
|
})
|
|
|
|
)
|
2024-05-09 09:06:28 +00:00
|
|
|
const pipelineResult = await PipelineImpl.create(ctx.newChild('pipeline-operations', {}), storage, constructors)
|
2024-01-19 15:43:37 +00:00
|
|
|
return pipelineResult
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 03:31:47 +00:00
|
|
|
class PipelineImpl implements Pipeline {
|
|
|
|
private head: Middleware | undefined
|
2022-04-23 03:45:55 +00:00
|
|
|
readonly modelDb: ModelDb
|
2023-03-02 03:31:47 +00:00
|
|
|
private constructor (readonly storage: ServerStorage) {
|
2022-04-23 03:45:55 +00:00
|
|
|
this.modelDb = storage.modelDb
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 03:31:47 +00:00
|
|
|
static async create (
|
|
|
|
ctx: MeasureContext,
|
|
|
|
storage: ServerStorage,
|
2024-05-09 09:06:28 +00:00
|
|
|
constructors: MiddlewareCreator[]
|
2023-03-02 03:31:47 +00:00
|
|
|
): Promise<PipelineImpl> {
|
|
|
|
const pipeline = new PipelineImpl(storage)
|
2024-05-09 09:06:28 +00:00
|
|
|
pipeline.head = await pipeline.buildChain(ctx, constructors)
|
2023-03-02 03:31:47 +00:00
|
|
|
return pipeline
|
|
|
|
}
|
|
|
|
|
2024-05-09 09:06:28 +00:00
|
|
|
private async buildChain (ctx: MeasureContext, constructors: MiddlewareCreator[]): Promise<Middleware | undefined> {
|
2022-04-14 05:30:30 +00:00
|
|
|
let current: Middleware | undefined
|
|
|
|
for (let index = constructors.length - 1; index >= 0; index--) {
|
|
|
|
const element = constructors[index]
|
2024-05-09 09:06:28 +00:00
|
|
|
current = await ctx.with('build chain', {}, async (ctx) => await element(ctx, this.storage, current))
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
2022-04-23 03:45:55 +00:00
|
|
|
async findAll<T extends Doc>(
|
2022-04-14 05:30:30 +00:00
|
|
|
ctx: SessionContext,
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): Promise<FindResult<T>> {
|
2023-03-02 03:31:47 +00:00
|
|
|
return this.head !== undefined
|
|
|
|
? await this.head.findAll(ctx, _class, query, options)
|
2024-05-09 09:06:28 +00:00
|
|
|
: await this.storage.findAll(ctx.ctx, _class, query, options)
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 07:35:09 +00:00
|
|
|
async searchFulltext (ctx: SessionContext, query: SearchQuery, options: SearchOptions): Promise<SearchResult> {
|
|
|
|
return this.head !== undefined
|
|
|
|
? await this.head.searchFulltext(ctx, query, options)
|
2024-05-09 09:06:28 +00:00
|
|
|
: await this.storage.searchFulltext(ctx.ctx, query, options)
|
2023-11-17 07:35:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 09:06:28 +00:00
|
|
|
async tx (ctx: SessionContext, tx: Tx): Promise<TxResult> {
|
2023-03-02 03:31:47 +00:00
|
|
|
if (this.head === undefined) {
|
2024-05-09 09:06:28 +00:00
|
|
|
return await this.storage.tx(ctx, tx)
|
2023-03-02 03:31:47 +00:00
|
|
|
} else {
|
|
|
|
return await this.head.tx(ctx, tx)
|
|
|
|
}
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async close (): Promise<void> {
|
|
|
|
await this.storage.close()
|
|
|
|
}
|
2022-05-23 15:53:33 +00:00
|
|
|
|
2024-04-08 04:41:14 +00:00
|
|
|
find (ctx: MeasureContext, domain: Domain): StorageIterator {
|
|
|
|
return this.storage.find(ctx, domain)
|
2022-05-23 15:53:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 04:41:14 +00:00
|
|
|
async load (ctx: MeasureContext, domain: Domain, docs: Ref<Doc>[]): Promise<Doc[]> {
|
|
|
|
return await this.storage.load(ctx, domain, docs)
|
2022-05-23 15:53:33 +00:00
|
|
|
}
|
2022-06-01 12:05:07 +00:00
|
|
|
|
2024-04-08 04:41:14 +00:00
|
|
|
async upload (ctx: MeasureContext, domain: Domain, docs: Doc[]): Promise<void> {
|
|
|
|
await this.storage.upload(ctx, domain, docs)
|
2022-06-01 12:05:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 04:41:14 +00:00
|
|
|
async clean (ctx: MeasureContext, domain: Domain, docs: Ref<Doc>[]): Promise<void> {
|
|
|
|
await this.storage.clean(ctx, domain, docs)
|
2022-06-01 12:05:07 +00:00
|
|
|
}
|
2022-04-14 05:30:30 +00:00
|
|
|
}
|