Merge branch 'app-paths' into android-test-plugins
[kugel-rb.git] / apps / tagcache.h
blobb52da76a0f264028efd635842598bcaf3ea10e60
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifdef HAVE_TAGCACHE
22 #ifndef _TAGCACHE_H
23 #define _TAGCACHE_H
25 #include "system.h"
26 #include "metadata.h"
28 /**
29 Note: When adding new tags, make sure to update index_entry_ec in
30 tagcache.c and bump up the header version too.
32 enum tag_type { tag_artist = 0, tag_album, tag_genre, tag_title,
33 tag_filename, tag_composer, tag_comment, tag_albumartist, tag_grouping, tag_year,
34 tag_discnumber, tag_tracknumber, tag_bitrate, tag_length, tag_playcount, tag_rating,
35 tag_playtime, tag_lastplayed, tag_commitid, tag_mtime,
36 /* Real tags end here, count them. */
37 TAG_COUNT,
38 /* Virtual tags */
39 tag_virt_length_min, tag_virt_length_sec,
40 tag_virt_playtime_min, tag_virt_playtime_sec,
41 tag_virt_entryage, tag_virt_autoscore };
43 /* Maximum length of a single tag. */
44 #define TAG_MAXLEN (MAX_PATH*2)
46 /* Allow a little drift to the filename ordering (should not be too high/low). */
47 #define POS_HISTORY_COUNT 4
49 /* How much to pre-load entries while committing to prevent seeking. */
50 #define IDX_BUF_DEPTH 64
52 /* Tag Cache Header version 'TCHxx'. Increment when changing internal structures. */
53 #define TAGCACHE_MAGIC 0x5443480d
55 /* How much to allocate extra space for ramcache. */
56 #define TAGCACHE_RESERVE 32768
58 /**
59 * Define how long one entry must be at least (longer -> less memory at commit).
60 * Must be at least 4 bytes in length for correct alignment.
62 #define TAGFILE_ENTRY_CHUNK_LENGTH 8
64 /* Used to guess the necessary buffer size at commit. */
65 #define TAGFILE_ENTRY_AVG_LENGTH 16
67 /* How many entries to fetch to the seek table at once while searching. */
68 #define SEEK_LIST_SIZE 32
70 /* Always strict align entries for best performance and binary compatibility. */
71 #define TAGCACHE_STRICT_ALIGN 1
73 /* Max events in the internal tagcache command queue. */
74 #define TAGCACHE_COMMAND_QUEUE_LENGTH 32
75 /* Idle time before committing events in the command queue. */
76 #define TAGCACHE_COMMAND_QUEUE_COMMIT_DELAY HZ*2
78 #define TAGCACHE_MAX_FILTERS 4
79 #define TAGCACHE_MAX_CLAUSES 32
81 /* Tag database files. */
83 /* Temporary database containing new tags to be committed to the main db. */
84 #define TAGCACHE_FILE_TEMP ROCKBOX_DIR "/database_tmp.tcd"
86 /* The main database master index and numeric data. */
87 #define TAGCACHE_FILE_MASTER ROCKBOX_DIR "/database_idx.tcd"
89 /* The main database string data. */
90 #define TAGCACHE_FILE_INDEX ROCKBOX_DIR "/database_%d.tcd"
92 /* ASCII dumpfile of the DB contents. */
93 #define TAGCACHE_FILE_CHANGELOG ROCKBOX_DIR "/database_changelog.txt"
95 /* Serialized DB. */
96 #define TAGCACHE_STATEFILE ROCKBOX_DIR "/database_state.tcd"
98 /* Tag to be used on untagged files. */
99 #define UNTAGGED "<Untagged>"
101 /* Numeric tags (we can use these tags with conditional clauses). */
102 #define TAGCACHE_NUMERIC_TAGS ((1LU << tag_year) | (1LU << tag_discnumber) | \
103 (1LU << tag_tracknumber) | (1LU << tag_length) | (1LU << tag_bitrate) | \
104 (1LU << tag_playcount) | (1LU << tag_rating) | (1LU << tag_playtime) | \
105 (1LU << tag_lastplayed) | (1LU << tag_commitid) | (1LU << tag_mtime) | \
106 (1LU << tag_virt_length_min) | (1LU << tag_virt_length_sec) | \
107 (1LU << tag_virt_playtime_min) | (1LU << tag_virt_playtime_sec) | \
108 (1LU << tag_virt_entryage) | (1LU << tag_virt_autoscore))
110 #define TAGCACHE_IS_NUMERIC(tag) (BIT_N(tag) & TAGCACHE_NUMERIC_TAGS)
112 /* Flags */
113 #define FLAG_DELETED 0x0001 /* Entry has been removed from db */
114 #define FLAG_DIRCACHE 0x0002 /* Filename is a dircache pointer */
115 #define FLAG_DIRTYNUM 0x0004 /* Numeric data has been modified */
116 #define FLAG_TRKNUMGEN 0x0008 /* Track number has been generated */
117 #define FLAG_RESURRECTED 0x0010 /* Statistics data has been resurrected */
119 enum clause { clause_none, clause_is, clause_is_not, clause_gt, clause_gteq,
120 clause_lt, clause_lteq, clause_contains, clause_not_contains,
121 clause_begins_with, clause_not_begins_with, clause_ends_with,
122 clause_not_ends_with, clause_oneof };
124 struct tagcache_stat {
125 bool initialized; /* Is tagcache currently busy? */
126 bool readyvalid; /* Has tagcache ready status been ascertained */
127 bool ready; /* Is tagcache ready to be used? */
128 bool ramcache; /* Is tagcache loaded in ram? */
129 bool commit_delayed; /* Has commit been delayed until next reboot? */
130 bool econ; /* Is endianess correction enabled? */
131 int commit_step; /* Commit progress */
132 int ramcache_allocated; /* Has ram been allocated for ramcache? */
133 int ramcache_used; /* How much ram has been really used */
134 int progress; /* Current progress of disk scan */
135 int processed_entries; /* Scanned disk entries so far */
136 int queue_length; /* Command queue length */
137 volatile const char
138 *curentry; /* Path of the current entry being scanned. */
139 volatile bool syncscreen;/* Synchronous operation with debug screen? */
140 // const char *uimessage; /* Pending error message. Implement soon. */
143 enum source_type {source_constant,
144 source_runtime,
145 source_current_path /* dont add items after this.
146 it is used as an index
147 into id3_to_search_mapping */
150 struct tagcache_search_clause
152 int tag;
153 int type;
154 bool numeric;
155 int source;
156 long numeric_data;
157 char *str;
160 struct tagcache_seeklist_entry {
161 int32_t seek;
162 int32_t flag;
163 int32_t idx_id;
166 struct tagcache_search {
167 /* For internal use only. */
168 int fd, masterfd;
169 int idxfd[TAG_COUNT];
170 struct tagcache_seeklist_entry seeklist[SEEK_LIST_SIZE];
171 int seek_list_count;
172 int32_t filter_tag[TAGCACHE_MAX_FILTERS];
173 int32_t filter_seek[TAGCACHE_MAX_FILTERS];
174 int filter_count;
175 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
176 int clause_count;
177 int list_position;
178 int seek_pos;
179 long position;
180 int entry_count;
181 bool valid;
182 bool initialized;
183 unsigned long *unique_list;
184 int unique_list_capacity;
185 int unique_list_count;
187 /* Exported variables. */
188 bool ramsearch; /* Is ram copy of the tagcache being used. */
189 bool ramresult; /* False if result is not static, and must be copied. */
190 int type;
191 char *result; /* The result data for all tags. */
192 int result_len; /* Length of the result including \0 */
193 int32_t result_seek; /* Current position in the tag data. */
194 int32_t idx_id; /* Entry number in the master index. */
197 void tagcache_build(const char *path);
199 #ifdef __PCTOOL__
200 void tagcache_reverse_scan(void);
201 #endif
203 const char* tagcache_tag_to_str(int tag);
205 #ifdef CPU_SH
206 bool tagcache_is_numeric_tag(int type);
207 #endif
208 bool tagcache_find_index(struct tagcache_search *tcs, const char *filename);
209 bool tagcache_check_clauses(struct tagcache_search *tcs,
210 struct tagcache_search_clause **clause, int count);
211 bool tagcache_is_busy(void);
212 bool tagcache_search(struct tagcache_search *tcs, int tag);
213 void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
214 void *buffer, long length);
215 bool tagcache_search_add_filter(struct tagcache_search *tcs,
216 int tag, int seek);
217 bool tagcache_search_add_clause(struct tagcache_search *tcs,
218 struct tagcache_search_clause *clause);
219 bool tagcache_get_next(struct tagcache_search *tcs);
220 bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
221 int tag, char *buf, long size);
222 void tagcache_search_finish(struct tagcache_search *tcs);
223 long tagcache_get_numeric(const struct tagcache_search *tcs, int tag);
224 long tagcache_increase_serial(void);
225 long tagcache_get_serial(void);
226 bool tagcache_import_changelog(void);
227 bool tagcache_create_changelog(struct tagcache_search *tcs);
228 void tagcache_update_numeric(int idx_id, int tag, long data);
229 bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
230 int tag, long data);
232 struct tagcache_stat* tagcache_get_stat(void);
233 int tagcache_get_commit_step(void);
234 bool tagcache_prepare_shutdown(void);
235 void tagcache_shutdown(void);
237 void tagcache_screensync_event(void);
238 void tagcache_screensync_enable(bool state);
240 #ifdef HAVE_TC_RAMCACHE
241 bool tagcache_is_ramcache(void);
242 #ifdef HAVE_DIRCACHE
243 bool tagcache_fill_tags(struct mp3entry *id3, const char *filename);
244 #endif
245 void tagcache_unload_ramcache(void);
246 #endif
247 void tagcache_init(void) INIT_ATTR;
248 bool tagcache_is_initialized(void);
249 bool tagcache_is_usable(void);
250 void tagcache_start_scan(void);
251 void tagcache_stop_scan(void);
252 bool tagcache_update(void);
253 bool tagcache_rebuild(void);
254 int tagcache_get_max_commit_step(void);
255 #endif
256 #endif