Skip to content

Commit

Permalink
Some modifications to MMapUtils and corresponding changes to RiceCode…
Browse files Browse the repository at this point in the history
…r. Also disable native compilation in build.xml by default.
  • Loading branch information
Ahad Rana authored and Ahad Rana committed Nov 14, 2011
1 parent 1eb8b37 commit 1deb913
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
36 changes: 33 additions & 3 deletions src/org/commoncrawl/util/shared/MMapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public static class MMapFile {
long length = -1;
ByteBuffer buffers[]=null;
int bufSizes[] = null;
private int refCount =0;
private boolean closePending = false;

public MMapFile(File input) throws IOException {
RandomAccessFile raf = new RandomAccessFile(input, "r");
Expand Down Expand Up @@ -159,9 +161,31 @@ public FSDataInputStream newInputStream() throws IOException {
return dataStream;
}

public void close()throws IOException {
for (ByteBuffer buffer : buffers) {
cleanMapping(buffer);
private synchronized void addRef() {
refCount++;
}

private synchronized void release() {
if (--refCount == 0 && closePending) {
try {
close();
} catch (IOException e) {
LOG.error(CCStringUtils.stringifyException(e));
}
}
}

public synchronized void close()throws IOException {
if (refCount == 0) {
if (buffers != null) {
for (ByteBuffer buffer : buffers) {
cleanMapping(buffer);
}
}
buffers = null;
}
else {
closePending = true;
}
}

Expand All @@ -180,6 +204,7 @@ public class MMapFileInputStream extends FSInputStream {
private ByteBuffer curBuf; // redundant for speed: buffers[curBufIndex]

public MMapFileInputStream() throws IOException {
addRef();
seek(0L);
}

Expand Down Expand Up @@ -223,6 +248,11 @@ public int read(byte[] bytes, int offset, int len) throws IOException {
}
}

@Override
public void close() throws IOException {
release();
}

@Override
public int available() throws IOException {
long amtAvailable = (length() - getPos());
Expand Down
6 changes: 0 additions & 6 deletions src/org/commoncrawl/util/shared/RiceCoder.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package org.commoncrawl.util.shared;

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.GZIPOutputStream;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSInputStream;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.WritableUtils;

/**
* Encode a set of Longs using Rice coding, a variant of Golomb coding,
Expand Down

0 comments on commit 1deb913

Please sign in to comment.