Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding get schema by hash route + add schema hash to list versions #241

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/lib/src/api/local/schemas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::api;
use crate::{api, util};

use crate::core::index::SchemaReader;
use crate::error::OxenError;
Expand Down Expand Up @@ -69,3 +69,14 @@ pub fn get_by_path_from_ref(
Err(OxenError::revision_not_found(revision.into()))
}
}

pub fn get_by_hash(repo: &LocalRepository, hash: String) -> Result<Option<Schema>, OxenError> {
let version_path = util::fs::version_path_from_schema_hash(repo.path.clone(), hash);
// Read schema from that path
if version_path.exists() {
let schema: Schema = serde_json::from_reader(std::fs::File::open(version_path)?)?;
Ok(Some(schema))
} else {
Ok(None)
}
}
1 change: 1 addition & 0 deletions src/lib/src/view/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct BranchEntryVersion {
pub struct CommitEntryVersion {
pub commit: crate::model::Commit,
pub resource: ResourceVersion,
pub schema_hash: Option<String>,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
7 changes: 7 additions & 0 deletions src/lib/src/view/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ pub struct ListSchemaResponse {
pub commit: Option<Commit>,
pub resource: Option<ResourceVersion>,
}

/* For getting schemas directly by hash - no path associated */
#[derive(Serialize, Deserialize, Debug)]
pub struct SchemaResponse {
pub status: StatusMessage,
pub schema: Schema,
}
20 changes: 18 additions & 2 deletions src/server/src/controllers/branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::params::{app_data, path_param, PageNumQuery};

use actix_web::{web, HttpRequest, HttpResponse};

use liboxen::core::index::Merger;
use liboxen::core::index::{Merger, SchemaReader};
use liboxen::error::OxenError;
use liboxen::util::paginate;
use liboxen::util::{self, paginate};
use liboxen::view::entry::ResourceVersion;
use liboxen::view::{
BranchLockResponse, BranchNewFromExisting, BranchRemoteMerge, BranchResponse, BranchUpdate,
Expand Down Expand Up @@ -269,12 +269,28 @@ pub async fn list_entry_versions(
let mut commit_versions: Vec<CommitEntryVersion> = Vec::new();

for (commit, entry) in commits_with_versions {
// For each version, get the schema hash if one exists.
let maybe_schema_hash = if util::fs::is_tabular(&entry.path) {
let schema_reader = SchemaReader::new(&repo, &commit.id)?;
let maybe_schema = schema_reader.get_schema_for_file(&entry.path)?;
match maybe_schema {
Some(schema) => Some(schema.hash),
None => {
log::error!("Could not get schema for tabular file {:?}", entry.path);
None
}
}
} else {
None
};

commit_versions.push(CommitEntryVersion {
commit: commit.clone(),
resource: ResourceVersion {
version: commit.id.clone(),
path: entry.path.to_string_lossy().into(),
},
schema_hash: maybe_schema_hash,
});
}

Expand Down
24 changes: 23 additions & 1 deletion src/server/src/controllers/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::params::{app_data, parse_resource, path_param};
use liboxen::core::df::tabular;
use liboxen::model::Schema;
use liboxen::opts::DFOpts;
use liboxen::view::schema::SchemaWithPath;
use liboxen::view::schema::{SchemaResponse, SchemaWithPath};
use liboxen::{api, util};

use actix_web::{HttpRequest, HttpResponse};
Expand Down Expand Up @@ -112,3 +112,25 @@ pub async fn list_or_get(req: HttpRequest) -> actix_web::Result<HttpResponse, Ox
};
Ok(HttpResponse::Ok().json(response))
}

pub async fn get_by_hash(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
let app_data = app_data(&req)?;

let namespace = path_param(&req, "namespace")?;
let repo_name = path_param(&req, "repo_name")?;
let repo = get_repo(&app_data.path, namespace, repo_name)?;

let hash = path_param(&req, "hash")?;

let maybe_schema = api::local::schemas::get_by_hash(&repo, hash)?;

if let Some(schema) = maybe_schema {
let response = SchemaResponse {
status: StatusMessage::resource_found(),
schema,
};
Ok(HttpResponse::Ok().json(response))
} else {
Err(OxenHttpError::NotFound)
}
}
4 changes: 4 additions & 0 deletions src/server/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ pub fn config(cfg: &mut web::ServiceConfig) {
web::get().to(controllers::entries::download_data_from_version_paths),
)
// ----- Schemas ----- //
.route(
"/{namespace}/{repo_name}/schemas/hash/{hash}",
web::get().to(controllers::schemas::get_by_hash),
)
.route(
"/{namespace}/{repo_name}/schemas/{resource:.*}",
web::get().to(controllers::schemas::list_or_get),
Expand Down
Loading