From 49211f84f706c3268ef26b2953d3fb737f213950 Mon Sep 17 00:00:00 2001 From: Thibaud Giovannetti Date: Thu, 25 Jan 2018 14:40:32 +0100 Subject: [PATCH] Improve cache error dispatching with delayError --- .../store2store/store/StoreService.java | 187 +++++++++--------- 1 file changed, 96 insertions(+), 91 deletions(-) diff --git a/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java b/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java index a1af04f..6a2fb73 100644 --- a/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java +++ b/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java @@ -102,68 +102,70 @@ public boolean isCache() { @Override public Flowable>> getAll(final Filter filter, final SortingMode sortingMode) { - List>>> flowables = new ArrayList<>(); Flowable>> flowStorage = dao.getAll(filter, sortingMode); - if (hasSyncedStore()) { - flowStorage = flowStorage - .flatMap(new Function>, Flowable>>>() { - @Override - public Flowable>> apply(Optional> items) throws Exception { - final List copy = new ArrayList<>(items.get()); - if (filter == null) { - // full replacement, we clean up the Store dao - return syncedStore.deleteAll().map(new Function>>() { - @Override - public Optional> apply(Integer integer) throws Exception { - return Optional.wrap(copy); - } - }); - } - return Flowable.just(Optional.wrap(copy)); - } - }) - .flatMap(new Function>, Flowable>>>() { - @Override - public Flowable>> apply(Optional> items) throws Exception { - return syncedStore.insertOrUpdate(items.get()); - } - }); - - if (syncedStore.isCache()) { - return flowStorage.startWith(syncedStore.getAll(filter, sortingMode)); - } - - flowables.add(syncedStore.getAll(filter, sortingMode)); + if (!hasSyncedStore()) { + return flowStorage; } + final List>>> flowables = new ArrayList<>(); + flowStorage = flowStorage + .flatMap(new Function>, Flowable>>>() { + @Override + public Flowable>> apply(Optional> items) throws Exception { + final List copy = new ArrayList<>(items.get()); + if (filter == null) { + // full replacement, we clean up the Store dao + return syncedStore.deleteAll().map(new Function>>() { + @Override + public Optional> apply(Integer integer) throws Exception { + return Optional.wrap(copy); + } + }); + } + return Flowable.just(Optional.wrap(copy)); + } + }) + .flatMap(new Function>, Flowable>>>() { + @Override + public Flowable>> apply(Optional> items) throws Exception { + return syncedStore.insertOrUpdate(items.get()); + } + }); + + flowables.add(syncedStore.getAll(filter, sortingMode)); flowables.add(flowStorage); - return Flowable.concat(flowables); + + if (syncedStore.isCache()) { + return Flowable.concatDelayError(flowables); + } else { + return Flowable.concat(flowables); + } } @Override public Flowable>> getAll(final List items) { - List>>> flowables = new ArrayList<>(); Flowable>> flowStorage = dao.getAll(items); - if (hasSyncedStore()) { - flowStorage = flowStorage - .flatMap(new Function>, Flowable>>>() { - @Override - public Flowable>> apply(Optional> items) throws Exception { - return syncedStore.insertOrUpdate(items.get()); - } - }); + if (!hasSyncedStore()) { + return flowStorage; + } - if (syncedStore.isCache()) { - return flowStorage.startWith(syncedStore.getAll(items)); + final List>>> flowables = new ArrayList<>(); + flowStorage = flowStorage.flatMap(new Function>, Flowable>>>() { + @Override + public Flowable>> apply(Optional> items) throws Exception { + return syncedStore.insertOrUpdate(items.get()); } + }); + flowables.add(syncedStore.getAll(items)); + flowables.add(flowStorage); - flowables.add(syncedStore.getAll(items)); + if (syncedStore.isCache()) { + return Flowable.concatDelayError(flowables); + } else { + return Flowable.concat(flowables); } - - flowables.add(flowStorage); - return Flowable.concat(flowables); } public final Flowable>> getAll(final Filter filter) { @@ -176,50 +178,52 @@ public final Flowable>> getAll() { @Override public Flowable> getOne(final Filter filter, final SortingMode sortingMode) { - List>> flowables = new ArrayList<>(); Flowable> flowStorage = dao.getOne(filter, sortingMode); - if (hasSyncedStore()) { - flowStorage = flowStorage - .flatMap(new Function, Flowable>>() { - @Override - public Flowable> apply(Optional item) throws Exception { - return syncedStore.insertOrUpdate(item.get()); - } - }); + if (!hasSyncedStore()) { + return flowStorage; + } - if (syncedStore.isCache()) { - return flowStorage.startWith(syncedStore.getOne(filter, sortingMode)); + final List>> flowables = new ArrayList<>(); + flowStorage = flowStorage.flatMap(new Function, Flowable>>() { + @Override + public Flowable> apply(Optional item) throws Exception { + return syncedStore.insertOrUpdate(item.get()); } + }); + flowables.add(syncedStore.getOne(filter, sortingMode)); + flowables.add(flowStorage); - flowables.add(syncedStore.getOne(filter, sortingMode)); + if (syncedStore.isCache()) { + return Flowable.concatDelayError(flowables); + } else { + return Flowable.concat(flowables); } - - flowables.add(flowStorage); - return Flowable.concat(flowables); } @Override public Flowable> getOne(final T item) { - List>> flowables = new ArrayList<>(); Flowable> flowStorage = dao.getOne(item); - if (hasSyncedStore()) { - flowStorage = flowStorage - .flatMap(new Function, Flowable>>() { - @Override - public Flowable> apply(Optional item) throws Exception { - return syncedStore.insertOrUpdate(item.get()); - } - }); - if (syncedStore.isCache()) { - return flowStorage.startWith(syncedStore.getOne(item)); - } - flowables.add(syncedStore.getOne(item)); + if (!hasSyncedStore()) { + return flowStorage; } + final List>> flowables = new ArrayList<>(); + flowStorage = flowStorage.flatMap(new Function, Flowable>>() { + @Override + public Flowable> apply(Optional item) throws Exception { + return syncedStore.insertOrUpdate(item.get()); + } + }); + flowables.add(syncedStore.getOne(item)); flowables.add(flowStorage); - return Flowable.concat(flowables); + + if (syncedStore.isCache()) { + return Flowable.concatDelayError(flowables); + } else { + return Flowable.concat(flowables); + } } public Flowable> getOne(final Filter filter) { @@ -236,25 +240,26 @@ public Flowable> getOne() { @Override public Flowable> getById(final int id) { - List>> flowables = new ArrayList<>(); Flowable> flowStorage = dao.getById(id); - - if (hasSyncedStore()) { - flowStorage = flowStorage - .flatMap(new Function, Flowable>>() { - @Override - public Flowable> apply(final Optional item) throws Exception { - return syncedStore.insertOrUpdate(item.get()); - } - }); - if (syncedStore.isCache()) { - return flowStorage.startWith(syncedStore.getById(id)); - } - flowables.add(syncedStore.getById(id)); + if (!hasSyncedStore()) { + return flowStorage; } + final List>> flowables = new ArrayList<>(); + flowStorage = flowStorage.flatMap(new Function, Flowable>>() { + @Override + public Flowable> apply(final Optional item) throws Exception { + return syncedStore.insertOrUpdate(item.get()); + } + }); + flowables.add(syncedStore.getById(id)); flowables.add(flowStorage); - return Flowable.concat(flowables); + + if (syncedStore.isCache()) { + return Flowable.concatDelayError(flowables); + } else { + return Flowable.concat(flowables); + } } @Override