use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Unit method saveDocument.
private void saveDocument(File file, Document docm) throws HongsException {
File fold = file.getParentFile();
if (!fold.exists()) {
fold.mkdirs();
}
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer tr = tf.newTransformer();
DOMSource ds = new DOMSource(docm);
StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tr.transform(ds, sr);
} catch (TransformerConfigurationException e) {
throw new HongsException(e);
} catch (UnsupportedEncodingException e) {
throw new HongsException(e);
} catch (IllegalArgumentException e) {
throw new HongsException(e);
} catch (FileNotFoundException e) {
throw new HongsException(e);
} catch (TransformerException e) {
throw new HongsException(e);
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class SearchEntity method revert.
@Override
public void revert() {
super.revert();
if (WRITES.isEmpty()) {
return;
}
try {
IndexWriter iw = getWriter();
synchronized (iw) {
iw.rollback();
}
} catch (HongsException ex) {
throw ex.toExemption();
} catch (IOException ex) {
throw new HongsExemption(1056, ex);
} finally {
WRITES.clear();
DOCK = null;
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class TitlesHelper method addItemsByForm.
public TitlesHelper addItemsByForm(String conf, String form, Map<String, Map> fs) throws HongsException {
Map ts = FormSet.getInstance("default").getEnum("__types__");
Iterator it = fs.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
Map mt = (Map) et.getValue();
String name = (String) et.getKey();
String type = (String) mt.get("__type__");
// 类型别名转换
type = (String) ts.get(type);
if (null != _cols && !_cols.contains(name)) {
continue;
}
if ("enum".equals(type) || "date".equals(type) || "number".equals(type) || "hidden".equals(type)) {
String xc = Synt.defxult((String) mt.get("conf"), conf);
String xn = Synt.defxult((String) mt.get("enum"), name);
Map fe;
try {
fe = FormSet.getInstance(xc).getEnumTranslated(xn);
} catch (HongsException ex) {
if (ex.getErrno() == 913) {
continue;
} else {
throw ex;
}
}
enums.put(name, fe);
} else if ("fork".equals(type)) {
forks.put(name, mt);
}
}
return this;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class StatisHelper method amount.
/**
* 分类计算
*
* <pre>
* rd.rn 为统计结果长度
* rd.rb 为要统计的字段
* rd.ob 为要排序的字段
* rd.ab 含 linked 时 rd.fn.rg 对应字段 fn 分块统计
* rd.fn.rn 可单独指定字段统计长度
* rd.fn.ar 可指定要统计的区间集合
* rd.fn.nr 可指定要忽略的区间集合(只是不作统计, 并非查询约束)
* </pre>
*
* @param rd
* @return
* @throws HongsException
*/
public Map amount(Map rd) throws HongsException {
IndexSearcher finder = that.getFinder();
Set<String> ab = Synt.toTerms(rd.get(Cnst.AB_KEY));
Set<String> rb = Synt.toTerms(rd.get(Cnst.RB_KEY));
Set<String> ob = Synt.toTerms(rd.get(Cnst.OB_KEY));
boolean ln = ab != null && ab.contains("linked");
int rl = rb != null ? rb.size() : 0;
Map<String, Map<Range, Ratio>> counts = new HashMap(rl);
// 排除
Map<String, Set<Range>> countx = new HashMap(rl);
Map<String, Map<Range, Ratio>> counts2 = new HashMap(rl);
Map<String, Set<Range>> countx2 = new HashMap(rl);
Map<String, Map<Range, Ratio>> counts3 = new HashMap(rl);
Map<String, Set<Range>> countx3 = new HashMap(rl);
if (rb != null && !rb.isEmpty())
for (String k : rb) {
Function f = getGraderFormat(k);
if (null == f || f == asString) {
throw new HongsException(400, "Field " + f + " is not exists or is not number");
}
Map vd = null;
Set vs = null;
Object vo = rd.get(k);
if (vo instanceof Map) {
Map vm = (Map) vo;
try {
// 特选
vs = Synt.asSet(vm.get(Cnst.AR_KEY));
if (vs != null && !vs.isEmpty()) {
Map vz = new HashMap(vs.size());
for (Object v : vs) {
Range s = new Range(v);
Ratio r = new Ratio();
vz.put(s, r);
}
counts.put(k, vz);
}
// 排除
vs = Synt.asSet(vm.get(Cnst.NR_KEY));
if (vs != null && !vs.isEmpty()) {
Set vz = new HashSet(vs.size());
for (Object v : vs) {
Range s = new Range(v);
// Ratio r = new Ratio( );
vz.add(s);
}
countx.put(k, vz);
}
} catch (ClassCastException ex) {
// 区间格式不对
throw new HongsException(400, ex);
}
// 分块条件
if (ln != true)
vs = null;
else {
vs = Synt.asSet(vm.get(Cnst.RG_REL));
if (vs != null && !vs.isEmpty()) {
vd = new HashMap(rd);
vm = new HashMap(vm);
vm.remove(Cnst.RG_REL);
vd.put(k, vm);
}
}
}
if (vs == null || vs.isEmpty()) {
Map<Range, Ratio> vz = counts.get(k);
if (vz != null)
counts2.put(k, vz);
Set<Range> vx = countx.get(k);
if (vz != null)
countx2.put(k, vx);
} else {
counts3.clear();
countx3.clear();
Map<Range, Ratio> vz = counts.get(k);
if (vz != null)
counts3.put(k, vz);
Set<Range> vx = countx.get(k);
if (vz != null)
countx3.put(k, vx);
amount(vd, finder, counts3, countx3);
}
}
int n = amount(rd, finder, counts2, countx2);
Map cnts = new HashMap();
cnts.put("__count__", n);
// ** 排序并截取统计数据 **/
// Top N
int rn = Synt.declare(rd.get(Cnst.RN_KEY), 0);
int od = 0;
if (ob != null) {
if (ob.contains("!") || ob.contains("-")) {
// 默认逆序
od = 2;
} else if (ob.contains("*")) {
// 默认正序
od = 1;
}
}
for (Map.Entry<String, Map<Range, Ratio>> et : counts.entrySet()) {
String k = et.getKey();
Map<Range, Ratio> m = et.getValue();
List<Object[]> a = new ArrayList(m.size());
for (Map.Entry<Range, Ratio> e : et.getValue().entrySet()) {
Range v = e.getKey();
Ratio c = e.getValue();
if (c.cnt != 0) {
a.add(new Object[] { v, null, c.cnt, c.sum, c.min, c.max });
}
}
// 排序
if (ob != null && !ob.isEmpty()) {
if (ob.contains(k + "!") || ob.contains("-" + k)) {
Collections.sort(a, MountD);
} else if (ob.contains(k)) {
Collections.sort(a, MountA);
} else if (od == 2) {
Collections.sort(a, MountD);
} else if (od == 1) {
Collections.sort(a, MountA);
}
}
// 截选 Top N
n = Dict.getValue(rd, rn, k, Cnst.RN_KEY);
if (n > 0 && n < a.size()) {
a = a.subList(0, n);
}
cnts.put(k, a);
}
return cnts;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class StatisHelper method assort.
/**
* 聚合统计
*
* <pre>
* rd.ob 为要排序的字段
* rd.rb 中根据字段类型区分维度指标, 维度将作为聚合条件, 指标将用于数值统计
* 字段格式为: filed!func, 未加后缀的字段将作为维度
* 指标后缀有:
* !ratio 计数,求和,最小,最大
* !count 计数
* !sum 求和
* !min 最小
* !max 最大
* !crowd 去重计数
* !first 取首个值
* !flock 取全部值
* 特殊维度有:
* !range,!scope 区间, 通过 rd.fn.ar 指定 fn 的区间集合
* </pre>
*
* @param rd
* @return
* @throws HongsException
*/
public List<Map> assort(Map rd) throws HongsException {
Set<String> rb = Synt.toTerms(rd.get(Cnst.RB_KEY));
Fields fs = this.getGatherFields(rb, rd);
if (null == fs) {
throw new NullPointerException("Assort fields required");
}
// 查询
List<Map> list;
try {
Query q = that.padQry(rd);
if (4 == (4 & Core.DEBUG)) {
CoreLogger.debug("StatisHelper.assort: " + q.toString());
}
IndexSearcher finder = that.getFinder();
StatisGather.Fetch c = new StatisGather.Fetch(fs.dimans, fs.indics);
finder.search(q, c);
list = c.getResult();
} catch (IOException e) {
throw new HongsException(e);
}
Set<String> ob = Synt.toTerms(rd.get(Cnst.OB_KEY));
if (ob == null || ob.isEmpty()) {
return list;
}
// 排序
String[] sb = ob.toArray(new String[ob.size()]);
Orders sort = new Orders(sb);
Collections.sort(list, sort);
return list;
}
Aggregations