mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
Merge remote-tracking branch 'remotes/upstreamatmire/Search-optimizations' into mydspace
# Conflicts: # resources/i18n/en.json # src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts # src/app/+search-page/search-filters/search-filter/search-filter.component.ts # src/app/+search-page/search-filters/search-filters.component.html # src/app/+search-page/search-filters/search-filters.component.ts # src/app/+search-page/search-page.component.ts # src/app/+search-page/search-page.module.ts # src/app/+search-page/search-service/search.service.ts # src/app/core/cache/builders/remote-data-build.service.ts # src/app/core/core.module.ts # src/app/core/data/paginated-list.ts # src/app/core/data/request.models.ts # src/app/core/data/request.service.spec.ts # src/app/core/data/request.service.ts # src/app/shared/services/route.service.ts # src/app/shared/shared.module.ts
This commit is contained in:
@@ -5,22 +5,79 @@ import { merge as observableMerge, Observable, of as observableOf, race as obser
|
||||
import { filter, find, first, map, mergeMap, switchMap, take } from 'rxjs/operators';
|
||||
import { remove } from 'lodash';
|
||||
|
||||
import { AppState } from '../../app.reducer';
|
||||
import { hasNoValue, hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util';
|
||||
import { CacheableObject } from '../cache/object-cache.reducer';
|
||||
import { ObjectCacheService } from '../cache/object-cache.service';
|
||||
import { DSOSuccessResponse, RestResponse } from '../cache/response.models';
|
||||
import { coreSelector, CoreState } from '../core.reducers';
|
||||
import { IndexName, IndexState } from '../index/index.reducer';
|
||||
import { pathSelector } from '../shared/selectors';
|
||||
import { CoreState } from '../core.reducers';
|
||||
import { IndexName, IndexState, MetaIndexState } from '../index/index.reducer';
|
||||
import {
|
||||
originalRequestUUIDFromRequestUUIDSelector,
|
||||
requestIndexSelector,
|
||||
uuidFromHrefSelector
|
||||
} from '../index/index.selectors';
|
||||
import { UUIDService } from '../shared/uuid.service';
|
||||
import { RequestConfigureAction, RequestExecuteAction, RequestRemoveAction } from './request.actions';
|
||||
import {
|
||||
RequestConfigureAction,
|
||||
RequestExecuteAction,
|
||||
RequestRemoveAction
|
||||
} from './request.actions';
|
||||
import { GetRequest, RestRequest } from './request.models';
|
||||
import { RequestEntry } from './request.reducer';
|
||||
import { RequestEntry, RequestState } from './request.reducer';
|
||||
import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
|
||||
import { RestRequestMethod } from './rest-request-method';
|
||||
import { getResponseFromEntry } from '../shared/operators';
|
||||
import { AddToIndexAction, RemoveFromIndexBySubstringAction } from '../index/index.actions';
|
||||
import { coreSelector } from '../core.selectors';
|
||||
|
||||
/**
|
||||
* The base selector function to select the request state in the store
|
||||
*/
|
||||
const requestCacheSelector = createSelector(
|
||||
coreSelector,
|
||||
(state: CoreState) => state['data/request']
|
||||
);
|
||||
|
||||
/**
|
||||
* Selector function to select a request entry by uuid from the cache
|
||||
* @param uuid The uuid of the request
|
||||
*/
|
||||
const entryFromUUIDSelector = (uuid: string): MemoizedSelector<CoreState, RequestEntry> => createSelector(
|
||||
requestCacheSelector,
|
||||
(state: RequestState) => {
|
||||
return hasValue(state) ? state[uuid] : undefined;
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Create a selector that fetches a list of request UUIDs from a given index substate of which the request href
|
||||
* contains a given substring
|
||||
* @param selector MemoizedSelector to start from
|
||||
* @param name The name of the index substate we're fetching request UUIDs from
|
||||
* @param href Substring that the request's href should contain
|
||||
*/
|
||||
const uuidsFromHrefSubstringSelector =
|
||||
(selector: MemoizedSelector<AppState, IndexState>, href: string): MemoizedSelector<AppState, string[]> => createSelector(
|
||||
selector,
|
||||
(state: IndexState) => getUuidsFromHrefSubstring(state, href)
|
||||
);
|
||||
|
||||
/**
|
||||
* Fetch a list of request UUIDs from a given index substate of which the request href contains a given substring
|
||||
* @param state The IndexState
|
||||
* @param href Substring that the request's href should contain
|
||||
*/
|
||||
const getUuidsFromHrefSubstring = (state: IndexState, href: string): string[] => {
|
||||
let result = [];
|
||||
if (isNotEmpty(state)) {
|
||||
result = Object.values(state)
|
||||
.filter((value: string) => value.startsWith(href));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* A service to interact with the request state in the store
|
||||
*/
|
||||
@Injectable()
|
||||
export class RequestService {
|
||||
private requestsOnTheirWayToTheStore: string[] = [];
|
||||
@@ -28,57 +85,16 @@ export class RequestService {
|
||||
constructor(private objectCache: ObjectCacheService,
|
||||
private uuidService: UUIDService,
|
||||
private store: Store<CoreState>,
|
||||
private indexStore: Store<IndexState>) {
|
||||
}
|
||||
|
||||
private entryFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, RequestEntry> {
|
||||
return pathSelector<CoreState, RequestEntry>(coreSelector, 'data/request', uuid);
|
||||
}
|
||||
|
||||
private uuidFromHrefSelector(href: string): MemoizedSelector<CoreState, string> {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.REQUEST, href);
|
||||
}
|
||||
|
||||
private originalUUIDFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, string> {
|
||||
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a selector that fetches a list of request UUIDs from a given index substate of which the request href
|
||||
* contains a given substring
|
||||
* @param selector MemoizedSelector to start from
|
||||
* @param name The name of the index substate we're fetching request UUIDs from
|
||||
* @param href Substring that the request's href should contain
|
||||
*/
|
||||
private uuidsFromHrefSubstringSelector(selector: MemoizedSelector<any, IndexState>, name: string, href: string): MemoizedSelector<any, string[]> {
|
||||
return createSelector(selector, (state: IndexState) => this.getUuidsFromHrefSubstring(state, name, href));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a list of request UUIDs from a given index substate of which the request href contains a given substring
|
||||
* @param state The IndexState
|
||||
* @param name The name of the index substate we're fetching request UUIDs from
|
||||
* @param href Substring that the request's href should contain
|
||||
*/
|
||||
private getUuidsFromHrefSubstring(state: IndexState, name: string, href: string): string[] {
|
||||
let result = [];
|
||||
if (isNotEmpty(state)) {
|
||||
const subState = state[name];
|
||||
if (isNotEmpty(subState)) {
|
||||
for (const value in subState) {
|
||||
if (value.indexOf(href) > -1) {
|
||||
result = [...result, subState[value]];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
private indexStore: Store<MetaIndexState>) {
|
||||
}
|
||||
|
||||
generateRequestId(): string {
|
||||
return `client/${this.uuidService.generate()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a request is currently pending
|
||||
*/
|
||||
isPending(request: GetRequest): boolean {
|
||||
// first check requests that haven't made it to the store yet
|
||||
if (this.requestsOnTheirWayToTheStore.includes(request.href)) {
|
||||
@@ -92,25 +108,30 @@ export class RequestService {
|
||||
.subscribe((re: RequestEntry) => {
|
||||
isPending = (hasValue(re) && !re.completed)
|
||||
});
|
||||
|
||||
return isPending;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a RequestEntry based on their uuid
|
||||
*/
|
||||
getByUUID(uuid: string): Observable<RequestEntry> {
|
||||
return observableRace(
|
||||
this.store.pipe(select(this.entryFromUUIDSelector(uuid))),
|
||||
this.store.pipe(select(entryFromUUIDSelector(uuid))),
|
||||
this.store.pipe(
|
||||
select(this.originalUUIDFromUUIDSelector(uuid)),
|
||||
select(originalRequestUUIDFromRequestUUIDSelector(uuid)),
|
||||
mergeMap((originalUUID) => {
|
||||
return this.store.pipe(select(this.entryFromUUIDSelector(originalUUID)))
|
||||
return this.store.pipe(select(entryFromUUIDSelector(originalUUID)))
|
||||
},
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a RequestEntry based on their href
|
||||
*/
|
||||
getByHref(href: string): Observable<RequestEntry> {
|
||||
return this.store.pipe(
|
||||
select(this.uuidFromHrefSelector(href)),
|
||||
select(uuidFromHrefSelector(href)),
|
||||
mergeMap((uuid: string) => this.getByUUID(uuid))
|
||||
);
|
||||
}
|
||||
@@ -173,7 +194,7 @@ export class RequestService {
|
||||
*/
|
||||
removeByHrefSubstring(href: string) {
|
||||
this.store.pipe(
|
||||
select(this.uuidsFromHrefSubstringSelector(pathSelector<CoreState, IndexState>(coreSelector, 'index'), IndexName.REQUEST, href)),
|
||||
select(uuidsFromHrefSubstringSelector(requestIndexSelector, href)),
|
||||
take(1)
|
||||
).subscribe((uuids: string[]) => {
|
||||
for (const uuid of uuids) {
|
||||
@@ -197,31 +218,11 @@ export class RequestService {
|
||||
* @param {GetRequest} request The request to check
|
||||
* @returns {boolean} True if the request is cached or still pending
|
||||
*/
|
||||
private isCachedOrPending(request: GetRequest) {
|
||||
let isCached = this.objectCache.hasBySelfLink(request.href);
|
||||
if (isCached) {
|
||||
const responses: Observable<RestResponse> = this.isReusable(request.uuid).pipe(
|
||||
filter((reusable: boolean) => reusable),
|
||||
switchMap(() => {
|
||||
return this.getByHref(request.href).pipe(
|
||||
getResponseFromEntry(),
|
||||
take(1)
|
||||
);
|
||||
}
|
||||
));
|
||||
private isCachedOrPending(request: GetRequest): boolean {
|
||||
const inReqCache = this.hasByHref(request.href);
|
||||
const inObjCache = this.objectCache.hasBySelfLink(request.href);
|
||||
const isCached = inReqCache || inObjCache;
|
||||
|
||||
const errorResponses = responses.pipe(filter((response) => !response.isSuccessful), map(() => true)); // TODO add a configurable number of retries in case of an error.
|
||||
const dsoSuccessResponses = responses.pipe(
|
||||
filter((response) => response.isSuccessful && hasValue((response as DSOSuccessResponse).resourceSelfLinks)),
|
||||
map((response: DSOSuccessResponse) => response.resourceSelfLinks),
|
||||
map((resourceSelfLinks: string[]) => resourceSelfLinks
|
||||
.every((selfLink) => this.objectCache.hasBySelfLink(selfLink))
|
||||
));
|
||||
|
||||
const otherSuccessResponses = responses.pipe(filter((response) => response.isSuccessful && !hasValue((response as DSOSuccessResponse).resourceSelfLinks)), map(() => true));
|
||||
|
||||
observableMerge(errorResponses, otherSuccessResponses, dsoSuccessResponses).subscribe((c) => isCached = c);
|
||||
}
|
||||
const isPending = this.isPending(request);
|
||||
return isCached || isPending;
|
||||
}
|
||||
@@ -244,7 +245,7 @@ export class RequestService {
|
||||
*/
|
||||
private trackRequestsOnTheirWayToTheStore(request: GetRequest) {
|
||||
this.requestsOnTheirWayToTheStore = [...this.requestsOnTheirWayToTheStore, request.href];
|
||||
this.store.pipe(select(this.entryFromUUIDSelector(request.href)),
|
||||
this.getByHref(request.href).pipe(
|
||||
filter((re: RequestEntry) => hasValue(re)),
|
||||
take(1)
|
||||
).subscribe((re: RequestEntry) => {
|
||||
@@ -274,31 +275,39 @@ export class RequestService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a Response should still be cached
|
||||
* Check whether a cached response should still be valid
|
||||
*
|
||||
* @param uuid
|
||||
* the uuid of the entry to check
|
||||
* @param entry
|
||||
* the entry to check
|
||||
* @return boolean
|
||||
* false if the uuid has no value, no entry could be found, the response was nog successful or its time to
|
||||
* live has exceeded, true otherwise
|
||||
* false if the uuid has no value, the response was not successful or its time to
|
||||
* live was exceeded, true otherwise
|
||||
*/
|
||||
private isReusable(uuid: string): Observable<boolean> {
|
||||
if (hasNoValue(uuid)) {
|
||||
return observableOf(false);
|
||||
private isValid(entry: RequestEntry): boolean {
|
||||
if (hasValue(entry) && entry.completed && entry.response.isSuccessful) {
|
||||
const timeOutdated = entry.response.timeAdded + entry.request.responseMsToLive;
|
||||
const isOutDated = new Date().getTime() > timeOutdated;
|
||||
return !isOutDated;
|
||||
} else {
|
||||
const requestEntry$ = this.getByUUID(uuid);
|
||||
return requestEntry$.pipe(
|
||||
filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
|
||||
map((entry: RequestEntry) => {
|
||||
if (hasValue(entry) && entry.response.isSuccessful) {
|
||||
const timeOutdated = entry.response.timeAdded + entry.request.responseMsToLive;
|
||||
const isOutDated = new Date().getTime() > timeOutdated;
|
||||
return !isOutDated;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the request with the specified href is cached
|
||||
*
|
||||
* @param href
|
||||
* The link of the request to check
|
||||
* @return boolean
|
||||
* true if the request with the specified href is cached,
|
||||
* false otherwise
|
||||
*/
|
||||
hasByHref(href: string): boolean {
|
||||
let result = false;
|
||||
this.getByHref(href).pipe(
|
||||
take(1)
|
||||
).subscribe((requestEntry: RequestEntry) => result = this.isValid(requestEntry));
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user