Couchbase compound keys in views with the C SDK - couchbase

I'm trying to implement a very basic C application that queries a Couchbase view using the C SDK. I got the SDK as such working since I can retrieve data from the DB server and even query views with a range query (startkey and endkey). However, if I create a view with a compound key, i.e. an index with two values, then I get nothing back. If I implement the same view in PHP or Node I get the correct data back. My understanding of the C SDK is that it uses the REST API of Couchbase, so the options string (optstr) is essentially the same as if I would use the Couchbase in-built web management console. Using single numeric start and end keys I get it all to work, but as soon as I start using compound keys like
startkey=["test",2]&endkey=["test",4]
I get no results back. The query string that does return data looks like
startkey=2&endkey=4
The bucket has only 5 documents of the following structure:
{number: 1, name: "test"}
with the number simply being between 1 and 5.
the view for the compound key looks like:
function (doc, meta) {
emit([doc.name, doc.number], doc);
}
and the one for the single key simply has the doc.number as the first value in the emit without being an array.
The code that I'm using for the compound key in C looks like:
lcb_CMDVIEWQUERY vq = {0};
vq.optstr = "startkey=[\"test\",2]&endkey=[\"test\",4]";
vq.noptstr = strlen(vq.optstr);
lcb_view_query_initcmd(&vq, "ddoc", "myView", NULL, viewCallback);
lcb_error_t rc = lcb_view_query(instance, NULL, &vq);
What am I missing for the compound views? The examples on the Couchbase website don't cover views using any keys and the examples in their Git repository don't have any examples either. The Couchbase C SDK seems to be a but light on documentation, even the API documentation doesn't seem to be very detailed.
Any help is appreciated.

It would help if the view with the compound key would actually being published! The code actually does work as intended. However, it would be interesting to get peoples views on the formatting of the query/option string and if it should be URL encoded or not when created.
Update:
After setting up a new Couchbase server and populating it with the same data set and rebuilding the view it actually does work. So I have no idea why it didn't work against the original server. I have gone through the map function with a fine toothed comb and checked the correct spelling (uppercase/lowercase) of all variables and parameters. At least it does work. :)

Related

Delete from json without the use of a 'real' id

In Angular:
I'm trying to delete items from a local json server using a http request.
The problem is that the items don't have 'real' id's. Their id's are strings which json doesn't recognises as id's (so far I know).
So when I try to search for an id (either to get it or delete it) I have to use for example:
"http://localhost:3000/watchlist?imdbID=tt5745872"
which gives an array with 1 item.
When using this in a delete request, it will result in a 404.
I was wondering if there is some kind of a workaround for doing this or do I really have to implement 'real' id's?
Context: I'm getting movies from an API and I then store those in an json server. As the API uses string id's, it would be a pain in the ass to try and implement a second id for the same object.

What is the URL when I DELETE an object

I'm running a local server playing around with an API using Django. I have a model called 'Users' populated with a few objects, and am using DefaultRouter.
I want to know what the URL would be if I were to DELETE a specific object from this model. For example, if I wanted to GET a user with an ID of 1 in this model, the URL would be: "localhost:8000/Users/1/". What would the equivalent be to DELETE this user?
I found an explanation of this on the REST API website (below), however, I don't understand what any of the syntaxes means.
What is {prefix}, {url_path}, {lookup} and [.format]? If anyone could provide an example of what this might be using a localhost that would be really helpful.
Thanks
Let us take an example of an API (URL) to update book data with id (pk) being 10. It would look something like this:
URL: http://www.example.com/api/v1/book/10/
Method: PUT/PATCH
With some data associated.
If you want to delete you just need to change method to DELETE instead of put or patch.
Regarding your second question lets compare the url with the parameters.
prefix: http://www.example.com/api/v1/book
lookup: 10
format: It specifies what type of data do you expect when you hit the API. Generally it is considered to be json.
url_path: In general, every thing after look up except query string is considered to be url_path.

How to include SR related work log long description when using maximo oslc rest api?

I am doing an HTTP GET request to /maximo/oslc/os/mxsr and using the oslc.select query string parameter to choose:
*,doclinks{*},worklog{*},rel.commlog{*},rel.woactivity{*,rel.woactivity{*}}
This lets me get related data, including related worklogs, but the worklog does not include the 'description_longdescription' field.
The only way I seem to be able to get that field is if I do a separate HTTP GET to query a worklog id directly through /maxrest/rest/mbo/worklog . Then it provides the description_longdescription field.
I understand this field is stored separately through the linked longdescription table, but I was hoping to get the data through the "next gen" oslc api with one http get request.
I've tried putting in 'worklog{*,description_longdescription}', as I read somewhere that longdescription is a "non-persistent" field and must be explicitly named for inclusion, but it had no effect.
I figured out that for the /maximo/oslc/os/mxsr object in the API, I needed to reference the related MODIFYWORKLOG object through the rel.modifyworklog syntax in the oslc.select query string:
oslc.select=*,doclinks{*},rel.modifyworklog{*,description_longdescription},rel.commlog{*},rel.woactivity{*,rel.woactivity{*}}
I also had to explicitly name the non-persistent field description_longdescription for it to be included.
Ref. for the "rel." syntax: https://developer.ibm.com/static/site-id/155/maximodev/restguide/Maximo_Nextgen_REST_API.html#_querying_maximo_asset_management_by_using_the_rest_api

What is the correct way to handle a POST request in Django class based view?

In my class based view I am handling a post request (which is an AJAX call).I am inserting some data in a database table and returning some json to the frontend.
def post(self,request,*args,**kwargs):
result_data = {}
doc = kwargs['doc']
doc_obj = Document.objects.get_document(doc)
doc_id = doc_obj.doc_id
reference_obj = Reference.objects.save_to_db(request,doc_id)
friendly_name = reference_obj.friendly_name
result_data['friendly_name'] = friendly_name
return HttpResponse(json.dumps(result_data),content_type='application/json')
My question is that is this the right way of handling a post request?My doubt arises here - I am writing some data into database and returning a json of properties of the same data I have written to the database.
Should I do it differently, ie first storing some data in database and returning just id of the row and again making GET request on the same URL?
I would say if it is an API, it is best to minimize the number of queries by returning the whole object the way you did. In case it is to serve a web user interface, it is simpler to return the id and get the rest of the data, if it needs to be displayed, from the client itself.
Either way is fine. The gain is negligible. I have also seen applications give a local positive feedback and then replace with a negative one if the request was unsuccessful. The technique can improve responsiveness, which is always appreciated.
Django-rest-framework returns the json of the inserted object, so i would think it reasonable to copy that behaviour.

How to Retrieve Large URL Json Data Set?

I am trying to obtain a data set via json and url, using SODA's API. The issue is that the data set is greater then 50K, and I need to sort the data set using multiple keys. Sorting by multiple keys is not something that is permitted by SODA's API. The question is how could I get around that?
Example (This table is small, but for illustrative purposes I have included it):
https://data.medicare.gov/resource/apyc-v239.json?$Limit=1000&$Order=measure_id
but once I attempt to add state to the order the API errors out.
The data set above is only 3800 recs, however there are other datasets with over 250000 recs, which require the same approach - sorting, then paging through the results...
Any assistance would be greatly appreciated.
Try the new API endpoint for that dataset: http://dev.socrata.com/foundry/#/data.medicare.gov/apyc-v239
It'll allow you to sort by multiple columns and there's no maximum for $limit on the new endpoints, so you can do stuff like this:
https://data.medicare.gov/resource/q7p2-jxeh.json?$order=state,measure_name&$limit=100000000