Skip to content

Commit

Permalink
Merge branch 'main' of https://git.smc.it/openk9/openk9 into 1076-fix…
Browse files Browse the repository at this point in the history
…-stile-frontend-di-ricerca
  • Loading branch information
lorev101 committed Sep 19, 2024
2 parents 6cf07df + 48e2175 commit bb91947
Show file tree
Hide file tree
Showing 16 changed files with 805 additions and 578 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void beforeUpdate(K9Entity k9Entity) {
_handle(k9Entity, EventType.UPDATE);
}


@PostRemove
public void postRemove(K9Entity k9Entity) {
_handle(k9Entity, EventType.DELETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.openk9.datasource.model.util.K9Entity;
import io.openk9.datasource.util.OpenSearchUtils;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -35,7 +37,10 @@
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import javax.persistence.PostUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "data_index")
Expand Down Expand Up @@ -75,6 +80,11 @@ public class DataIndex extends K9Entity {
@JoinColumn(name = "vector_index_id", referencedColumnName = "id")
private VectorIndex vectorIndex;

@Transient
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private String indexName;

public void addDocType(DocType docType) {
docTypes.add(docType);
}
Expand All @@ -83,8 +93,45 @@ public void removeDocType(DocType docType) {
docTypes.remove(docType);
}

public String getIndexName() {
return getTenant() + "-" + name;
public String getIndexName() throws UnknownTenantException {
if (indexName == null) {
setupIndexName();
}

return indexName;
}

@PostLoad
@PostUpdate
protected void setupIndexName() throws UnknownTenantException {
String tenantId = getTenant();

// This is a workaround needed when a new DataIndex is being created.
// The tenant is not identified, likely because the entity has not
// been persisted yet. Therefore, it is obtained from an entity that
// is already in the persistence context, typically, the first
// DocType associated with the new DataIndex, or alternatively,
// the Datasource associated with it.
if (tenantId == null) {
var iterator = docTypes.iterator();
if (iterator.hasNext()) {
var docType = iterator.next();
tenantId = docType.getTenant();
}
else {
var ds = getDatasource();
if (ds != null) {
tenantId = ds.getTenant();
}
}
if (tenantId == null) {
throw new UnknownTenantException(
String.format("Cannot identify the tenant for DataIndex: %s", getName()));
}
}

this.indexName = OpenSearchUtils.indexNameSanitizer(
String.format("%s-%s", tenantId, getName())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020-present SMC Treviso s.r.l. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.openk9.datasource.model;

import io.openk9.datasource.service.exception.K9Error;

public class UnknownTenantException extends K9Error {

public UnknownTenantException(String message) {
super(message);
}

public UnknownTenantException(String message, Throwable cause) {
super(message, cause);
}

public UnknownTenantException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import io.openk9.common.graphql.util.relay.Connection;
import io.openk9.common.util.SortBy;
import io.openk9.datasource.index.IndexService;
import io.openk9.datasource.index.mappings.MappingsKey;
import io.openk9.datasource.index.mappings.MappingsUtil;
import io.openk9.datasource.mapper.DataIndexMapper;
import io.openk9.datasource.mapper.IngestionPayloadMapper;
import io.openk9.datasource.model.DataIndex;
import io.openk9.datasource.model.DataIndex_;
import io.openk9.datasource.model.Datasource;
import io.openk9.datasource.model.DocType;
import io.openk9.datasource.model.UnknownTenantException;
import io.openk9.datasource.model.VectorIndex;
import io.openk9.datasource.model.dto.DataIndexDTO;
import io.openk9.datasource.plugindriver.HttpPluginDriverClient;
Expand All @@ -39,19 +42,32 @@
import io.openk9.datasource.util.OpenSearchUtils;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import org.hibernate.reactive.mutiny.Mutiny;
import org.opensearch.OpenSearchStatusException;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.IndicesClient;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.indices.PutComposableIndexTemplateRequest;
import org.opensearch.cluster.metadata.ComposableIndexTemplate;
import org.opensearch.cluster.metadata.Template;
import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.settings.Settings;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

@ApplicationScoped
public class DataIndexService
Expand All @@ -70,6 +86,8 @@ public class DataIndexService
@Inject
IngestionPayloadMapper ingestionPayloadMapper;

private static final String DETAILS_FIELD = "details";

DataIndexService(DataIndexMapper mapper) {
this.mapper = mapper;
}
Expand Down Expand Up @@ -212,26 +230,26 @@ public Uni<DataIndex> deleteById(long entityId) {
.transformToUni(dataIndex -> Uni.createFrom()
.<AcknowledgedResponse>emitter(emitter -> {

DeleteIndexRequest deleteIndexRequest =
new DeleteIndexRequest(dataIndex.getIndexName());
try {
DeleteIndexRequest deleteIndexRequest =
new DeleteIndexRequest(dataIndex.getIndexName());

deleteIndexRequest
.indicesOptions(
IndicesOptions.fromMap(
Map.of("ignore_unavailable", true),
deleteIndexRequest.indicesOptions()
)
);
deleteIndexRequest
.indicesOptions(
IndicesOptions.fromMap(
Map.of("ignore_unavailable", true),
deleteIndexRequest.indicesOptions()
)
);

try {
AcknowledgedResponse delete = restHighLevelClient.indices().delete(
deleteIndexRequest,
RequestOptions.DEFAULT
);

emitter.complete(delete);
}
catch (IOException e) {
catch (UnknownTenantException | IOException e) {
emitter.fail(e);
}
})
Expand Down Expand Up @@ -279,4 +297,122 @@ public Uni<DataIndex> createByDatasource(Mutiny.Session session, Datasource data
});
}

public Uni<DataIndex> createDataIndexFromDocTypes(
long datasourceId, List<Long> docTypeIds, String name,
Map<String, Object> indexSettings) {

String dataIndexName = name == null ? "data-" + OffsetDateTime.now() : name;

return sessionFactory.withTransaction((s, t) -> docTypeService
.findDocTypes(docTypeIds, s)
.flatMap(docTypeList -> {

if (docTypeList.size() != docTypeIds.size()) {
throw new RuntimeException(
"docTypeIds found: " + docTypeList.size() +
" docTypeIds requested: " + docTypeIds.size());
}

DataIndex dataIndex = new DataIndex();

dataIndex.setDescription("auto-generated");

dataIndex.setName(dataIndexName);

dataIndex.setDocTypes(new LinkedHashSet<>(docTypeList));

dataIndex.setDatasource(s.getReference(Datasource.class, datasourceId));

return persist(s, dataIndex)
.map(__ -> {
Map<MappingsKey, Object> mappings =
MappingsUtil.docTypesToMappings(dataIndex.getDocTypes());

Settings settings;

Map<String, Object> settingsMap =
indexSettings != null && !indexSettings.isEmpty() ?
indexSettings :
MappingsUtil.docTypesToSettings(dataIndex.getDocTypes());

if (settingsMap.isEmpty()) {
settings = Settings.EMPTY;
}
else {
settings = Settings.builder()
.loadFromMap(settingsMap)
.build();
}

PutComposableIndexTemplateRequest
putComposableIndexTemplateRequest =
new PutComposableIndexTemplateRequest();

ComposableIndexTemplate composableIndexTemplate = null;

try {
var indexName = dataIndex.getIndexName();

composableIndexTemplate = new ComposableIndexTemplate(
List.of(indexName),
new Template(settings, new CompressedXContent(
Json.encode(mappings)), null),
null, null, null, null
);

putComposableIndexTemplateRequest
.name(indexName + "-template")
.indexTemplate(composableIndexTemplate);

return putComposableIndexTemplateRequest;
}
catch (UnknownTenantException e) {
throw new WebApplicationException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(JsonObject.of(
DETAILS_FIELD, "cannot obtain a proper index name"
))
.build());
}
catch (IOException e) {
throw new WebApplicationException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(JsonObject.of(
DETAILS_FIELD, "failed creating IndexTemplate"
))
.build());
}

})
.call((req) -> Uni.createFrom().emitter((sink) -> {

try {
IndicesClient indices = restHighLevelClient.indices();

indices.putIndexTemplate(req, RequestOptions.DEFAULT);

sink.complete(null);
}
catch (OpenSearchStatusException e) {
sink.fail(new WebApplicationException(javax.ws.rs.core.Response
.status(e.status().getStatus())
.entity(JsonObject.of(
DETAILS_FIELD, e.getMessage()))
.build()));
}
catch (Exception e) {
sink.fail(new WebApplicationException(javax.ws.rs.core.Response
.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR)
.entity(JsonObject.of(
DETAILS_FIELD, e.getMessage()))
.build()));
}

}))
.map(__ -> dataIndex);

})
);
}

}
Loading

0 comments on commit bb91947

Please sign in to comment.