Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(692)

Unified Diff: pkg/analysis_server/lib/src/index/file_page_manager.dart

Issue 321953002: FilePageManager - disk based PageManager. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Improved tests. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/index/file_page_manager.dart
diff --git a/pkg/analysis_server/lib/src/index/file_page_manager.dart b/pkg/analysis_server/lib/src/index/file_page_manager.dart
new file mode 100644
index 0000000000000000000000000000000000000000..55c2c7563f0664fc8470dba76f4b81234860f009
--- /dev/null
+++ b/pkg/analysis_server/lib/src/index/file_page_manager.dart
@@ -0,0 +1,84 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library index.file_page_manager;
+
+import 'dart:io';
+import 'dart:typed_data';
+
+import 'package:analysis_server/src/index/page_node_manager.dart';
+
+
+/**
+ * A [PageManager] that stores pages on disk.
+ */
+class FilePageManager implements PageManager {
+ final int pageSizeInBytes;
+
+ RandomAccessFile _file;
+ File _fileRef;
+ List<int> _freePagesList = new List<int>();
+ Set<int> _freePagesSet = new Set<int>();
+ int _nextPage = 0;
+
+ FilePageManager(this.pageSizeInBytes, String path) {
+ _fileRef = new File(path);
+ _file = _fileRef.openSync(mode: FileMode.WRITE);
+ }
+
+ @override
+ int alloc() {
+ if (_freePagesList.isNotEmpty) {
+ int id = _freePagesList.removeLast();
+ _freePagesSet.remove(id);
+ return id;
+ }
+ int id = _nextPage++;
+ Uint8List page = new Uint8List(pageSizeInBytes);
+ _file.setPositionSync(id * pageSizeInBytes);
+ _file.writeFromSync(page);
+ return id;
+ }
+
+ /**
+ * Closes this [FilePageManager].
+ */
+ void close() {
+ _file.closeSync();
+ }
+
+ /**
+ * Deletes the underlaying file.
+ */
+ void delete() {
+ if (_fileRef.existsSync()) {
+ _fileRef.deleteSync();
+ }
+ }
+
+ @override
+ void free(int id) {
+ if (!_freePagesSet.add(id)) {
+ throw new StateError('Page $id has been already freed.');
+ }
+ _freePagesList.add(id);
+ }
+
+ @override
+ Uint8List read(int id) {
+ Uint8List page = new Uint8List(pageSizeInBytes);
+ _file.setPositionSync(id * pageSizeInBytes);
+ int actual = 0;
+ while (actual != page.length) {
+ actual += _file.readIntoSync(page, actual);
+ }
+ return page;
+ }
+
+ @override
+ void write(int id, Uint8List page) {
+ _file.setPositionSync(id * pageSizeInBytes);
+ _file.writeFromSync(page);
+ }
+}
« no previous file with comments | « pkg/analysis_server/lib/src/index/b_plus_tree.dart ('k') | pkg/analysis_server/lib/src/index/page_node_manager.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698