当前位置: 首页 > 工具软件 > db4o > 使用案例 >

db4o java_java com.db4o 类

唐景山
2023-12-01

packageorg.rx.repository.db4o.impl;importcom.db4o.Db4o;importcom.db4o.ObjectContainer;importcom.db4o.ObjectSet;importcom.db4o.config.Configuration;importlombok.SneakyThrows;importorg.rx.api.dto.common.PagedResponse;importorg.rx.api.dto.common.PagingRequest;importorg.rx.beans.BeanMapper;importorg.rx.beans.DateTime;importorg.rx.common.App;importorg.rx.common.InvalidOperationException;importorg.rx.common.NQuery;importorg.rx.repository.db4o.IRepository;importorg.rx.repository.db4o.DataObject;importorg.springframework.stereotype.Component;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.UUID;importjava.util.function.Function;importjava.util.function.Predicate;import staticorg.rx.common.Contract.require;

@Componentpublic class Db4oRepository implements IRepository{privateString dbPath;privateConfiguration config;

@SneakyThrowspublicDb4oRepository() {

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(Functionfunc) {return NQuery.of(invoke((Function[]) newFunction[]{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 (Functionfunction : funcList) {

result.add(function.apply(db));

}

db.commit();

}catch(Exception e) {

db.rollback();throwe;

}//finally {//db.close();//}

returnresult;

}publicT save(T model) {

require(model);if (!(model instanceofDataObject)) {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);returndataObj;

});

}

@OverridepublicT delete(UUID id) {

T model=single(id);if (model == null) {return null;

}

model.setDeleted(true);returnsave(model);

}

@OverridepublicT single(UUID id) {return single(p ->p.getId().equals(id));

}

@Overridepublic T single(Predicatecondition) {returnNQuery.of(list(condition)).firstOrDefault();

}

@Overridepublic long count(Predicatecondition) {return executeReader(condition, null, false).count();

}

@Overridepublic List list(Predicatecondition) {return list(condition, null);

}

@Overridepublic List list(Predicate condition, FunctionkeySelector) {return executeReader(condition, keySelector, false).toList();

}

@Overridepublic List listDescending(Predicate condition, FunctionkeySelector) {return executeReader(condition, keySelector, true).toList();

}

@Overridepublic PagedResponse page(Predicate condition, FunctionkeySelector, PagingRequest pagingParam) {

require(pagingParam);

NQuery nQuery = executeReader(condition, keySelector, false);returnpagingParam.page(nQuery);

}

@Overridepublic PagedResponse pageDescending(Predicate condition, FunctionkeySelector, PagingRequest pagingParam) {

require(pagingParam);

NQuery nQuery = executeReader(condition, keySelector, true);returnpagingParam.page(nQuery);

}private NQuery executeReader(Predicate condition, Function keySelector, booleanisDescending) {

require(condition);

com.db4o.query.Predicate predicate = new com.db4o.query.Predicate() {public booleanmatch(T candidate) {return !candidate.isDeleted() &&condition.test(candidate);

}

};return invoke(db ->{

ObjectSetobjectSet;if (keySelector == null) {

objectSet=db.query(predicate);

}else{

Comparator comparator =getComparator(keySelector);if(isDescending) {

comparator=comparator.reversed();

}

objectSet=db.query(predicate, comparator);

}returnNQuery.of(objectSet);

});

}private Comparator getComparator(FunctionkeySelector) {if (keySelector == null) {return(Comparator) Comparator.naturalOrder();

}returnNQuery.getComparator(keySelector);

}

}

 类似资料: