OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 library index.file_page_manager; | |
6 | |
7 import 'dart:io'; | |
8 import 'dart:typed_data'; | |
9 | |
10 import 'package:analysis_server/src/index/page_node_manager.dart'; | |
11 | |
12 | |
13 /** | |
14 * A [PageManager] that stores pages on disk. | |
15 */ | |
16 class FilePageManager implements PageManager { | |
17 final int pageSizeInBytes; | |
18 | |
19 RandomAccessFile _file; | |
20 File _fileRef; | |
21 List<int> _freePagesList = new List<int>(); | |
22 Set<int> _freePagesSet = new Set<int>(); | |
23 int _nextPage = 0; | |
24 | |
25 FilePageManager(this.pageSizeInBytes, String path) { | |
26 _fileRef = new File(path); | |
27 _file = _fileRef.openSync(mode: FileMode.WRITE); | |
28 } | |
29 | |
30 @override | |
31 int alloc() { | |
32 if (_freePagesList.isNotEmpty) { | |
33 int id = _freePagesList.removeLast(); | |
34 _freePagesSet.remove(id); | |
35 return id; | |
36 } | |
37 int id = _nextPage++; | |
38 Uint8List page = new Uint8List(pageSizeInBytes); | |
39 _file.setPositionSync(id * pageSizeInBytes); | |
40 _file.writeFromSync(page); | |
41 return id; | |
42 } | |
43 | |
44 /** | |
45 * Closes this [FilePageManager]. | |
46 */ | |
47 void close() { | |
48 _file.closeSync(); | |
49 } | |
50 | |
51 /** | |
52 * Deletes the underlaying file. | |
53 */ | |
54 void delete() { | |
55 if (_fileRef.existsSync()) { | |
56 _fileRef.deleteSync(); | |
57 } | |
58 } | |
59 | |
60 @override | |
61 void free(int id) { | |
62 if (!_freePagesSet.add(id)) { | |
63 throw new StateError('Page $id has been already freed.'); | |
64 } | |
65 _freePagesList.add(id); | |
66 } | |
67 | |
68 @override | |
69 Uint8List read(int id) { | |
70 Uint8List page = new Uint8List(pageSizeInBytes); | |
71 _file.setPositionSync(id * pageSizeInBytes); | |
72 int actual = 0; | |
73 while (actual != page.length) { | |
74 actual += _file.readIntoSync(page, actual); | |
75 } | |
76 return page; | |
77 } | |
78 | |
79 @override | |
80 void write(int id, Uint8List page) { | |
81 _file.setPositionSync(id * pageSizeInBytes); | |
82 _file.writeFromSync(page); | |
83 } | |
84 } | |
OLD | NEW |