NuGet Perf, Part VIII–Correcting Mistakes and Doing Aggregations
Join the DZone community and get the full member experience.Join For Free
i hope this is the last installment of the series, because i keep losing track of the current part number.
at any rate, someone pointed out to me that i made an error in importing the data. i assumed that the downloadcount field that i got from the nuget api is the download count for the specific package, but it appears that this is the total downloads count, across all versions of this package. the actual download number for a specific package is: versiondownloadcount.
that changes things a bit, because the way nuget sorts things is based on the total download count, not the download count for a specific version. the reason this complicate things is that we aren’t going to store the total download count in all the version documents. first, let us see the sort of query we need to write. in sql, it would look like this:
select top 30 skip 30 id, packageid, created, (select sum(versiondownloadcount) from packages all where all.packageid = p.packageid) as totaldownloadscount from packages p where isprerelease = 0 order by totaldownloadscount desc, created
this is a much simplified version of the real query, and something that you can’t actually write this simply in sql, most probably. but it gets the point.
note that in order to process this query, the rdmbs would have to first aggregate all of the data (for each row, mind) then do the paging, then give you the results. sure, you can keep a counter for all the downloads for a package, but considering the fact that downloads are highly parallel and happen all the time, waiting for writers to finish doing their update.
instead, with ravendb, we are going to use a map/reduce index and query on that.
this should be fairly simple to follow. in the map we go over all the packages, and output their package id, whatever they have been released, the specific version download count and the date it was created.
in the reduce, we group by the package id and whether it was pre-released or not -- i'm assuming that we usually don’t want to show the pre-release stuff there.
finally, we sum up all of the individual package downloads and we output the oldest created date. using all of that, we can now move to the next step, and actually query that:
there is a small bug here, since i don’t see ravendb in the results, but i guess i’ll have to wait until i get the updated data from nuget.
actually, that is not quite true. for pre-released software, we are pretty high up:
that explains much. ravendb 1.2 is pretty awesome.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.