From 1fabbae4f6b055295dc9370bd7e246e61fcc5bbb Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 28 Nov 2016 10:43:38 +0800 Subject: [PATCH] --- test/org/redkale/test/source/LoginRecord.java | 10 +- .../org/redkale/test/source/LoginRecord2.java | 158 ++++++++++++++++++ .../redkale/test/source/LoginUserRecord.java | 92 ++++++++++ 3 files changed, 255 insertions(+), 5 deletions(-) create mode 100644 test/org/redkale/test/source/LoginRecord2.java create mode 100644 test/org/redkale/test/source/LoginUserRecord.java diff --git a/test/org/redkale/test/source/LoginRecord.java b/test/org/redkale/test/source/LoginRecord.java index 74c577f73..89f5d1da0 100644 --- a/test/org/redkale/test/source/LoginRecord.java +++ b/test/org/redkale/test/source/LoginRecord.java @@ -19,7 +19,7 @@ public class LoginRecord extends BaseEntity { @Id @GeneratedValue @Column(comment = "UUID") - private String seqid = ""; //UUID + private String loginid = ""; //UUID @Column(updatable = false, comment = "C端用户ID") private long userid; //C端用户ID @@ -44,12 +44,12 @@ public class LoginRecord extends BaseEntity { /** 以下省略getter setter方法 */ // - public void setSeqid(String seqid) { - this.seqid = seqid; + public void setLoginid(String loginid) { + this.loginid = loginid; } - public String getSeqid() { - return this.seqid; + public String getLoginid() { + return this.loginid; } public void setUserid(long userid) { diff --git a/test/org/redkale/test/source/LoginRecord2.java b/test/org/redkale/test/source/LoginRecord2.java new file mode 100644 index 000000000..b7acdd4db --- /dev/null +++ b/test/org/redkale/test/source/LoginRecord2.java @@ -0,0 +1,158 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.test.source; + +import java.io.Serializable; +import javax.persistence.*; +import org.redkale.source.*; +import org.redkale.util.Utility; + +/** + * + * @author zhangjx + */ +@DistributeTable(strategy = LoginRecord2.TableStrategy.class) +public class LoginRecord2 extends BaseEntity { + + @Id + @Column(comment = "主键ID; 值=UUID+create36time") + private String loginid = ""; //主键ID; 值=UUID+create36time + + @Column(updatable = false, comment = "C端用户ID") + private long userid; //C端用户ID + + @Column(updatable = false, comment = "登录网络类型; wifi/4g/3g") + private String netmode = ""; //登录网络类型; wifi/4g/3g + + @Column(updatable = false, comment = "APP版本信息") + private String appversion = ""; //APP版本信息 + + @Column(updatable = false, comment = "APP操作系统信息") + private String appos = ""; //APP操作系统信息 + + @Column(updatable = false, comment = "登录时客户端信息") + private String loginagent = ""; //登录时客户端信息 + + @Column(updatable = false, comment = "登录时的IP") + private String loginaddr = ""; //登录时的IP + + @Column(updatable = false, comment = "创建时间") + private long createtime; //创建时间 + + /** 以下省略getter setter方法 */ + // + public void setLoginid(String loginid) { + this.loginid = loginid; + } + + public String getLoginid() { + return this.loginid; + } + + public void setUserid(long userid) { + this.userid = userid; + } + + public long getUserid() { + return this.userid; + } + + public void setNetmode(String netmode) { + this.netmode = netmode; + } + + public String getNetmode() { + return this.netmode; + } + + public String getAppversion() { + return appversion; + } + + public void setAppversion(String appversion) { + this.appversion = appversion; + } + + public String getAppos() { + return appos; + } + + public void setAppos(String appos) { + this.appos = appos; + } + + public void setLoginagent(String loginagent) { + this.loginagent = loginagent; + } + + public String getLoginagent() { + return this.loginagent; + } + + public void setLoginaddr(String loginaddr) { + this.loginaddr = loginaddr; + } + + public String getLoginaddr() { + return this.loginaddr; + } + + public void setCreatetime(long createtime) { + this.createtime = createtime; + } + + public long getCreatetime() { + return this.createtime; + } + + //创建对象 + public static void main(String[] args) throws Throwable { + LoginRecord2 record = new LoginRecord2(); + long now = System.currentTimeMillis(); + record.setCreatetime(now); //设置创建时间 + String create36time = Long.toString(now, 36); //时间的36进制 + if (create36time.length() < 9) create36time = "0" + create36time; //当前时间值的36进制只可能是8位或9位,不足9位填充0 + record.setLoginid(Utility.uuid() + create36time); //主键的生成策略 + //.... 填充其他字段 + //source.insert(record); + } + + public static class TableStrategy implements DistributeTableStrategy { + + private static final String dayformat = "%1$tY%1$tm%1$td"; + + private static final String yearformat = "%1$tY"; + + //过滤查询时调用本方法 + @Override + public String getTable(String table, FilterNode node) { + Serializable day = node.findValue("#day"); + if (day != null) getTable(table, (Integer) day, 0L); //存在#day参数则直接使用day值 + Serializable time = node.findValue("#createtime"); //存在createtime则使用最小时间,且createtime的范围必须在一天内,因为本表以天为单位建表 + return getTable(table, 0, (time == null ? 0L : (time instanceof Range ? ((Range.LongRange) time).getMin() : (Long) time))); + } + + //创建或单个查询时调用本方法 + @Override + public String getTable(String table, LoginRecord2 bean) { + return getTable(table, 0, bean.getCreatetime()); + } + + //根据主键ID查询单个记录时调用本方法 + @Override + public String getTable(String table, Serializable primary) { + String loginid = (String) primary; + String create36time = loginid.substring(loginid.length() - 9); //固定最后9位为创建时间的36进制值 + return getTable(table, 0, Long.parseLong(create36time, 36)); + } + + private String getTable(String table, int day, long createtime) { + int pos = table.indexOf('.'); + String year = (day > 0 ? "" + day / 10000 : String.format(yearformat, createtime)); + return "platf_login_" + year + "." + table.substring(pos + 1) + "_" + (day > 0 ? day : String.format(dayformat, createtime)); + } + } +} diff --git a/test/org/redkale/test/source/LoginUserRecord.java b/test/org/redkale/test/source/LoginUserRecord.java new file mode 100644 index 000000000..6bb4d7e0f --- /dev/null +++ b/test/org/redkale/test/source/LoginUserRecord.java @@ -0,0 +1,92 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.test.source; + +import java.io.Serializable; +import javax.persistence.*; +import org.redkale.source.*; + +/** + * + * @author zhangjx + */ +@DistributeTable(strategy = LoginUserRecord.TableStrategy.class) +public class LoginUserRecord extends BaseEntity { + + @Id + @GeneratedValue + @Column(comment = "UUID") + private String seqid = ""; //UUID + + @Column(updatable = false, comment = "C端用户ID") + private long userid; //C端用户ID + + @Column(comment = "LoginRecord主键") + private String loginid = ""; //LoginRecord主键 + + @Column(updatable = false, comment = "创建时间") + private long createtime; //创建时间 + + /** 以下省略getter setter方法 */ + // + public String getSeqid() { + return seqid; + } + + public void setSeqid(String seqid) { + this.seqid = seqid; + } + + public long getUserid() { + return userid; + } + + public void setUserid(long userid) { + this.userid = userid; + } + + public String getLoginid() { + return loginid; + } + + public void setLoginid(String loginid) { + this.loginid = loginid; + } + + public long getCreatetime() { + return createtime; + } + + public void setCreatetime(long createtime) { + this.createtime = createtime; + } + + public static class TableStrategy implements DistributeTableStrategy { + + @Override + public String getTable(String table, LoginUserRecord bean) { + return getTable(table, bean.getUserid()); + } + + @Override + public String getTable(String table, FilterNode node) { + Serializable id = node.findValue("userid"); + if (id != null) return getTable(table, id); + return getHashTable(table, (Integer) node.findValue("#hash")); + } + + @Override + public String getTable(String table, Serializable userid) { + return getHashTable(table, (int) (((Long) userid) % 100)); + } + + private String getHashTable(String table, int hash) { + int pos = table.indexOf('.'); + return "platf_login." + table.substring(pos + 1) + "_" + (hash > 9 ? hash : ("0" + hash)); + } + + } +}