summaryrefslogtreecommitdiff
path: root/src/libaudgui/jump-to-track-cache.cc
blob: 7dcf2427b1c8982ca7b8918a0d85d86382bb639b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * jump-to-track-cache.c
 * Copyright 2008-2014 Jussi Judin and John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "jump-to-track-cache.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <glib.h>  /* for GRegex */

#include <libaudcore/audstrings.h>
#include <libaudcore/playlist.h>
#include <libaudcore/runtime.h>

/**
 * Creates an regular expression list usable in searches from search keyword.
 *
 * In searches, every regular expression on this list is matched against
 * the search title and if they all match, the title is declared as
 * matching one.
 *
 * Regular expressions in list are formed by splitting the 'keyword' to words
 * by splitting the keyword string with space character.
 */
Index<GRegex *> jump_to_track_cache_regex_list_create (const char * keyword)
{
    Index<GRegex *> regex_list;

    /* Chop the key string into ' '-separated key regex-pattern strings */
    Index<String> words = str_list_to_index (keyword, " ");

    /* create a list of regex using the regex-pattern strings */
    for (const char * word : words)
    {
        // Ignore empty words.
        if (! word[0])
            continue;

        GRegex * regex = g_regex_new (word, G_REGEX_CASELESS, (GRegexMatchFlags) 0, nullptr);
        if (regex)
            regex_list.append (regex);
    }

    return regex_list;
}

/**
 * Checks if 'song' matches all regular expressions in 'regex_list'.
 */
static bool jump_to_track_match (const char * name, Index<GRegex *> & regex_list)
{
    if (! name)
        return false;

    for (GRegex * regex : regex_list)
    {
        if (! g_regex_match (regex, name, (GRegexMatchFlags) 0, nullptr))
            return false;
    }

    return true;
}

/**
 * Returns all songs that match 'keyword'.
 *
 * Searches are conducted against entries in 'search_space' variable
 * and after the search, search result is added to 'cache'.
 *
 * @param cache The result of this search is added to cache.
 * @param search_space Entries inside which the search is conducted.
 * @param keyword Normalized string for searches.
 */
const KeywordMatches * JumpToTrackCache::search_within
 (const KeywordMatches * subset, const char * keyword)
{
    Index<GRegex *> regex_list = jump_to_track_cache_regex_list_create (keyword);

    KeywordMatches * k = add (String (keyword), KeywordMatches ());

    for (const KeywordMatch & item : * subset)
    {
        if (! regex_list.len () ||
         jump_to_track_match (item.title, regex_list) ||
         jump_to_track_match (item.artist, regex_list) ||
         jump_to_track_match (item.album, regex_list) ||
         jump_to_track_match (item.path, regex_list))
            k->append (item);
    }

    for (GRegex * regex : regex_list)
        g_regex_unref (regex);

    return k;
}

/**
 * Creates a new song search cache.
 *
 * Returned value should be freed with ui_jump_to_track_cache_free() function.
 */
void JumpToTrackCache::init ()
{
    int playlist = aud_playlist_get_active ();
    int entries = aud_playlist_entry_count (playlist);

    // the empty string will match all playlist entries
    KeywordMatches & k = * add (String (""), KeywordMatches ());

    k.insert (0, entries);

    for (int entry = 0; entry < entries; entry ++)
    {
        KeywordMatch & item = k[entry];
        item.entry = entry;
        item.path = String (uri_to_display (aud_playlist_entry_get_filename (playlist, entry)));

        Tuple tuple = aud_playlist_entry_get_tuple (playlist, entry, Playlist::NoWait);
        item.title = tuple.get_str (Tuple::Title);
        item.artist = tuple.get_str (Tuple::Artist);
        item.album = tuple.get_str (Tuple::Album);
    }
}

/**
 * Searches 'keyword' inside 'playlist' by using 'cache' to speed up searching.
 *
 * Searches are basically conducted as follows:
 *
 * Cache is checked if it has the information about right playlist and
 * initialized with playlist data if needed.
 *
 * Keyword is normalized for searching (Unicode NFKD, case folding)
 *
 * Cache is checked if it has keyword and if it has, we can immediately get
 * the search results and return. If not, searching goes as follows:
 *
 * Search for the longest word that is in cache that matches the beginning
 * of keyword and use the cached matches as base for the current search.
 * The shortest word that can be matched against is the empty string "", so
 * there should always be matches in cache.
 *
 * After that conduct the search by splitting keyword into words separated
 * by space and using regular expressions.
 *
 * When the keyword is searched, search result is added to cache to
 * corresponding keyword that can be used as base for new searches.
 *
 * The motivation for caching is that to search word 'some cool song' one
 * has to type following strings that are all searched individually:
 *
 * s
 * so
 * som
 * some
 * some
 * some c
 * some co
 * some coo
 * some cool
 * some cool
 * some cool s
 * some cool so
 * some cool son
 * some cool song
 *
 * If the search results are cached in every phase and the result of
 * the maximum length matching string is used as base for concurrent
 * searches, we can probably get the matches reduced to some hundreds
 * after a few letters typed on playlists with thousands of songs and
 * reduce useless iteration quite a lot.
 *
 * Return: GArray of int
 */
const KeywordMatches * JumpToTrackCache::search (const char * keyword)
{
    if (! n_items ())
        init ();

    StringBuf match_string = str_copy (keyword);
    const KeywordMatches * matches;

    while (! (matches = lookup (String (match_string))))
    {
        // try to reuse the result of a previous search
        // (the empty string is always present as a fallback)
        assert (match_string[0]);
        match_string[strlen (match_string) - 1] = 0;
    }

    // exact match?
    if (! strcmp (match_string, keyword))
        return matches;

    // search within the previous result
    return search_within (matches, keyword);
}