Skip to content

Commit

Permalink
write letFile etc.and byte helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
chao committed Nov 7, 2011
1 parent 57e30cb commit 3a8ff7f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
116 changes: 116 additions & 0 deletions ctalk/src/com/db/letdb/LetFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.db.letdb;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.helper.ByteHelper;


/**
*
* @author chao
*
*/
public class LetFile {
private static final Log log = LogFactory.getLog(LetFile.class);
private static final String DB_DIR = "letdb";
private static final String LOCK_FILE = "letdb/.lock";
private static final String INDEX_FILE = "letdb/.index";
private static Map<Long,Index> indexMap = new Hashtable<Long,Index>();

public void init() throws IOException{
File file = new File(DB_DIR);
if (!file.exists() || !file.isDirectory()){
log.info("crate dir letdb/ ");
file.mkdir();
}else{
File indexFile = new File(INDEX_FILE);
if (!indexFile.exists()){
log.info("crate file .index ");
indexFile.createNewFile();
}
loadIndex();
}
}

/**
* load index from .index file to indexMap in memory
* @throws IOException
*/
private void loadIndex() throws IOException{
InputStream in = new FileInputStream(new File(INDEX_FILE));
log.debug(".index found");
int off = 0;
byte[] b = new byte[24];
while (in.read(b, off, 24) != -1){
Index index = Index.getInstance(b);
if (index != null){
indexMap.put(index.getId(),index);
off += 24;
}
}
log.debug("load index in map account: "+indexMap.size());
}


}

/**
* doc map index
* @author chao
*
*/
class Index{
private long id = -1; //doc id
private int state = 0; //-1:this doc has been delete; 0:normal
private long pos = 0; //doc start postion
private int len = 0; //doc take size
public static int INDEX_LEN = 24;

public static Index getInstance(byte[] content){
if (content.length == INDEX_LEN){
Index index = new Index();

index.setId(ByteHelper.getLong(content, 0));
index.setState(ByteHelper.getInt(content, 4));
index.setPos(ByteHelper.getLong(content, 12));
index.setLen(ByteHelper.getInt(content, 20));

return index;
}else{
return null;
}
}

public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public long getPos() {
return pos;
}
public void setPos(long pos) {
this.pos = pos;
}
public int getLen() {
return len;
}
public void setLen(int len) {
this.len = len;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
18 changes: 18 additions & 0 deletions ctalk/src/com/db/letdb/Letdb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.db.letdb;


/**
*
* @author chao
* Letdb is a simple database,only support localhost store,
* it's similar with nosql database ,data format is json
*/
public class Letdb {

;
;
;

;

}
25 changes: 25 additions & 0 deletions ctalk/src/com/helper/ByteHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.helper;
/**
* @created: 2011-11-7
* @author : jias chao<[email protected]>
*/
public class ByteHelper {

public static long getLong(byte[] bytes, int offset) {
long result = 0;
for (int i = offset; i < offset + 8; i++) {
result = (result << 8) | (0x00ff & bytes[i]);
}
return result;
}

public static int getInt(byte[] bytes, int begin) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = result | (0x00ff & bytes[i + begin]) << ((3 - i) * 8);
}
return result;
}


}

0 comments on commit 3a8ff7f

Please sign in to comment.