-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update saveorupdate() and get() interface in Letdb.java
- Loading branch information
linong.cao
committed
Mar 4, 2012
1 parent
94e424c
commit 526c52d
Showing
9 changed files
with
117 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -19,8 +22,7 @@ public class DocIndexTest { | |
|
||
@Before | ||
public void setup() throws NoSuchAlgorithmException { | ||
messageDigest = MessageDigest.getInstance("MD5"); | ||
messageDigest.reset(); | ||
|
||
} | ||
|
||
|
||
|
@@ -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); | ||
|
||
} | ||
|
@@ -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()); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.