Skip to content

Commit

Permalink
db 数据库配置与索引 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
LDmoxeii committed Apr 17, 2024
1 parent d870f8d commit 59f5508
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/log
/db
116 changes: 116 additions & 0 deletions eid/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { Agd, Aut, Cdt, Dbt, Ern, Lit, Soc, Usr, Wsl } from "./typ.ts"
import { Collection, Document, Filter, IndexOptions, MongoClient, UpdateFilter } from 'https://deno.land/x/[email protected]/mod.ts';

export type Coll<T extends Document> = Collection<T>
export type Fltr<T> = Filter<T>
export type Proj<T, P extends keyof T> = Partial<{ [K in P]: 0} | { [K in P ]: 1}>
export type Upfy<T> = UpdateFilter<T>
export type DocC<_Id> = Promise<NonNullable<_Id> | null>
export type DocR<Dco> = Promise<NonNullable<Dco> | null>
export type DocU = Promise<0 | 1 | null>
export type DocD = Promise<0 | 1 | null>

const conn = new MongoClient()
conn.connect('mongodb://localhost:27017')

const nam: IndexOptions[] = [{
key: {nam: 1},
name: "nam",
unique: true,
}]

const nbr: IndexOptions[] = [{
key: {nbr: 1},
name: "nbr",
unique: true,
partialFilterExpression: {nbr: {$exists: true}},
}]

const adm: IndexOptions[] = [{
key: {adm1: 1},
name: "adm1",
unique: true,
},
{
key: {adm2: 1},
name: "adm2",
unique: true,
}]

const sec: IndexOptions[] = [{
key: {sec: 1},
name: "sec",
unique: true,
}]

const soc: IndexOptions[] = [{
key: {soc: 1},
name: "soc",
unique: true,
}]

const rec: IndexOptions[] = [{
key: {"_id.usr": 1, "_id.utc": -1,},
name: "usr-utc",
},{
key: {"_id.soc": 1, "_id.utc": -1,},
name: "soc-utc",
},{
key: {"_id.usr": 1, "_id.soc": 1, "_id.utc": -1,},
name: "usr-soc-utc",
}]

const utc: IndexOptions[] = [{
key: {"_id.usr": 1, "utc.eft": -1, "utc.exp": -1},
name: "usr-eft",
},{
key: {"_id.soc": 1, "utc.eft": -1, "utc.exp": -1},
name: "soc-eft",
}]

const pin: IndexOptions[] = [{
key: {"pin": 1, "utc.put": -1},
name: "pin",
partialFilterExpression: {pin: {$exists: true}},
}]

export async function db(
dbnam: "ismism" | "tst",
reset = false,
) {
const db = conn.database(dbnam)
const c = {
usr: db.collection<Usr>("usr"),
soc: db.collection<Soc>("soc"),
agd: db.collection<Agd>("agd"),

cdt: db.collection<Cdt>("cdt"),
dbt: db.collection<Dbt>("cmt"),
ern: db.collection<Ern>("ern"),

wsl: db.collection<Wsl>("wsl"),
lit: db.collection<Lit>("lit"),

aut: db.collection<Aut>("aut"),
}
if (reset) {
await db.dropDatabase()

await c.usr.createIndexes({indexes: [...nam, ...nbr]})
await c.soc.createIndexes({indexes: [...nam, ...adm, ...sec]})
await c.agd.createIndexes({indexes: [...soc]})

await c.cdt.createIndexes({indexes: [...rec, ...utc]})
await c.dbt.createIndexes({indexes: [...rec]})
await c.ern.createIndexes({indexes: [...rec]})

await c.wsl.createIndexes({indexes: [...pin]})
await c.lit.createIndexes({indexes: [...pin]})
await c.aut.insertOne({_id: 1, sup: [1,2], aut: [2], wsl: [], lit: []})
}

if(dbnam === "ismism") coll = c
return c
}

export let coll = await db("tst")
2 changes: 1 addition & 1 deletion eid/typ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Soc = Id & {
agr: {msg: string, utc: number},
}

export type Age = Id & {
export type Agd = Id & {
soc: Soc["_id"],
txt: string,
utc: number,
Expand Down
31 changes: 31 additions & 0 deletions mongod.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
EnvironmentFile=-/etc/default/mongod
WorkingDirectory=/home/ismism
ExecStart=mongod --config mongod.yaml
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
10 changes: 10 additions & 0 deletions mongod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
systemLog:
destination: file
path: log/mongod.log
logAppend: false
storage:
dbPath: db
net:
bindIp: 127.0.0.1, ::1
setParameter:
notablescan: 1

0 comments on commit 59f5508

Please sign in to comment.