-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnail_cache.go
206 lines (151 loc) · 4.38 KB
/
thumbnail_cache.go
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
// Album thumbnail cache
package main
import "sync"
// Album thumbnail cache entry
type ThumbnailCacheEntry struct {
is_pending bool // True if pending
pending_wait_group *sync.WaitGroup // Group to wait for pending
invalid bool // True if invalid (marked when deleting, but pending)
has_thumbnail bool // Has thumbnail?
has_asset_thumbnail bool // Has asset thumbnail?
media uint64 // First media of the album
asset uint64 // Thumbnail asset
}
// Cache for album thumbnails
type ThumbnailCache struct {
mutex *sync.Mutex
cache map[uint64]*ThumbnailCacheEntry
}
// Creates a blank cache
func makeThumbnailCache() *ThumbnailCache {
res := ThumbnailCache{
mutex: &sync.Mutex{},
cache: make(map[uint64]*ThumbnailCacheEntry),
}
return &res
}
// Tries to get an entry. If not found, creates one, marks it as pending
func (c *ThumbnailCache) getEntryOrMarkAsPending(album uint64) (result_entry *ThumbnailCacheEntry, wait_group *sync.WaitGroup) {
c.mutex.Lock()
defer c.mutex.Unlock()
entry := c.cache[album]
var wg *sync.WaitGroup = nil
if entry == nil {
wg = &sync.WaitGroup{}
wg.Add(1)
// Mark as pending
c.cache[album] = &ThumbnailCacheEntry{
is_pending: true,
pending_wait_group: wg,
invalid: false,
has_thumbnail: false,
has_asset_thumbnail: false,
media: 0,
asset: 0,
}
}
return entry, wg
}
// Removes cache entry
func (c *ThumbnailCache) removeEntry(album uint64) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.cache, album)
}
// Removes cache entry, or marks as invalid if pending
func (c *ThumbnailCache) RemoveEntryOrMarkInvalid(album uint64) {
c.mutex.Lock()
defer c.mutex.Unlock()
entry := c.cache[album]
if entry == nil {
return
}
if entry.is_pending {
entry.invalid = true
return
}
delete(c.cache, album)
}
// Resolves cache entry
func (c *ThumbnailCache) resolveEntry(album uint64, has_thumbnail bool, media uint64, asset uint64, has_asset_thumbnail bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
entry := c.cache[album]
if entry == nil {
return
}
entry.is_pending = false
entry.has_thumbnail = has_thumbnail
entry.media = media
entry.asset = asset
entry.has_asset_thumbnail = has_asset_thumbnail
if entry.invalid {
delete(c.cache, album)
}
}
// Gets the album thumbnail asset from cache of the vault data
func (c *ThumbnailCache) GetAlbumThumbnail(album uint64, key []byte) (has_thumbnail bool, media uint64, asset uint64, has_asset_thumbnail bool) {
entry, wg := c.getEntryOrMarkAsPending(album)
if entry != nil {
c.mutex.Lock()
if entry.is_pending {
entry_waiter := entry.pending_wait_group
c.mutex.Unlock()
entry_waiter.Wait()
// We waited, now it's ready
c.mutex.Lock()
has_thumbnail_tmp := entry.has_thumbnail
media_tmp := entry.media
asset_tmp := entry.asset
is_asset_thumb := entry.has_asset_thumbnail
c.mutex.Unlock()
return has_thumbnail_tmp, media_tmp, asset_tmp, is_asset_thumb
} else {
has_thumbnail_tmp := entry.has_thumbnail
media_tmp := entry.media
asset_tmp := entry.asset
is_asset_thumb := entry.has_asset_thumbnail
c.mutex.Unlock()
return has_thumbnail_tmp, media_tmp, asset_tmp, is_asset_thumb
}
}
// There is no entry
// So it was marked as pending
// We have to fetch the data from the vault
defer wg.Done()
albumsData, err := GetVault().albums.readData(key)
if err != nil {
c.removeEntry(album)
return false, 0, 0, false
}
albumData := albumsData.Albums[album]
if albumData == nil {
c.removeEntry(album)
return false, 0, 0, false
}
if albumData.Thumbnail != nil {
c.resolveEntry(album, true, 0, *albumData.Thumbnail, true)
return true, 0, *albumData.Thumbnail, true
}
if len(albumData.List) == 0 {
// No thumbnail
c.resolveEntry(album, false, 0, 0, false)
return false, 0, 0, false
}
firstMediaId := albumData.List[0]
firstMedia := GetVault().media.AcquireMediaResource(firstMediaId)
meta, err := firstMedia.ReadMetadata(key)
GetVault().media.ReleaseMediaResource(firstMediaId)
if err != nil {
LogError(err)
}
if meta == nil {
// Not found media
c.removeEntry(album)
return false, 0, 0, false
}
has_thumbnail = meta.ThumbnailReady
thumbnail_asset := meta.ThumbnailAsset
c.resolveEntry(album, has_thumbnail, firstMediaId, thumbnail_asset, false)
return has_thumbnail, firstMediaId, thumbnail_asset, false
}