From: miker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Fri, 10 Sep 2010 20:11:15 +0000 (+0000)
Subject: Allow the caller to specify a max number of facet values per facet
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=acec3d1e2b85f75889427e074514a5b43e7e4f50;p=evergreen%2Fmasslnc.git

Allow the caller to specify a max number of facet values per facet

git-svn-id: svn://svn.open-ils.org/ILS/trunk@17586 dcc99617-32d9-48b4-a31d-7c20da2025e4
---

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm b/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
index 3d33837e3c..bfe63e241d 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
@@ -1316,10 +1316,28 @@ sub retrieve_cached_facets {
     my $self   = shift;
     my $client = shift;
     my $key    = shift;
+    my $limit    = shift;
 
     return undef unless ($key and $key =~ /_facets$/);
 
-    return $cache->get_cache($key) || {};
+    my $blob = $cache->get_cache($key) || {};
+
+    my $facets = {};
+    if ($limit) {
+       for my $f ( keys %$blob ) {
+            my @sorted = map{ { $$_[1] => $$_[0] } } sort {$$b[0] <=> $$a[0]} map { [$_, $$blob{$f}{$_}] } keys %{ $$blob{$f} };
+            @sorted = @sorted[0 .. $limit - 1] if (scalar(@sorted) > $limit);
+            for my $s ( @sorted ) {
+                my ($k) = keys(%$s);
+                my ($v) = values(%$s);
+                $$facets{$f}{$k} = $v;
+            }
+        }
+    } else {
+        $facets = $blob;
+    }
+
+    return $facets;
 }
 
 __PACKAGE__->register_method(