Skip to content

Commit

Permalink
使用ConcurrentHashMap避免新增或查找User、Role时产生的同步问题
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Oct 19, 2015
1 parent f32d8bb commit a91b349
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lealone-db/src/main/java/org/lealone/db/Database.java
Expand Up @@ -729,8 +729,8 @@ public synchronized void removeMeta(ServerSession session, int id) {
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private HashMap<String, DbObject> getMap(int type) { private Map<String, DbObject> getMap(int type) {
HashMap<String, ? extends DbObject> result; Map<String, ? extends DbObject> result;
switch (type) { switch (type) {
case DbObject.USER: case DbObject.USER:
result = Auth.getUsersMap(); result = Auth.getUsersMap();
Expand Down Expand Up @@ -792,7 +792,7 @@ public synchronized void addDatabaseObject(ServerSession session, DbObject obj)
if (id > 0 && !starting) { if (id > 0 && !starting) {
checkWritingAllowed(); checkWritingAllowed();
} }
HashMap<String, DbObject> map = getMap(obj.getType()); Map<String, DbObject> map = getMap(obj.getType());
// if (obj.getType() == DbObject.USER) { // if (obj.getType() == DbObject.USER) {
// User user = (User) obj; // User user = (User) obj;
// if (user.isAdmin() && systemUser.getName().equals(SYSTEM_USER_NAME)) { // if (user.isAdmin() && systemUser.getName().equals(SYSTEM_USER_NAME)) {
Expand Down Expand Up @@ -1233,7 +1233,7 @@ private synchronized void updateMetaAndFirstLevelChildren(ServerSession session,
public synchronized void renameDatabaseObject(ServerSession session, DbObject obj, String newName) { public synchronized void renameDatabaseObject(ServerSession session, DbObject obj, String newName) {
checkWritingAllowed(); checkWritingAllowed();
int type = obj.getType(); int type = obj.getType();
HashMap<String, DbObject> map = getMap(type); Map<String, DbObject> map = getMap(type);
if (SysProperties.CHECK) { if (SysProperties.CHECK) {
if (!map.containsKey(obj.getName())) { if (!map.containsKey(obj.getName())) {
DbException.throwInternalError("not found: " + obj.getName()); DbException.throwInternalError("not found: " + obj.getName());
Expand Down Expand Up @@ -1305,7 +1305,7 @@ public synchronized void removeDatabaseObject(ServerSession session, DbObject ob
checkWritingAllowed(); checkWritingAllowed();
String objName = obj.getName(); String objName = obj.getName();
int type = obj.getType(); int type = obj.getType();
HashMap<String, DbObject> map = getMap(type); Map<String, DbObject> map = getMap(type);
if (SysProperties.CHECK && !map.containsKey(objName)) { if (SysProperties.CHECK && !map.containsKey(objName)) {
DbException.throwInternalError("not found: " + objName); DbException.throwInternalError("not found: " + objName);
} }
Expand Down
14 changes: 7 additions & 7 deletions lealone-db/src/main/java/org/lealone/db/LealoneDatabase.java
Expand Up @@ -18,11 +18,11 @@
package org.lealone.db; package org.lealone.db;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;


import org.lealone.common.util.New;
import org.lealone.db.auth.Auth; import org.lealone.db.auth.Auth;


/** /**
Expand All @@ -47,7 +47,7 @@ public static LealoneDatabase getInstance() {
return INSTANCE; return INSTANCE;
} }


private final HashMap<String, Database> databases = New.hashMap(); private final ConcurrentHashMap<String, Database> databases = new ConcurrentHashMap<>();


private LealoneDatabase() { private LealoneDatabase() {
super(0, NAME, null); super(0, NAME, null);
Expand All @@ -74,7 +74,7 @@ protected void initTraceSystem(ConnectionInfo ci) {
Auth.init(this); Auth.init(this);
} }


public synchronized Database findDatabase(String dbName) { public Database findDatabase(String dbName) {
return databases.get(dbName); return databases.get(dbName);
} }


Expand All @@ -85,15 +85,15 @@ synchronized Database createDatabase(String dbName, ConnectionInfo ci) {
return db; return db;
} }


synchronized void closeDatabase(String dbName) { void closeDatabase(String dbName) {
databases.remove(dbName); databases.remove(dbName);
} }


synchronized List<Database> getDatabases() { List<Database> getDatabases() {
return new ArrayList<>(databases.values()); return new ArrayList<>(databases.values());
} }


synchronized HashMap<String, Database> getDatabasesMap() { Map<String, Database> getDatabasesMap() {
return databases; return databases;
} }
} }
15 changes: 8 additions & 7 deletions lealone-db/src/main/java/org/lealone/db/auth/Auth.java
Expand Up @@ -18,7 +18,8 @@
package org.lealone.db.auth; package org.lealone.db.auth;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import org.lealone.api.ErrorCode; import org.lealone.api.ErrorCode;
import org.lealone.common.message.DbException; import org.lealone.common.message.DbException;
Expand All @@ -40,9 +41,9 @@ public class Auth {
*/ */
private static final String SYSTEM_USER_NAME = "DBA"; private static final String SYSTEM_USER_NAME = "DBA";


private static final HashMap<String, Role> roles = New.hashMap(); private static final ConcurrentHashMap<String, Role> roles = new ConcurrentHashMap<>();
private static final HashMap<String, User> users = New.hashMap(); private static final ConcurrentHashMap<String, User> users = new ConcurrentHashMap<>();
private static final HashMap<String, Right> rights = New.hashMap(); private static final ConcurrentHashMap<String, Right> rights = new ConcurrentHashMap<>();


private static User systemUser; private static User systemUser;
private static Role publicRole; private static Role publicRole;
Expand Down Expand Up @@ -82,7 +83,7 @@ public static ArrayList<Role> getAllRoles() {
return New.arrayList(roles.values()); return New.arrayList(roles.values());
} }


public static HashMap<String, Role> getRolesMap() { public static Map<String, Role> getRolesMap() {
return roles; return roles;
} }


Expand Down Expand Up @@ -116,15 +117,15 @@ public static ArrayList<User> getAllUsers() {
return New.arrayList(users.values()); return New.arrayList(users.values());
} }


public static HashMap<String, User> getUsersMap() { public static Map<String, User> getUsersMap() {
return users; return users;
} }


public static ArrayList<Right> getAllRights() { public static ArrayList<Right> getAllRights() {
return New.arrayList(rights.values()); return New.arrayList(rights.values());
} }


public static HashMap<String, Right> getRightsMap() { public static Map<String, Right> getRightsMap() {
return rights; return rights;
} }
} }
Expand Up @@ -364,7 +364,7 @@ public boolean isDDL() {


@Override @Override
public boolean isBatch() { public boolean isBatch() {
return statement.isDDL(); return statement.isBatch();
} }


@Override @Override
Expand Down
4 changes: 2 additions & 2 deletions lealone-sql/src/main/java/org/lealone/sql/ddl/CreateUser.java
Expand Up @@ -77,7 +77,7 @@ static void setPassword(User user, ServerSession session, Expression password) {
char[] passwordChars = pwd == null ? new char[0] : pwd.toCharArray(); char[] passwordChars = pwd == null ? new char[0] : pwd.toCharArray();
byte[] userPasswordHash; byte[] userPasswordHash;
String userName = user.getName(); String userName = user.getName();
if (userName.length() == 0 && passwordChars.length == 0) { if (userName.isEmpty() && passwordChars.length == 0) {
userPasswordHash = new byte[0]; userPasswordHash = new byte[0];
} else { } else {
userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars); userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars);
Expand All @@ -89,7 +89,6 @@ static void setPassword(User user, ServerSession session, Expression password) {
public int update() { public int update() {
session.getUser().checkAdmin(); session.getUser().checkAdmin();
session.commit(true); session.commit(true);
Database db = LealoneDatabase.getInstance();
if (Auth.findRole(userName) != null) { if (Auth.findRole(userName) != null) {
throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName); throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName);
} }
Expand All @@ -99,6 +98,7 @@ public int update() {
} }
throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName); throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName);
} }
Database db = LealoneDatabase.getInstance();
int id = getObjectId(db); int id = getObjectId(db);
User user = new User(db, id, userName, false); User user = new User(db, id, userName, false);
user.setAdmin(admin); user.setAdmin(admin);
Expand Down

0 comments on commit a91b349

Please sign in to comment.