Refactoring.

This commit is contained in:
nvt 2012-01-28 03:34:16 +01:00
parent 1e85183754
commit 4c75a250e6
10 changed files with 64 additions and 153 deletions

View file

@ -1,3 +1,4 @@
name_length 16
fs_size 32704 fs_size 32704
sector_size 64 sector_size 64
page_size 64 page_size 64

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeConfiguration.java,v 1.5 2009/09/22 16:31:36 nvt-se Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -45,20 +41,19 @@ public class CoffeeConfiguration {
public static final int FD_SET_SIZE = 256; public static final int FD_SET_SIZE = 256;
public static final int MAX_OPEN_FILES = 256; public static final int MAX_OPEN_FILES = 256;
public static final int LOG_TABLE_LIMIT = 256; public static final int LOG_TABLE_LIMIT = 256;
public static final int NAME_LENGTH = 16; public final int nameLength;
public static int fsSize, sectorSize, pageSize; public final int fsSize, sectorSize, pageSize;
public static int startOffset, pageTypeSize; public final int startOffset, pageTypeSize;
public static int defaultFileSize, defaultLogSize; public final int defaultFileSize, defaultLogSize;
public static int pagesPerSector; public final int pagesPerSector;
public static boolean useMicroLogs; public final boolean useMicroLogs;
public CoffeeConfiguration(String filename) public CoffeeConfiguration(String filename)
throws CoffeeException, IOException { throws CoffeeException, IOException {
String[] validParameters = {"use_micro_logs", "fs_size", String[] requiredParameters = {"name_length", "use_micro_logs", "fs_size",
"page_size", "sector_size", "page_size", "sector_size",
"start_offset", "default_file_size", "start_offset", "default_file_size",
"default_log_size", "page_type_size"}; "default_log_size", "page_type_size"};
String property;
Properties prop = new Properties(); Properties prop = new Properties();
InputStream stream = CoffeeConfiguration.class.getResourceAsStream("/" + filename); InputStream stream = CoffeeConfiguration.class.getResourceAsStream("/" + filename);
if (stream == null) { if (stream == null) {
@ -66,20 +61,21 @@ public class CoffeeConfiguration {
} }
prop.load(stream); prop.load(stream);
for (int i = 0; i < validParameters.length; i++) { for (int i = 0; i < requiredParameters.length; i++) {
if (prop.getProperty(validParameters[i]) == null) { if (prop.getProperty(requiredParameters[i]) == null) {
throw new CoffeeException("missing the parameter \"" + validParameters[i] + "\" in the configuration file " + filename); throw new CoffeeException("missing the parameter \"" + requiredParameters[i] + "\" in the configuration file " + filename);
} }
} }
nameLength = Integer.parseInt(prop.getProperty("name_length"));
useMicroLogs = new Boolean(prop.getProperty("use_micro_logs")).booleanValue(); useMicroLogs = new Boolean(prop.getProperty("use_micro_logs")).booleanValue();
fsSize = new Integer(prop.getProperty("fs_size")).intValue(); fsSize = Integer.parseInt(prop.getProperty("fs_size"));
sectorSize = new Integer(prop.getProperty("sector_size")).intValue(); sectorSize = Integer.parseInt(prop.getProperty("sector_size"));
pageSize = new Integer(prop.getProperty("page_size")).intValue(); pageSize = Integer.parseInt(prop.getProperty("page_size"));
defaultFileSize = new Integer(prop.getProperty("default_file_size")).intValue(); defaultFileSize = Integer.parseInt(prop.getProperty("default_file_size"));
defaultLogSize = new Integer(prop.getProperty("default_log_size")).intValue(); defaultLogSize = Integer.parseInt(prop.getProperty("default_log_size"));
startOffset = new Integer(prop.getProperty("start_offset")).intValue(); startOffset = Integer.parseInt(prop.getProperty("start_offset"));
pageTypeSize = new Integer(prop.getProperty("page_type_size")).intValue(); pageTypeSize = Integer.parseInt(prop.getProperty("page_type_size"));
pagesPerSector = sectorSize / pageSize; pagesPerSector = sectorSize / pageSize;
} }

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeFS.java,v 1.5 2009/08/11 17:03:59 fros4943 Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -64,7 +60,7 @@ public class CoffeeFS {
} }
private int pageCount(long size) { private int pageCount(long size) {
int headerSize = conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6; int headerSize = conf.nameLength + conf.pageTypeSize * 2 + 6;
return (int)(size + headerSize + conf.pageSize - 1) / conf.pageSize; return (int)(size + headerSize + conf.pageSize - 1) / conf.pageSize;
} }
@ -100,8 +96,7 @@ public class CoffeeFS {
} }
public CoffeeHeader readHeader(int page) throws IOException { public CoffeeHeader readHeader(int page) throws IOException {
byte[] bytes = new byte[conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6]; byte[] bytes = new byte[conf.nameLength + conf.pageTypeSize * 2 + 6];
int index = 0;
image.read(bytes, bytes.length, page * conf.pageSize); image.read(bytes, bytes.length, page * conf.pageSize);
CoffeeHeader header = new CoffeeHeader(this, page, bytes); CoffeeHeader header = new CoffeeHeader(this, page, bytes);
@ -184,13 +179,18 @@ public class CoffeeFS {
return true; return true;
} }
static class CoffeeException extends RuntimeException { public static class CoffeeException extends RuntimeException {
private static final long serialVersionUID = 1146474084441011154L;
public CoffeeException(String message) { public CoffeeException(String message) {
super("Coffee error: " + message); super("Coffee error: " + message);
} }
} }
static class CoffeeFileException extends RuntimeException { public static class CoffeeFileException extends RuntimeException {
private static final long serialVersionUID = -2954553141887245203L;
public CoffeeFileException(String message) { public CoffeeFileException(String message) {
super("Coffee file error: " + message); super("Coffee file error: " + message);
} }

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeFile.java,v 1.6 2009/08/11 17:03:59 fros4943 Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -43,7 +39,6 @@ public class CoffeeFile {
protected CoffeeHeader header; protected CoffeeHeader header;
private String name; private String name;
private int length; private int length;
private int startPage;
private int reservedSize; private int reservedSize;
private CoffeeMicroLog microLog; private CoffeeMicroLog microLog;
private boolean knownLength; private boolean knownLength;
@ -52,7 +47,6 @@ public class CoffeeFile {
this.coffeeFS = coffeeFS; this.coffeeFS = coffeeFS;
this.header = header; this.header = header;
name = header.name; name = header.name;
startPage = header.getPage();
reservedSize = header.maxPages * coffeeFS.getConfiguration().pageSize; reservedSize = header.maxPages * coffeeFS.getConfiguration().pageSize;
if (header.isModified() && if (header.isModified() &&
coffeeFS.getConfiguration().useMicroLogs == true) { coffeeFS.getConfiguration().useMicroLogs == true) {

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeHeader.java,v 1.2 2009/08/10 12:51:52 nvt-se Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -37,9 +33,8 @@
package se.sics.coffee; package se.sics.coffee;
class CoffeeHeader { class CoffeeHeader {
private CoffeeFS coffeeFS; private final CoffeeConfiguration conf;
private CoffeeConfiguration conf; private final int page;
private int page;
private static final int HDR_FLAG_VALID = 0x1; private static final int HDR_FLAG_VALID = 0x1;
private static final int HDR_FLAG_ALLOCATED = 0x2; private static final int HDR_FLAG_ALLOCATED = 0x2;
@ -54,36 +49,33 @@ class CoffeeHeader {
int maxPages; int maxPages;
String name; String name;
private boolean valid; private int flags;
private boolean allocated;
private boolean obsolete;
private boolean modified;
private boolean log;
private boolean isolated;
public CoffeeHeader(CoffeeFS coffeeFS, int page) { public CoffeeHeader(CoffeeFS coffeeFS, int page) {
this.coffeeFS = coffeeFS;
this.page = page; this.page = page;
conf = coffeeFS.getConfiguration(); conf = coffeeFS.getConfiguration();
} }
public CoffeeHeader(CoffeeFS coffeeFS, int page, byte[] bytes) { public CoffeeHeader(CoffeeFS coffeeFS, int page, byte[] bytes) {
this.coffeeFS = coffeeFS; this(coffeeFS, page);
this.page = page;
processRawHeader(bytes); processRawHeader(bytes);
} }
private int getInt(byte[] bytes, int index) {
return (bytes[index] & 0xff) + ((bytes[index + 1] & 0xff) << 8);
}
private void processRawHeader(byte[] bytes) { private void processRawHeader(byte[] bytes) {
int index = 0; int index = 0;
logPage = getPageValue(bytes, 0); logPage = getPageValue(bytes, 0);
index += conf.pageTypeSize; index += conf.pageTypeSize;
logRecords = bytes[index] + (bytes[index + 1] << 8); logRecords = getInt(bytes, index);
index += 2; index += 2;
logRecordSize = bytes[index] + (bytes[index + 1] << 8); logRecordSize = getInt(bytes, index);
index += 2; index += 2;
maxPages = getPageValue(bytes, index); maxPages = getPageValue(bytes, index);
@ -91,64 +83,17 @@ class CoffeeHeader {
index++; // Skip deprecated EOF hint field. index++; // Skip deprecated EOF hint field.
processFlags((int)bytes[index]); flags = bytes[index] & 0xff;
index++; index++;
name = new String(bytes).substring(index, name = new String(bytes).substring(index,
index + conf.NAME_LENGTH); index + conf.nameLength);
int nullCharOffset = name.indexOf(0); int nullCharOffset = name.indexOf(0);
if (nullCharOffset >= 0) { if (nullCharOffset >= 0) {
name = name.substring(0, nullCharOffset); name = name.substring(0, nullCharOffset);
} }
} }
private byte composeFlags() {
byte flags = 0;
if (valid) {
flags |= HDR_FLAG_VALID;
}
if (allocated) {
flags |= HDR_FLAG_ALLOCATED;
}
if (obsolete) {
flags |= HDR_FLAG_OBSOLETE;
}
if (modified) {
flags |= HDR_FLAG_MODIFIED;
}
if (log) {
flags |= HDR_FLAG_LOG;
}
if (isolated) {
flags |= HDR_FLAG_ISOLATED;
}
return flags;
}
private void processFlags(int flags) {
if ((flags & HDR_FLAG_VALID) != 0) {
valid = true;
}
if ((flags & HDR_FLAG_ALLOCATED) != 0) {
allocated = true;
}
if ((flags & HDR_FLAG_OBSOLETE) != 0) {
obsolete = true;
}
if ((flags & HDR_FLAG_MODIFIED) != 0) {
modified = true;
}
if ((flags & HDR_FLAG_LOG) != 0) {
log = true;
}
if ((flags & HDR_FLAG_ISOLATED) != 0) {
isolated = true;
}
}
private byte[] setPageValue(int page) { private byte[] setPageValue(int page) {
byte[] bytes = new byte[conf.pageTypeSize]; byte[] bytes = new byte[conf.pageTypeSize];
@ -160,16 +105,15 @@ class CoffeeHeader {
private int getPageValue(byte[] bytes, int offset) { private int getPageValue(byte[] bytes, int offset) {
int page = 0; int page = 0;
for (int i = 0; i < conf.pageTypeSize; i++) { for (int i = 0; i < conf.pageTypeSize; i++) {
page |= bytes[offset + i] << (8 * i); page |= (bytes[offset + i] & 0xff) << (8 * i);
} }
return page; return page;
} }
public byte[] toRawHeader() { public byte[] toRawHeader() {
byte[] bytes = new byte[2 * conf.pageTypeSize + byte[] bytes = new byte[2 * conf.pageTypeSize +
conf.NAME_LENGTH + 6]; conf.nameLength + 6];
int index = 0; int index = 0;
System.arraycopy(setPageValue(logPage), 0, bytes, 0, System.arraycopy(setPageValue(logPage), 0, bytes, 0,
@ -187,18 +131,18 @@ class CoffeeHeader {
index += conf.pageTypeSize; index += conf.pageTypeSize;
bytes[index++] = 0; // Deprecated EOF hint field. bytes[index++] = 0; // Deprecated EOF hint field.
bytes[index++] = composeFlags(); bytes[index++] = (byte) flags;
byte[] nameBytes = name.getBytes(); byte[] nameBytes = name.getBytes();
int copyLength = nameBytes.length > conf.NAME_LENGTH ? int copyLength = nameBytes.length > conf.nameLength ?
conf.NAME_LENGTH : nameBytes.length; conf.nameLength : nameBytes.length;
System.arraycopy(nameBytes, 0, bytes, index, copyLength); System.arraycopy(nameBytes, 0, bytes, index, copyLength);
return bytes; return bytes;
} }
public int rawLength() { public int rawLength() {
return 2 * conf.pageTypeSize + conf.NAME_LENGTH + 6; return 2 * conf.pageTypeSize + conf.nameLength + 6;
} }
public int getPage() { public int getPage() {
@ -206,27 +150,27 @@ class CoffeeHeader {
} }
public boolean isValid() { public boolean isValid() {
return valid; return (flags & HDR_FLAG_VALID) != 0;
} }
public boolean isAllocated() { public boolean isAllocated() {
return allocated; return (flags & HDR_FLAG_ALLOCATED) != 0;
} }
public boolean isObsolete() { public boolean isObsolete() {
return obsolete; return (flags & HDR_FLAG_OBSOLETE) != 0;
} }
public boolean isModified() { public boolean isModified() {
return modified; return (flags & HDR_FLAG_MODIFIED) != 0;
} }
public boolean isIsolated() { public boolean isIsolated() {
return isolated; return (flags & HDR_FLAG_ISOLATED) != 0;
} }
public boolean isLog() { public boolean isLog() {
return log; return (flags & HDR_FLAG_LOG) != 0;
} }
public boolean isFree() { public boolean isFree() {
@ -238,11 +182,11 @@ class CoffeeHeader {
} }
public void allocate() { public void allocate() {
allocated = true; flags |= HDR_FLAG_ALLOCATED;
} }
public void makeObsolete() { public void makeObsolete() {
obsolete = true; flags |= HDR_FLAG_OBSOLETE;
} }
public void setName(String name) { public void setName(String name) {

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeImage.java,v 1.2 2009/08/11 17:03:59 fros4943 Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeImageFile.java,v 1.2 2009/08/10 12:51:52 nvt-se Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -41,12 +37,10 @@ import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
public class CoffeeImageFile implements CoffeeImage { public class CoffeeImageFile implements CoffeeImage {
private String filename;
private RandomAccessFile imageFile; private RandomAccessFile imageFile;
private CoffeeConfiguration conf; private CoffeeConfiguration conf;
public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException { public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException {
this.filename = filename;
this.conf = conf; this.conf = conf;
File file = new File(filename); File file = new File(filename);
imageFile = new RandomAccessFile(file, "rw"); imageFile = new RandomAccessFile(file, "rw");

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeManager.java,v 1.6 2009/08/13 12:15:35 nvt-se Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -39,7 +35,6 @@ package se.sics.coffee;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import se.sics.coffee.CoffeeFS.CoffeeException; import se.sics.coffee.CoffeeFS.CoffeeException;
import se.sics.coffee.CoffeeFS.CoffeeFileException; import se.sics.coffee.CoffeeFS.CoffeeFileException;
@ -161,8 +156,7 @@ public class CoffeeManager {
Iterator<Map.Entry<String, CoffeeFile>> iterator = Iterator<Map.Entry<String, CoffeeFile>> iterator =
coffeeFS.getFiles().entrySet().iterator(); coffeeFS.getFiles().entrySet().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<String, CoffeeFile> pair = (Map.Entry<String, CoffeeFile>) iterator.next(); Map.Entry<String, CoffeeFile> pair = iterator.next();
String key = pair.getKey();
CoffeeFile file = pair.getValue(); CoffeeFile file = pair.getValue();
bytesWritten += file.getLength(); bytesWritten += file.getLength();
bytesReserved += file.getHeader().getReservedSize(); bytesReserved += file.getHeader().getReservedSize();
@ -188,8 +182,7 @@ public class CoffeeManager {
try { try {
Iterator<Map.Entry<String, CoffeeFile>> iterator = files.entrySet().iterator(); Iterator<Map.Entry<String, CoffeeFile>> iterator = files.entrySet().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<String, CoffeeFile> pair = (Map.Entry<String, CoffeeFile>) iterator.next(); Map.Entry<String, CoffeeFile> pair = iterator.next();
String key = pair.getKey();
CoffeeFile file = pair.getValue(); CoffeeFile file = pair.getValue();
System.out.println(file.getName() + " " + file.getLength()); System.out.println(file.getName() + " " + file.getLength());
} }

View file

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* This file is part of the Contiki operating system.
*
* $Id: CoffeeMicroLog.java,v 1.2 2009/08/11 14:42:58 nvt-se Exp $
*
* @author Nicolas Tsiftes * @author Nicolas Tsiftes
* *
*/ */
@ -54,7 +50,6 @@ public class CoffeeMicroLog extends CoffeeFile {
if (header.logRecordSize == 0) { if (header.logRecordSize == 0) {
logRecordSize = conf.pageSize; logRecordSize = conf.pageSize;
} }
int logAreaSize;
if (header.logRecords == 0) { if (header.logRecords == 0) {
logRecords = conf.defaultLogSize / logRecordSize; logRecords = conf.defaultLogSize / logRecordSize;
} else { } else {
@ -78,9 +73,6 @@ public class CoffeeMicroLog extends CoffeeFile {
} }
public byte[] getRegion(int region) throws IOException { public byte[] getRegion(int region) throws IOException {
int headerSize = header.rawLength();
int indexSize = logRecords * 2;
for (int i = logRecords - 1; i >= 0; i--) { for (int i = logRecords - 1; i >= 0; i--) {
if (index[i] - 1 == region) { if (index[i] - 1 == region) {
byte[] bytes = new byte[logRecordSize]; byte[] bytes = new byte[logRecordSize];

View file

@ -1,3 +1,4 @@
name_length 16
fs_size 983040 fs_size 983040
sector_size 65536 sector_size 65536
page_size 256 page_size 256