Skip to content

Commit

Permalink
update saveorupdate() and get() interface in Letdb.java
Browse files Browse the repository at this point in the history
  • Loading branch information
linong.cao committed Mar 4, 2012
1 parent 94e424c commit 526c52d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 80 deletions.
69 changes: 38 additions & 31 deletions ctalk/src/com/db/letdb/DocIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
public class DocIndex {

public static int LENGTH_INDEX = 40;
public static int LENGTH_INDEX = 128;

public static Hashtable<ByteArray,DocIndex> indexTable = new Hashtable<ByteArray,DocIndex>();
public static Hashtable<Long,DocIndex> indexTable = new Hashtable<Long,DocIndex>();

//doc type,class type
private String clazz;
Expand All @@ -27,41 +27,43 @@ public class DocIndex {
private int length;

//doc begin position,take 4 bytes
private int offset;
private long offset;

//doc store in fileName,take 16 bytes
private String fileName;

//md5 key,take 16 bytes
private ByteArray md5key;
private Long id;


public DocIndex(){

}

public DocIndex(byte[] bytes){
this.md5key = new ByteArray(Arrays.copyOfRange(bytes, 0, 16));
this.id = ByteHelper.getLong(bytes, 0);
this.offset = ByteHelper.getLong(bytes, 8);
this.length = ByteHelper.getInt(bytes, 16);
this.offset = ByteHelper.getInt(bytes, 20);
this.fileName = ByteHelper.getString(bytes, 24);
this.fileName = ByteHelper.getString(bytes, 20,18);
this.clazz = ByteHelper.getString(bytes,38);
}

public DocIndex(ByteArray md5key,int length,int offset,String fileName){
this.md5key = md5key;
public DocIndex(Long id,long offset,int length,String fileName,String clazz){
this.id = id;
this.length = length;
this.offset = offset;
this.fileName = fileName;
this.clazz = clazz;

}


public byte[] getBytes() throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(this.getMd5key().getArray());
out.write(ByteHelper.getBytes(this.getLength()));
out.write(ByteHelper.getBytes(this.getId()));
out.write(ByteHelper.getBytes(this.getOffset()));
out.write(ByteHelper.getBytes(this.getLength()));
out.write(ByteHelper.getBytes(this.getFileName()));
out.write(ByteHelper.getBytes(this.getClazz()));
for (int i = out.size();i < LENGTH_INDEX;i++){
out.write(0);
}
Expand All @@ -71,9 +73,9 @@ public byte[] getBytes() throws IOException{
//flush hashtable to disk
public static int sync() throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (Iterator<ByteArray> it = indexTable.keySet().iterator();it.hasNext();){
ByteArray md5key = it.next();
out.write(((DocIndex)indexTable.get(md5key)).getBytes());
for (Iterator<Long> it = indexTable.keySet().iterator();it.hasNext();){
Long id = it.next();
out.write(((DocIndex)indexTable.get(id)).getBytes());
}

LetdbFile.docindexFile.delete();
Expand All @@ -86,12 +88,16 @@ public static int sync() throws IOException{
return out.toByteArray().length;
}

public static DocIndex getDocIndex(ByteArray md5key){
return (DocIndex)indexTable.get(md5key);
public static DocIndex getDocIndex(Long id){
return (DocIndex)indexTable.get(id);
}

public static void updateIndex(){

public static void updateIndex(DocIndex index){
DocIndex tmp = getDocIndex(index.getId());
if (tmp != null){
indexTable.remove(tmp.getId());
}
indexTable.put(index.getId(), index);
}

public String getClazz() {
Expand All @@ -110,14 +116,6 @@ public void setLength(int length) {
this.length = length;
}

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

public String getFileName() {
return fileName;
}
Expand All @@ -126,12 +124,21 @@ public void setFileName(String fileName) {
this.fileName = fileName;
}

public ByteArray getMd5key() {
return md5key;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public void setMd5key(ByteArray md5key) {
this.md5key = md5key;
public long getOffset() {
return offset;
}

public void setOffset(long offset) {
this.offset = offset;
}

}
9 changes: 7 additions & 2 deletions ctalk/src/com/db/letdb/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected byte[] getBytes(){
return jstr.getBytes();
}

public static Document getInstance(String jstr){
Document doc = JSON.parseObject(jstr, Document.class);
public static Document getInstance(String jstr,String clazz) throws ClassNotFoundException{
Document doc = null;
if (clazz.equals(ExampleEntity.class.getName())){
doc = JSON.parseObject(jstr,ExampleEntity.class);
}else if(clazz.equals(Document.class.getName())){
doc = JSON.parseObject(jstr,Document.class);
}
return doc;
}

Expand Down
6 changes: 3 additions & 3 deletions ctalk/src/com/db/letdb/InWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InWrite {

public static void write(File file,byte[] bin,boolean append) throws IOException{
OutputStream os = new FileOutputStream(file);
public static long write(File file,byte[] bin,boolean append) throws IOException{
OutputStream os = new FileOutputStream(file,append);
os.write(bin);
os.flush();
if (os != null)
os.close();
return file.length();
}


Expand Down
37 changes: 29 additions & 8 deletions ctalk/src/com/db/letdb/Letdb.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,55 @@
import java.io.File;
import java.io.IOException;

import com.helper.ByteHelper;

/**
* @created: Mar 2, 2012
* @author : jias chao<[email protected]>
*/
public class Letdb {

public void saveOrUpdate(Document doc){
public void saveOrUpdate(Document doc) throws IOException{

if (doc.getId() != null){
if (doc.getId() == null){
doc.setId(System.currentTimeMillis());
}
byte[] bin = doc.getBytes();
DocIndex index = DocIndex.getDocIndex(doc.getId());
StorageFileIndex sfi = null;
if (index == null){//add
sfi = StorageFileIndex.getUseableStorageFileIndex();
}else{//update
sfi = StorageFileIndex.getStorageFileIndex(index.getFileName());
}

DocIndex tmp = new DocIndex();
tmp.setId(doc.getId());
tmp.setFileName(sfi.getFileName());
tmp.setClazz(doc.getClass().getName());
tmp.setLength(bin.length);
tmp.setOffset(sfi.getSize());

long size = InWrite.write(sfi.getFd(), bin, true);

;
StorageFileIndex.updateIndex(tmp.getFileName(), size);
DocIndex.updateIndex(tmp);
}

public Document get(Long id) throws IOException{

public Document get(Long id) throws IOException, ClassNotFoundException{
Document doc = null;
DocIndex index = DocIndex.getDocIndex(new ByteArray(ByteHelper.getBytes(id)));
DocIndex index = DocIndex.getDocIndex(id);
if (index != null){
File fd = StorageFileIndex.getFD(index.getFileName());
if (fd != null){
byte[] b = InWrite.read(fd, index.getOffset(), index.getLength());
doc = Document.getInstance(new String(b));

doc = Document.getInstance(new String(b),index.getClazz());
}
}
return doc;
}

private void t(){

}
}
10 changes: 6 additions & 4 deletions ctalk/src/com/db/letdb/LetdbFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ private void clearAllChildren(File folder){
}

//load all index form disk in to hashtab
public int loadDocIndex() throws IOException{
public static int loadDocIndex() throws IOException{
int count = 0;
if (docindexFile == null || !docindexFile.exists())
docindexFile = new File(DbRoot+DocIndexName);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(docindexFile));
byte[] indexBytes = new byte[DocIndex.LENGTH_INDEX];
while (in.read(indexBytes) != -1){
byte[] md5keybytes = Arrays.copyOfRange(indexBytes, 0, DocIndex.LENGTH_INDEX);
DocIndex.indexTable.put(new ByteArray(md5keybytes),
new DocIndex(Arrays.copyOfRange(indexBytes,16,DocIndex.LENGTH_INDEX)));
Long id = ByteHelper.getLong(indexBytes, 0);
DocIndex.indexTable.put(id,
new DocIndex(Arrays.copyOfRange(indexBytes,0,DocIndex.LENGTH_INDEX)));
count++;
}
return count;
Expand Down
22 changes: 16 additions & 6 deletions ctalk/src/com/db/letdb/StorageFileIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public StorageFileIndex(){
public StorageFileIndex(File file){
this.fileName = file.getName();
this.lastModifyTime = file.lastModified();
this.size = file.getTotalSpace();
this.size = file.length();
this.fd = file;
if (this.getSize() < SIZE_DEFAULT_MAX){
this.status = STATUS_NEW;
Expand All @@ -50,7 +50,7 @@ public StorageFileIndex(File file){
}
}

public StorageFileIndex getUseableFile() throws IOException{
public static StorageFileIndex getUseableStorageFileIndex() throws IOException{
for (Iterator<StorageFileIndex> it = indexMap.values().iterator();it.hasNext();){
StorageFileIndex index = it.next();
if (index.getStatus() == STATUS_NEW){
Expand All @@ -75,18 +75,28 @@ public static int loadIndex(){
return count;
}

public void updateIndex(File file){
StorageFileIndex sindex = new StorageFileIndex(file);
indexMap.put(file.getName(), sindex);
public static void updateIndex(String fileName,long size){
StorageFileIndex index = indexMap.get(fileName);
if (index != null){
index.setSize(size);
if (index.getSize() >= SIZE_DEFAULT_MAX){
index.setStatus(STATUS_OLD);
}
indexMap.put(fileName, index);
}
}

public static File getFD(String fileName){
StorageFileIndex index = indexMap.get(fileName);
return index.getFd();
}

public static StorageFileIndex getStorageFileIndex(String fileName){
StorageFileIndex index = indexMap.get(fileName);
return index;
}

private StorageFileIndex createNewStoreFile() throws IOException{
private static StorageFileIndex createNewStoreFile() throws IOException{
File file = new File(LetdbFile.DbRoot+FILENAME_DEFAULT+System.currentTimeMillis());
if ( file.exists()){
file.delete();
Expand Down
15 changes: 4 additions & 11 deletions ctalk/test/com/db/letdb/DocIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.junit.Before;
import org.junit.Test;

import com.db.letdb.ByteArray;
import com.db.letdb.DocIndex;

/**
* @created: Mar 1, 2012
* @author : jias chao<[email protected]>
Expand All @@ -19,8 +22,7 @@ public class DocIndexTest {

@Before
public void setup() throws NoSuchAlgorithmException {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();

}


Expand All @@ -32,14 +34,8 @@ public void testDocIndex2bytes() throws IOException{
index.setFileName("sotre1.txt");
index.setClazz(DocIndex.class.getName());

messageDigest.reset();
messageDigest.update("12589abc1".getBytes());
index.setMd5key(new ByteArray(messageDigest.digest()));

byte[] bytes = index.getBytes();



assertEquals(DocIndex.LENGTH_INDEX,bytes.length);

}
Expand All @@ -52,9 +48,6 @@ public void testDocIndex() throws IOException{
index.setFileName("sotre1.txt");
index.setClazz(DocIndex.class.getName());

messageDigest.reset();
messageDigest.update("12589abc1".getBytes());
index.setMd5key(new ByteArray(messageDigest.digest()));

DocIndex tmp = new DocIndex(index.getBytes());

Expand Down
6 changes: 3 additions & 3 deletions ctalk/test/com/db/letdb/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void testDoc2json(){


@Test
public void testJson2Doc(){
public void testJson2Doc() throws ClassNotFoundException{

String str = "{\"id\":\"abc-12\"}";
Document doc = new Document().getInstance(str);
assertEquals("abc-12",doc.getId());
Document doc = Document.getInstance(str,Document.class.getName());
assertEquals(Document.class.getName(),doc.getClass().getName());
}

}
Loading

0 comments on commit 526c52d

Please sign in to comment.