aboutsummaryrefslogtreecommitdiffstats
path: root/source/net/net_store.h
blob: d329ab4c596fc0030238d99334a60378df262d57 (plain) (blame)
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
#ifndef NET_STORE_H
#define NET_STORE_H

// NOTE: on disk audio cache + the download worker pool.
//
// files live at <data_dir>/audio/<qari relative_path><sss>.mp3, while downloading they are
// <...>.mp3.part and get renamed when complete. downloads resume from an existing .part.
//
// the decoder can play a track while it is still downloading: it reads up to bytes_done and
// parks on store->progress until more arrives (see audio_decode.cpp). so pressing play on a
// remote track starts within one network round trip instead of after the whole file.

typedef enum TrackState {
	TRACK_REMOTE = 0,  // not on disk
	TRACK_PARTIAL,     // .part on disk, not being downloaded
	TRACK_QUEUED,
	TRACK_DOWNLOADING,
	TRACK_DONE,
	TRACK_FAILED,
} TrackState;

// NOTE: the state lives in its own array (TrackStore.states, one byte per track): "is it on disk"
// scans then touch 20KB instead of 640KB. the rest is per download progress, written by one
// worker at a time and network bound.
typedef struct TrackEntry TrackEntry;
struct TrackEntry {
	u32 padding0;
	u32 http_status;
	u64 bytes_done;
	u64 bytes_total; // 0 until the response headers arrived
	u32 retries;
	u32 padding;
};

#define STORE_MAX_WORKERS 16

typedef struct DownloadWorker DownloadWorker;
struct DownloadWorker {
	CacheAligned struct TrackStore *store;
	Thread thread;
	u32    index;
	u32    current_key;
	u64    last_wake_ns;
	u64    bytes_since_wake;
};

typedef struct TrackStore TrackStore;
struct TrackStore {
	Catalog    *catalog;
	String8     audio_dir;
	TrackEntry *entries;
	u8         *states;      // TrackState per key
	u32         entry_count;
	// NOTE: per qari "on disk" summary, kept current at every transition into / out of DONE so the
	// ui never has to scan the table for it
	u32        *qari_done;
	u64        *qari_done_bytes;
	u8         *qari_scanned;

	CacheAligned Signal progress; // bumped whenever bytes land or a track changes state

	JobQueue queue;

	DownloadWorker workers[STORE_MAX_WORKERS];
	u32            worker_count;

	CacheAligned u64 bytes_downloaded; // total this session, for the speed readout
	u32 active;
	u32 completed;
	u32 failed;
	b32 quiet; // no ui wakeups (sync mode)
};

internal void        store_init(Arena *arena, TrackStore *store, Catalog *catalog, String8 data_dir);
internal void        store_start_workers(Arena *arena, TrackStore *store, u32 worker_count);
internal void        store_stop_workers(TrackStore *store);
internal void        store_scan_qari(TrackStore *store, u32 qari_index);
internal void        store_scan_disk(TrackStore *store);              // every qari, via readdir
internal b32         store_delete(TrackStore *store, u32 key);        // remove the local file
internal u32         store_disk_count(TrackStore *store, u32 qari_index);
internal b32         store_on_disk(TrackStore *store, u32 key);
internal b32         store_request(TrackStore *store, u32 key, b32 urgent);
internal u32         store_request_qari(TrackStore *store, u32 qari_index);
internal TrackEntry *store_entry(TrackStore *store, u32 key);
internal void        store_set_state(TrackStore *store, u32 key, TrackState state);
#define TrackStatePtr(store, entry) (&(store)->states[(entry) - (store)->entries])
internal u32         store_track_path(TrackStore *store, u32 key, b32 partial, char *out, u32 out_size);

#endif // NET_STORE_H