Commit 99da03a6 authored by hirono's avatar hirono Committed by Commit bot

Files.app: Add model classes to manage cache states of new MetadataCache.

New MetadataCache enables us to load metadata property one by one.  The CL adds
MetadataCacheItem (for each FileEntry), MetadataCacheItemProperty (for each
property) to manage cache state of each property.

BUG=410766
TEST=FileManagerJsTest.MetadataCacheItemTest

Review URL: https://codereview.chromium.org/869923004

Cr-Commit-Position: refs/heads/master@{#314116}
parent 52a26e4c
...@@ -124,3 +124,8 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, Banners) { ...@@ -124,3 +124,8 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, Banners) {
RunTest(base::FilePath( RunTest(base::FilePath(
FILE_PATH_LITERAL("foreground/js/ui/banners_unittest.html"))); FILE_PATH_LITERAL("foreground/js/ui/banners_unittest.html")));
} }
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, MetadataCacheItem) {
RunTest(base::FilePath(FILE_PATH_LITERAL(
"foreground/js/metadata/metadata_cache_item_unittest.html")));
}
...@@ -86,6 +86,7 @@ ...@@ -86,6 +86,7 @@
'./import_controller.js', './import_controller.js',
'./launch_param.js', './launch_param.js',
'./metadata/metadata_cache.js', './metadata/metadata_cache.js',
'./metadata/metadata_cache_item.js',
'./metadata_update_controller.js', './metadata_update_controller.js',
'./naming_controller.js', './naming_controller.js',
'./navigation_list_model.js', './navigation_list_model.js',
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Cache of metadata for a FileEntry.
* @constructor
* @struct
*/
function MetadataCacheItem() {
/**
* Map of property name and MetadataCacheItemProperty.
* @private {!Object<string, !MetadataCacheItemProperty>}
* @const
*/
this.properties_ = {};
}
/**
* Marks invalidated properies as loading and returns property names that are
* need to be loaded.
* @param {number} requestId
* @param {!Array<string>} names
* @return {!Array<string>} Load requested names.
*/
MetadataCacheItem.prototype.startRequests = function(requestId, names) {
var loadRequested = [];
for (var i = 0; i < names.length; i++) {
var name = names[i];
// Check if the property needs to be updated.
if (this.properties_[name] &&
this.properties_[name].state !==
MetadataCacheItemPropertyState.INVALIDATED) {
continue;
}
if (!this.properties_[name])
this.properties_[name] = new MetadataCacheItemProperty();
this.properties_[name].requestId = requestId;
this.properties_[name].state = MetadataCacheItemPropertyState.LOADING;
loadRequested.push(name);
}
return loadRequested;
};
/**
* Feeds the result of startRequests.
* @param {number} requestId Request ID passed when calling startRequests.
* @param {!Object} object Map of property name and value.
* @return {boolean} Whether at least one property is updated or not.
*/
MetadataCacheItem.prototype.storeProperties = function(requestId, object) {
var changed = false;
for (var name in object) {
if (!this.properties_[name])
this.properties_[name] = new MetadataCacheItemProperty();
if (requestId < this.properties_[name].requestId)
continue;
changed = true;
this.properties_[name].requestId = requestId;
this.properties_[name].value = object[name];
this.properties_[name].state = MetadataCacheItemPropertyState.FULFILLED;
}
return changed;
};
/**
* Marks the caches of all properties in the item as invalidates and forces to
* reload at the next time of startRequests.
* @param {number} requestId Request ID of the invalidation request. This must
* be larger than other requets ID passed to the item before.
*/
MetadataCacheItem.prototype.invalidate = function(requestId) {
for (var name in this.properties_) {
assert(this.properties_[name].requestId < requestId);
this.properties_[name].requestId = requestId;
this.properties_[name].state = MetadataCacheItemPropertyState.INVALIDATED;
}
};
/**
* Obtains property for entries and names.
* Note that it returns invalidated properties also.
* @param {!Array<string>} names
* @return {!Object}
*/
MetadataCacheItem.prototype.get = function(names) {
var result = {};
for (var i = 0; i < names.length; i++) {
var name = names[i];
if (this.properties_[name])
result[name] = this.properties_[name].value;
}
return result;
};
/**
* @enum {string}
*/
var MetadataCacheItemPropertyState = {
INVALIDATED: 'invalidated',
LOADING: 'loading',
FULFILLED: 'fulfilled'
};
/**
* Cache of metadata for a property.
* @constructor
* @struct
*/
function MetadataCacheItemProperty() {
/**
* Cached value of property.
* @public {*}
*/
this.value = null;
/**
* Last request ID.
* @public {number}
*/
this.requestId = -1;
/**
* Cache state of the property.
* @public {MetadataCacheItemPropertyState}
*/
this.state = MetadataCacheItemPropertyState.INVALIDATED;
}
<!DOCTYPE html>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->
<script src="../../../../../../ui/webui/resources/js/assert.js"></script>
<script src="../../../common/js/unittest_util.js"></script>
<script src="metadata_cache_item.js"></script>
<script src="metadata_cache_item_unittest.js"></script>
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function testMetadataCacheItemBasic() {
var item = new MetadataCacheItem();
var loadRequested = item.startRequests(1, ['propertyA']);
assertEquals(1, loadRequested.length);
assertEquals('propertyA', loadRequested[0]);
assertTrue(item.storeProperties(1, {propertyA: 'value'}));
var result = item.get(['propertyA']);
assertEquals('value', result.propertyA);
}
function testMetadataCacheItemAvoidDoubleLoad() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
var loadRequested = item.startRequests(2, ['propertyA']);
assertEquals(0, loadRequested.length);
assertTrue(item.storeProperties(1, {propertyA: 'value'}));
var result = item.get(['propertyA']);
assertEquals('value', result.propertyA);
}
function testMetadataCacheItemInvalidate() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
item.invalidate(2);
assertFalse(item.storeProperties(1, {propertyA: 'value'}));
var loadRequested = item.startRequests(3, ['propertyA']);
assertEquals(1, loadRequested.length);
}
function testMetadataCacheItemStoreInReverseOrder() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
item.startRequests(2, ['propertyA']);
assertTrue(item.storeProperties(2, {propertyA: 'value2'}));
assertFalse(item.storeProperties(1, {propertyA: 'value1'}));
var result = item.get(['propertyA']);
assertEquals('value2', result.propertyA);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment