Mercurial > hg > audiodb
changeset 728:d3407d1e2f57
* Initial insertion from data code
* Added some tests for insertion
* Added a simple example
author | mas01mj |
---|---|
date | Mon, 26 Jul 2010 16:19:56 +0000 |
parents | 4d9e4ff0a9cd |
children | a9978a6d0bb3 |
files | bindings/java/examples/Simple.java bindings/java/ext/libAudioDB_JNI.c bindings/java/src/org/omras2/AudioDB.java bindings/java/test/TestInsert.java bindings/java/test/TestQuery.java |
diffstat | 5 files changed, 102 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bindings/java/examples/Simple.java Mon Jul 26 16:19:56 2010 +0000 @@ -0,0 +1,31 @@ +package examples; + +import org.omras2.*; +import java.io.File; +import java.util.Vector; + +public class Simple +{ + public static void main(String args[]) + { + AudioDB testDB = new AudioDB(new File("simple.adb")); + testDB.create(5, 5, 5); + testDB.open(AudioDB.Mode.O_RDWR); + System.out.println("Inserting 3 features"); + testDB.insert("feature1", 2, 5, new double[] { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 }); + testDB.insert("feature2", 1, 5, new double[] { 6, 7, 8, 9, 10 }); + testDB.insert("feature3", 5, 5, new double[] { 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 4, 4, 4, 4, 4, 5, 5, 5, 9, 10, 1, 2, 3, 4, 5 , 4, 4, 4, 4, 4}); + + System.out.println("Performing query"); + Query query = new Query(); + query.setSeqLength(1); + query.setSeqStart(0); + Vector<Result> results = testDB.query("feature1", query); + + System.out.println("Results:"); + for(Result result: results) + { + System.out.println(result.getKey()+" "+result.getDistance()+" "+result.getIpos()+" "+result.getQpos()); + } + } +}
--- a/bindings/java/ext/libAudioDB_JNI.c Mon Jul 26 14:58:13 2010 +0000 +++ b/bindings/java/ext/libAudioDB_JNI.c Mon Jul 26 16:19:56 2010 +0000 @@ -143,6 +143,36 @@ (*env)->DeleteLocalRef(env, adbClass); } +JNIEXPORT jboolean JNICALL Java_org_omras2_AudioDB_audiodb_1insert_1data(JNIEnv *env, jobject obj, jstring key, int nvectors, int dim, jdoubleArray features, jdoubleArray power, jdoubleArray times) +{ + adb_t *handle = get_handle(env, obj); + if(!handle) + return JNI_FALSE; + + adb_datum_t* ins = (adb_datum_t *)malloc(sizeof(adb_datum_t)); + if(!features || !key) + return JNI_FALSE; + + ins->data = (*env)->GetDoubleArrayElements(env, features, NULL); + ins->power = NULL; + ins->times = NULL; + + if(power) + ins->power = (*env)->GetDoubleArrayElements(env, power, NULL); + if(times) + ins->times = (*env)->GetDoubleArrayElements(env, times, NULL); + + ins->key = (*env)->GetStringUTFChars(env, key, 0); + ins->nvectors = nvectors; + ins->dim = dim; + + int result = audiodb_insert_datum(handle, ins); + if(result) + return JNI_FALSE; + + return JNI_TRUE; +} + JNIEXPORT jboolean JNICALL Java_org_omras2_AudioDB_audiodb_1insert_1path(JNIEnv *env, jobject obj, jstring key, jstring features, jstring power, jstring times) { adb_t *handle = get_handle(env, obj);
--- a/bindings/java/src/org/omras2/AudioDB.java Mon Jul 26 14:58:13 2010 +0000 +++ b/bindings/java/src/org/omras2/AudioDB.java Mon Jul 26 16:19:56 2010 +0000 @@ -10,6 +10,7 @@ public native void audiodb_close(); public native Status audiodb_status(); public native boolean audiodb_insert_path(String key, String features, String power, String times); + public native boolean audiodb_insert_data(String key, int nvectors, int dim, double[] features, double[] power, double[] times); public native Vector<Result> audiodb_query_by_key(String key, Query config); public enum Mode { O_RDONLY, O_RDWR } @@ -29,19 +30,39 @@ public boolean insert(File features) { - return audiodb_insert_path(null, features.getPath(), null, null); + return insert(null, features, null, null); } public boolean insert(String key, File features) { - return audiodb_insert_path(key, features.getPath(), null, null); + return insert(key, features, null, null); + } + + public boolean insert(String key, File features, File power) + { + return insert(key, features, power, null); } public boolean insert(String key, File features, File power, File times) { return audiodb_insert_path(key, features.getPath(), (power == null ? null : power.getPath()), (times == null ? null : times.getPath())); } + + public boolean insert(String key, int nvectors, int dim, double[] features) + { + return insert(key, nvectors, dim, features, null, null); + } + public boolean insert(String key, int nvectors, int dim, double[] features, double[] power) + { + return insert(key, nvectors, dim, features, power, null); + } + + public boolean insert(String key, int nvectors, int dim, double[] features, double[] power, double[] times) + { + return audiodb_insert_data(key, nvectors, dim, features, power, times); + } + public boolean create(int datasize, int ntracks, int datadim) { return audiodb_create(path.toString(), datasize, ntracks, datadim);
--- a/bindings/java/test/TestInsert.java Mon Jul 26 14:58:13 2010 +0000 +++ b/bindings/java/test/TestInsert.java Mon Jul 26 16:19:56 2010 +0000 @@ -32,4 +32,18 @@ testDB.open(AudioDB.Mode.O_RDONLY); assertFalse("Insert feature file", testDB.insert(testFeatureFile)); } + + public void testInsertData() + { + AudioDB testDB = new AudioDB(testDBFile); + assertTrue("DB created", testDB.create(100, 100, 1)); + testDB.open(AudioDB.Mode.O_RDWR); + Status status = testDB.getStatus(); + assertTrue("Insert feature 1", testDB.insert("feature1", 5, 1, new double[] { 1, 2, 3, 4, 5 })); + assertTrue("Insert feature 2", testDB.insert("feature2", 5, 1, new double[] { 1, 2, 3, 4, 5 })); + assertTrue("Insert feature 3", testDB.insert("feature3", 5, 1, new double[] { 5, 4, 3, 2, 1 })); + assertFalse("Insert feature 3 again", testDB.insert("feature3", 5, 1, new double[] { 5, 4, 3, 2, 1 })); + assertFalse("Insert bad feature", testDB.insert("feature4", 1, 3, new double[] { 5, 4, 3 })); + assertEquals("3 features in db", 3, testDB.getStatus().getNumFiles()); + } }
--- a/bindings/java/test/TestQuery.java Mon Jul 26 14:58:13 2010 +0000 +++ b/bindings/java/test/TestQuery.java Mon Jul 26 16:19:56 2010 +0000 @@ -35,14 +35,14 @@ query.setIncludeKeys(new String[]{"feat1"}); query.setExcludeKeys(new String[]{"feat2"}); Vector<Result> results = testDB.query("feat1", query); - System.out.println(results.size()); +/* System.out.println(results.size()); for(Result result: results) { System.out.println(result.getKey()); System.out.println(result.getDistance()); System.out.println(result.getQpos()); System.out.println(result.getIpos()); - } + }*/ } /* public void testAdvanced() @@ -54,9 +54,8 @@ Query query = new Query(); query.setSeqLength(16); query.setSeqStart(0); - query.setExcludeKeys(new String[]{"KSA_CHARM_27", "KSA_CHARM_336", "KSA_CHARM_300"}); - query.setSeqStart(0); - Vector<Result> results = testDB.query("KSA_CHARM_27", query); + query.setExcludeKeys(new String[]{"KSA_CHARM_27", "KSA_CHARM_300"}); + Vector<Result> results = testDB.query("KSA_CHARM_336", query); System.out.println(results.size()); for(Result result: results) {