Skip to content

Commit

Permalink
Fix Datastore pageable return type compatibility (#569)
Browse files Browse the repository at this point in the history
Fix Datastore pageable return type compatibility; add placeholders for deleteAllById in repositories
  • Loading branch information
elefeint committed Aug 10, 2021
1 parent ebef98a commit 197a8c3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
/**
* A pageable implementation for Cloud Datastore that uses the cursor for efficient reads.
*
* The static methods can take either paged or unpaged {@link Pageable}, while instance methods only deal with a paged
* self object.
*
* @author Dmitry Solomakha
* @author Chengyuan Zhao
*/
Expand All @@ -42,10 +45,26 @@ private DatastorePageable(Pageable pageable, String urlSafeCursor, Long totalCou
this(pageable, cursor.toUrlSafe(), totalCount);
}

/**
* Creates a {@link DatastorePageable} wrapper for a paged request, but passes unpaged requests back unchanged.
*
* @param pageable The source {@link Pageable} that can be paged or unpaged
* @param cursor Current cursor; null if not applicable
* @param totalCount Total result count
* @return an instance of {@link DatastorePageable} or the original unpaged {@link Pageable}.
*/
public static Pageable from(Pageable pageable, Cursor cursor, Long totalCount) {
return from(pageable, cursor == null ? null : cursor.toUrlSafe(), totalCount);
}

/**
* Creates a {@link DatastorePageable} wrapper for a paged request, but passes unpaged requests back unchanged.
*
* @param pageable The source {@link Pageable} that can be paged or unpaged
* @param urlSafeCursor Current cursor as ; null if not applicable
* @param totalCount Current cursor; null if not applicable
* @return an instance of {@link DatastorePageable} or the original unpaged {@link Pageable}.
*/
public static Pageable from(Pageable pageable, String urlSafeCursor, Long totalCount) {
if (pageable.isUnpaged()) {
return pageable;
Expand All @@ -58,8 +77,10 @@ public String getUrlSafeCursor() {
}

@Override
public Pageable next() {
return from(super.next(), this.urlSafeCursor, this.totalCount);
public PageRequest next() {
Pageable nextPage = PageRequest.of(getPageNumber() + 1, getPageSize(), getSort());
// Cast is safe because from() either returns the original PageRequest or a DatastorePageable.
return (PageRequest) from(nextPage, this.urlSafeCursor, this.totalCount);
}

public Cursor toCursor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@ private static Cursor getCursor(Pageable pageable) {
private static Long getOrComputeTotalCount(Pageable pageable, LongSupplier countCall) {
return pageable instanceof DatastorePageable ? ((DatastorePageable) pageable).getTotalCount() : countCall.getAsLong();
}

// TODO: Restore @Override when not supporting Spring Boot 2.4 anymore
public void deleteAllById(Iterable<? extends I> iterable) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,7 @@
import org.springframework.data.domain.Sort;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
Expand Down Expand Up @@ -350,4 +351,10 @@ public void findAllSortAsc() {
)).build();
verify(this.datastoreTemplate).findAll(Object.class, opts);
}

@Test
public void deleteAllByIdUnimplemented() {
assertThatThrownBy(() -> this.simpleDatastoreRepository.deleteAllById(new ArrayList<>()))
.isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ public Mono<Void> deleteAll(Publisher entityStream) {
public Mono<Void> deleteAll() {
return this.firestoreTemplate.deleteAll(this.type);
}

// TODO: Restore @Override when not supporting Spring Boot 2.4 anymore
//@Override
public Mono<Void> deleteAllById(Iterable<? extends String> ids) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spring.data.firestore;

import java.util.ArrayList;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

public class SimpleFirestoreReactiveRepositoryTests {

@Test
public void deleteAllByIdUnimplemented() {
FirestoreTemplate mockTemplate = mock(FirestoreTemplate.class);
SimpleFirestoreReactiveRepository<String> repository =
new SimpleFirestoreReactiveRepository<>(mockTemplate, String.class);
assertThatThrownBy(() -> repository.deleteAllById(new ArrayList<>()))
.isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public Page<T> findAll(Pageable pageable) {
pageable, this.spannerTemplate.count(this.entityType));
}

// TODO: Restore @Override when not supporting Spring Boot 2.4 anymore
//@Override
public void deleteAllById(Iterable<? extends I> is) {
throw new UnsupportedOperationException();
}

private Key toKey(Object id) {
return this.spannerTemplate.getSpannerEntityProcessor().convertToKey(id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spring.data.spanner;

import java.util.ArrayList;

import com.google.cloud.spring.data.spanner.core.SpannerTemplate;
import com.google.cloud.spring.data.spanner.repository.support.SimpleSpannerRepository;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

public class SimpleSpannerRepositoryTests {

@Test
public void deleteAllByIdUnimplemented() {
SpannerTemplate mockTemplate = mock(SpannerTemplate.class);
SimpleSpannerRepository<Book, String> repository = new SimpleSpannerRepository<>(mockTemplate, Book.class);

assertThatThrownBy(() -> repository.deleteAllById(new ArrayList<>()))
.isInstanceOf(UnsupportedOperationException.class);
}

static class Book {
String id;
}
}

0 comments on commit 197a8c3

Please sign in to comment.