Skip to content

Commit

Permalink
Move search module.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Dec 4, 2023
1 parent bac3cf0 commit 263f8db
Show file tree
Hide file tree
Showing 25 changed files with 447 additions and 465 deletions.
6 changes: 2 additions & 4 deletions src/commands/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use futures::future::LocalBoxFuture;
use terminal_banner::{Banner, Padding};

use sos_net::sdk::{
account::{
search::{ArchiveFilter, Document, DocumentView},
AccountRef,
},
account::AccountRef,
storage::search::{ArchiveFilter, Document, DocumentView},
vault::{
secret::{Secret, SecretId, SecretMeta, SecretRef, SecretRow},
FolderRef, Summary,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use terminal_banner::{Banner, Padding};

use secrecy::{ExposeSecret, SecretString};
use sos_net::sdk::{
account::search::Document,
hex, secrecy,
storage::search::Document,
url::Url,
vault::{
secret::{FileContent, Secret, SecretId, SecretMeta, SecretRef},
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/local_account/search/favorites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use crate::test_utils::{mock, setup, teardown};
use anyhow::Result;
use maplit2::{hashmap, hashset};
use sos_net::sdk::{
account::{
search::{ArchiveFilter, DocumentView, QueryFilter},
LocalAccount, UserPaths,
},
account::{LocalAccount, UserPaths},
passwd::diceware::generate_passphrase,
storage::search::{ArchiveFilter, DocumentView, QueryFilter},
vault::secret::{IdentityKind, SecretType},
};

Expand Down
6 changes: 2 additions & 4 deletions tests/integration/local_account/search/view_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use crate::test_utils::{mock, setup, teardown};
use anyhow::Result;
use maplit2::{hashmap, hashset};
use sos_net::sdk::{
account::{
search::{ArchiveFilter, DocumentView, QueryFilter},
LocalAccount, UserPaths,
},
account::{LocalAccount, UserPaths},
passwd::diceware::generate_passphrase,
storage::search::{ArchiveFilter, DocumentView, QueryFilter},
vault::secret::{IdentityKind, SecretType},
};

Expand Down
12 changes: 7 additions & 5 deletions workspace/net/src/client/account/network_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ use mpc_protocol::generate_keypair;
use secrecy::SecretString;
use sos_sdk::{
account::{
search::{
AccountStatistics, ArchiveFilter, Document,
DocumentCount, DocumentView, QueryFilter, SearchIndex,
},
AccessOptions, Account, AccountBuilder, AccountData, DetachedView,
Identity, NewAccount, UserPaths,
},
commit::{CommitHash, CommitState},
crypto::AccessKey,
events::{Event, ReadEvent},
signer::ecdsa::Address,
storage::FolderStorage,
storage::{
search::{
AccountStatistics, ArchiveFilter, Document, DocumentCount,
DocumentView, QueryFilter, SearchIndex,
},
FolderStorage,
},
vault::{
secret::{Secret, SecretId, SecretMeta, SecretRow},
Summary, VaultId,
Expand Down
7 changes: 2 additions & 5 deletions workspace/net/src/client/account/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ use crate::client::{
};
use async_trait::async_trait;
use sos_sdk::{
account::AccountHandler,
commit::CommitState,
events::Event,
storage::FolderStorage,
vault::Summary,
account::AccountHandler, commit::CommitState, events::Event,
storage::FolderStorage, vault::Summary,
};
use std::{any::Any, sync::Arc};
use tokio::sync::RwLock;
Expand Down
11 changes: 5 additions & 6 deletions workspace/sdk/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ use std::{
};

use crate::{
account::{
search::{DocumentCount, SearchIndex},
AccountBuilder, FolderKeys, Identity, NewAccount,
UserPaths,
},
account::{AccountBuilder, FolderKeys, Identity, NewAccount, UserPaths},
commit::{CommitHash, CommitState},
constants::VAULT_EXT,
crypto::{AccessKey, SecureAccessKey},
Expand All @@ -23,7 +19,10 @@ use crate::{
AuditProvider, Event, EventKind, EventReducer, ReadEvent, WriteEvent,
},
signer::ecdsa::Address,
storage::FolderStorage,
storage::{
search::{DocumentCount, SearchIndex},
FolderStorage,
},
vault::{
secret::{Secret, SecretId, SecretMeta, SecretRow, SecretType},
Gatekeeper, Header, Summary, Vault, VaultId,
Expand Down
4 changes: 1 addition & 3 deletions workspace/sdk/src/account/archive/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::{
};

use serde::{Deserialize, Serialize};
use tokio::{
io::{AsyncRead, AsyncSeek},
};
use tokio::io::{AsyncRead, AsyncSeek};
use web3_address::ethereum::Address;

use uuid::Uuid;
Expand Down
8 changes: 2 additions & 6 deletions workspace/sdk/src/account/files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use crate::{
vault::{secret::SecretId, VaultId},
vfs, Error, Result,
};
use std::{
collections::HashSet, fmt, path::Path,
str::FromStr,
};
use std::{collections::HashSet, fmt, path::Path, str::FromStr};

mod external_files;
mod external_files_sync;
Expand Down Expand Up @@ -135,8 +132,7 @@ pub(super) async fn list_folder_files(
.as_ref()
.parse::<SecretId>()
{
let external_files =
list_secret_files(path).await?;
let external_files = list_secret_files(path).await?;
tracing::debug!(files_len = external_files.len());
files.push((secret_id, external_files));
}
Expand Down
117 changes: 117 additions & 0 deletions workspace/sdk/src/account/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Account search index.
use crate::{
crypto::AccessKey,
storage::search::*,
vault::{
secret::{SecretId, SecretType},
Gatekeeper, Summary, Vault, VaultId,
},
vcard4, Result,
};
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use tokio::sync::RwLock;

use super::account::Account;

impl<D> Account<D> {
/// Compute the account statistics.
///
/// If the account is not authenticated returns
/// a default statistics object (all values will be zero).
pub async fn statistics(&self) -> AccountStatistics {
if self.authenticated.is_some() {
let storage = self.storage().unwrap();
let reader = storage.read().await;
if let Ok(index) = reader.index() {
let search_index = index.search();
let index = search_index.read().await;
let statistics = index.statistics();
let count = statistics.count();

let documents: usize = count.vaults().values().sum();
let mut folders = Vec::new();
let mut types = HashMap::new();

for (id, v) in count.vaults() {
if let Some(summary) = self.find(|s| s.id() == id).await {
folders.push((summary, *v));
}
}

for (k, v) in count.kinds() {
if let Ok(kind) = SecretType::try_from(*k) {
types.insert(kind, *v);
}
}

AccountStatistics {
documents,
folders,
types,
tags: count.tags().clone(),
favorites: count.favorites(),
}
} else {
Default::default()
}
} else {
Default::default()
}
}

/// Search index for the account.
pub async fn index(&self) -> Result<Arc<RwLock<SearchIndex>>> {
let storage = self.storage()?;
let reader = storage.read().await;
Ok(reader.index()?.search())
}

/// Query with document views.
pub async fn query_view(
&self,
views: Vec<DocumentView>,
archive: Option<ArchiveFilter>,
) -> Result<Vec<Document>> {
let storage = self.storage()?;
let reader = storage.read().await;
reader.index()?.query_view(views, archive).await
}

/// Query the search index.
pub async fn query_map(
&self,
query: &str,
filter: QueryFilter,
) -> Result<Vec<Document>> {
let storage = self.storage()?;
let reader = storage.read().await;
reader.index()?.query_map(query, filter).await
}

/// Get the search index document count statistics.
pub async fn document_count(&self) -> Result<DocumentCount> {
let storage = self.storage()?;
let reader = storage.read().await;
let search = reader.index()?.search();
let index = search.read().await;
Ok(index.statistics().count().clone())
}

/// Determine if a document exists in a folder.
pub async fn document_exists_in_folder(
&self,
vault_id: &VaultId,
label: &str,
id: Option<&SecretId>,
) -> Result<bool> {
let storage = self.storage()?;
let reader = storage.read().await;
let search = reader.index()?.search();
let index = search.read().await;
Ok(index.find_by_label(vault_id, label, id).is_some())
}
}
Loading

0 comments on commit 263f8db

Please sign in to comment.