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
sector_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
* 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
*
*/
@ -45,20 +41,19 @@ public class CoffeeConfiguration {
public static final int FD_SET_SIZE = 256;
public static final int MAX_OPEN_FILES = 256;
public static final int LOG_TABLE_LIMIT = 256;
public static final int NAME_LENGTH = 16;
public static int fsSize, sectorSize, pageSize;
public static int startOffset, pageTypeSize;
public static int defaultFileSize, defaultLogSize;
public static int pagesPerSector;
public static boolean useMicroLogs;
public final int nameLength;
public final int fsSize, sectorSize, pageSize;
public final int startOffset, pageTypeSize;
public final int defaultFileSize, defaultLogSize;
public final int pagesPerSector;
public final boolean useMicroLogs;
public CoffeeConfiguration(String filename)
throws CoffeeException, IOException {
String[] validParameters = {"use_micro_logs", "fs_size",
String[] requiredParameters = {"name_length", "use_micro_logs", "fs_size",
"page_size", "sector_size",
"start_offset", "default_file_size",
"default_log_size", "page_type_size"};
String property;
Properties prop = new Properties();
InputStream stream = CoffeeConfiguration.class.getResourceAsStream("/" + filename);
if (stream == null) {
@ -66,20 +61,21 @@ public class CoffeeConfiguration {
}
prop.load(stream);
for (int i = 0; i < validParameters.length; i++) {
if (prop.getProperty(validParameters[i]) == null) {
throw new CoffeeException("missing the parameter \"" + validParameters[i] + "\" in the configuration file " + filename);
for (int i = 0; i < requiredParameters.length; i++) {
if (prop.getProperty(requiredParameters[i]) == null) {
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();
fsSize = new Integer(prop.getProperty("fs_size")).intValue();
sectorSize = new Integer(prop.getProperty("sector_size")).intValue();
pageSize = new Integer(prop.getProperty("page_size")).intValue();
defaultFileSize = new Integer(prop.getProperty("default_file_size")).intValue();
defaultLogSize = new Integer(prop.getProperty("default_log_size")).intValue();
startOffset = new Integer(prop.getProperty("start_offset")).intValue();
pageTypeSize = new Integer(prop.getProperty("page_type_size")).intValue();
fsSize = Integer.parseInt(prop.getProperty("fs_size"));
sectorSize = Integer.parseInt(prop.getProperty("sector_size"));
pageSize = Integer.parseInt(prop.getProperty("page_size"));
defaultFileSize = Integer.parseInt(prop.getProperty("default_file_size"));
defaultLogSize = Integer.parseInt(prop.getProperty("default_log_size"));
startOffset = Integer.parseInt(prop.getProperty("start_offset"));
pageTypeSize = Integer.parseInt(prop.getProperty("page_type_size"));
pagesPerSector = sectorSize / pageSize;
}

View File

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* 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
*
*/
@ -64,7 +60,7 @@ public class CoffeeFS {
}
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;
}
@ -100,8 +96,7 @@ public class CoffeeFS {
}
public CoffeeHeader readHeader(int page) throws IOException {
byte[] bytes = new byte[conf.NAME_LENGTH + conf.pageTypeSize * 2 + 6];
int index = 0;
byte[] bytes = new byte[conf.nameLength + conf.pageTypeSize * 2 + 6];
image.read(bytes, bytes.length, page * conf.pageSize);
CoffeeHeader header = new CoffeeHeader(this, page, bytes);
@ -184,16 +179,21 @@ public class CoffeeFS {
return true;
}
static class CoffeeException extends RuntimeException {
public CoffeeException(String message) {
super("Coffee error: " + message);
}
public static class CoffeeException extends RuntimeException {
private static final long serialVersionUID = 1146474084441011154L;
public CoffeeException(String message) {
super("Coffee error: " + message);
}
}
static class CoffeeFileException extends RuntimeException {
public CoffeeFileException(String message) {
super("Coffee file error: " + message);
}
public static class CoffeeFileException extends RuntimeException {
private static final long serialVersionUID = -2954553141887245203L;
public CoffeeFileException(String 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
* 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
*
*/
@ -43,7 +39,6 @@ public class CoffeeFile {
protected CoffeeHeader header;
private String name;
private int length;
private int startPage;
private int reservedSize;
private CoffeeMicroLog microLog;
private boolean knownLength;
@ -52,7 +47,6 @@ public class CoffeeFile {
this.coffeeFS = coffeeFS;
this.header = header;
name = header.name;
startPage = header.getPage();
reservedSize = header.maxPages * coffeeFS.getConfiguration().pageSize;
if (header.isModified() &&
coffeeFS.getConfiguration().useMicroLogs == true) {

View File

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

View File

@ -26,10 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* 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
*
*/
@ -41,12 +37,10 @@ import java.io.IOException;
import java.io.RandomAccessFile;
public class CoffeeImageFile implements CoffeeImage {
private String filename;
private RandomAccessFile imageFile;
private CoffeeConfiguration conf;
public CoffeeImageFile(String filename, CoffeeConfiguration conf) throws IOException {
this.filename = filename;
this.conf = conf;
File file = new File(filename);
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
* 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
*
*/
@ -39,7 +35,6 @@ package se.sics.coffee;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import se.sics.coffee.CoffeeFS.CoffeeException;
import se.sics.coffee.CoffeeFS.CoffeeFileException;
@ -161,8 +156,7 @@ public class CoffeeManager {
Iterator<Map.Entry<String, CoffeeFile>> iterator =
coffeeFS.getFiles().entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, CoffeeFile> pair = (Map.Entry<String, CoffeeFile>) iterator.next();
String key = pair.getKey();
Map.Entry<String, CoffeeFile> pair = iterator.next();
CoffeeFile file = pair.getValue();
bytesWritten += file.getLength();
bytesReserved += file.getHeader().getReservedSize();
@ -188,8 +182,7 @@ public class CoffeeManager {
try {
Iterator<Map.Entry<String, CoffeeFile>> iterator = files.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, CoffeeFile> pair = (Map.Entry<String, CoffeeFile>) iterator.next();
String key = pair.getKey();
Map.Entry<String, CoffeeFile> pair = iterator.next();
CoffeeFile file = pair.getValue();
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
* 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
*
*/
@ -54,7 +50,6 @@ public class CoffeeMicroLog extends CoffeeFile {
if (header.logRecordSize == 0) {
logRecordSize = conf.pageSize;
}
int logAreaSize;
if (header.logRecords == 0) {
logRecords = conf.defaultLogSize / logRecordSize;
} else {
@ -78,9 +73,6 @@ public class CoffeeMicroLog extends CoffeeFile {
}
public byte[] getRegion(int region) throws IOException {
int headerSize = header.rawLength();
int indexSize = logRecords * 2;
for (int i = logRecords - 1; i >= 0; i--) {
if (index[i] - 1 == region) {
byte[] bytes = new byte[logRecordSize];

View File

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