~unity-team/unity-lens-applications/unity-lens-applications-0.4

« back to all changes in this revision

Viewing changes to src/unity-package-search.cc

  • Committer: Mikkel Kamstrup Erlandsen
  • Date: 2011-09-23 13:42:31 UTC
  • Revision ID: mikkel.kamstrup@gmail.com-20110923134231-0wrq028t724lctgm
More robust exception handling when interacting with xapian

Show diffs side-by-side

added added

removed removed

Lines of Context:
453
453
            {
454
454
              doc = searcher->db->get_document (
455
455
                             g_rand_int_range (searcher->random, 1, lastdocid));
 
456
                        UnityPackageInfo *pkginfo = _pkginfo_from_document (doc);
 
457
              if (g_hash_table_lookup_extended (unique, pkginfo->package_name, NULL, NULL))
 
458
                {
 
459
                  _free_package_info (pkginfo);
 
460
                }
 
461
              else
 
462
                {
 
463
                  g_hash_table_insert (unique, pkginfo->package_name, NULL);
 
464
                  result->results = g_slist_prepend (result->results, pkginfo);
 
465
                  n_unique++;
 
466
                }
456
467
            }
457
 
          catch (Xapian::DocNotFoundError e)
 
468
          catch (Xapian::Error e)
458
469
            {
 
470
              g_warning ("Error getting random apps: %s", e.get_msg().c_str());
459
471
              continue;
460
472
            }
461
 
          UnityPackageInfo *pkginfo = _pkginfo_from_document (doc);
462
 
          if (g_hash_table_lookup_extended (unique, pkginfo->package_name, NULL, NULL))
463
 
            {
464
 
              _free_package_info (pkginfo);
465
 
            }
466
 
          else
467
 
            {
468
 
              g_hash_table_insert (unique, pkginfo->package_name, NULL);
469
 
              result->results = g_slist_prepend (result->results, pkginfo);
470
 
              n_unique++;
471
 
            }
472
473
        }
473
474
    }
474
475
  else
477
478
      try
478
479
      {
479
480
        query = searcher->query_parser->parse_query (filter_query, QUERY_PARSER_FILTER_FLAGS);
 
481
        searcher->enquire->set_sort_by_relevance ();
 
482
        searcher->enquire->set_query(query);
 
483
        Xapian::MSet matches = searcher->enquire->get_mset(0, searcher->db->get_doccount ());
 
484
        for (i = 0, n_unique = 0; i < n_apps*4, n_unique < n_apps; i++)
 
485
          {
 
486
            docid = g_rand_int_range (searcher->random, 0, matches.size ());
 
487
            Xapian::MSetIterator iter = matches[docid];
 
488
            Xapian::Document doc = iter.get_document ();
 
489
            UnityPackageInfo *pkginfo = _pkginfo_from_document (doc);
 
490
            if (g_hash_table_lookup_extended (unique, pkginfo->package_name, NULL, NULL))
 
491
              {
 
492
                _free_package_info (pkginfo);
 
493
              }
 
494
            else
 
495
              {
 
496
                g_hash_table_insert (unique, pkginfo->package_name, NULL);
 
497
                result->results = g_slist_prepend (result->results, pkginfo);
 
498
                n_unique++;
 
499
              }
 
500
          }
480
501
      }
481
 
      catch (Xapian::QueryParserError e)
 
502
      catch (Xapian::Error e)
482
503
      {
483
 
        g_debug ("Error parsing filter query '%s': %s", filter_query, e.get_msg().c_str());
 
504
        g_debug ("Error getting random apps for query '%s': %s",
 
505
                 filter_query, e.get_msg().c_str());
484
506
        return g_slice_new0 (UnityPackageSearchResult);
485
507
      }
486
 
      searcher->enquire->set_sort_by_relevance ();
487
 
      searcher->enquire->set_query(query);
488
 
      Xapian::MSet matches = searcher->enquire->get_mset(0, searcher->db->get_doccount ());
489
 
      for (i = 0, n_unique = 0; i < n_apps*4, n_unique < n_apps; i++)
490
 
        {
491
 
          docid = g_rand_int_range (searcher->random, 0, matches.size ());
492
 
          Xapian::MSetIterator iter = matches[docid];
493
 
          Xapian::Document doc = iter.get_document ();
494
 
          UnityPackageInfo *pkginfo = _pkginfo_from_document (doc);
495
 
          if (g_hash_table_lookup_extended (unique, pkginfo->package_name, NULL, NULL))
496
 
            {
497
 
              _free_package_info (pkginfo);
498
 
            }
499
 
          else
500
 
            {
501
 
              g_hash_table_insert (unique, pkginfo->package_name, NULL);
502
 
              result->results = g_slist_prepend (result->results, pkginfo);
503
 
              n_unique++;
504
 
            }
505
 
        }
506
508
    }
507
509
 
508
510
  g_hash_table_unref (unique);