one ,
What you have there is a way to replace the UUID, to ensure that no duplication world !
Ps: Needless to say, with time + random characters , that is to say , the technology say that there may be repeated ! Needless to say
Base64 or Base58, this is also based on the UUID
now talking about is what you make of "UUID"
two ,
Oracle has Sequence, then how do you do Sequence in the case , to make a similar Sequence effect? Let's say I have the greatest current Id is 5, then the next one should be is six , but I may have one second of data into the millions , how do you achieve it
------ Solution ----- ---------------------------------------
one , except you said two kinds of I can not think , wait for the answer.
Second, you can write your own Sequence Builder.
I used item :
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
/**
*
* <b>Note</b>: Java实现的Sequence工具
*/
public class SequenceUtil {
/**
* 单例模式
*/
private static SequenceUtil instance = new SequenceUtil();
/**
* 存放序列的MAP
*/
private Map<String, KeyInfo> keyMap = new HashMap<String, KeyInfo>(20);
private static final int POOL_SIZE = 1;
/**
* 防止外部实例化
*/
private SequenceUtil() {
}
/**
* 单例模式,获取单例
*
* @return SequenceUtils的单例对象
*/
public static SequenceUtil getInstance() {
return instance;
}
/**
* 获取下一个sequence值
*
* @param keyName
* Sequence名称
* @return 下一个Sequence键值
*/
public synchronized int getNextKeyValue(String keyName) {
KeyInfo keyInfo = null;
Integer keyObject = null;
try {
if (keyMap.containsKey(keyName)) {
keyInfo = keyMap.get(keyName);
} else {
keyInfo = new KeyInfo(keyName, POOL_SIZE);
keyMap.put(keyName, keyInfo);
}
keyObject = keyInfo.getNextKey();
} catch (SQLException e) {
e.printStackTrace();
}
return keyObject;
}
}
class KeyInfo {
/**
* 当前Sequence载体的最大值
*/
private int maxKey;
/**
* 当前Sequence的最小值
*/
private int minKey;
/**
* 下一个Sequence值
*/
private int nextKey;
/**
* Sequence缓存值
*/
private int poolSize;
/**
* Sequence的名称
*/
private String keyName;
/**
* 更新存放Sequence表的语句
*/
private static final String sql_update = "UPDATE intpub_Sequence SET KEYVALUE = KEYVALUE + ? WHERE KEYNAME = ?";
/**
* 查询Sequence表中的当前值
*/
private static final String sql_query = "SELECT KEYVALUE FROM intpub_Sequence WHERE KEYNAME = ?";
public KeyInfo(String keyName, int poolSize) throws SQLException {
this.poolSize = poolSize;
this.keyName = keyName;
retrieveFromDB();
}
public String getKeyName() {
return keyName;
}
public int getMaxKey() {
return maxKey;
}
public int getMinKey() {
return minKey;
}
public int getPoolSize() {
return poolSize;
}
/**
* 获取下一个Sequence值
*
* @return 下一个Sequence值
* @throws SQLException
*/
public synchronized int getNextKey() throws SQLException {
if (nextKey > maxKey) {
retrieveFromDB();
}
return nextKey++;
}
/**
* 执行Sequence表初始化和更新工作
*
* @throws SQLException
*/
private void retrieveFromDB() throws SQLException {
Connection conn = ConnectionPool.getInstance().getConnection();
// 查询数据库
PreparedStatement pstmt_query = conn.prepareStatement(sql_query);
pstmt_query.setString(1, keyName);
ResultSet rs = pstmt_query.executeQuery();
if (rs.next()) {
maxKey = rs.getInt(1) + poolSize;
minKey = maxKey - poolSize + 1;
nextKey = minKey;
rs.close();
pstmt_query.close();
} else {
String init_sql = "INSERT INTO intpub_Sequence(KEYNAME,KEYVALUE) VALUES('"
+ keyName + "',10000 + " + poolSize + ")";
Statement stmt = conn.createStatement();
stmt.executeUpdate(init_sql);
maxKey = 10000 + poolSize;
minKey = maxKey - poolSize + 1;
nextKey = minKey;
stmt.close();
return;
}
// 更新数据库
conn.setAutoCommit(false);
PreparedStatement pstmt_up = conn.prepareStatement(sql_update);
pstmt_up.setLong(1, poolSize);
pstmt_up.setString(2, keyName);
pstmt_up.executeUpdate();
pstmt_up.close();
conn.commit();
rs.close();
pstmt_query.close();
conn.close();
}
}
------ Solution ------------------------------------- -------
recommended to see if the answer to the first question !
------ Solution ---------------------------------------- ----
ObjectID of algorithms using MONGODB it , so far I encountered the most elegant ID generation strategy ....
------ Solution ---------- ----------------------------------
Can you explain , I use mongodb, but did not go to this id generation principle studied
In order to explain to you I went back to the book turned a bit afraid interpretation was wrong to mislead you . .
MongDB the id using 12 -byte ( 24 characters ) to handle
0123 | 456 | 78 | 91011 |
stamp machine PID counter
This sort and slice , very good handling , sorting algorithms generally look at the length of the character , followed by a character from the first than the size .
By such a design can be a good random code to complete this sort by slicing through the counter to do to ensure that data is distributed evenly
------ For reference only ------- --------------------------------
time + random number that is not a problem , how it may be repeated
------ For reference only -------------------------------------- -
justified top one
------ For reference only ------------------------------ ---------
simple terms is equivalent to add a counter , and then sync to control , and has been getNext ... probably so , and rarely 100W instances of simultaneous requests to request getNext method , select 'd have a chance , so with the above said synchronization control getNext value is still possible, but I think it should be after the start of the project should first read the database table maximum, and then assigned to the variable , it is impossible to access the database on every request to get the next value .. this overhead not
------ For reference only ---------------------------------- -----
Yes Yes, Dingding
------ For reference only ------------------------- --------------
own can certainly write out such card number + time + ms + random string of random numbers this is certainly unique
but meaningful? Or is it necessary?
Are you sure you want to write their own is better than Sun's written ? Including time and space
Since you did not write their own people's good Why fart pants
If anything else to write their own significance lies in the JDK where
------ For reference only ------------------- --------------------
1 time + random characters also can not guarantee anything . UUID is the mac address + system clock , global synchronization , it is still counted as part mac address , part of the system clock counted out to fight together .
2 As long as the effect , I do not do anything , you need to look at the sequence when I wrote a function to rowid mapped into digital for you to see .
------ For reference only -------------------------------------- -
HASH (MAC address ) + time to the millisecond
------ For reference only ------------------ ---------------------
for the first question , a lot of people say Mac + milliseconds.
But Mac is really OK thing ? I am not sure I am using Mac modifier to modify what is not physical for Mac, but I modified the same Mac and my roommate , we connect two routers , who, after getting on the Internet connection who , since Mac repeat !
The number of milliseconds , this is even more pit :
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class TestTimes {
public static void main (String args []) {
SimpleDateFormat formatter = new SimpleDateFormat (
"yyyy-MM-dd HH: mm: ss: SSS");
int size = 4000000;
long times [] = new long [size];
for (int i = 0; i
long time = times [0];
long previousTime = times [0];
long count = 0;
Set
long delay = 0;
long mindelay = Long.MAX_VALUE;
long maxdelay = Long.MIN_VALUE;
for (int i = 0; i
delay = time - previousTime;
delays.add (delay);
if (delay> 0 && delay
} else if (delay> maxdelay) {
maxdelay = delay;
}
System.out.print ("raw =");
System.out.print (time);
System.out.print ("| formatted =");
System.out.print (formatter.format (new Date (time)));
System.out.print ("| frequency =");
System.out.print (count);
System.out.print ("| delay =");
System.out.print (delay);
System.out.println ("ms");
previousTime = time;
time = times [i];
count = 0;
} else {
count + +;
}
}
System.out.println ("");
System.out.println ("Minimum delay:" + mindelay + "ms");
System.out.println ("Maximum delay:" + maxdelay + "ms");
}
}
This is the Internet directly to the test code , I tested a few times , the results appalling ah :
------ For reference only ----- ----------------------------------
ah , write your own Sequence good ! I also wrote a try ...
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------------
read ah ..
------ For reference only ------------------------------- --------
previously used : the user id + '-' + time + random characters , normal use is not going to repeat , mainly in order to distinguish between users . uuid easy to use , why should create their own one . .
------ For reference only -------------------------------------- -
wrong problem , generating similar functionality uuid underlying bias , really do not understand . .
------ For reference only -------------------------------------- -
try line does not thank
------ For reference only -------------------------- -------------
1, oracle sequence increment
2, using the oracle comes SYS_UID ()
3, Java comes random.getUUID (). toSring ()
------ For reference only ------------------- --------------------
------ For reference only ---------------------------------- -----
Yes Yes, Dingding
------ For reference only ----------------------- ----------------
days to participate in re-examination , the technology to ask :
one ,
What you have there is a way to replace the UUID, to ensure that no duplication world !
Ps: Needless to say, with time + random characters , that is to say , the technology say that there may be repeated ! Needless to say
Base64 or Base58, this is also based on
------ For reference only --------------------- ------------------
say not to use a self-energizing ; comes is sys_guid () , right ? ; Java comes with a UUID.randomUUID (). ToString () , right ?
------ For reference only ---------------------------------------
good esoteric things ah
------ For reference only --------------------------------------- < br>
Can you explain , I use mongodb, but did not go to this id generation principle studied
------ For reference only --------------- ------------------------
wait LZ give points to the sub . . . . .
------ For reference only -------------------------------------- -
This looked good, something today , come back at night under study , is good knot stickers ... But then my first post ... how does it still knot .
----- - For reference only ---------------------------------------
I talk about my idea:
when the application starts to generate a set of random numbers into the SET ( eg 5000 ) , the SET little memory for it, then use it with the user ID + current time + SET where one of the elements , if No user login system to use the current time + SET where one of the elements , only personal thoughts !
------ For reference only -------------------------------------- -
What are you saying UUID? Or that sequence it ?
------ For reference only -------------------------------------- -
This is the standard answer haha
------ For reference only --------------------------- ------------
What are you saying UUID? Or that sequence it ?
UUID
------ For reference only --------------------------------- ------
time + random number that is not a problem , how may have to repeat it
------ For reference only -------------- -------------------------
time the possibility of duplication can be almost 100%, and the possibility of a non- random number small right ?
------ For reference only ---------------------------------- -----
you say is not the UUID GUID
------ For reference only ---------------------------------- -----
constructor in :
long l = l + +; such , is not enough to use arrays
then set a buffer = 1000, data for each additional 1000 to write once disc . On the next start time data from the last recorded data +1000 starts counting.
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
difference
没有评论:
发表评论