Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
fixed merge
Browse files Browse the repository at this point in the history
  • Loading branch information
StaticallyTypedAnxiety committed Feb 5, 2020
2 parents 3c6122f + 2c6cb99 commit 17b54dc
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 107 deletions.
102 changes: 13 additions & 89 deletions crates/core/src/network/handler/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@ use crate::{
network::query::{
GetLinksNetworkQuery, GetLinksNetworkResult, NetworkQuery, NetworkQueryResult,
},
nucleus,
workflows::get_entry_result::get_entry_result_workflow,
NEW_RELIC_LICENSE_KEY,
nucleus, NEW_RELIC_LICENSE_KEY,
};
use holochain_core_types::{
crud_status::CrudStatus,
eav::Attribute,
entry::{Entry, EntryWithMetaAndHeader},
entry::EntryWithMetaAndHeader,
error::HolochainError,
network::query::{GetLinkData, GetLinksQueryConfiguration},
};
use holochain_json_api::json::JsonString;
use holochain_persistence_api::cas::content::Address;
use holochain_wasm_utils::api_serialization::get_entry::{
GetEntryArgs, GetEntryOptions, GetEntryResultType,
};

use lib3h_protocol::data_types::{QueryEntryData, QueryEntryResultData};
use std::{convert::TryInto, sync::Arc};

pub type LinkTag = String;
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CORE)]

#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CORE)]
fn get_links(
context: &Arc<Context>,
Expand All @@ -35,11 +31,10 @@ fn get_links(
tag: String,
crud_status: Option<CrudStatus>,
query_configuration: GetLinksQueryConfiguration,
) -> Result<Vec<GetLinkData>, HolochainError> {
) -> Result<Vec<GetLinkFromRemoteData>, HolochainError> {
//get links
let dht_store = context.state().unwrap().dht();

let (get_link, error): (Vec<_>, Vec<_>) = dht_store
Ok(dht_store
.get_links(
base,
link_type,
Expand All @@ -49,8 +44,7 @@ fn get_links(
)
.unwrap_or_default()
.into_iter()
//get tag
.map(|(eavi, crud)| {
.map(|(eavi, crud_status)| {
let tag = match eavi.attribute() {
Attribute::LinkTag(_, tag) => Ok(tag),
Attribute::RemovedLink(_, tag) => Ok(tag),
Expand All @@ -59,85 +53,15 @@ fn get_links(
)),
}
.expect("INVALID ATTRIBUTE ON EAV GET, SOMETHING VERY WRONG IN EAV QUERY");
(eavi.value(), crud, tag)
})
//get targets from dht
.map(|(link_add_address, crud, tag)| {
let error = format!(
"Could not find Entries for Address :{}, tag: {}",
link_add_address.clone(),
tag.clone()
);
let link_add_entry_args = GetEntryArgs {
address: link_add_address.clone(),
options: GetEntryOptions {
GetLinkFromRemoteData {
tag,
crud_status,
link_add_address: eavi.value(),
headers: query_configuration.headers,
..Default::default()
},
};

context
.block_on(get_entry_result_workflow(
&context.clone(),
&link_add_entry_args,
))
.map(|get_entry_result| match get_entry_result.result {
GetEntryResultType::Single(entry_with_meta_and_headers) => {
let maybe_entry_headers = if query_configuration.headers {
Some(entry_with_meta_and_headers.headers)
} else {
None
};
entry_with_meta_and_headers
.entry
.map(|single_entry| match single_entry {
Entry::LinkAdd(link_add) => Ok(GetLinkData::new(
link_add_address.clone(),
crud,
link_add.link().target().clone(),
tag.clone(),
maybe_entry_headers,
)),
Entry::LinkRemove(link_remove) => Ok(GetLinkData::new(
link_add_address.clone(),
crud,
link_remove.0.link().target().clone(),
tag.clone(),
maybe_entry_headers,
)),
_ => Err(HolochainError::ErrorGeneric(
"Wrong entry type for Link content".to_string(),
)),
})
.unwrap_or(Err(HolochainError::ErrorGeneric(error)))
}
_ => Err(HolochainError::ErrorGeneric(
"Single Entry required for Get Entry".to_string(),
)),
})
.unwrap_or_else(|e| {
Err(HolochainError::ErrorGeneric(format!(
"Could not get entry for Link Data {:?}",
e
)))
})
}
})
.partition(Result::is_ok);

//if can't find target throw error
if error.is_empty() {
Ok(get_link
.iter()
.map(|s| s.clone().unwrap())
.collect::<Vec<_>>())
} else {
Err(HolochainError::List(
error
.iter()
.map(|e| e.clone().unwrap_err())
.collect::<Vec<_>>(),
))
}
.collect())
}

#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CORE)]
Expand Down
31 changes: 26 additions & 5 deletions crates/core/src/wasm_engine/api/remove_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::{
network::{
actions::query::{query, QueryMethod},
query::{
GetLinksNetworkQuery, GetLinksNetworkResult, GetLinksQueryConfiguration,
NetworkQueryResult,
GetLinkFromRemoteData, GetLinksNetworkQuery, GetLinksNetworkResult,
GetLinksQueryConfiguration, NetworkQueryResult,
},
},
wasm_engine::{api::ZomeApiResult, Runtime},
workflows::author_entry::author_entry,
workflows::{author_entry::author_entry, get_link_result::get_link_data_from_link_addresses},
NEW_RELIC_LICENSE_KEY,
};

Expand Down Expand Up @@ -87,6 +87,7 @@ pub fn invoke_remove_link(runtime: &mut Runtime, args: &RuntimeArgs) -> ZomeApiR
"Could not get links for type".to_string(),
)),
};

if links_result.is_err() {
log_error!(context, "zome : Could not get links for remove_link method");
ribosome_error_code!(WorkflowFailed)
Expand All @@ -96,10 +97,30 @@ pub fn invoke_remove_link(runtime: &mut Runtime, args: &RuntimeArgs) -> ZomeApiR
GetLinksNetworkResult::Links(links) => links,
_ => return ribosome_error_code!(WorkflowFailed),
};

let filtered_links = links
.into_iter()
.filter(|link_for_filter| &link_for_filter.target == link.target())
.map(|s| s.address)
.map(
|GetLinkFromRemoteData {
link_add_address,
tag,
..
}| {
// make DHT calls to get the entries for the links
(
get_link_data_from_link_addresses(
&context,
&link_add_address,
&tag,
false,
)
.unwrap(),
link_add_address,
)
},
)
.filter(|(link_for_filter, _)| &link_for_filter.target == link.target())
.map(|response| response.1)
.collect::<Vec<_>>();

let entry = Entry::LinkRemove((link_remove, filtered_links));
Expand Down
120 changes: 109 additions & 11 deletions crates/core/src/workflows/get_link_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ use crate::{
network::{
actions::query::{query, QueryMethod},
query::{
GetLinksNetworkQuery, GetLinksNetworkResult, GetLinksQueryConfiguration,
GetLinkData, GetLinksNetworkQuery, GetLinksNetworkResult, GetLinksQueryConfiguration,
NetworkQueryResult,
},
},
workflows::get_entry_result::get_entry_result_workflow,
NEW_RELIC_LICENSE_KEY,
};
use holochain_persistence_api::cas::content::Address;
use holochain_wasm_utils::api_serialization::get_entry::{
GetEntryArgs, GetEntryOptions, GetEntryResultType,
};

use holochain_core_types::error::HolochainError;
use holochain_core_types::{
crud_status::CrudStatus, entry::Entry, error::HolochainError,
network::query::GetLinkFromRemoteData,
};
use holochain_wasm_utils::api_serialization::get_links::{
GetLinksArgs, GetLinksResult, LinksResult,
};
Expand All @@ -37,20 +45,110 @@ pub async fn get_link_result_workflow<'a>(

match links_result {
GetLinksNetworkResult::Links(links) => {
let get_links_result = links
links
.into_iter()
.map(|get_entry_crud| LinksResult {
address: get_entry_crud.target.clone(),
headers: get_entry_crud.headers.unwrap_or_default(),
status: get_entry_crud.crud_status,
tag: get_entry_crud.tag.clone(),
.map(
|GetLinkFromRemoteData {
link_add_address,
tag,
crud_status,
}| {
// make DHT calls to get the entries for the links
(
get_link_data_from_link_addresses(
context,
&link_add_address,
&tag,
link_args.options.headers,
),
crud_status,
)
},
)
.map(|(maybe_get_entry_result, crud_status)| {
maybe_get_entry_result.map(|get_entry_result| LinksResult {
address: get_entry_result.target.clone(),
headers: get_entry_result.headers.unwrap_or_default(),
status: crud_status,
tag: get_entry_result.tag.clone(),
})
})
.collect::<Vec<LinksResult>>();

Ok(GetLinksResult::new(get_links_result))
.collect::<Result<Vec<LinksResult>, HolochainError>>()
.map(|get_links_result| GetLinksResult::new(get_links_result))
}
_ => Err(HolochainError::ErrorGeneric(
"Could not get links".to_string(),
)),
}
}

// given the address of a link_add/link_remove entry, build a GetLinkData struct by retrieving the data from the DHT
pub fn get_link_data_from_link_addresses(
context: &Arc<Context>,
link_add_address: &Address,
tag: &String,
include_headers: bool,
) -> Result<GetLinkData, HolochainError> {
let get_link_add_entry_args = GetEntryArgs {
address: link_add_address.clone(),
options: GetEntryOptions {
headers: include_headers,
..Default::default()
},
};
context
.block_on(get_entry_result_workflow(
&context.clone(),
&get_link_add_entry_args,
))
.map(|get_entry_result| match get_entry_result.result {
GetEntryResultType::Single(entry_with_meta_and_headers) => {
let maybe_entry_headers = if include_headers {
Some(entry_with_meta_and_headers.headers)
} else {
None
};
let crud = entry_with_meta_and_headers
.meta
.map(|m| m.crud_status)
.unwrap_or(CrudStatus::Live);
entry_with_meta_and_headers
.entry
.map(|single_entry| match single_entry {
Entry::LinkAdd(link_add) => Ok(GetLinkData::new(
link_add_address.clone(),
crud,
link_add.link().target().clone(),
tag.clone(),
maybe_entry_headers,
)),
Entry::LinkRemove(link_remove) => Ok(GetLinkData::new(
link_add_address.clone(),
crud,
link_remove.0.link().target().clone(),
tag.clone(),
maybe_entry_headers,
)),
_ => Err(HolochainError::ErrorGeneric(
format!("Wrong entry type for Link content. Expected LinkAdd or LinkRemove, got: {:?}", single_entry),
)),
})
.unwrap_or_else(|| {
Err(HolochainError::ErrorGeneric(format!(
"Could not find Entries for Address: {}, tag: {}",
link_add_address.clone(),
tag.clone()
)))
})
}
_ => Err(HolochainError::ErrorGeneric(
"Single Entry required for Get Entry".to_string(),
)),
})
.unwrap_or_else(|e| {
Err(HolochainError::ErrorGeneric(format!(
"Could not get entry for Link Data {:?}",
e
)))
})
}
2 changes: 1 addition & 1 deletion crates/core/src/workflows/hold_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn hold_link_workflow(

log_debug!(context, "workflow/hold_link: added! {:?}", link);

//4. store link_add entry so we have all we need to respond to get links queries without any other network look-up
//4. store link_add entry so we have all we need to respond to get links queries without any other network look-up <== THIS!
hold_entry_workflow(&entry_with_header, context.clone()).await?;
log_debug!(
context,
Expand Down
10 changes: 9 additions & 1 deletion crates/core_types/src/network/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ pub enum GetLinksNetworkQuery {
Links(GetLinksQueryConfiguration),
}

// The data returned by a remote node on responding to a get_links query
#[derive(Debug, Serialize, Deserialize, PartialEq, DefaultJson, Clone)]
pub struct GetLinkFromRemoteData {
pub link_add_address: Address,
pub crud_status: CrudStatus,
pub tag: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, DefaultJson, Clone)]
pub struct GetLinkData {
pub address: Address,
Expand Down Expand Up @@ -49,7 +57,7 @@ impl GetLinkData {
#[derive(Debug, Serialize, Deserialize, PartialEq, DefaultJson, Clone)]
pub enum GetLinksNetworkResult {
Count(usize),
Links(Vec<GetLinkData>),
Links(Vec<GetLinkFromRemoteData>),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, DefaultJson, Clone)]
Expand Down

0 comments on commit 17b54dc

Please sign in to comment.