transaction ordering

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-08-20 18:20:22 +02:00
parent 6b2f153358
commit 81871cbeeb
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 19 additions and 8 deletions

View File

@ -17,6 +17,7 @@ import type { Doc, Ref, Class } from './classes'
import type { Tx } from './tx'
import type { Storage, DocumentQuery, FindOptions, FindResult } from './storage'
import { SortingOrder } from './storage'
import { Hierarchy } from './hierarchy'
import { ModelDb } from './memdb'
import { DOMAIN_MODEL } from './classes'
@ -96,7 +97,7 @@ export async function createClient (
}
const conn = await connect(txHander)
const txes = await conn.findAll(core.class.Tx, { objectSpace: core.space.Model })
const txes = await conn.findAll(core.class.Tx, { objectSpace: core.space.Model }, { sort: { _id: SortingOrder.Ascending } })
const txMap = new Map<Ref<Tx>, Ref<Tx>>()
for (const tx of txes) txMap.set(tx._id, tx._id)

View File

@ -57,7 +57,7 @@ export type FindOptions<T extends Doc> = {
* @public
*/
export type SortingQuery<T extends Doc> = {
[P in keyof T]?: T[P] extends object ? never : SortingOrder
[P in keyof T]?: SortingOrder
}
/**

View File

@ -13,11 +13,10 @@
// limitations under the License.
//
import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, ServerStorage } from '@anticrm/core'
import core, { DOMAIN_MODEL, TxProcessor, ModelDb, Hierarchy, DOMAIN_TX, TxFactory } from '@anticrm/core'
import core, { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, ServerStorage, SortingOrder, DOMAIN_MODEL, TxProcessor, ModelDb, Hierarchy, DOMAIN_TX, TxFactory } from '@anticrm/core'
import { Triggers } from '@anticrm/server-core'
import { MongoClient, Db, Filter, Document } from 'mongodb'
import { MongoClient, Db, Filter, Document, Sort } from 'mongodb'
function translateQuery<T extends Doc> (query: DocumentQuery<T>): Filter<T> {
return query as Filter<T>
@ -69,7 +68,18 @@ class MongoStorage implements ServerStorage {
const domain = this.hierarchy.getDomain(_class)
console.log('findAll', _class, domain, query)
if (domain === DOMAIN_MODEL) return await this.modeldb.findAll(_class, query, options)
return await this.db.collection(domain).find<T>(translateQuery(query)).toArray()
let cursor = this.db.collection(domain).find<T>(translateQuery(query))
if (options !== null && options !== undefined) {
if (options.sort !== undefined) {
const sort: Sort = {}
for (const key in options.sort) {
const order = options.sort[key] === SortingOrder.Ascending ? 1 : -1
sort[key] = order
}
cursor = cursor.sort(sort)
}
}
return await cursor.toArray()
}
async tx (tx: Tx): Promise<Tx[]> {
@ -97,7 +107,7 @@ export async function createStorage (url: string, dbName: string): Promise<Serve
const hierarchy = new Hierarchy()
const triggers = new Triggers(new TxFactory(core.account.System))
const txes = await db.collection(DOMAIN_TX).find<Tx>({ objectSpace: core.space.Model }).toArray()
const txes = await db.collection(DOMAIN_TX).find<Tx>({ objectSpace: core.space.Model }).sort({ _id: 1 }).toArray()
for (const tx of txes) {
hierarchy.tx(tx)
await triggers.tx(tx)

View File

@ -1,6 +1,6 @@
{
"name": "@anticrm/server",
"version": "0.6.0",
"version": "0.6.1",
"main": "lib/index.js",
"author": "Anticrm Platform Contributors",
"license": "EPL-2.0",