-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedSet.java
More file actions
240 lines (202 loc) · 5.92 KB
/
SortedSet.java
File metadata and controls
240 lines (202 loc) · 5.92 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
/*
Java by default does not support UNIX Domain Sockets. Use junixsocket 3rd party library instead.
Documentaion: http://goo.gl/kveo6z
*/
class ProcessRunnable implements Runnable {
private Socket sock = null;
private Store store = null;
private static int getInt(InputStream is) throws IOException {
final byte[] buf = new byte[4];
is.read(buf);
final ByteBuffer wrapped = ByteBuffer.wrap(buf); // big-endian by
// default
return wrapped.getInt();
}
ProcessRunnable(Socket sock, Store store) {
this.sock = sock;
this.store = store;
}
/**
* @param sock
* @throws IOException
*/
private void process(Socket sock) throws IOException {
System.out.println("Connected: " + sock);
try (InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream()) {
boolean run = true;
while (run) {
final int numberOfParams = getInt(is);
final ArrayList<Integer> params = new ArrayList<Integer>();
for (int p = 0; p < numberOfParams; p++) {
params.add(getInt(is));
}
System.out.println(params.toString());
switch (params.get(0)) {
case 1:
store.add(params);
os.write(ByteBuffer.allocate(4).putInt(0).array());
os.flush();
break;
case 2:
store.remove(params);
os.write(ByteBuffer.allocate(4).putInt(0).array());
break;
case 3:
final int size = store.size(params);
os.write(ByteBuffer.allocate(4).putInt(1).array());
os.write(ByteBuffer.allocate(4).putInt(size).array());
os.flush();
break;
case 4:
final int score = store.score(params);
os.write(ByteBuffer.allocate(4).putInt(1).array());
os.write(ByteBuffer.allocate(4).putInt(score).array());
os.flush();
break;
case 5:
store.range(params);
final Set<Item> set = store.range(params);
os.write(ByteBuffer.allocate(4).putInt(set.size() * 2).array());
for (final Item item : set) {
os.write(ByteBuffer.allocate(4).putInt(item.getKey()).array());
os.write(ByteBuffer.allocate(4).putInt(item.getScore()).array());
}
os.flush();
break;
case 6:
run = false;
os.flush();
break;
default:
System.out.println("not reconized" + params.get(1));
break;
}
}
}
sock.close();
}
@Override
public void run() {
try {
process(this.sock);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
class Item implements Comparable<Item>{
private final int key;
private final int score;
public Item(int key, int score) {
this.key = key;
this.score = score;
}
public int getKey() {
return key;
}
public int getScore() {
return score;
}
@Override
public int compareTo(Item o) {
if (this.key != o.key) {
return this.key - o.key;
}
return this.score - o.score;
}
}
class Store {
private final ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Integer>> sortedSets;
public Store() {
sortedSets = new ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Integer>>();
}
public synchronized void add(List<Integer> params) {
ConcurrentHashMap<Integer, Integer> hashmap = sortedSets.get(params.get(1));
if (hashmap == null) {
hashmap = new ConcurrentHashMap<Integer, Integer>();
sortedSets.put(params.get(1), hashmap);
}
hashmap.put(params.get(2), params.get(3));
}
public synchronized void remove(List<Integer> params) {
final ConcurrentHashMap<Integer, Integer> hashmap = sortedSets.get(params.get(1));
if (hashmap == null) {
return;
}
hashmap.remove(params.get(2));
if (hashmap.size() == 0) {
sortedSets.remove(params.get(1));
}
}
public synchronized int size(List<Integer> params) {
final ConcurrentHashMap<Integer, Integer> hashmap = sortedSets.get(params.get(1));
if (hashmap == null) {
return 0;
}
return hashmap.size();
}
public synchronized int score(List<Integer> params) {
System.out.println("get score" + params.toString());
final ConcurrentHashMap<Integer, Integer> hashmap = sortedSets.get(params.get(1));
if (hashmap == null) {
return 0;
}
final Integer score = hashmap.get(params.get(2));
if (score == null) {
return 0;
}
return score;
}
public synchronized Set<Item> range(List<Integer> params) {
final int low = params.get(params.size() - 2);
final int high = params.get(params.size() - 1);
final TreeSet<Item> set = new TreeSet<Item>();
for (int i = 1; i < params.size() - 3; i++) {
final ConcurrentHashMap<Integer, Integer> hashmap = sortedSets.get(params.get(i));
if (hashmap == null) {
continue;
}
for (final Map.Entry<Integer, Integer> entry : hashmap.entrySet()) {
if (entry.getValue() >= low && entry.getValue() <= high) {
set.add(new Item(entry.getKey(), entry.getValue()));
}
}
}
return set;
}
}
public class Solution {
// NOTE: Use this path to create the UDS Server socket
static String SERVER_SOCKET_PATH = "./socket";
static int BUFFER_SIZE = 4;
public static void main(String[] args) throws IOException {
final File socketFile = new File(SERVER_SOCKET_PATH);
final Store store = new Store();
final ExecutorService executor = Executors.newFixedThreadPool(8);
try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) {
server.bind(new AFUNIXSocketAddress(socketFile));
System.out.println("server: " + server);
while (!Thread.interrupted()) {
System.out.println("Waiting for connection...");
final Socket sock = server.accept();
executor.execute(new ProcessRunnable(sock, store));
}
}
}
}