|
| 1 | +package us.hxbc.clusterhq.queue; |
| 2 | + |
| 3 | +import javax.ws.rs.ClientErrorException; |
| 4 | +import javax.ws.rs.core.Response; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.channels.FileChannel; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.StandardOpenOption; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static java.util.Objects.requireNonNull; |
| 17 | + |
| 18 | +public class Queue { |
| 19 | + private static final long CHUNK_SIZE = 4 * 1024; // 4KB |
| 20 | + private final Path dataDir, subscriptionDir; |
| 21 | + private final Map<String, Subscriber> subscriptions = new HashMap<>(); |
| 22 | + private final DataStore dataStore; |
| 23 | + |
| 24 | + public Queue(Path dir, long chunkSize) throws IOException { |
| 25 | + requireNonNull(dir); |
| 26 | + if (!Files.isDirectory(dir)) { |
| 27 | + throw new IllegalArgumentException(dir + " is not a directory"); |
| 28 | + } |
| 29 | + |
| 30 | + dataDir = dir.resolve("data"); |
| 31 | + Files.createDirectory(dataDir); |
| 32 | + subscriptionDir = dir.resolve("subscriptions"); |
| 33 | + Files.createDirectory(subscriptionDir); |
| 34 | + dataStore = new DataStore(dataDir, chunkSize); |
| 35 | + } |
| 36 | + |
| 37 | + public void subscribe(String user) throws IOException { |
| 38 | + synchronized (subscriptions) { |
| 39 | + if (subscriptions.containsKey(user)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + Path p = subscriptionDir.resolve(user); |
| 43 | + long nextLSN = dataStore.getNextLSN(); |
| 44 | + try (FileChannel out = FileChannel.open(p, |
| 45 | + StandardOpenOption.CREATE, |
| 46 | + StandardOpenOption.WRITE, |
| 47 | + StandardOpenOption.TRUNCATE_EXISTING)) { |
| 48 | + ByteBuffer buf = ByteBuffer.allocate(8).putLong(nextLSN); |
| 49 | + buf.position(0); |
| 50 | + out.write(buf); |
| 51 | + out.force(true); |
| 52 | + } |
| 53 | + subscriptions.put(user, new Subscriber(user, nextLSN)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void post(InputStream data) throws IOException { |
| 58 | + dataStore.post(data); |
| 59 | + } |
| 60 | + |
| 61 | + public DataStore.Message get(String user) throws IOException { |
| 62 | + Subscriber subscriber; |
| 63 | + synchronized (subscriptions) { |
| 64 | + subscriber = subscriptions.get(user); |
| 65 | + if (subscriber == null) { |
| 66 | + throw new ClientErrorException(Response.Status.NOT_FOUND); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + synchronized (subscriber) { |
| 71 | + DataStore.Message m = dataStore.get(subscriber.nextLSN); |
| 72 | + Path p = subscriptionDir.resolve(user); |
| 73 | + try (FileChannel out = FileChannel.open(p, |
| 74 | + StandardOpenOption.WRITE, |
| 75 | + StandardOpenOption.TRUNCATE_EXISTING)) { |
| 76 | + ByteBuffer buf = ByteBuffer.allocate(8).putLong(m.nextLSN); |
| 77 | + buf.position(0); |
| 78 | + out.write(buf); |
| 79 | + out.force(true); |
| 80 | + } |
| 81 | + subscriber.nextLSN = m.nextLSN; |
| 82 | + return m; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void unsubscribe(String user) throws IOException { |
| 87 | + synchronized (subscriptions) { |
| 88 | + Path p = subscriptionDir.resolve(user); |
| 89 | + if (!Files.deleteIfExists(p)) { |
| 90 | + throw new ClientErrorException(Response.Status.NOT_FOUND); |
| 91 | + } |
| 92 | + subscriptions.remove(user); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static class Subscriber { |
| 97 | + final String name; |
| 98 | + long nextLSN; |
| 99 | + |
| 100 | + Subscriber(String name, long nextLSN) { |
| 101 | + this.name = requireNonNull(name); |
| 102 | + this.nextLSN = nextLSN; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments