OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:collection'; | |
6 | |
7 /** | |
8 * Store of bytes associated with string keys. | |
9 * | |
10 * Note that associations are not guaranteed to be persistent. The value | |
11 * associated with a key can change or become `null` at any point of time. | |
Paul Berry
2016/10/23 10:39:19
s/of/in/
scheglov
2016/10/23 20:54:34
Done.
| |
12 */ | |
13 abstract class ByteStore { | |
Paul Berry
2016/10/23 10:39:19
Since this is a synchronous interface, it means th
scheglov
2016/10/23 20:54:33
I thought about this.
Maybe even global, LAN or c
Brian Wilkerson
2016/10/25 17:24:12
Those performance measures are consistent with Bob
| |
14 /** | |
15 * Return the bytes associated with the given [key]. | |
16 * Return `null` if the association does not exist. | |
17 */ | |
18 List<int> get(String key); | |
Paul Berry
2016/10/23 10:39:19
Since FileByteStore uses the key as a filename, we
scheglov
2016/10/23 20:54:33
Done.
In theory as long as keys are short enough,
| |
19 | |
20 /** | |
21 * Associate the given [bytes] with the [key]. | |
22 */ | |
23 void put(String key, List<int> bytes); | |
24 } | |
25 | |
26 /** | |
27 * [ByteStore] with LRU cache. | |
Paul Berry
2016/10/23 10:39:19
When I first read this comment it wasn't clear to
scheglov
2016/10/23 20:54:34
Done.
| |
28 */ | |
29 class MemoryCachingByteStore implements ByteStore { | |
30 final ByteStore store; | |
31 final int maxEntries; | |
Paul Berry
2016/10/23 10:39:19
As a future improvement, consider measuring the li
scheglov
2016/10/23 20:54:34
Agreed.
We could look at Google Guava for inspirat
| |
32 | |
33 final _map = <String, List<int>>{}; | |
34 final _keys = new LinkedHashSet<String>(); | |
35 | |
36 MemoryCachingByteStore(this.store, this.maxEntries); | |
37 | |
38 @override | |
39 List<int> get(String key) { | |
40 _keys.remove(key); | |
41 _keys.add(key); | |
42 _evict(); | |
43 return _map.putIfAbsent(key, () => store.get(key)); | |
44 } | |
45 | |
46 @override | |
47 void put(String key, List<int> bytes) { | |
Brian Wilkerson
2016/10/25 17:24:12
Is there a need to be able to remove items from th
scheglov
2016/10/25 18:38:45
AFAIK _evict() ensures that there is a limit on th
| |
48 store.put(key, bytes); | |
49 _map[key] = bytes; | |
50 _keys.add(key); | |
51 _evict(); | |
52 } | |
53 | |
54 void _evict() { | |
55 if (_keys.length > maxEntries) { | |
56 String key = _keys.first; | |
57 _keys.remove(key); | |
58 _map.remove(key); | |
59 } | |
60 } | |
61 } | |
OLD | NEW |