use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class LuceneRecord method getAnalyzer.
/**
* 存储分析器
* @param fc 字段配置
* @return
* @throws HongsException
*/
protected Analyzer getAnalyzer(Map fc) throws HongsException {
try {
CustomAnalyzer.Builder cb = CustomAnalyzer.builder();
String kn, an, ac;
Map oc;
// 分词器
an = Synt.declare(fc.get("lucene-tokenizer"), "");
if (!"".equals(an)) {
int p = an.indexOf('{');
if (p != -1) {
ac = an.substring(p);
an = an.substring(0, p - 1).trim();
oc = Synt.asMap(Dawn.toObject(ac));
cb.withTokenizer(an, oc);
} else {
cb.withTokenizer(an);
}
} else {
cb.withTokenizer("Standard");
}
// 过滤器
for (Object ot2 : fc.entrySet()) {
Map.Entry et2 = (Map.Entry) ot2;
kn = (String) et2.getKey();
// 存储参数为 char,token
if (kn.startsWith("lucene-char-filter")) {
an = (String) et2.getValue();
an = an.trim();
if ("".equals(an)) {
continue;
}
int p = an.indexOf('{');
if (p != -1) {
ac = an.substring(p);
an = an.substring(0, p - 1).trim();
oc = Synt.asMap(Dawn.toObject(ac));
cb.addCharFilter(an, oc);
} else {
cb.addCharFilter(an);
}
} else if (kn.startsWith("lucene-token-filter")) {
an = (String) et2.getValue();
an = an.trim();
if ("".equals(an)) {
continue;
}
int p = an.indexOf('{');
if (p != -1) {
ac = an.substring(p);
an = an.substring(0, p - 1).trim();
oc = Synt.asMap(Dawn.toObject(ac));
cb.addTokenFilter(an, oc);
} else {
cb.addTokenFilter(an);
}
}
}
return cb.build();
} catch (IOException ex) {
throw new HongsException(ex);
} catch (IllegalArgumentException ex) {
throw new HongsException(ex);
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class LuceneCmdlet method update.
@Cmdlet("update")
public static void update(String[] args) throws HongsException {
Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "id*s", "!A" });
args = (String[]) opts.get("");
String conf = Synt.asString(opts.remove("conf"));
String name = Synt.asString(opts.remove("name"));
List<String> ds = Synt.asList(opts.remove("id"));
LuceneRecord so = LuceneRecord.getInstance(conf, name);
Map rd = data(args[0]);
try {
so.begin();
for (String id : ds) {
so.put(id, rd);
}
so.commit();
} catch (HongsException ex) {
so.revert();
throw ex;
} finally {
so.close();
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class StatisHelper method acount.
/**
* 分类计数
*
* <pre>
* rd.rn 为统计结果长度
* rd.rb 为要统计的字段
* rd.ob 为要排序的字段
* rd.ab 含 linked 时 rd.fn.in 对应字段 fn 分块统计
* rd.fn.rn 可单独指定字段统计长度
* rd.fn.ar 可指定要统计的取值集合
* rd.fn.nr 可指定要忽略的取值集合(只是不作统计, 并非查询约束)
* </pre>
*
* @param rd
* @return
* @throws HongsException
*/
public Map acount(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<Object, Long>> counts = new HashMap(rl);
// 排除
Map<String, Set<Object>> countx = new HashMap(rl);
Map<String, Map<Object, Long>> counts2 = new HashMap(rl);
Map<String, Set<Object>> countx2 = new HashMap(rl);
Map<String, Map<Object, Long>> counts3 = new HashMap(rl);
Map<String, Set<Object>> countx3 = new HashMap(rl);
if (rb != null && !rb.isEmpty())
for (String k : rb) {
Function f = getGraderFormat(k);
if (null == f) {
throw new HongsException(400, "Field " + f + " is not exists");
}
// 默认统计
counts.put(k, new HashMap());
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) {
Object s = f.apply(v);
vz.put(s, 0L);
}
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) {
Object s = f.apply(v);
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.IN_REL));
if (vs != null && !vs.isEmpty()) {
vd = new HashMap(rd);
vm = new HashMap(vm);
vm.remove(Cnst.IN_REL);
vd.put(k, vm);
}
}
}
if (vs == null || vs.isEmpty()) {
Map<Object, Long> vz = counts.get(k);
if (vz != null)
counts2.put(k, vz);
Set<Object> vx = countx.get(k);
if (vx != null)
countx2.put(k, vx);
} else {
counts3.clear();
countx3.clear();
Map<Object, Long> vz = counts.get(k);
if (vz != null)
counts3.put(k, vz);
Set<Object> vx = countx.get(k);
if (vx != null)
countx3.put(k, vx);
acount(vd, finder, counts3, countx3);
}
}
int n = acount(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<Object, Long>> et : counts.entrySet()) {
String k = et.getKey();
Map<Object, Long> m = et.getValue();
List<Object[]> a = new ArrayList(m.size());
for (Map.Entry<Object, Long> e : et.getValue().entrySet()) {
Object v = e.getKey();
long c = e.getValue();
if (c != 0) {
a.add(new Object[] { v, null, c });
}
}
// 排序
if (ob != null && !ob.isEmpty()) {
if (ob.contains(k + "!") || ob.contains("-" + k)) {
Collections.sort(a, CountD);
} else if (ob.contains(k)) {
Collections.sort(a, CountA);
} else if (od == 2) {
Collections.sort(a, CountD);
} else if (od == 1) {
Collections.sort(a, CountA);
}
}
// 截选 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 Data method getFields.
/**
* 获取字段
* 当前表单不在管理区之内时,
* 会用当前表单覆盖管理表单,
* 配置文件不存在则抛出异常 404
* @return
*/
@Override
public Map getFields() {
try {
return gotFields();
} catch (NullPointerException ex) {
// 使用超类管理字段
// 拿不到就进行填充
}
/**
* 字段以 centra/data 的字段为基础
* 但可在 centre/data 重设部分字段
*
* 配置文件不得放在资源包里面
* 此处会校验表单文件是否存在
*/
Map fields = null;
Map fieldx = null;
String cnf = conf;
do {
try {
fields = FormSet.getInstance(cnf).getForm(form);
} catch (HongsException ex) {
if (ex.getErrno() != 910 && ex.getErrno() != 912) {
// 非表单缺失
throw ex.toExemption();
}
break;
}
if (fields == null) {
break;
}
cnf = getBgConf();
if (cnf.equals(conf)) {
break;
}
try {
fieldx = FormSet.getInstance(cnf).getForm(form);
} catch (HongsException ex) {
if (ex.getErrno() != 910 && ex.getErrno() != 912) {
// 非表单缺失
throw ex.toExemption();
}
break;
}
if (fieldx == null) {
break;
}
if (fieldx.isEmpty()) {
break;
}
/**
* 注意:
* 1. 不可破坏原始配置
* 2. 当前的覆盖后台的
*/
fieldx = new LinkedHashMap(fieldx);
for (Object ot : fields.entrySet()) {
Map.Entry et = (Map.Entry) ot;
String fn = (String) et.getKey();
Map fc = (Map) et.getValue();
Map fx = (Map) fieldx.get(fn);
if (fx != null) {
fx = new LinkedHashMap(fx);
fx.putAll(fc);
fieldx.put(fn, fx);
} else {
fieldx.put(fn, fc);
}
}
fields = fieldx;
} while (false);
if (null == fields) {
throw new HongsExemption(910, "Data form '" + conf + "." + form + "' is not exists").setLocalizedContent("matrix.form.not.exists").setLocalizedContext("matrix");
}
setFields(fields);
return fields;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Data method del.
/**
* 删除记录
*
* 注意:
* 删除时产生新节点,
* 重复调用不会多增.
*
* @param id
* @param rd
* @param ctime
* @return 有更新为 1, 无更新为 0
* @throws HongsException
*/
public int del(String id, Map rd, long ctime) throws HongsException {
delDoc(id);
Table table = getTable();
if (table == null) {
return 1;
}
String uid = getUserId();
String fid = getFormId();
Object[] param = new String[] { id, fid, "0" };
String where = "`id`=? AND `form_id`=? AND `etime`=?";
Map od = table.fetchCase().filter(where, param).select("ctime,state,data,name").getOne();
if (od.isEmpty() || Synt.declare(od.get("state"), 0) == 0) {
// 删除是幂等的可重复调用
return 0;
}
if (Synt.declare(od.get("ctime"), 0L) >= ctime) {
throw new HongsException(400, "Wait 1 second to del '" + id + "' in " + getDbName()).setLocalizedContent("matrix.wait.one.second").setLocalizedContext("matrix");
}
Map ud = new HashMap();
ud.put("etime", ctime);
Map nd = new HashMap();
nd.put("ctime", ctime);
nd.put("etime", 0);
nd.put("state", 0);
nd.put("id", id);
nd.put("form_id", fid);
nd.put("user_id", uid);
// 拷贝快照和日志标题
nd.put("data", od.get("data"));
nd.put("name", od.get("name"));
// 操作备注和终端代码
if (rd.containsKey("memo")) {
nd.put("memo", getText(rd, "memo"));
}
if (rd.containsKey("meno")) {
nd.put("meno", getText(rd, "meno"));
}
table.update(ud, where, param);
table.insert(nd);
return 1;
}
Aggregations