Skip to content

Commit

Permalink
feat: add mongodb client index
Browse files Browse the repository at this point in the history
  • Loading branch information
frsechet committed Nov 2, 2022
1 parent 289b16a commit ed6e57a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 21 deletions.
30 changes: 23 additions & 7 deletions csml_engine/src/db_connectors/mongodb/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ pub fn close_conversation(

let filter = doc! {
"_id": bson::oid::ObjectId::parse_str(id).unwrap(),
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.update_one(
Expand All @@ -85,7 +87,9 @@ pub fn close_all_conversations(client: &Client, db: &MongoDbClient) -> Result<()
let collection = db.client.collection::<Document>("conversation");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.update_many(
Expand All @@ -108,7 +112,9 @@ pub fn get_latest_open(

let filter = doc! {
"status": "OPEN",
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};
let find_options = mongodb::options::FindOneOptions::builder()
.sort(doc! { "$natural": -1 })
Expand All @@ -135,7 +141,9 @@ pub fn update_conversation(

let filter = doc! {
"_id": bson::oid::ObjectId::parse_str(conversation_id).unwrap(),
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

let doc = match (flow_id, step_id) {
Expand Down Expand Up @@ -165,7 +173,9 @@ pub fn delete_user_conversations(client: &Client, db: &MongoDbClient) -> Result<
let collection = db.client.collection::<Document>("conversation");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.delete_many(filter, None)?;
Expand All @@ -189,11 +199,17 @@ pub fn get_client_conversations(
let filter = match pagination_key {
Some(key) => {
doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"_id": {"$gt": bson::oid::ObjectId::parse_str(&key).unwrap() }
}
}
None => doc! {"client": bson::to_bson(&client)?},
None => doc! {
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
},
};

let find_options = mongodb::options::FindOptions::builder()
Expand Down
20 changes: 15 additions & 5 deletions csml_engine/src/db_connectors/mongodb/memories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub fn internal_use_get_memories(
let collection = db.client.collection::<Document>("memory");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};
let find_options = mongodb::options::FindOptions::builder()
.sort(doc! { "$natural": -1 })
Expand All @@ -107,7 +109,9 @@ pub fn get_memories(client: &Client, db: &MongoDbClient) -> Result<serde_json::V
let collection = db.client.collection::<Document>("memory");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};
let find_options = mongodb::options::FindOptions::builder()
.sort(doc! { "$natural": -1 })
Expand Down Expand Up @@ -141,7 +145,9 @@ pub fn get_memory(
let collection = db.client.collection::<Document>("memory");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"key": key,
};
let find_options = mongodb::options::FindOneOptions::builder()
Expand Down Expand Up @@ -175,7 +181,9 @@ pub fn delete_client_memory(
let collection = db.client.collection::<Document>("memory");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"key": key,
};

Expand All @@ -188,7 +196,9 @@ pub fn delete_client_memories(client: &Client, db: &MongoDbClient) -> Result<(),
let collection = db.client.collection::<Document>("memory");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.delete_many(filter, None)?;
Expand Down
20 changes: 15 additions & 5 deletions csml_engine/src/db_connectors/mongodb/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ pub fn delete_user_messages(client: &Client, db: &MongoDbClient) -> Result<(), E
let collection = db.client.collection::<Document>("message");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.delete_many(filter, None)?;
Expand Down Expand Up @@ -132,7 +134,9 @@ pub fn get_client_messages(
};

doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"_id": {"$gt": bson::oid::ObjectId::parse_str(&key).unwrap() },
"created_at": {"$gte": from_date, "$lt": to_date}
}
Expand All @@ -145,18 +149,24 @@ pub fn get_client_messages(
};

doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"created_at": {"$gte": from_date, "$lt": to_date}
}
}
(Some(key), None) => {
doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"_id": {"$gt": bson::oid::ObjectId::parse_str(&key).unwrap() },
}
}
(None, None) => doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
},
};

Expand Down
59 changes: 58 additions & 1 deletion csml_engine/src/db_connectors/mongodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn init() -> Result<Database, EngineError> {
let client = mongodb::sync::Client::with_uri_str(&uri)?;
let mongodb_client = MongoDbClient::new(client.database(&dbname));
create_ttl_indexes(&mongodb_client);
create_client_indexes(&mongodb_client);

let db = Database::Mongo(mongodb_client);

Expand Down Expand Up @@ -138,4 +139,60 @@ fn create_ttl_indexes(
.options(Some(IndexOptions::builder().expire_after(CoreDuration::new(0, 0)).build()))
.build();
state.create_index(index,None).ok();
}
}

fn create_client_indexes(
db: &MongoDbClient,
) {
// create compound client index for conversation
let conversation = db.client.collection::<Document>("conversation");
let index: IndexModel = IndexModel::builder()
.keys(
doc! {
"client.bot_id": 1,
"client.channel_id": 1,
"client.user_id": 1
}
)
.build();
conversation.create_index(index, None).ok();

// create compound client index for memory
let memory = db.client.collection::<Document>("memory");
let index: IndexModel = IndexModel::builder()
.keys(
doc! {
"client.bot_id": 1,
"client.channel_id": 1,
"client.user_id": 1
}
)
.build();
memory.create_index(index, None).ok();

// create compound client index for message
let message = db.client.collection::<Document>("message");
let index: IndexModel = IndexModel::builder()
.keys(
doc! {
"client.bot_id": 1,
"client.channel_id": 1,
"client.user_id": 1
}
)
.build();
message.create_index(index,None).ok();

// create compound client index for state
let state = db.client.collection::<Document>("state");
let index: IndexModel = IndexModel::builder()
.keys(
doc! {
"client.bot_id": 1,
"client.channel_id": 1,
"client.user_id": 1
}
)
.build();
state.create_index(index,None).ok();
}
12 changes: 9 additions & 3 deletions csml_engine/src/db_connectors/mongodb/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ pub fn get_state_key(
let state = db.client.collection::<Document>("state");

let filter = doc! {
"client": bson::to_bson(client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"type": _type,
"key": key,
};
Expand All @@ -80,7 +82,9 @@ pub fn get_current_state(
let state = db.client.collection::<Document>("state");

let filter = doc! {
"client": bson::to_bson(client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
"type": "hold",
"key": "position",
};
Expand Down Expand Up @@ -125,7 +129,9 @@ pub fn delete_user_state(client: &Client, db: &MongoDbClient) -> Result<(), Engi
let collection = db.client.collection::<Document>("state");

let filter = doc! {
"client": bson::to_bson(&client)?,
"client.bot_id": client.bot_id.to_owned(),
"client.user_id": client.user_id.to_owned(),
"client.channel_id": client.channel_id.to_owned(),
};

collection.delete_many(filter, None)?;
Expand Down

0 comments on commit ed6e57a

Please sign in to comment.