Search in sources :

Example 1 with PersistencePath

use of eu.okaeri.persistence.PersistencePath in project okaeri-persistence by OkaeriPoland.

the class FlatPersistence method findMissingIndexes.

@Override
@SneakyThrows
public Set<PersistencePath> findMissingIndexes(@NonNull PersistenceCollection collection, @NonNull Set<IndexProperty> indexProperties) {
    Map<String, InMemoryIndex> collectionIndexes = this.indexMap.get(collection.getValue());
    if (collectionIndexes.isEmpty()) {
        return Collections.emptySet();
    }
    Path collectionFile = this.getBasePath().sub(collection).toPath();
    return this.scanCollection(collectionFile).map(this.fileToKeyMapper).map(key -> collectionIndexes.values().stream().allMatch(flatIndex -> flatIndex.getKeyToValue().containsKey(key)) ? null : PersistencePath.of(key)).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : Path(java.nio.file.Path) PersistencePath(eu.okaeri.persistence.PersistencePath) InMemoryIndex(eu.okaeri.persistence.document.index.InMemoryIndex)

Example 2 with PersistencePath

use of eu.okaeri.persistence.PersistencePath in project okaeri-persistence by OkaeriPoland.

the class FlatPersistence method readByProperty.

@Override
public Stream<PersistenceEntity<String>> readByProperty(@NonNull PersistenceCollection collection, @NonNull PersistencePath property, @NonNull Object propertyValue) {
    if (!this.canUseToString(propertyValue)) {
        return this.streamAll(collection);
    }
    InMemoryIndex flatIndex = this.indexMap.get(collection.getValue()).get(property.getValue());
    if (flatIndex == null)
        return this.streamAll(collection);
    Set<String> keys = flatIndex.getValueToKeys().get(String.valueOf(propertyValue));
    if ((keys == null) || keys.isEmpty()) {
        return Stream.of();
    }
    return new ArrayList<>(keys).stream().map(key -> {
        PersistencePath path = PersistencePath.of(key);
        return this.read(collection, path).map(data -> new PersistenceEntity<>(path, data)).orElseGet(() -> {
            this.dropIndex(collection, path);
            return null;
        });
    }).filter(Objects::nonNull);
}
Also used : java.util(java.util) RawPersistence(eu.okaeri.persistence.raw.RawPersistence) PersistenceCollection(eu.okaeri.persistence.PersistenceCollection) Function(java.util.function.Function) Level(java.util.logging.Level) Path(java.nio.file.Path) PersistencePath(eu.okaeri.persistence.PersistencePath) IndexProperty(eu.okaeri.persistence.document.index.IndexProperty) Files(java.nio.file.Files) InMemoryConfigurer(eu.okaeri.configs.configurer.InMemoryConfigurer) BufferedWriter(java.io.BufferedWriter) Predicate(java.util.function.Predicate) FileWriter(java.io.FileWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) lombok(lombok) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Stream(java.util.stream.Stream) ConfigManager(eu.okaeri.configs.ConfigManager) PersistenceEntity(eu.okaeri.persistence.PersistenceEntity) ConfigurerProvider(eu.okaeri.persistence.document.ConfigurerProvider) InMemoryIndex(eu.okaeri.persistence.document.index.InMemoryIndex) InMemoryIndex(eu.okaeri.persistence.document.index.InMemoryIndex) PersistencePath(eu.okaeri.persistence.PersistencePath)

Example 3 with PersistencePath

use of eu.okaeri.persistence.PersistencePath in project okaeri-persistence by OkaeriPoland.

the class FlatPersistence method registerCollection.

@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public void registerCollection(@NonNull PersistenceCollection collection) {
    PersistencePath collectionPath = this.getBasePath().sub(collection);
    File collectionFile = collectionPath.toFile();
    collectionFile.mkdirs();
    Map<String, InMemoryIndex> indexes = this.indexMap.computeIfAbsent(collection.getValue(), col -> new ConcurrentHashMap<>());
    for (IndexProperty index : collection.getIndexes()) {
        InMemoryIndex flatIndex = ConfigManager.create(InMemoryIndex.class);
        flatIndex.setConfigurer(this.indexProvider.get());
        Path path = collectionPath.append("_").append(index.toSafeFileName()).append(".index").toPath();
        flatIndex.withBindFile(path);
        if (this.isSaveIndex() && Files.exists(path)) {
            flatIndex.load(path);
        }
        indexes.put(index.getValue(), flatIndex);
    }
    super.registerCollection(collection);
}
Also used : Path(java.nio.file.Path) PersistencePath(eu.okaeri.persistence.PersistencePath) InMemoryIndex(eu.okaeri.persistence.document.index.InMemoryIndex) PersistencePath(eu.okaeri.persistence.PersistencePath) IndexProperty(eu.okaeri.persistence.document.index.IndexProperty) File(java.io.File)

Example 4 with PersistencePath

use of eu.okaeri.persistence.PersistencePath in project okaeri-persistence by OkaeriPoland.

the class H2Persistence method write.

@Override
public long write(@NonNull PersistenceCollection collection, @NonNull Map<PersistencePath, String> entities) {
    this.checkCollectionRegistered(collection);
    String sql = "insert into `" + this.table(collection) + "` (`key`, `value`) values (?, ?) on duplicate key update `value` = ?";
    try (Connection connection = this.getDataSource().getConnection()) {
        PreparedStatement prepared = connection.prepareStatement(sql);
        connection.setAutoCommit(false);
        for (Map.Entry<PersistencePath, String> entry : entities.entrySet()) {
            prepared.setString(1, entry.getKey().getValue());
            prepared.setString(2, entry.getValue());
            prepared.setString(3, entry.getValue());
            prepared.addBatch();
        }
        int changes = prepared.executeUpdate();
        connection.commit();
        return changes;
    } catch (SQLException exception) {
        throw new RuntimeException("cannot write " + entities + " to " + collection, exception);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) PersistencePath(eu.okaeri.persistence.PersistencePath) Map(java.util.Map)

Example 5 with PersistencePath

use of eu.okaeri.persistence.PersistencePath in project okaeri-persistence by OkaeriPoland.

the class JdbcPersistence method read.

@Override
public Map<PersistencePath, String> read(@NonNull PersistenceCollection collection, @NonNull Collection<PersistencePath> paths) {
    this.checkCollectionRegistered(collection);
    String keys = paths.stream().map(path -> "`key` = ?").collect(Collectors.joining(" or "));
    String sql = "select `key`, `value` from `" + this.table(collection) + "` where " + keys;
    Map<PersistencePath, String> map = new LinkedHashMap<>();
    try (Connection connection = this.getDataSource().getConnection()) {
        PreparedStatement prepared = connection.prepareStatement(sql);
        int currentIndex = 1;
        for (PersistencePath path : paths) {
            prepared.setString(currentIndex++, path.getValue());
        }
        ResultSet resultSet = prepared.executeQuery();
        while (resultSet.next()) {
            String key = resultSet.getString("key");
            String value = resultSet.getString("value");
            map.put(PersistencePath.of(key), value);
        }
    } catch (SQLException exception) {
        throw new RuntimeException("cannot read " + paths + " from " + collection, exception);
    }
    return map;
}
Also used : PersistencePath(eu.okaeri.persistence.PersistencePath) java.util(java.util) RawPersistence(eu.okaeri.persistence.raw.RawPersistence) Connection(java.sql.Connection) PersistenceCollection(eu.okaeri.persistence.PersistenceCollection) IndexProperty(eu.okaeri.persistence.document.index.IndexProperty) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) NonNull(lombok.NonNull) Predicate(java.util.function.Predicate) IOException(java.io.IOException) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) HikariConfig(com.zaxxer.hikari.HikariConfig) SQLException(java.sql.SQLException) Stream(java.util.stream.Stream) ResultSet(java.sql.ResultSet) HikariDataSource(com.zaxxer.hikari.HikariDataSource) PersistenceEntity(eu.okaeri.persistence.PersistenceEntity) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistencePath(eu.okaeri.persistence.PersistencePath) PreparedStatement(java.sql.PreparedStatement)

Aggregations

PersistencePath (eu.okaeri.persistence.PersistencePath)27 PersistenceEntity (eu.okaeri.persistence.PersistenceEntity)10 PersistenceCollection (eu.okaeri.persistence.PersistenceCollection)9 IndexProperty (eu.okaeri.persistence.document.index.IndexProperty)9 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 Stream (java.util.stream.Stream)7 RawPersistence (eu.okaeri.persistence.raw.RawPersistence)6 IOException (java.io.IOException)6 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 NonNull (lombok.NonNull)6 HikariConfig (com.zaxxer.hikari.HikariConfig)5 DocumentPersistence (eu.okaeri.persistence.document.DocumentPersistence)5 Predicate (java.util.function.Predicate)5 Logger (java.util.logging.Logger)5 Getter (lombok.Getter)5 SneakyThrows (lombok.SneakyThrows)5 File (java.io.File)4