Improve DB work.

This commit is contained in:
AB
2020-12-12 13:45:22 +03:00
parent cb90079781
commit 1d2a93c4c8

View File

@ -354,6 +354,7 @@ pub(crate) async fn add_sentence(
// Save stemmed words
let words = mystem.stemming(text).await?;
conn.execute("BEGIN TRANSACTION", params![]);
for word in words {
match add_word(&word).await {
Ok(id) => {
@ -366,7 +367,7 @@ pub(crate) async fn add_sentence(
Err(_) => debug!("Word {} is in stop list.", &word),
}
}
conn.execute("END TRANSACTION", params![]);
Ok(())
}
@ -405,6 +406,10 @@ pub(crate) async fn get_top(
// SCHEME
static SCHEME: &str = "
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Table: alert
CREATE TABLE IF NOT EXISTS alert (
conf_id TEXT NOT NULL,
user_id TEXT NOT NULL,
@ -412,6 +417,9 @@ CREATE TABLE IF NOT EXISTS alert (
time TEXT NOT NULL,
message TEXT
);
-- Table: conf
CREATE TABLE IF NOT EXISTS conf (
id NUMERIC NOT NULL
UNIQUE,
@ -421,17 +429,26 @@ CREATE TABLE IF NOT EXISTS conf (
id
)
);
-- Table: file
CREATE TABLE IF NOT EXISTS file (
path TEXT NOT NULL,
user_id TEXT NOT NULL,
conf_id TEXT NOT NULL,
file_id STRING PRIMARY KEY
);
-- Table: messages
CREATE TABLE IF NOT EXISTS messages (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
text TEXT UNIQUE
);
-- Table: relations
CREATE TABLE IF NOT EXISTS relations (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
@ -453,6 +470,9 @@ CREATE TABLE IF NOT EXISTS relations (
)
REFERENCES conf (id)
);
-- Table: reset
CREATE TABLE IF NOT EXISTS reset (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
@ -464,9 +484,15 @@ CREATE TABLE IF NOT EXISTS reset (
)
REFERENCES user (id)
);
-- Table: stop_words
CREATE TABLE IF NOT EXISTS stop_words (
word TEXT
);
-- Table: user
CREATE TABLE IF NOT EXISTS user (
id INTEGER NOT NULL
UNIQUE,
@ -479,7 +505,48 @@ CREATE TABLE IF NOT EXISTS user (
)
ON CONFLICT REPLACE
);
-- Table: word
CREATE TABLE IF NOT EXISTS word (
id INTEGER PRIMARY KEY AUTOINCREMENT,
word TEXT UNIQUE
);";
);
-- Index: conf_ids
CREATE INDEX IF NOT EXISTS conf_ids ON conf (
id
);
-- Index: file_ids
CREATE INDEX IF NOT EXISTS file_ids ON file (
file_id
);
-- Index: relations_ids
CREATE INDEX IF NOT EXISTS relations_ids ON relations (
conf_id,
word_id,
user_id,
msg_id
);
-- Index: user_ids
CREATE INDEX IF NOT EXISTS user_ids ON user (
id,
username
);
-- Index: word_id
CREATE INDEX IF NOT EXISTS word_id ON word (
id
);
COMMIT TRANSACTION;
PRAGMA foreign_keys = on;";