Permalink
Browse files

Import from internal tree.

  • Loading branch information...
1 parent 26f2adc commit 3eeac3d5d0ef08039154dbc8828f5d7d339872fb @JakeWharton JakeWharton committed Sep 15, 2012
View
@@ -107,7 +107,7 @@
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
- <!--s/module name="VisibilityModifier"/-->
+ <!--module name="VisibilityModifier"/-->
<!-- Miscellaneous other checks. -->
View
@@ -1,98 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.sonatype.oss</groupId>
- <artifactId>oss-parent</artifactId>
- <version>7</version>
- </parent>
+ <parent>
+ <groupId>org.sonatype.oss</groupId>
+ <artifactId>oss-parent</artifactId>
+ <version>7</version>
+ </parent>
- <groupId>com.squareup</groupId>
- <artifactId>tape</artifactId>
- <packaging>jar</packaging>
- <version>1.0.0-SNAPSHOT</version>
+ <groupId>com.squareup</groupId>
+ <artifactId>tape</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0.0-SNAPSHOT</version>
- <name>FastPass</name>
- <description>A lightning fast, transactional, file-based FIFO for Java.</description>
+ <name>Tape</name>
+ <description>A lightning fast, transactional, file-based FIFO for Java.</description>
- <scm>
- <url>http://github.com/square/tape</url>
- <connection>scm:git:git://github.com/square/tape.git</connection>
- <developerConnection>scg:git:git@github.com:square/tape.git</developerConnection>
- </scm>
+ <scm>
+ <url>http://github.com/square/tape</url>
+ <connection>scm:git:git://github.com/square/tape.git</connection>
+ <developerConnection>scg:git:git@github.com:square/tape.git</developerConnection>
+ </scm>
- <organization>
- <name>Square, Inc.</name>
- <url>http://squareup.com</url>
- </organization>
+ <organization>
+ <name>Square, Inc.</name>
+ <url>http://squareup.com</url>
+ </organization>
- <issueManagement>
- <system>GitHub Issues</system>
- <url>http://github.com/square/tape/issues</url>
- </issueManagement>
+ <issueManagement>
+ <system>GitHub Issues</system>
+ <url>http://github.com/square/tape/issues</url>
+ </issueManagement>
- <licenses>
- <license>
- <name>Apache License Version 2.0</name>
- <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
- <distribution>repo</distribution>
- </license>
- </licenses>
+ <licenses>
+ <license>
+ <name>Apache License Version 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <java.version>1.6</java.version>
- <junit.version>4.10</junit.version>
- <fest.version>1.4</fest.version>
- </properties>
+ <java.version>1.6</java.version>
+ <junit.version>4.10</junit.version>
+ <fest.version>1.4</fest.version>
+ <commons-io.version>2.4</commons-io.version>
+ </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.easytesting</groupId>
- <artifactId>fest-assert</artifactId>
- <version>${fest.version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert</artifactId>
+ <version>${fest.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>${commons-io.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.5</version>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- </configuration>
- </plugin>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.5</version>
+ <configuration>
+ <source>${java.version}</source>
+ <target>${java.version}</target>
+ </configuration>
+ </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.9.1</version>
- <configuration>
- <failsOnError>true</failsOnError>
- <configLocation>checkstyle.xml</configLocation>
- </configuration>
- <executions>
- <execution>
- <phase>verify</phase>
- <goals>
- <goal>checkstyle</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <version>2.9.1</version>
+ <configuration>
+ <failsOnError>true</failsOnError>
+ <configLocation>checkstyle.xml</configLocation>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>verify</phase>
+ <goals>
+ <goal>checkstyle</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
</project>
@@ -0,0 +1,19 @@
+// Copyright 2012 Square, Inc.
+package com.squareup.tape;
+
+import java.io.File;
+import java.io.IOException;
+
+/** Encapsulates an {@link IOException} in an extension of {@link RuntimeException}. */
+public class FileException extends RuntimeException {
+ private final File file;
+
+ public FileException(String message, IOException e, File file) {
+ super(message, e);
+ this.file = file;
+ }
+
+ public File getFile() {
+ return file;
+ }
+}
@@ -0,0 +1,121 @@
+// Copyright 2012 Square, Inc.
+package com.squareup.tape;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Base queue class, implements common functionality for a QueueFile-backed
+ * queue manager. This class is not thread safe; instances should be kept
+ * thread-confined.
+ * <p>
+ * The {@link #add( Object )}, {@link #peek()}, {@link #remove()}, and
+ * {@link #setListener(ObjectQueue.Listener)} methods may throw a
+ * {@link FileException} if the underlying {@link QueueFile} experiences an
+ * {@link java.io.IOException}.
+ *
+ * @param <T> The type of elements in the queue.
+ */
+public class FileObjectQueue<T> implements ObjectQueue<T> {
+ /** Backing storage implementation. */
+ private final QueueFile queueFile;
+ /** Reusable byte output buffer. */
+ private final DirectByteArrayOutputStream bytes = new DirectByteArrayOutputStream();
+ /** Keep file around for error reporting. */
+ private final File file;
+ private final Converter<T> converter;
+ private Listener<T> listener;
+
+ public FileObjectQueue(File file, Converter<T> converter) throws IOException {
+ this.file = file;
+ this.converter = converter;
+ this.queueFile = new QueueFile(file);
+ }
+
+ @Override public int size() {
+ return queueFile.size();
+ }
+
+ @Override public final void add(T entry) {
+ try {
+ bytes.reset();
+ converter.toStream(entry, bytes);
+ queueFile.add(bytes.getArray(), 0, bytes.size());
+ if (listener != null) listener.onAdd(entry);
+ } catch (IOException e) {
+ throw new FileException("Failed to add entry.", e, file);
+ }
+ }
+
+ @Override public T peek() {
+ try {
+ byte[] bytes = queueFile.peek();
+ if (bytes == null) return null;
+ return converter.from(bytes);
+ } catch (IOException e) {
+ throw new FileException("Failed to peek.", e, file);
+ }
+ }
+
+ @Override public final void remove() {
+ try {
+ queueFile.remove();
+ if (listener != null) listener.onRemove();
+ } catch (IOException e) {
+ throw new FileException("Failed to remove.", e, file);
+ }
+ }
+
+ /**
+ * Scans each entry of the backing QueueFile.
+ *
+ * @see QueueFile#forEach(com.squareup.tape.QueueFile.ElementReader)
+ */
+ @Override public void setListener(final Listener<T> listener) {
+ try {
+ queueFile.forEach(new QueueFile.ElementReader() {
+ @Override
+ public void read(InputStream in, int length) throws IOException {
+ byte[] data = new byte[length];
+ in.read(data, 0, length);
+
+ listener.onAdd(converter.from(data));
+ }
+ });
+ } catch (IOException e) {
+ throw new FileException("Unable to iterate over QueueFile contents.", e, file);
+ }
+ this.listener = listener;
+ }
+
+ /**
+ * Convert a byte stream to and from a concrete type.
+ *
+ * @param <T> Object type.
+ */
+ public interface Converter<T> {
+ /** Converts bytes to an object. */
+ T from(byte[] bytes) throws IOException;
+
+ /** Converts o to bytes written to the specified stream. */
+ void toStream(T o, OutputStream bytes) throws IOException;
+ }
+
+ /** Enables direct access to the internal array. Avoids unnecessary copying. */
+ private static class DirectByteArrayOutputStream extends ByteArrayOutputStream {
+ public DirectByteArrayOutputStream() {
+ super();
+ }
+
+ /**
+ * Gets a reference to the internal byte array. The {@link #size()} method indicates how many
+ * bytes contain actual data added since the last {@link #reset()} call.
+ */
+ public byte[] getArray() {
+ return buf;
+ }
+ }
+}
@@ -0,0 +1,43 @@
+// Copyright 2012 Square, Inc.
+package com.squareup.tape;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * A queue for objects that are not serious enough to be written to disk. Objects in this queue
+ * are kept in memory and will not be serialized.
+ *
+ * @param <T> The type of elements in the queue.
+ */
+public class InMemoryObjectQueue<T> implements ObjectQueue<T> {
+ private Startable startable;
+ private Queue<T> tasks;
+
+ @SuppressWarnings("unchecked")
+ public InMemoryObjectQueue(Startable startable) {
+ this.startable = startable;
+ tasks = (Queue<T>) new LinkedList();
+ }
+
+ @Override public void add(T entry) {
+ tasks.add(entry);
+ startable.start();
+ }
+
+ @Override public T peek() {
+ return tasks.peek();
+ }
+
+ @Override public int size() {
+ return tasks.size();
+ }
+
+ @Override public void remove() {
+ tasks.remove();
+ }
+
+ @Override public void setListener(Listener<T> listener) {
+ throw new UnsupportedOperationException("Listener not supported for in-memory queue");
+ }
+}
Oops, something went wrong. Retry.

0 comments on commit 3eeac3d

Please sign in to comment.