2021-08-27 09:17:18 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 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.
|
|
|
|
//
|
|
|
|
|
2021-12-10 09:47:54 +00:00
|
|
|
import core, { ServerStorage, Domain, Tx, TxCUD, Doc, Ref, Class, DocumentQuery, FindResult, FindOptions, Storage, TxBulkWrite, TxResult, TxCollectionCUD, AttachedDoc, DOMAIN_MODEL, Hierarchy, DOMAIN_TX, ModelDb, TxFactory } from '@anticrm/core'
|
2021-09-13 21:21:39 +00:00
|
|
|
import type { FullTextAdapterFactory, FullTextAdapter } from './types'
|
2021-08-27 09:17:18 +00:00
|
|
|
import { FullTextIndex } from './fulltext'
|
|
|
|
import { Triggers } from './triggers'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbAdapter extends Storage {
|
|
|
|
/**
|
|
|
|
* Method called after hierarchy is ready to use.
|
|
|
|
*/
|
|
|
|
init: (model: Tx[]) => Promise<void>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TxAdapter extends DbAdapter {
|
|
|
|
getModel: () => Promise<Tx[]>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-10-13 08:38:35 +00:00
|
|
|
export type DbAdapterFactory = (hierarchy: Hierarchy, url: string, db: string, modelDb: ModelDb) => Promise<DbAdapter>
|
2021-08-27 09:17:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbAdapterConfiguration {
|
|
|
|
factory: DbAdapterFactory
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbConfiguration {
|
|
|
|
adapters: Record<string, DbAdapterConfiguration>
|
|
|
|
domains: Record<string, string>
|
|
|
|
defaultAdapter: string
|
|
|
|
workspace: string
|
|
|
|
fulltextAdapter: {
|
|
|
|
factory: FullTextAdapterFactory
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TServerStorage implements ServerStorage {
|
2021-09-13 21:21:39 +00:00
|
|
|
private readonly fulltext: FullTextIndex
|
|
|
|
|
2021-08-27 09:17:18 +00:00
|
|
|
constructor (
|
|
|
|
private readonly domains: Record<string, string>,
|
|
|
|
private readonly defaultAdapter: string,
|
|
|
|
private readonly adapters: Map<string, DbAdapter>,
|
|
|
|
private readonly hierarchy: Hierarchy,
|
|
|
|
private readonly triggers: Triggers,
|
2021-10-13 08:38:35 +00:00
|
|
|
fulltextAdapter: FullTextAdapter,
|
|
|
|
private readonly modelDb: ModelDb
|
2021-08-27 09:17:18 +00:00
|
|
|
) {
|
2021-09-13 21:21:39 +00:00
|
|
|
this.fulltext = new FullTextIndex(hierarchy, fulltextAdapter, this)
|
2021-08-27 09:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getAdapter (domain: Domain): DbAdapter {
|
|
|
|
const name = this.domains[domain] ?? this.defaultAdapter
|
|
|
|
const adapter = this.adapters.get(name)
|
|
|
|
if (adapter === undefined) {
|
|
|
|
throw new Error('adapter not provided: ' + name)
|
|
|
|
}
|
|
|
|
return adapter
|
|
|
|
}
|
|
|
|
|
2021-10-13 16:46:48 +00:00
|
|
|
private async routeTx (tx: Tx): Promise<TxResult> {
|
2021-08-27 09:17:18 +00:00
|
|
|
if (this.hierarchy.isDerived(tx._class, core.class.TxCUD)) {
|
|
|
|
const txCUD = tx as TxCUD<Doc>
|
|
|
|
const domain = this.hierarchy.getDomain(txCUD.objectClass)
|
2021-10-08 09:18:06 +00:00
|
|
|
return await this.getAdapter(domain).tx(txCUD)
|
2021-08-27 09:17:18 +00:00
|
|
|
} else {
|
2021-10-08 09:18:06 +00:00
|
|
|
if (this.hierarchy.isDerived(tx._class, core.class.TxBulkWrite)) {
|
|
|
|
const bulkWrite = tx as TxBulkWrite
|
|
|
|
for (const tx of bulkWrite.txes) {
|
|
|
|
await this.tx(tx)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('not implemented (routeTx)')
|
|
|
|
}
|
2021-10-13 16:46:48 +00:00
|
|
|
return {}
|
2021-08-27 09:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:13:55 +00:00
|
|
|
async processCollection (tx: Tx): Promise<Tx[]> {
|
2021-11-16 18:34:20 +00:00
|
|
|
if (tx._class === core.class.TxCollectionCUD) {
|
|
|
|
const colTx = tx as TxCollectionCUD<Doc, AttachedDoc>
|
|
|
|
const _id = colTx.objectId
|
|
|
|
const _class = colTx.objectClass
|
|
|
|
let attachedTo: Doc | undefined
|
2021-12-10 09:47:54 +00:00
|
|
|
|
|
|
|
// Skip model operations
|
|
|
|
if (this.hierarchy.getDomain(_class) === DOMAIN_MODEL) {
|
|
|
|
// We could not update increments for model classes
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:34:20 +00:00
|
|
|
if (colTx.tx._class === core.class.TxCreateDoc) {
|
|
|
|
attachedTo = (await this.findAll(_class, { _id }))[0]
|
|
|
|
const txFactory = new TxFactory(tx.modifiedBy)
|
|
|
|
return [txFactory.createTxUpdateDoc(_class, attachedTo.space, _id, { $inc: { [colTx.collection]: 1 } })]
|
2021-11-29 11:11:27 +00:00
|
|
|
} else if (colTx.tx._class === core.class.TxRemoveDoc) {
|
|
|
|
attachedTo = (await this.findAll(_class, { _id }))[0]
|
|
|
|
const txFactory = new TxFactory(tx.modifiedBy)
|
|
|
|
return [txFactory.createTxUpdateDoc(_class, attachedTo.space, _id, { $inc: { [colTx.collection]: -1 } })]
|
2021-11-16 18:34:20 +00:00
|
|
|
}
|
2021-10-30 10:13:55 +00:00
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-08-27 09:17:18 +00:00
|
|
|
async findAll<T extends Doc> (
|
|
|
|
clazz: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): Promise<FindResult<T>> {
|
|
|
|
const domain = this.hierarchy.getDomain(clazz)
|
2021-09-13 19:50:43 +00:00
|
|
|
console.log('server findall', query)
|
|
|
|
if (Object.keys(query)[0] === '$search') {
|
|
|
|
return await this.fulltext.findAll(clazz, query, options)
|
|
|
|
}
|
2021-08-27 09:17:18 +00:00
|
|
|
return await this.getAdapter(domain).findAll(clazz, query, options)
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:58:14 +00:00
|
|
|
async tx (tx: Tx): Promise<[TxResult, Tx[]]> {
|
2021-08-27 09:17:18 +00:00
|
|
|
// store tx
|
|
|
|
await this.getAdapter(DOMAIN_TX).tx(tx)
|
|
|
|
|
|
|
|
if (tx.objectSpace === core.space.Model) {
|
|
|
|
// maintain hiearachy and triggers
|
|
|
|
this.hierarchy.tx(tx)
|
|
|
|
await this.triggers.tx(tx)
|
2021-10-13 08:38:35 +00:00
|
|
|
await this.modelDb.tx(tx)
|
|
|
|
}
|
|
|
|
// store object
|
2021-10-13 18:58:14 +00:00
|
|
|
const result = await this.routeTx(tx)
|
2021-10-13 08:38:35 +00:00
|
|
|
// invoke triggers and store derived objects
|
2021-10-30 10:13:55 +00:00
|
|
|
const derived = [...await this.processCollection(tx), ...await this.triggers.apply(tx.modifiedBy, tx, this.findAll.bind(this), this.hierarchy)]
|
2021-10-13 08:38:35 +00:00
|
|
|
for (const tx of derived) {
|
2021-08-27 09:17:18 +00:00
|
|
|
await this.routeTx(tx)
|
2021-10-13 08:38:35 +00:00
|
|
|
}
|
|
|
|
// index object
|
|
|
|
await this.fulltext.tx(tx)
|
|
|
|
// index derived objects
|
|
|
|
for (const tx of derived) {
|
2021-08-27 09:17:18 +00:00
|
|
|
await this.fulltext.tx(tx)
|
|
|
|
}
|
2021-10-13 08:38:35 +00:00
|
|
|
|
2021-10-13 18:58:14 +00:00
|
|
|
return [result, derived]
|
2021-08-27 09:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function createServerStorage (conf: DbConfiguration): Promise<ServerStorage> {
|
|
|
|
const hierarchy = new Hierarchy()
|
|
|
|
const triggers = new Triggers()
|
|
|
|
const adapters = new Map<string, DbAdapter>()
|
2021-10-13 08:38:35 +00:00
|
|
|
const modelDb = new ModelDb(hierarchy)
|
2021-08-27 09:17:18 +00:00
|
|
|
|
|
|
|
for (const key in conf.adapters) {
|
|
|
|
const adapterConf = conf.adapters[key]
|
2021-10-13 08:38:35 +00:00
|
|
|
adapters.set(key, await adapterConf.factory(hierarchy, adapterConf.url, conf.workspace, modelDb))
|
2021-08-27 09:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const txAdapter = adapters.get(conf.domains[DOMAIN_TX]) as TxAdapter
|
|
|
|
if (txAdapter === undefined) {
|
|
|
|
console.log('no txadapter found')
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = await txAdapter.getModel()
|
|
|
|
|
|
|
|
for (const tx of model) {
|
|
|
|
hierarchy.tx(tx)
|
|
|
|
await triggers.tx(tx)
|
|
|
|
}
|
|
|
|
|
2021-10-13 08:38:35 +00:00
|
|
|
for (const tx of model) {
|
|
|
|
await modelDb.tx(tx)
|
|
|
|
}
|
|
|
|
|
2021-08-27 09:17:18 +00:00
|
|
|
for (const [, adapter] of adapters) {
|
|
|
|
await adapter.init(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fulltextAdapter = await conf.fulltextAdapter.factory(conf.fulltextAdapter.url, conf.workspace)
|
|
|
|
|
2021-10-13 08:38:35 +00:00
|
|
|
return new TServerStorage(conf.domains, conf.defaultAdapter, adapters, hierarchy, triggers, fulltextAdapter, modelDb)
|
2021-08-27 09:17:18 +00:00
|
|
|
}
|