Commit 322fd48b authored by Daichi Hirono's avatar Daichi Hirono

Files.app: Split the startRequests method of metadata models into createRequests and startRequests.

Currently the startRequests method of metadata models has a responsibility to
create requests as well as to mark the cache as loading.

In the following patch, we need to apply the requests that are generated by
another cache set. Thus the method should be split into two methods (one is for
creating requests and another is for marking cache as loading).

BUG=410766
TEST=FileManagerJsTest.MetadataCacheSet,FileManagerJsTest.MetadataCacheItem
R=yawano@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#314270}
parent f400ce53
......@@ -17,13 +17,11 @@ function MetadataCacheItem() {
}
/**
* Marks invalidated properies as loading and returns property names that are
* need to be loaded.
* @param {number} requestId
* Creates requested names that need to be loaded.
* @param {!Array<string>} names
* @return {!Array<string>} Load requested names.
* @return {!Array<string>} Property names that need to be loaded.
*/
MetadataCacheItem.prototype.startRequests = function(requestId, names) {
MetadataCacheItem.prototype.createRequests = function(names) {
var loadRequested = [];
for (var i = 0; i < names.length; i++) {
var name = names[i];
......@@ -33,13 +31,24 @@ MetadataCacheItem.prototype.startRequests = function(requestId, names) {
MetadataCacheItemPropertyState.INVALIDATED) {
continue;
}
loadRequested.push(name);
}
return loadRequested;
};
/**
* Marks the given properies as loading.
* @param {number} requestId
* @param {!Array<string>} names
*/
MetadataCacheItem.prototype.startRequests = function(requestId, names) {
for (var i = 0; i < names.length; i++) {
var name = names[i];
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;
};
/**
......
......@@ -4,10 +4,11 @@
function testMetadataCacheItemBasic() {
var item = new MetadataCacheItem();
var loadRequested = item.startRequests(1, ['propertyA']);
var loadRequested = item.createRequests(['propertyA']);
assertEquals(1, loadRequested.length);
assertEquals('propertyA', loadRequested[0]);
item.startRequests(1, loadRequested);
assertTrue(item.storeProperties(1, {propertyA: 'value'}));
var result = item.get(['propertyA']);
......@@ -17,9 +18,10 @@ function testMetadataCacheItemBasic() {
function testMetadataCacheItemAvoidDoubleLoad() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
var loadRequested = item.startRequests(2, ['propertyA']);
var loadRequested = item.createRequests(['propertyA']);
assertEquals(0, loadRequested.length);
item.startRequests(2, loadRequested);
assertTrue(item.storeProperties(1, {propertyA: 'value'}));
var result = item.get(['propertyA']);
......@@ -28,18 +30,18 @@ function testMetadataCacheItemAvoidDoubleLoad() {
function testMetadataCacheItemInvalidate() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
item.startRequests(1, item.createRequests(['propertyA']));
item.invalidate(2);
assertFalse(item.storeProperties(1, {propertyA: 'value'}));
var loadRequested = item.startRequests(3, ['propertyA']);
var loadRequested = item.createRequests(['propertyA']);
assertEquals(1, loadRequested.length);
}
function testMetadataCacheItemStoreInReverseOrder() {
var item = new MetadataCacheItem();
item.startRequests(1, ['propertyA']);
item.startRequests(2, ['propertyA']);
item.startRequests(1, item.createRequests(['propertyA']));
item.startRequests(2, item.createRequests(['propertyA']));
assertTrue(item.storeProperties(2, {propertyA: 'value2'}));
assertFalse(item.storeProperties(1, {propertyA: 'value1'}));
......
......@@ -7,7 +7,7 @@
* @param {!MetadataCacheSetStorage} items Storage object containing
* MetadataCacheItem.
* @constructor
* @const
* @struct
*/
function MetadataCacheSet(items) {
/**
......@@ -18,26 +18,38 @@ function MetadataCacheSet(items) {
}
/**
* Starts requests for invalidated properties.
* @param {number} requestId
* Creates list of MetadataRequest based on the cache state.
* @param {!Array<!FileEntry>} entries
* @param {!Array<string>} names
* @return {!Array<!MetadataRequest>} Requests to be passed NewMetadataProvider.
* @return {!Array<!MetadataRequest>}
*/
MetadataCacheSet.prototype.startRequests = function(requestId, entries, names) {
MetadataCacheSet.prototype.createRequests = function(entries, names) {
var requests = [];
for (var i = 0; i < entries.length; i++) {
var url = entries[i].toURL();
var item = this.items_.get(url);
var item = this.items_.peek(entries[i].toURL());
var requestedNames = item ? item.createRequests(names) : names;
if (requestedNames.length)
requests.push(new MetadataRequest(entries[i], requestedNames));
}
return requests;
};
/**
* Updates cache states to start the given requests.
* @param {number} requestId
* @param {!Array<!MetadataRequest>} requests
*/
MetadataCacheSet.prototype.startRequests = function(requestId, requests) {
for (var i = 0; i < requests.length; i++) {
var request = requests[i];
var url = request.entry.toURL();
var item = this.items_.peek(url);
if (!item) {
item = new MetadataCacheItem();
this.items_.put(url, item);
}
var loadRequested = item.startRequests(requestId, names);
if (loadRequested.length)
requests.push(new MetadataRequest(entries[i], loadRequested));
item.startRequests(requestId, request.names);
}
return requests;
};
/**
......@@ -131,7 +143,7 @@ function MetadataCacheSetStorage() {
/**
* Returns an item corresponding to the given URL.
* @param {string} url Entry URL.
* @return {!MetadataCacheItem}
* @return {MetadataCacheItem}
*/
MetadataCacheSetStorage.prototype.get = function(url) {};
......@@ -139,7 +151,7 @@ MetadataCacheSetStorage.prototype.get = function(url) {};
* Returns an item corresponding to the given URL without changing orders in
* the cache list.
* @param {string} url Entry URL.
* @return {!MetadataCacheItem}
* @return {MetadataCacheItem}
*/
MetadataCacheSetStorage.prototype.peek = function(url) {};
......
......@@ -12,7 +12,7 @@ var entryB = {
function testMetadataCacheSetBasic() {
var set = new MetadataCacheSet(new MetadataCacheSetStorageForObject({}));
var loadRequested = set.startRequests(1, [entryA, entryB], ['property']);
var loadRequested = set.createRequests([entryA, entryB], ['property']);
assertEquals(2, loadRequested.length);
assertEquals(entryA, loadRequested[0].entry);
assertEquals(1, loadRequested[0].names.length);
......@@ -21,6 +21,7 @@ function testMetadataCacheSetBasic() {
assertEquals(1, loadRequested[1].names.length);
assertEquals('property', loadRequested[1].names[0]);
set.startRequests(1, loadRequested);
assertTrue(set.storeProperties(
1, [entryA, entryB], [{property: 'valueA'}, {property: 'valueB'}]));
......@@ -32,7 +33,7 @@ function testMetadataCacheSetBasic() {
function testMetadataCacheSetStorePartial() {
var set = new MetadataCacheSet(new MetadataCacheSetStorageForObject({}));
set.startRequests(1, [entryA, entryB], ['property']);
set.startRequests(1, set.createRequests([entryA, entryB], ['property']));
assertTrue(set.storeProperties(
1, [entryA], [{property: 'valueA'}]));
......@@ -51,11 +52,11 @@ function testMetadataCacheSetStorePartial() {
function testMetadataCacheSetCachePartial() {
var set = new MetadataCacheSet(new MetadataCacheSetStorageForObject({}));
set.startRequests(1, [entryA], ['property']);
set.startRequests(1, set.createRequests([entryA], ['property']));
set.storeProperties(1, [entryA], [{property: 'valueA'}]);
// entryA has already been cached.
var loadRequested = set.startRequests(2, [entryA, entryB], ['property']);
var loadRequested = set.createRequests([entryA, entryB], ['property']);
assertEquals(1, loadRequested.length);
assertEquals(entryB, loadRequested[0].entry);
assertEquals(1, loadRequested[0].names.length);
......@@ -64,7 +65,7 @@ function testMetadataCacheSetCachePartial() {
function testMetadataCacheSetInvalidatePartial() {
var set = new MetadataCacheSet(new MetadataCacheSetStorageForObject({}));
set.startRequests(1, [entryA, entryB], ['property']);
set.startRequests(1, set.createRequests([entryA, entryB], ['property']));
set.invalidate(2, [entryA]);
assertTrue(set.storeProperties(
......@@ -75,7 +76,7 @@ function testMetadataCacheSetInvalidatePartial() {
assertEquals(null, results[0].property);
assertEquals('valueB', results[1].property);
var loadRequested = set.startRequests(3, [entryA, entryB], ['property']);
var loadRequested = set.createRequests([entryA, entryB], ['property']);
assertEquals(1, loadRequested.length);
assertEquals(entryA, loadRequested[0].entry);
assertEquals(1, loadRequested[0].names.length);
......
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