加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

id生成器

发布时间:2020-12-12 20:39:56 所属栏目:百科 来源:网络整理
导读:在android的Sqlite中对于INTEGERPRIMARYKEY, 如果插入数据A,B,C,它们的_id为1,2,3,那么如果把他们都删除了,再插入一条数据,那么它的id为1而不是4。 因此我就写了这个工具来生成id,它能让生成相同的id相隔尽量的远。 SerialManager.java文件 packag
在android的Sqlite中对于INTEGERPRIMARYKEY,
如果插入数据A,B,C,它们的_id为1,2,3,那么如果把他们都删除了,再插入一条数据,那么它的id为1而不是4。
因此我就写了这个工具来生成id,它能让生成相同的id相隔尽量的远。

SerialManager.java文件
packagecom.teleca;

publicclass SerialManager {
Noderoot=null;
longmin=Long.MIN_VALUE;
longmax=Long.MAX_VALUE;
longcursor=1;
public SerialManager ()
{
}
publicSerialManager(longmin,longmax)
{
if(min>max)
{
longtemp=min;
min=max;
max=temp;
}
this.min=min;
this.max=max;
}
publicvoid setSerial(longid,booleanflag)
{
longtemp=0;
Nodenode=findNode(id);
if (flag)
{
if(node==null)
{
node=findNode(id-1);
if (node!=null)
{
node.data.end=id;
Nodenode2=findNode(id+1);
if (node2!=null) { node.data.end=node2.data.end; this.removeNode(node2); } } else { Nodenode2=findNode(id+1); if(node2!=null) { node2.data.start=id; } else addNewNode(id,id); } } return; } else { if(node==null) return; Blockdata=node.data; if(data.start==data.end) { removeNode(node); } elseif(id==data.start) data.start=id+1; elseif(id==data.end) data.end=id-1; else { temp=data.end; data.end=id-1; addNewNode(id+1,temp); } } } publicvoidsetSerial(longid1,longid2) { longtemp=0; if(id1>id2) { temp=id1; id1=id2; id2=temp; } Nodenode1=findNode(id1); Nodenode2=null; if(node1==null) { node1=findNode(id1-1); if(node1!=null) { node1.data.end=id2; node2=findNode(id2+1); if(node2!=null) { node1.data.end=node2.data.end; this.removeNode(node2); } return; } else { node2=findNode(id2); if(node2==null) { node2=findNode(id2+1); if(node2!=null) node2.data.start=id1; else addNewNode(id1,id2); return; } else { node2.data.start=id1; return; } } } else { Blockdata=node1.data; if(id2<=data.end) { return; } else { node2=findNode(id2); if(node2==null) { data.end=id2; return; } else { data.end=node2.data.end; removeNode(node2); } } } } publiclonggetSerial() { Nodenode=findNode(cursor); longstart=cursor; while(node!=null) { cursor=node.data.end+1; if(cursor>max) cursor=min; elseif(cursor==0) cursor++; if(cursor==start) { return0; } node=findNode(cursor); } longres=cursor; cursor++; if(cursor>max) cursor=min; elseif(cursor==0) cursor++; returnres; } publicbooleanisKeyUsed(longid) { returnfindNode(id)!=null; } privateNodefindNode(longid) { Nodenode=null; NodetempNode=root; Blockblock=null; while(tempNode!=null) { block=tempNode.data; if(block.start<=id&&id<=block.end) { node=tempNode; break; } tempNode=tempNode.next; } returnnode; } privatevoidaddNewNode(longid1,longid2) { Nodenode=newNode(); node.data=newBlock(id1,id2); addNode(node); } privatevoidaddNode(Nodenode) { if(root==null) { root=node; node.prev=null; node.next=null; return; } NodetempNode=root; while(tempNode!=null) { if(tempNode.data.start>node.data.end) { if(tempNode==root) { node.prev=null; node.next=root; tempNode.prev=node; root=node; } else { node.prev=tempNode.prev; node.next=tempNode; tempNode.prev.next=node; tempNode.prev=node; } break; } elseif(tempNode.next==null) { tempNode.next=node; node.prev=tempNode; node.next=null; break; } tempNode=tempNode.next; } } privatevoidremoveNode(Nodenode) { Nodeprev=node.prev; if(prev==null) { root=node.next; } else { prev.next=node.next; } if(node.next!=null) node.next.prev=prev; node.prev=null; node.next=null; } publicvoidclear() { NodetempNode=root; Nodenode=null; while(tempNode!=null) { node=tempNode; tempNode=tempNode.next; node.prev=null; node.next=null; } root=null; cursor=1; } publicvoidprintln() { NodetempNode=root; while(tempNode!=null) { System.out.println("start:"+tempNode.data.start+"end:"+tempNode.data.end); tempNode=tempNode.next; } } } classNode { Nodeprev=null; Blockdata; Nodenext=null; } classBlock { longstart=0; longend=0; Block(longid1,longid2) { start=id1; end=id2; } publiclonggetStart(){ returnstart; } publicvoidsetStart(longstart){ this.start=start; } publiclonggetEnd(){ returnend; } publicvoidsetEnd(longend){ this.end=end; } } 测试程序如下: importjava.util.Random; importcom.teleca.SerialManager; publicclasstestimplementsRunnable{ staticRandomrandom=newRandom(System.currentTimeMillis()); staticlongbuffer[]=newlong[10]; staticintindex=0; staticlongcounter[]=newlong[100]; staticintcountIndex=0; staticSerialManagerserialManager=newSerialManager(Short.MIN_VALUE,Short.MAX_VALUE); publicvoidrun() { inti=0; for(i=0;i<100;i++) serialManager.setSerial(random.nextLong()%Short.MAX_VALUE,true); longtemp=0; booleanblErro=false; booleanblRun=true; while(blRun) { temp=serialManager.getSerial(); for(intj=0;j<buffer.length;j++) { if(buffer[j]!=0&&buffer[j]==temp) { blErro=true; break; } } if(temp==0) { blErro=true; System.out.println("allserialhasbeenused"); } if(blErro) break; buffer[index]=temp; serialManager.setSerial(buffer[index],true); longval=0; for(i=0;i<buffer.length;i++) { val=random.nextLong()%buffer.length; if(val<0) val=-val; val=buffer[(int)val]; if(val==0||serialManager.isKeyUsed(val)) break; } serialManager.setSerial(val,false); index++; index=index%buffer.length; if(counter[countIndex]++>=Short.MAX_VALUE) { countIndex++; System.out.println("countIndex:"+countIndex); if(countIndex>=counter.length) { blRun=false; countIndex=counter.length-1; } } if(counter[countIndex]%1000==0) { System.out.println(temp+"countIndex:"+countIndex+"num:"+counter[countIndex]); try{ Thread.sleep(1); }catch(Exceptione) { e.printStackTrace(); } } } if(blErro) { System.out.println("erro:---------"+temp); serialManager.println(); } else System.out.println("Itpassedthetest!"); } /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Threadt=newThread(newtest()); t.start(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读