db4o java_java com.db4o 类

db4o java_java com.db4o 类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…

大家好,又见面了,我是你们的朋友全栈君。

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);

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162577.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号