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());
}
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);
}
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);
}
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);
}
}
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;
}
Aggregations