Skip to content

Commit

Permalink
delete some file and use ByteArray is query key
Browse files Browse the repository at this point in the history
  • Loading branch information
linong.cao committed Mar 1, 2012
1 parent 0d11878 commit 122c038
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 607 deletions.
Binary file added ctalk/lib/fastjson-1.1.15.jar
Binary file not shown.
8 changes: 1 addition & 7 deletions ctalk/src/com/db/MongoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,4 @@ public static MongoDB getInstance(){
return mogdb;
}

}






}
57 changes: 57 additions & 0 deletions ctalk/src/com/db/letdb/ByteArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.db.letdb;

import java.util.Arrays;

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

private byte[] array;

private int len;

public ByteArray(byte[] array){
this(array,array.length);
}

public ByteArray(byte[] array,int len){
this.array = array;
this.len = len;
}

public byte[] getArray(){
return array;
}

@Override
public boolean equals(Object other){
if (other instanceof ByteArray){
ByteArray oa = (ByteArray)other;
if (oa.getLen() == this.len){
for (int i=0;i<len;i++){
if (oa.getArray()[i] != array[i]){
return false;
}
}
return true;
}
}
return false;
}

@Override
public int hashCode(){
int hash = Arrays.hashCode(array);
return hash;
}

public int getLen() {
return len;
}

public void setLen(int len) {
this.len = len;
}
}
7 changes: 7 additions & 0 deletions ctalk/src/com/db/letdb/DocIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public DocIndex(byte[] bytes){
this.fileName = ByteHelper.getString(bytes, 8);
}

public DocIndex(int length,int offset,String fileName){
this.length = length;
this.offset = offset;
this.fileName = fileName;

}


public byte[] getBytes() throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
55 changes: 0 additions & 55 deletions ctalk/src/com/db/letdb/Example.java

This file was deleted.

19 changes: 16 additions & 3 deletions ctalk/src/com/db/letdb/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@
*/
public class Index {

public static int LENGTH_INDEX = 56;
public static int LENGTH_INDEX = 40;

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

private String md5key;

private ByteArray md5key;

private DocIndex docIndex;

public Index(){

}

public Index(ByteArray md5key,DocIndex docIndex){
this.md5key = md5key;
//this.docIndex = docIndex;
}

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

}
83 changes: 0 additions & 83 deletions ctalk/src/com/db/letdb/LetFile.java

This file was deleted.

24 changes: 0 additions & 24 deletions ctalk/src/com/db/letdb/Letdb.java

This file was deleted.

37 changes: 31 additions & 6 deletions ctalk/src/com/db/letdb/LetdbFile.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.db.letdb;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.fastjson.JSON;

/**
* @created: Feb 29, 2012
Expand All @@ -21,6 +26,8 @@ public class LetdbFile {

public static File root;

public static File indexFile;

public boolean checkFileisExist(){
root = new File(dbRoot);
if (!root.exists()){
Expand All @@ -42,7 +49,7 @@ public boolean checkFileisExist(){
}

private void checkIndex() throws IOException{
File indexFile = new File(dbRoot+"index.txt");
indexFile = new File(dbRoot+"index");
if (!indexFile.exists() || indexFile.isDirectory()){
log.info("checked index not exist or is directory,\n" +
"will clear current directory and create new index file");
Expand All @@ -63,19 +70,37 @@ private void clearAllChildren(File folder){
}
}

public void loadIndex(File indexFile) throws IOException{
//load all index form disk in to hashtab
public int loadIndex() throws IOException{
int count = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(indexFile));
byte[] indexBytes = new byte[Index.LENGTH_INDEX];

while (in.read(indexBytes) != -1){
byte[] md5keybytes = Arrays.copyOfRange(indexBytes, 0, Index.LENGTH_INDEX);
Index.indexTable.put(new String(md5keybytes),
new DocIndex(Arrays.copyOfRange(indexBytes,Index.LENGTH_INDEX,DocIndex.LENGTH_DOCINDEX)));
Index.indexTable.put(new ByteArray(md5keybytes),
new DocIndex(Arrays.copyOfRange(indexBytes,16,DocIndex.LENGTH_DOCINDEX)));
count++;
}
return count;
}

public void sync(){
//flush hashtable to disk
public int sync() throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (Iterator<ByteArray> it = Index.indexTable.keySet().iterator();it.hasNext();){
ByteArray md5key = it.next();
out.write(md5key.getArray());
out.write(((DocIndex)Index.indexTable.get(md5key)).getBytes());
}

indexFile.delete();
indexFile.createNewFile();
OutputStream os = new FileOutputStream(indexFile);
os.write(out.toByteArray());
os.flush();
if (os != null)
os.close();
return out.toByteArray().length;
}

}
Loading

0 comments on commit 122c038

Please sign in to comment.