import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.text.MessageFormat;
private boolean needStatInformation;
+ private boolean fsync;
+
private long commitLastModified;
private final FS fs;
try {
final FileInputStream fis = new FileInputStream(ref);
try {
- final byte[] buf = new byte[2048];
- int r;
- while ((r = fis.read(buf)) >= 0)
- os.write(buf, 0, r);
+ if (fsync) {
+ FileChannel in = fis.getChannel();
+ long pos = 0;
+ long cnt = in.size();
+ while (0 < cnt) {
+ long r = os.getChannel().transferFrom(in, pos, cnt);
+ pos += r;
+ cnt -= r;
+ }
+ } else {
+ final byte[] buf = new byte[2048];
+ int r;
+ while ((r = fis.read(buf)) >= 0)
+ os.write(buf, 0, r);
+ }
} finally {
fis.close();
}
public void write(final byte[] content) throws IOException {
requireLock();
try {
- os.write(content);
- os.flush();
+ if (fsync) {
+ FileChannel fc = os.getChannel();
+ ByteBuffer buf = ByteBuffer.wrap(content);
+ while (0 < buf.remaining())
+ fc.write(buf);
+ fc.force(true);
+ } else {
+ os.write(content);
+ }
fLck.release();
os.close();
os = null;
*/
public OutputStream getOutputStream() {
requireLock();
+
+ final OutputStream out;
+ if (fsync)
+ out = Channels.newOutputStream(os.getChannel());
+ else
+ out = os;
+
return new OutputStream() {
@Override
public void write(final byte[] b, final int o, final int n)
throws IOException {
- os.write(b, o, n);
+ out.write(b, o, n);
}
@Override
public void write(final byte[] b) throws IOException {
- os.write(b);
+ out.write(b);
}
@Override
public void write(final int b) throws IOException {
- os.write(b);
+ out.write(b);
}
@Override
public void flush() throws IOException {
- os.flush();
+ out.flush();
}
@Override
public void close() throws IOException {
try {
- os.flush();
+ out.flush();
+ if (fsync)
+ os.getChannel().force(true);
fLck.release();
- os.close();
+ out.close();
os = null;
} catch (IOException ioe) {
unlock();
needStatInformation = on;
}
+ /**
+ * Request that {@link #commit()} force dirty data to the drive.
+ *
+ * @param on
+ * true if dirty data should be forced to the drive.
+ */
+ public void setFSync(final boolean on) {
+ fsync = on;
+ }
+
/**
* Wait until the lock file information differs from the old file.
* <p>
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.LinkedList;
write = false;
if (write) {
+ WriteConfig wc = getRepository().getConfig().get(WriteConfig.KEY);
FileOutputStream out;
try {
out = new FileOutputStream(log, true);
out = new FileOutputStream(log, true);
}
try {
- out.write(rec);
+ if (wc.getFSyncRefFiles()) {
+ FileChannel fc = out.getChannel();
+ ByteBuffer buf = ByteBuffer.wrap(rec);
+ while (0 < buf.remaining())
+ fc.write(buf);
+ fc.force(true);
+ } else {
+ out.write(rec);
+ }
} finally {
out.close();
}
@Override
protected void writeFile(String name, byte[] content)
throws IOException {
+ lck.setFSync(true);
lck.setNeedStatInformation(true);
try {
lck.write(content);
@Override
protected Result doUpdate(final Result status) throws IOException {
+ WriteConfig wc = database.getRepository().getConfig()
+ .get(WriteConfig.KEY);
+
+ lock.setFSync(wc.getFSyncRefFiles());
lock.setNeedStatInformation(true);
lock.write(getNewObjectId());
@Override
protected Result doLink(final String target) throws IOException {
+ WriteConfig wc = database.getRepository().getConfig()
+ .get(WriteConfig.KEY);
+
+ lock.setFSync(wc.getFSyncRefFiles());
lock.setNeedStatInformation(true);
lock.write(encode(RefDirectory.SYMREF + target + '\n'));
private final boolean fsyncObjectFiles;
+ private final boolean fsyncRefFiles;
+
private WriteConfig(final Config rc) {
compression = rc.get(CoreConfig.KEY).getCompression();
fsyncObjectFiles = rc.getBoolean("core", "fsyncobjectfiles", false);
+ fsyncRefFiles = rc.getBoolean("core", "fsyncreffiles", false);
}
int getCompression() {
boolean getFSyncObjectFiles() {
return fsyncObjectFiles;
}
+
+ boolean getFSyncRefFiles() {
+ return fsyncRefFiles;
+ }
}