summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/UUIDMap.java
blob: e77a9149d1080e5b3ce8143bb7116ac9ca546772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.earth2me.essentials;

import com.google.common.io.Files;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;


public class UUIDMap
{
	private final transient net.ess3.api.IEssentials ess;
	private File userList;
	private final transient Pattern splitPattern = Pattern.compile(",");
	private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
	private final AtomicInteger pendingDiskWrites = new AtomicInteger(0);

	public UUIDMap(final net.ess3.api.IEssentials ess)
	{
		this.ess = ess;
		userList = new File(ess.getDataFolder(), "usermap.csv");

	}

	public void loadAllUsers(final ConcurrentSkipListMap<String, UUID> names, final ConcurrentSkipListMap<UUID, ArrayList<String>> history)
	{
		try
		{
			if (!userList.exists())
			{
				userList.createNewFile();
			}

			synchronized (pendingDiskWrites)
			{
				if (ess.getSettings().isDebug())
				{
					ess.getLogger().log(Level.INFO, "Reading usermap from disk");
				}

				names.clear();
				history.clear();

				final BufferedReader reader = new BufferedReader(new FileReader(userList));
				try
				{
					while (true)
					{
						final String line = reader.readLine();
						if (line == null)
						{
							break;
						}
						else
						{
							final String[] values = splitPattern.split(line);
							if (values.length == 2)
							{
								final String name = values[0];
								final UUID uuid = UUID.fromString(values[1]);
								names.put(name, uuid);
								if (!history.containsKey(uuid))
								{
									final ArrayList<String> list = new ArrayList<String>();
									list.add(name);
									history.put(uuid, list);
								}
								else
								{
									final ArrayList<String> list = history.get(uuid);
									if (!list.contains(name))
									{
										list.add(name);
									}
								}
							}
						}
					}
				}
				finally
				{
					reader.close();
				}
			}
		}
		catch (IOException ex)
		{
			Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
		}
	}

	public void writeUUIDMap()
	{
		_writeUUIDMap();
	}

	public void forceWriteUUIDMap()
	{
		if (ess.getSettings().isDebug())
		{
			ess.getLogger().log(Level.INFO, "Forcing usermap write to disk");
		}
		try
		{
			Future<?> future = _writeUUIDMap();;
			if (future != null)
			{
				future.get();
			}
		}
		catch (InterruptedException ex)
		{
			ess.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
		}
		catch (ExecutionException ex)
		{
			ess.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
		}
	}

	public Future<?> _writeUUIDMap()
	{
		final ConcurrentSkipListMap<String, UUID> names = ess.getUserMap().getNames();
		if (names.size() < 1)
		{
			return null;
		}
		pendingDiskWrites.incrementAndGet();
		Future<?> future = EXECUTOR_SERVICE.submit(new WriteRunner(ess.getDataFolder(), userList, names, pendingDiskWrites));
		return future;
	}


	private static class WriteRunner implements Runnable
	{
		private final File location;
		private final File endFile;
		private final ConcurrentSkipListMap<String, UUID> names;
		private final AtomicInteger pendingDiskWrites;

		private WriteRunner(final File location, final File endFile, final ConcurrentSkipListMap<String, UUID> names, final AtomicInteger pendingDiskWrites)
		{
			this.location = location;
			this.endFile = endFile;
			this.names = names;
			this.pendingDiskWrites = pendingDiskWrites;
		}

		@Override
		public void run()
		{
			synchronized (pendingDiskWrites)
			{
				if (pendingDiskWrites.get() > 1)
				{
					pendingDiskWrites.decrementAndGet();
					return;
				}

				File configFile = null;

				try
				{
					configFile = File.createTempFile("usermap", ".tmp.csv", location);

					final BufferedWriter bWriter = new BufferedWriter(new FileWriter(configFile));
					for (Map.Entry<String, UUID> entry : names.entrySet())
					{
						bWriter.write(entry.getKey() + "," + entry.getValue().toString());
						bWriter.newLine();
					}

					bWriter.close();
					Files.move(configFile, endFile);
				}
				catch (IOException ex)
				{
					try
					{
						if (configFile != null && configFile.exists())
						{
							Files.move(configFile, new File(endFile.getParentFile(), "usermap.bak.csv"));
						}
					}
					catch (Exception ex2)
					{
						Bukkit.getLogger().log(Level.SEVERE, ex2.getMessage(), ex2);
					}
					Bukkit.getLogger().log(Level.WARNING, ex.getMessage(), ex);
				}
				finally
				{
					pendingDiskWrites.decrementAndGet();
				}
			}
		}
	}
}