Skip to content

Commit

Permalink
Tidy storage APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Feb 5, 2025
1 parent 414a81b commit 6781d6f
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 111 deletions.
26 changes: 14 additions & 12 deletions crates/account/src/local_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl LocalAccount {

#[allow(unused_mut)]
let mut storage = ClientStorage::new_authenticated(
*account_id,
account_id,
BackendTarget::FileSystem(paths),
user,
)
Expand Down Expand Up @@ -641,7 +641,7 @@ impl LocalAccount {
let paths = Paths::new(data_dir, account_id.to_string());

let storage = ClientStorage::new_unauthenticated(
account_id,
&account_id,
BackendTarget::FileSystem(paths.clone()),
)
.await?;
Expand Down Expand Up @@ -713,7 +713,7 @@ impl LocalAccount {
let (authenticated_user, public_account) = new_account.into();

let mut storage = ClientStorage::new_authenticated(
account_id,
&account_id,
BackendTarget::FileSystem(paths),
authenticated_user,
/*
Expand Down Expand Up @@ -781,7 +781,7 @@ impl Account for LocalAccount {
let paths = self.paths();

let mut storage = ClientStorage::new_unauthenticated(
account_id,
&account_id,
BackendTarget::FileSystem((&*paths).clone()),
)
.await?;
Expand Down Expand Up @@ -1213,10 +1213,11 @@ impl Account for LocalAccount {
}

async fn root_commit(&self, summary: &Summary) -> Result<CommitHash> {
let folder =
self.storage.folders().get(summary.id()).ok_or_else(|| {
StorageError::FolderNotFound(*summary.id())
})?;
let folder = self
.storage
.folders()
.get(summary.id())
.ok_or_else(|| StorageError::FolderNotFound(*summary.id()))?;
let event_log = folder.event_log();
let log_file = event_log.read().await;
Ok(log_file
Expand Down Expand Up @@ -1337,10 +1338,11 @@ impl Account for LocalAccount {
.await?
.ok_or(Error::NoFolderPassword(*summary.id()))?;

let folder =
self.storage.folders().get(summary.id()).ok_or_else(|| {
StorageError::FolderNotFound(*summary.id())
})?;
let folder = self
.storage
.folders()
.get(summary.id())
.ok_or_else(|| StorageError::FolderNotFound(*summary.id()))?;

let event_log = folder.event_log();
let log_file = event_log.read().await;
Expand Down
9 changes: 5 additions & 4 deletions crates/database_upgrader/src/upgrader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn compute_fs_account_status(
) -> Result<SyncStatus> {
let sync_status = if account_paths.is_server() {
let storage = ServerStorage::new(
account_paths.documents_dir(),
account_paths,
account.account_id(),
BackendTarget::FileSystem(account_paths.clone()),
)
Expand All @@ -138,7 +138,7 @@ async fn compute_fs_account_status(
storage.sync_status().await?
} else {
let storage = ClientStorage::new_unauthenticated(
*account.account_id(),
account.account_id(),
BackendTarget::FileSystem(account_paths.clone()),
)
.await?;
Expand All @@ -156,14 +156,15 @@ async fn compute_db_account_status(
) -> Result<SyncStatus> {
let sync_status = if account_paths.is_server() {
let storage = ServerStorage::new(
account_paths.documents_dir(),
account_paths,
account.account_id(),
BackendTarget::Database(client.clone()),
)
.await?;
storage.sync_status().await?
} else {
todo!("compute for client accounts");
Default::default()
// todo!("compute for client accounts");
/*
let storage = ClientFileSystemStorage::new_unauthenticated(
*account.account_id(),
Expand Down
2 changes: 0 additions & 2 deletions crates/login/src/identity_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,6 @@ impl IdentityFolder {
account_id: AccountId,
keeper: &AccessPoint,
) -> Result<(UrnLookup, PrivateIdentity)> {
println!("doing login private identity...");

let (index, identity_secret) =
Self::lookup_identity_secrets(keeper).await?;

Expand Down
16 changes: 10 additions & 6 deletions crates/server/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ impl Backend {
"server_backend::read_dir",
);

let paths = Paths::new_global_server(&self.directory);

let account = ServerStorage::new(
&self.directory,
&paths,
&account_id,
BackendTarget::FileSystem(
Paths::new_global_server(&self.directory),
),
BackendTarget::FileSystem(paths.clone()),
)
.await?;

Expand Down Expand Up @@ -121,9 +121,13 @@ impl Backend {
"server_backend::create_account",
);

let account = ServerStorage::create_fs_account(
&self.directory,
let paths = Paths::new_global_server(&self.directory)
.with_account_id(account_id);

let account = ServerStorage::create_account(
&paths,
account_id,
BackendTarget::FileSystem(paths.clone()),
&account_data,
)
.await?;
Expand Down
8 changes: 4 additions & 4 deletions crates/storage/client/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct ClientFileSystemStorage {
impl ClientFileSystemStorage {
/// Create unauthenticated folder storage for client-side access.
pub async fn new_unauthenticated(
account_id: AccountId,
account_id: &AccountId,
paths: Paths,
) -> Result<Self> {
paths.ensure().await?;
Expand All @@ -142,7 +142,7 @@ impl ClientFileSystemStorage {
let paths = Arc::new(paths);

let mut storage = Self {
account_id,
account_id: *account_id,
summaries: Vec::new(),
current: Arc::new(Mutex::new(None)),
folders: Default::default(),
Expand All @@ -169,7 +169,7 @@ impl ClientFileSystemStorage {

/// Create folder storage for client-side access.
pub async fn new_authenticated(
account_id: AccountId,
account_id: &AccountId,
paths: Paths,
authenticated_user: Identity,
/*
Expand Down Expand Up @@ -214,7 +214,7 @@ impl ClientFileSystemStorage {
let paths = Arc::new(paths);

Ok(Self {
account_id,
account_id: *account_id,
summaries: Vec::new(),
current: Arc::new(Mutex::new(None)),
folders: Default::default(),
Expand Down
38 changes: 10 additions & 28 deletions crates/storage/client/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum ClientStorage {
impl ClientStorage {
/// Create new client storage.
pub async fn new_unauthenticated(
account_id: AccountId,
account_id: &AccountId,
target: BackendTarget,
) -> Result<Self> {
match target {
Expand All @@ -71,7 +71,7 @@ impl ClientStorage {

/// Create new client storage in authenticated state.
pub async fn new_authenticated(
account_id: AccountId,
account_id: &AccountId,
target: BackendTarget,
authenticated_user: Identity,
) -> Result<Self> {
Expand All @@ -97,9 +97,11 @@ impl ClientStorage {

/// Create new file system storage.
async fn new_unauthenticated_fs(
account_id: AccountId,
account_id: &AccountId,
paths: Paths,
) -> Result<Self> {
debug_assert!(!paths.is_server());

Ok(Self::FileSystem(
ClientFileSystemStorage::new_unauthenticated(account_id, paths)
.await?,
Expand All @@ -108,56 +110,36 @@ impl ClientStorage {

/// Create new file system storage in authenticated state.
async fn new_authenticated_fs(
account_id: AccountId,
account_id: &AccountId,
paths: Paths,
authenticated_user: Identity,
) -> Result<Self> {
debug_assert!(!paths.is_server());

Ok(Self::FileSystem(
ClientFileSystemStorage::new_authenticated(
account_id,
paths,
authenticated_user,
/*
identity_log,
device,
*/
)
.await?,
))
}

/// Create new file system storage.
async fn new_unauthenticated_db(
account_id: AccountId,
account_id: &AccountId,
client: Client,
) -> Result<Self> {
/*
Ok(Self::FileSystem(
ClientFileSystemStorage::new_unauthenticated(account_id, paths).await?,
))
*/

todo!("unauthenticated db storage");
}

/// Create new file system storage in authenticated state.
async fn new_authenticated_db(
account_id: AccountId,
account_id: &AccountId,
client: Client,
authenticated_user: Identity,
) -> Result<Self> {
/*
Ok(Self::FileSystem(
ClientFileSystemStorage::new_authenticated(
account_id,
paths,
identity_log,
device,
)
.await?,
))
*/

todo!("authenticated db storage");
}
}
Expand Down
14 changes: 3 additions & 11 deletions crates/storage/server/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,11 @@ impl ServerDatabaseStorage {
pub async fn new(
client: Client,
account_id: AccountId,
data_dir: Option<PathBuf>,
identity_log: Arc<RwLock<FolderEventLog>>,
paths: Paths,
) -> Result<Self> {
let data_dir = if let Some(data_dir) = data_dir {
data_dir
} else {
Paths::data_dir().map_err(|_| Error::NoCache)?
};

let dirs = Paths::new_server(data_dir, account_id.to_string());
Self::new_client(client, Arc::new(dirs), account_id, identity_log)
debug_assert!(!paths.is_global());
Self::new_client(client, Arc::new(paths), account_id, identity_log)
.await
}

Expand All @@ -101,8 +95,6 @@ impl ServerDatabaseStorage {
.into());
}

paths.ensure().await?;

let account_row =
Self::lookup_account(&mut client, &account_id).await?;

Expand Down
12 changes: 3 additions & 9 deletions crates/storage/server/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,12 @@ pub struct ServerFileStorage {
impl ServerFileStorage {
/// Create folder storage for server-side access.
pub async fn new(
paths: Paths,
account_id: AccountId,
identity_log: Arc<RwLock<FolderEventLog>>,
data_dir: Option<PathBuf>,
) -> Result<Self> {
let data_dir = if let Some(data_dir) = data_dir {
data_dir
} else {
Paths::data_dir().map_err(|_| Error::NoCache)?
};

let dirs = Paths::new_server(data_dir, account_id.to_string());
Self::new_paths(Arc::new(dirs), account_id, identity_log).await
debug_assert!(!paths.is_global());
Self::new_paths(Arc::new(paths), account_id, identity_log).await
}

/// Create new storage backed by files on disc.
Expand Down
Loading

0 comments on commit 6781d6f

Please sign in to comment.