Use redis pipeline for community player statuses

This commit is contained in:
cnr 2016-12-23 22:28:27 -07:00
parent 08d65f3662
commit 73cefb69c9
3 changed files with 53 additions and 10 deletions

View File

@ -2,14 +2,17 @@ package mineplex.core.communities.storage;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.timing.TimingManager;
import mineplex.core.common.util.Callback;
import mineplex.core.communities.Community;
import mineplex.core.communities.CommunityJoinRequestInfo;
@ -91,11 +94,6 @@ public class CommunityRepository extends MinecraftRepository
boolean readingChat = memberSet.getBoolean("readingChat");
CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin);
PlayerStatus status = _repo.getElement(name.toLowerCase());
if (status != null)
{
info.update(lastLogin, true, status.getServer());
}
info.ReadingChat = readingChat;
Community community = communityMap.get(communityId);
@ -152,11 +150,16 @@ public class CommunityRepository extends MinecraftRepository
{
return;
}
TimingManager.start("member elements");
for (Community c : communities)
{
for (CommunityMemberInfo info : c.getMembers().values())
List<CommunityMemberInfo> members = new ArrayList<>(c.getMembers().values());
List<PlayerStatus> statuses = _repo.getElementsSequential(members.stream().map(info -> info.UUID.toString()).collect(Collectors.toList()));
for (int i = 0; i < members.size(); i++)
{
PlayerStatus status = _repo.getElement(info.UUID.toString());
CommunityMemberInfo info = members.get(i);
PlayerStatus status = statuses.get(i);
boolean online = false;
String server = "";
if (status != null)
@ -178,6 +181,7 @@ public class CommunityRepository extends MinecraftRepository
}
}
}
TimingManager.stop("member elements");
}
public void updateJoinRequests(LinkedList<Community> communities)
@ -186,11 +190,16 @@ public class CommunityRepository extends MinecraftRepository
{
return;
}
TimingManager.start("request elements");
for (Community c : communities)
{
for (CommunityJoinRequestInfo info : c.getJoinRequests().values())
List<CommunityJoinRequestInfo> requests = new ArrayList<>(c.getJoinRequests().values());
List<PlayerStatus> statuses = _repo.getElementsSequential(requests.stream().map(info -> info.UUID.toString()).collect(Collectors.toList()));
for (int i = 0; i < requests.size(); i++)
{
PlayerStatus status = _repo.getElement(info.UUID.toString());
CommunityJoinRequestInfo info = requests.get(i);
PlayerStatus status = statuses.get(i);
if (status != null)
{
if (!info.Name.equals(status.getName()))
@ -200,6 +209,7 @@ public class CommunityRepository extends MinecraftRepository
}
}
}
TimingManager.stop("request elements");
}
public void loadInvites(int accountId, List<Integer> invites)

View File

@ -1,6 +1,7 @@
package mineplex.serverdata.data;
import java.util.Collection;
import java.util.List;
/**
* DataRepository is used to store {@link Data} objects in a central database
@ -18,6 +19,8 @@ public interface DataRepository<T extends Data>
public Collection<T> getElements(Collection<String> dataIds);
public List<T> getElementsSequential(List<String> dataIds);
public void addElement(T element, int timeout);
public void addElement(T element);

View File

@ -103,6 +103,36 @@ public class RedisDataRepository<T extends Data> extends RedisRepository impleme
return elements;
}
@Override
public List<T> getElementsSequential(List<String> dataIds)
{
List<T> elements = new ArrayList<>();
try(Jedis jedis = getResource(false))
{
Pipeline pipeline = jedis.pipelined();
List<Response<String>> responses = new ArrayList<>();
for (String dataId : dataIds)
{
responses.add(pipeline.get(generateKey(dataId)));
}
// Block until all requests have received pipelined responses
pipeline.sync();
for (Response<String> response : responses)
{
String serializedData = response.get();
T element = deserialize(serializedData);
elements.add(element);
}
}
return elements;
}
@Override
public T getElement(String dataId)
{