How to use cursors for navigating to previous pages using GQL and the new gcloud-java API? - google-cloud-datastore

I'm using the new gcloud-java API (https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-datastore/src/main/java/com/google/cloud/datastore) for working with the Cloud Datastore. My specific question is on using GQL for pagination with cursors. I was able to page through the results one page at a time in the forward direction using cursors, but not having any luck with paging backwards.
Example Scenario:
Let's say I've 20 entities in a Kind with IDs 1 through 20. I have a page size of 5. Once I'm on the 3rd page (IDs 11 through 15), if I need to go one page back; i.e. retrieve IDs 6 through 10, what would be the correct GQL/sample code? Again, I prefer not to use offset with a number, but would like to use Cursors.
From what I can tell (actually tested), it looks like one needs to keep track of Start/End cursors for each page as they navigate in the forward direction, then use the saved cursors when there is a need to go back. I just want to make sure if this is the correct/only way or there is a simpler way to accomplish this.
Thanks in advance for your help.

If you add to your original query a sort by key (appended to the end of your "order by" clause), you should be able to reverse each property's sort order and use the latest cursor from your original query to get results in reverse.
Suppose you've iterated through some of the values from your forward query's QueryResults. You can call QueryResults's cursorAfter() method, which will return a cursor pointing right after the last result you saw from your original query. Now you can issue a new query (with the opposite sort order on each property, including the key property) using that cursor as the start cursor. You'll probably want to skip the first result, since it will be the last result you saw from the original query.

Related

Avoiding copy/paste code for similar queries in sqlite?

I'm new to sqlite. For a game I'm working on I need a simple view of the top 5 values of a property with their associated player, which works fine like this:
SELECT
player,
reputation,
races.name,
races.color
FROM
games_player
INNER JOIN
races ON races.id = games_player.race
ORDER BY
reputation DESC
LIMIT 5
I can save this view in the database using DB Browser for SQLite. However, I need 3 more of those, for properties other than "reputation" (e.g. "strength" or whatever). I can easily create these views by duplicating the code and modifying the property name in each one, but if I later need to add some slight modification to the query format (even if it's only the ORDER BY value), I'll need to do it four times.
Is there a mechanism such a variable, an include system or anything else that can help me avoid this?

Passing Queries on multiple across multiple pages

When I generate a lead I get the values passed on via the URL Query which is the following:
?fname=[value]&lname=[value]&email=[value]
I’d like to know how I can carry this query over to the next page as well? (so the URL is passed over two pages instead of just the one).
So it would flow like this:
LeadPageExample.com -> Lead entered details -> LeadPageExample.com/Upsell?fname=Alex&lname=Smith&email=asmith#email.com -> Lead clicks on cancel or add to cart (I want fname=Alex&lname=Smith&email=asmith#email.com to be passed on again) -> LeadPageExample.com/checkout/?add-to-cart=312&fname=Alex&lname=Smith&email=asmith#email.com
Essentially I want the fname=Alex&lname=Smith&email=asmith#email.com query to be re-attached to the next link and passed on twice in this scenario.
To illustrate:
LeadPageExample.com/checkout/?add-to-cart=312&fname=Alex&lname=Smith&email=asmith#email.com
Any ideas? Is it possible? If so, how?
To be even more specific, I ONLY want this to happen on specific webpages on my wordpress site.
Thanks so much for the help.

Count of the icon tab filter

I would really like to understand how to use the "count" property of the IconTabFilter for SAPUI5 to dynamically show the count of the result set of a table.
I have the following code -
<IconTabFilter
count="{DataSet/$count}">
<Table items="{DataSet}">
But the count is not filled automatically.
I am using an oData model that is bound on the view level. I do not want to make another backend request just for the counts. What am I doing wrong here? Is there a different mechanism that can be used?
I also tried using the updateFinished event on the table to then get the count and set it via JS but the event is triggered only on DOM placement of the table. In my case the table is hidden behind the IconTab and is not placed into the DOM till the first time the user clicks the tab so its useless.
Really would appreciate some insight into how to use this!
Thanks!
Okay, so what I did was I bound my information to a local model and did an oData $expand query to fetch the entire pages information in one call.
This worked out for me because I had several sets of data to be fetched. Before they were bound individually to tables, now they are all in one query.
In the .done() method of the call I just used the setCount method of the IconTabFilter to set the count as per the return data set.

Access 2010 Query with Parameter and Sort

I have a problem that I've been going round and round with in Access 2010. Imagine a table with these columns:
Name Date Time
Now, I have a query that asks the user to input a begin date and an end date and returns all records that are between those two dates. This works fine. However, as soon as I add a sort to the Date column things go awry. Once you put a sort on a column with a parameter the user gets asked to enter the parameter twice. From what I've been able to find out this is normal (although annoying) behavior in Access.
If I add the Date column in a second time and show the column with the sort and don't show the column with the parameter it works fine. The query would look something like:
Name Date (shown & sorted) Date (not shown & parameters) Time
Now when I run the query it all works well and comes out the way I want it to. This would obviously be a great solution then. However, there's another problem. When I save the query, leave, and reopen the query the two columns are merged back into each other. Thus, the change is lost and the user again sees two inputs.
My question is this: what can I do differently to achieve the desired results?
Some possible things I've thought about but don't know the answer to are:
Is there a way to make it so the columns don't merge? Do I have to use a form with the input boxes and take the data from that (I'd prefer not to do that as it will require a lot of additional work to handle the various things I am doing in the database). Is there some obvious thing I'm missing?
Thanks for any suggestions.
FYI: Here is the SQL from the query
SELECT Intentions.Intention, Intentions.MassDate, Intentions.[Time Requested], Intentions.[Place Requested], Intentions.[Offered By], Intentions.Completed
FROM Intentions
WHERE (((Intentions.MassDate) Between [Enter start date] And [Enter end date]))
ORDER BY Intentions.MassDate, Intentions.[Time Requested];
It is true that sometimes the Query Designer in Access will "reorganize" a query when you save it. However, I don't recall an instance where such a reorganization actually broke anything.
For what it's worth, the following query seems to do what you desire. After saving and re-opening it looks and behaves just the same:
For reference, the SQL behind it is
PARAMETERS startDate DateTime, endDate DateTime;
SELECT NameDateTime.Name, NameDateTime.Date, NameDateTime.Time
FROM NameDateTime
WHERE (((NameDateTime.Date) Between [startDate] And [endDate]))
ORDER BY NameDateTime.Date DESC , NameDateTime.Time DESC;
I have had the same problem and I have discovered the reason:
If, after you have run your query, sort a collumn in the result grid and the say yes to save changes to the query the sort action will be stored with the query. This will actually cause the query to run twice. First to create the result and then one more time to sort. You'll therefore be asked twice for the parameters.
SOLUTION: Run the query (entering your parameters twice ;-) ). Then remove the Sorting by clicking on the AZ-eraser symbol in the task bar above (in the sorting compartment).
Then open your query in design-mode and add the sorting order to the appropriate collumn.
Your are then good to go.
Regards
Jan

pagination in cassandra based web application

How can i do a pagination in cassandra based web application. I am using spring MVC on server side and jquery on client side. I tried this but was not sutisfied.
My row key is UUIDType and every time i am sending the start key as string from client browser so dont know how to convert it back to UUID. A simple example will be appriciated.
Spring-data has this functionality pre-rolled :
http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#web-pagination
If you use PlayOrm for cassandra it returns a cursor when you query and as your first page reads in the first 20 results and displays it, the next page can just use the same cursor in your session and it picks up right where it left off without rescanning the first 20 rows again.
Dean
I would suggest generic solution which shall work for any language. I used python pycassa to work this out:
First approach:
-Say if column_count = 10 (How many results to display on front end)
-collect first 11 rows, sort first 10, send it to user (front end) as JSON object and also parameter last=11th_column
-User then calls for page 2, with prev = 1st_column_id, column_start=11th_column and column_count = 10. Based on this, I query cassandra like cf.get('cf', key, column_start=11th_column, column_count=10)
-This way, I can traverse, next page and previous page.
-Only issue with this approach is, I don't have all columns in super column sorted. So this did not work.
Second approach ( I used in production ):
-fetch all super columns and columns for a row key. e.g in python pycassa, cf.get('cf',key)
-Sort this in python using sorted and lambda function based on column values.
-Once sorted, prepare buckets and each bucked size is of page size/column count. Also filter out any rogue data if needed before bucketing.
-Store page by page results in Redis with keys such as 'row_key|page_1|super_column' and keep refreshing redis periodically.
My second approach worked pretty good with small/medium amount of data.

Resources