org.eclipse.jnosql.databases.ravendb.communication.RavenDBDocumentManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnosql-ravendb Show documentation
Show all versions of jnosql-ravendb Show documentation
The Eclipse JNoSQL layer to RavenDB
The newest version!
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.ravendb.communication;
import net.ravendb.client.documents.DocumentStore;
import net.ravendb.client.documents.queries.Query;
import net.ravendb.client.documents.session.IDocumentQuery;
import net.ravendb.client.documents.session.IDocumentSession;
import net.ravendb.client.documents.session.IEnumerableQuery;
import net.ravendb.client.documents.session.IMetadataDictionary;
import net.ravendb.client.exceptions.RavenException;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.DatabaseManager;
import org.eclipse.jnosql.communication.semistructured.DeleteQuery;
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static net.ravendb.client.Constants.Documents.Metadata.COLLECTION;
import static net.ravendb.client.Constants.Documents.Metadata.EXPIRES;
/**
* The RavenDB implementation to {@link DatabaseManager} that does not support TTL methods
* {@link RavenDBDocumentManager#insert(CommunicationEntity, Duration)}
*/
public class RavenDBDocumentManager implements DatabaseManager {
private final DocumentStore store;
private final String database;
RavenDBDocumentManager(DocumentStore store, String database) {
this.store = store;
this.store.initialize();
this.database = database;
}
@Override
public String name() {
return database;
}
@Override
public CommunicationEntity insert(CommunicationEntity entity) {
Objects.requireNonNull(entity, "entity is required");
try (IDocumentSession session = store.openSession()) {
insert(entity, null, session);
}
return entity;
}
@Override
public CommunicationEntity insert(CommunicationEntity entity, Duration ttl) {
Objects.requireNonNull(entity, "entity is required");
Objects.requireNonNull(ttl, "ttl is required");
LocalDateTime time = LocalDateTime.now(Clock.systemUTC()).plus(ttl);
try (IDocumentSession session = store.openSession()) {
insert(entity, time, session);
}
return entity;
}
@Override
public Iterable insert(Iterable entities) {
Objects.requireNonNull(entities, "entities is required");
return StreamSupport.stream(entities.spliterator(), false)
.map(this::insert)
.collect(Collectors.toList());
}
@Override
public Iterable insert(Iterable entities, Duration ttl) {
Objects.requireNonNull(entities, "entities is required");
Objects.requireNonNull(ttl, "ttl is required");
return StreamSupport.stream(entities.spliterator(), false)
.map(e -> insert(e, ttl))
.collect(Collectors.toList());
}
@Override
public CommunicationEntity update(CommunicationEntity entity) {
Objects.requireNonNull(entity, "entity is required");
try (IDocumentSession session = store.openSession()) {
Element id = entity.find(EntityConverter.ID_FIELD)
.orElseThrow(() -> new RavenException("Id is required to Raven Update operation"));
HashMap map = session.load(HashMap.class, id.get(String.class));
map.putAll(EntityConverter.getMap(entity));
session.saveChanges();
}
return entity;
}
@Override
public Iterable update(Iterable entities) {
Objects.requireNonNull(entities, "entities is required");
return StreamSupport.stream(entities.spliterator(), false)
.map(this::update)
.collect(Collectors.toList());
}
@Override
public void delete(DeleteQuery query) {
Objects.requireNonNull(query, "query is required");
try (IDocumentSession session = store.openSession()) {
Stream
© 2015 - 2025 Weber Informatics LLC | Privacy Policy