JAva com.db 4 oクラス
17458 ワード
com.db4o
com.db4o
7.7.67
com.db4o
com.db4o.nativequery
7.7.67
package org.rx.repository.db4o;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
@Data
public abstract class DataObject implements Serializable {
private UUID id;
private Date createTime, modifyTime;
private boolean isDeleted;
}
package org.rx.repository.db4o;
import org.rx.core.dto.common.PagedResponse;
import org.rx.core.dto.common.PagingRequest;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
public interface IRepositoryextends DataObject> {
T save(T model);
T delete(UUID id);
T single(UUID id);
T single(Predicate condition);
long count(Predicate condition);
List list(Predicate condition);
List list(Predicate condition, Function keySelector);
List listDescending(Predicate condition, Function keySelector);
PagedResponse page(Predicate condition, Function keySelector, PagingRequest pagingParam);
PagedResponse pageDescending(Predicate condition, Function keySelector, PagingRequest pagingParam);
}
package org.rx.repository.db4o.impl;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.Configuration;
import lombok.SneakyThrows;
import org.rx.api.dto.common.PagedResponse;
import org.rx.api.dto.common.PagingRequest;
import org.rx.beans.BeanMapper;
import org.rx.beans.DateTime;
import org.rx.common.App;
import org.rx.common.InvalidOperationException;
import org.rx.common.NQuery;
import org.rx.repository.db4o.IRepository;
import org.rx.repository.db4o.DataObject;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import static org.rx.common.Contract.require;
@Component
public class Db4oRepositoryextends DataObject> implements IRepository {
private String dbPath;
private Configuration config;
@SneakyThrows
public Db4oRepository() {
dbPath = App.readSetting("app.repository.dbFile");
if (dbPath == null) {
throw new InvalidOperationException("app.repository.dbFile is empty");
}
String dir = dbPath;
int i = dir.lastIndexOf("/");
if (i != -1) {
dir = dir.substring(0, i);
}
App.createDirectory(dir);
config = Db4o.newConfiguration();
}
protected R invoke(Function func) {
return NQuery.of(invoke((Function[]) new Function[]{func})).firstOrDefault();
}
protected synchronized List invoke(Function... funcList) {
require(funcList);
List result = new ArrayList<>();
// ObjectContainer db = Db4o.openFile(config, dbPath);
ObjectContainer db = App.getOrStore("Db4oRepository-threadDb", k -> Db4o.openFile(config, dbPath));
try {
for (Function function : funcList) {
result.add(function.apply(db));
}
db.commit();
} catch (Exception e) {
db.rollback();
throw e;
}
// finally {
// db.close();
// }
return result;
}
public T save(T model) {
require(model);
if (!(model instanceof DataObject)) {
throw new IllegalArgumentException("model is not a DataObject");
}
return invoke(db -> {
T dataObj = single(p -> p.getId().equals(model.getId()));
if (dataObj != null) {
dataObj = BeanMapper.getInstance().map(model, dataObj, BeanMapper.Flags.NonCheckMatch | BeanMapper.Flags.SkipNull);
} else {
dataObj = model;
}
if (dataObj.getId() == null) {
dataObj.setId(UUID.randomUUID());
}
if (dataObj.getCreateTime() == null) {
dataObj.setCreateTime(DateTime.now());
}
dataObj.setModifyTime(DateTime.now());
db.store(dataObj);
return dataObj;
});
}
@Override
public T delete(UUID id) {
T model = single(id);
if (model == null) {
return null;
}
model.setDeleted(true);
return save(model);
}
@Override
public T single(UUID id) {
return single(p -> p.getId().equals(id));
}
@Override
public T single(Predicate condition) {
return NQuery.of(list(condition)).firstOrDefault();
}
@Override
public long count(Predicate condition) {
return executeReader(condition, null, false).count();
}
@Override
public List list(Predicate condition) {
return list(condition, null);
}
@Override
public List list(Predicate condition, Function keySelector) {
return executeReader(condition, keySelector, false).toList();
}
@Override
public List listDescending(Predicate condition, Function keySelector) {
return executeReader(condition, keySelector, true).toList();
}
@Override
public PagedResponse page(Predicate condition, Function keySelector, PagingRequest pagingParam) {
require(pagingParam);
NQuery nQuery = executeReader(condition, keySelector, false);
return pagingParam.page(nQuery);
}
@Override
public PagedResponse pageDescending(Predicate condition, Function keySelector, PagingRequest pagingParam) {
require(pagingParam);
NQuery nQuery = executeReader(condition, keySelector, true);
return pagingParam.page(nQuery);
}
private NQuery executeReader(Predicate condition, Function keySelector, boolean isDescending) {
require(condition);
com.db4o.query.Predicate predicate = new com.db4o.query.Predicate() {
public boolean match(T candidate) {
return !candidate.isDeleted() && condition.test(candidate);
}
};
return invoke(db -> {
ObjectSet objectSet;
if (keySelector == null) {
objectSet = db.query(predicate);
} else {
Comparator comparator = getComparator(keySelector);
if (isDescending) {
comparator = comparator.reversed();
}
objectSet = db.query(predicate, comparator);
}
return NQuery.of(objectSet);
});
}
private Comparator getComparator(Function keySelector) {
if (keySelector == null) {
return (Comparator) Comparator.naturalOrder();
}
return NQuery.getComparator(keySelector);
}
}