Skip to content

Commit 3ea9b66

Browse files
committed
Makes interface on document manager at ES
1 parent cb7e597 commit 3ea9b66

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.elasticsearch.document;
16+
17+
18+
import org.elasticsearch.action.search.SearchResponse;
19+
import org.elasticsearch.client.Client;
20+
import org.elasticsearch.index.query.QueryBuilder;
21+
import org.jnosql.diana.api.document.Document;
22+
import org.jnosql.diana.api.document.DocumentDeleteQuery;
23+
import org.jnosql.diana.api.document.DocumentEntity;
24+
import org.jnosql.diana.api.document.DocumentQuery;
25+
26+
import javax.json.bind.Jsonb;
27+
import javax.json.bind.JsonbBuilder;
28+
import java.time.Duration;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Objects;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.stream.Collectors;
34+
35+
import static java.nio.charset.StandardCharsets.UTF_8;
36+
import static java.util.Objects.requireNonNull;
37+
import static java.util.stream.StreamSupport.stream;
38+
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
39+
import static org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD;
40+
import static org.jnosql.diana.elasticsearch.document.EntityConverter.getMap;
41+
42+
/**
43+
* The Default implementation of {@link ElasticsearchDocumentCollectionManager}
44+
*/
45+
class DefaultElasticsearchDocumentCollectionManager implements ElasticsearchDocumentCollectionManager {
46+
47+
48+
protected static final Jsonb JSONB = JsonbBuilder.create();
49+
50+
private final Client client;
51+
52+
private final String index;
53+
54+
DefaultElasticsearchDocumentCollectionManager(Client client, String index) {
55+
this.client = client;
56+
this.index = index;
57+
}
58+
59+
@Override
60+
public DocumentEntity insert(DocumentEntity entity) throws NullPointerException {
61+
requireNonNull(entity, "entity is required");
62+
Document id = entity.find(ID_FIELD)
63+
.orElseThrow(() -> new ElasticsearchKeyFoundException(entity.toString()));
64+
Map<String, Object> jsonObject = getMap(entity);
65+
byte[] bytes = JSONB.toJson(jsonObject).getBytes(UTF_8);
66+
try {
67+
client.prepareIndex(index, entity.getName(), id.get(String.class)).setSource(bytes)
68+
.execute().get();
69+
return entity;
70+
} catch (InterruptedException | ExecutionException e) {
71+
throw new ElasticsearchException("An error to try to save/update entity on elasticsearch", e);
72+
}
73+
74+
}
75+
76+
77+
@Override
78+
public DocumentEntity insert(DocumentEntity entity, Duration ttl) throws NullPointerException, UnsupportedOperationException {
79+
requireNonNull(entity, "entity is required");
80+
requireNonNull(ttl, "ttl is required");
81+
Document id = entity.find(ID_FIELD)
82+
.orElseThrow(() -> new ElasticsearchKeyFoundException(entity.toString()));
83+
Map<String, Object> jsonObject = getMap(entity);
84+
byte[] bytes = JSONB.toJson(jsonObject).getBytes(UTF_8);
85+
try {
86+
client.prepareIndex(index, entity.getName(), id.get(String.class))
87+
.setSource(bytes)
88+
.setTTL(timeValueMillis(ttl.toMillis()))
89+
.execute().get();
90+
} catch (InterruptedException | ExecutionException e) {
91+
throw new ElasticsearchException("An error to try to save with TTL entity on elasticsearch", e);
92+
}
93+
return entity;
94+
}
95+
96+
@Override
97+
public DocumentEntity update(DocumentEntity entity) throws NullPointerException {
98+
return insert(entity);
99+
}
100+
101+
@Override
102+
public void delete(DocumentDeleteQuery query) throws NullPointerException {
103+
requireNonNull(query, "query is required");
104+
List<DocumentEntity> entities = select(DocumentQuery.of(query.getCollection())
105+
.and(query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"))));
106+
107+
entities.stream()
108+
.map(entity -> entity.find(ID_FIELD).get().get(String.class))
109+
.forEach(id -> {
110+
try {
111+
client.prepareDelete(index, query.getCollection(), id).execute().get();
112+
} catch (InterruptedException | ExecutionException e) {
113+
throw new ElasticsearchException("An error to delete entities on elasticsearch", e);
114+
}
115+
});
116+
117+
}
118+
119+
120+
@Override
121+
public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException {
122+
requireNonNull(query, "query is required");
123+
return EntityConverter.query(query, client, index);
124+
}
125+
126+
@Override
127+
public List<DocumentEntity> select(QueryBuilder query, String... types) throws NullPointerException {
128+
Objects.requireNonNull(query, "query is required");
129+
130+
SearchResponse searchResponse = null;
131+
try {
132+
searchResponse = client.prepareSearch(index)
133+
.setTypes(types)
134+
.setQuery(query)
135+
.execute().get();
136+
137+
return stream(searchResponse.getHits().spliterator(), false)
138+
.map(h -> new ElasticsearchEntry(h.getId(), h.getIndex(), h.sourceAsMap()))
139+
.filter(ElasticsearchEntry::isNotEmpty)
140+
.map(ElasticsearchEntry::toEntity)
141+
.collect(Collectors.toList());
142+
} catch (InterruptedException | ExecutionException e) {
143+
throw new ElasticsearchException("An error when do find from QueryBuilder on elasticsearch", e);
144+
}
145+
146+
147+
}
148+
149+
@Override
150+
public void close() {
151+
152+
}
153+
}

0 commit comments

Comments
 (0)