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

Provide hdk::query_result API to allow return of Headers if desired #868

Merged
merged 41 commits into from
Jan 24, 2019

Conversation

pjkundert
Copy link
Contributor

@pjkundert pjkundert commented Jan 16, 2019

The existing hdk::query("post".into(), 0, 0) returning Vec<Address> is maintained.

A new hdk::query_result("post.into(), QueryArgsOptions::default()) is provided, returning an enum containing Vec or Vec. If QueryArgsOptions { headers: Some(true),... } is provided it returns a Vec<ChainHeader>; otherwise, a Vec<Addresses>.

I'd like to collect commentary on the API. It's somewhat like hdk::get_entry_result, but the return type is an enum, instead of a struct w/ a result member.

I've also cleaned up the .skip and .take a bit to avoid cloning more than I have to, since ChainHeaders are somewhat bigger than just an Address to copy.

Also, instead of hdk::query taking u32 values for start and limit, I've switched to usize for consistency with what they're used for internally. Is that a problem for JSON serialization or anything?

@maackle
Copy link
Collaborator

maackle commented Jan 16, 2019

@pjkundert does this mean you can only query for headers or the entries, but not both?

I'm wondering how this fits into the work I am about to start, which is to flesh out GetEntryOptions, which instructs get_entry_results whether or not to return headers as well as entries. Let's sync up tomorrow.

@pjkundert
Copy link
Contributor Author

pjkundert commented Jan 16, 2019

We need both to be improved; I couldn't quite figure out how to get the headers in the get_entry flow, but I noticed we were just throwing them away in the query, so I took a crack at obtaining them there.

The APIs should be improved in both cases to make them symmetrical; the get_entry's GetEntryOptions had an extra unnecessary option for query (the StatusRequestKind), so I made a QueryArgsOptions, but perhaps we could just re-use the same struct and document that the ...Kind is ignored? Also, the return types differ, and query_result would probably need to be made consistent with get_entry_result; I tried to retain the basic hdk::query API (returns a simple Vec<Address> only), but we'd have to sacrifice that and change it; not too big a deal, at this point.

@pjkundert pjkundert changed the title WIP: Provide hdk::query_result API to allow return of Headers if desired Provide hdk::query_result API to allow return of Headers if desired Jan 21, 2019
@zippy
Copy link
Member

zippy commented Jan 21, 2019

I think it's fine that Query would return something different then Get so it has it's own options.

Copy link
Member

@zippy zippy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I would approve this for the sake of expedience and getting us what we need for closed alpha, but I do think that the underlying API in core should allow you to get both headers and entries in one query. If that seems like to much to get done for this PR, lets just make sure it gets into the tree as a followup.

}

#[derive(Debug)]
pub enum ChainStoreQueryResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree with @maackle that this shouldn't be an enum, but instead a struct with Option<Vec

> and Option<Vec> that have Some() or None() based on the query so that you don't have to do two queries if you want both. Then in the request we would have an enum for "Entries, Headers, Both".

It would be fine to have helper functions in the hdk that assume you only want one or the other and do the ChainStoreQueryOptions setting correctly and unrwap to just one or the other.. But the core function should allow both to be returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maackle and I just paired; We'll be able to Addresses, Headers, and Entries without too much trouble. Should have it implemented for review by end of day, hopefully.

o Work in progress; needs unit testing
@pjkundert
Copy link
Contributor Author

OK, the hdk::query_results API has been enhanced to optionally return Entry data. I'd like feedback on the API and return types.

Not yet ready for merging; hasn't been heavily tested (ie. at all).

.collect();
match maybe_headers_with_entries {
Ok(headers_with_entries) => QueryResult::HeadersWithEntries(headers_with_entries),
Err(_e) => return ribosome_error_code!(UnknownEntryType), // TODO: return actual error?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works, this is probably fine now, but would rather see the entry-getting stuff DRYed up and refactored into a function

Copy link
Collaborator

@maackle maackle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @zippy, this is fine for now, once we know what we're doing with get_entry_result we can fine tune the API and unify this with that.

Copy link
Collaborator

@lucksus lucksus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query should not get entries form DHT but the chain instead.

Err(_code) => return ribosome_error_code!(UnknownEntryType),
};

runtime.store_result(result)
}

/// Get an Entry via the provided context, returning Entry or HolochainError on failure
fn get_entry_from_context(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't query supposed to operate on the chain only? Why from_context which actually is do a get from_dht? This only works because we currently reuse the same CAS for both, chain and dht, but we do have to separate CASes in the context to keep them separate. This function should be get_entry_from_chain which only operates on the source chain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've augmented core/src/nucleus/actions/get_entry.rs to provide crate-local APIs to access Entry values from either the either the context's dht or the agent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

o Reduce some unnecessary copying of Addresses
o Change hdk::query to get local-chain entries only from agent CAS
@lucksus lucksus merged commit d83535b into develop Jan 24, 2019
@zippy zippy deleted the feature-query-headers branch July 4, 2019 19:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants