/
SimpleCouchbaseRepository.java
195 lines (170 loc) · 7.21 KB
/
SimpleCouchbaseRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2013-2024 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 org.springframework.data.couchbase.repository.support;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
import com.couchbase.client.java.query.QueryScanConsistency;
/**
* Repository base implementation for Couchbase.
*
* @author Michael Nitschinger
* @author Mark Paluch
* @author Jens Schauder
* @author Michael Reiche
*/
public class SimpleCouchbaseRepository<T, ID> extends CouchbaseRepositoryBase<T, ID>
implements CouchbaseRepository<T, ID> {
/**
* Holds the reference to the {@link org.springframework.data.couchbase.core.CouchbaseTemplate}.
*/
private final CouchbaseOperations operations;
/**
* Create a new Repository.
*
* @param entityInformation the Metadata for the entity.
* @param couchbaseOperations the reference to the template used.
* @param repositoryInterface the repository interface being fronted
*/
public SimpleCouchbaseRepository(CouchbaseEntityInformation<T, String> entityInformation,
CouchbaseOperations couchbaseOperations, Class<?> repositoryInterface) {
super(entityInformation, repositoryInterface);
this.operations = couchbaseOperations;
}
@Override
@SuppressWarnings("unchecked")
public <S extends T> S save(S entity) {
String scopeName = getScope();
String collectionName = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return operations.save(entity, scopeName, collectionName);
}
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
String scopeName = getScope();
String collectionName = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return Streamable.of(entities).stream().map((e) -> operations.save(e,scopeName, collectionName)).collect(StreamUtils.toUnmodifiableList());
}
@Override
public Optional<T> findById(ID id) {
Assert.notNull(id, "The given id must not be null!");
return Optional.ofNullable(
operations.findById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString()));
}
@Override
public List<T> findAllById(Iterable<ID> ids) {
Assert.notNull(ids, "The given Iterable of ids must not be null!");
List<String> convertedIds = Streamable.of(ids).stream().map(Objects::toString).collect(Collectors.toList());
Collection<? extends T> all = operations.findById(getJavaType()).inScope(getScope()).inCollection(getCollection())
.all(convertedIds);
return Streamable.of(all).stream().collect(StreamUtils.toUnmodifiableList());
}
@Override
public boolean existsById(ID id) {
Assert.notNull(id, "The given id must not be null!");
return operations.existsById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString());
}
@Override
public void deleteById(ID id) {
Assert.notNull(id, "The given id must not be null!");
operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString());
}
@Override
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null!");
operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection()).oneEntity(entity);
}
@Override
public void deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(ids, "The given Iterable of ids must not be null!");
operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection())
.all(Streamable.of(ids).map(Objects::toString).toList());
}
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection())
.allEntities((Collection<Object>)Streamable.of(entities).toList());
}
@Override
public long count() {
return operations.findByQuery(getJavaType()).withConsistency(getQueryScanConsistency()).inScope(getScope())
.inCollection(getCollection()).count();
}
@Override
public void deleteAll() {
operations.removeByQuery(getJavaType()).withConsistency(getQueryScanConsistency()).inScope(getScope())
.inCollection(getCollection()).all();
}
@Override
public List<T> findAll() {
return findAll(new Query());
}
@Override
public List<T> findAll(Sort sort) {
return findAll(new Query().with(sort));
}
@Override
public List<T> findAll(QueryScanConsistency queryScanConsistency) {
return findAll(new Query().scanConsistency(queryScanConsistency));
}
@Override
public Page<T> findAll(Pageable pageable) {
List<T> results = findAll(new Query().with(pageable));
return new PageImpl<>(results, pageable, count());
}
/**
* Helper method to assemble a n1ql find all query, taking annotations into acocunt.
*
* @param query the originating query.
* @return the list of found entities, already executed.
*/
private List<T> findAll(Query query) {
return operations.findByQuery(getJavaType()).withConsistency(getQueryScanConsistency()).inScope(getScope())
.inCollection(getCollection()).matching(query).all();
}
@Override
public CouchbaseOperations getOperations() {
return operations;
}
@Override
protected ReactiveCouchbaseTemplate getReactiveTemplate() {
return ((CouchbaseTemplate) getOperations()).reactive();
}
}