Skip to content

Commit ab4754a

Browse files
committedOct 3, 2012
fixes #4 added a content provider for LevelDB (although its rather over-kill given that its NoSQL) as well as converted the benchmarks into using the content provider
1 parent c0c7c1c commit ab4754a

File tree

7 files changed

+713
-315
lines changed

7 files changed

+713
-315
lines changed
 

‎AndroidManifest.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
<application android:label="@string/app_name"
88
>
99

10-
<provider android:name="com.google.code.p.leveldb.provider.NotePadLevelDBProvider"
10+
<!--<provider android:name="com.google.code.p.leveldb.provider.NotePadLevelDBProvider"
1111
android:authorities="com.google.provider.NotePad"
12-
/>
12+
/>-->
13+
14+
<provider
15+
android:name="com.google.code.p.leveldb.provider.LevelDBProvider"
16+
android:authorities="com.google.code.p.leveldb.provider.UnstructuredData" />
17+
1318

1419
<!-- <provider android:name="com.google.code.p.leveldb.provider.NotePadSQLiteProvider"
1520
android:authorities="com.google.provider.NotePad"

‎NotePadSample/AndroidManifest.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@
2828
<application
2929
android:icon="@drawable/app_notes"
3030
android:label="@string/app_name" >
31-
<provider
31+
32+
<!-- <provider
3233
android:name="com.google.code.p.leveldb.provider.NotePadLevelDBProvider"
33-
android:authorities="com.google.provider.NotePad" />
34+
android:authorities="com.google.provider.NotePad" />-->
3435

36+
<provider
37+
android:name="com.google.code.p.leveldb.provider.LevelDBProvider"
38+
android:authorities="com.google.code.p.leveldb.provider.UnstructuredData" />
39+
3540
<!--
3641
<provider android:name="com.google.code.p.leveldb.provider.NotePadSQLiteProvider"
3742
android:authorities="com.google.provider.NotePad"

‎NotePadSample/src/com/example/android/notepad/DBBenchmarkActivity.java

+156-50
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,108 @@
1414
import org.json.JSONObject;
1515

1616
import com.google.code.p.leveldb.LevelDB;
17+
import com.google.code.p.leveldb.provider.LevelDBProvider;
18+
import com.google.code.p.leveldb.provider.NotePad.Notes;
1719

1820
import android.app.Activity;
21+
import android.content.ContentResolver;
22+
import android.content.ContentValues;
1923
import android.content.res.AssetManager;
24+
import android.database.Cursor;
25+
import android.util.Log;
2026
import android.widget.TextView;
2127
import android.widget.Toast;
2228
import android.net.Uri;
2329
import android.os.Bundle;
30+
import android.provider.Settings;
2431

2532
public class DBBenchmarkActivity extends Activity {
26-
String mDBdir;
27-
LevelDB db;
33+
// String mDBdir;
34+
// LevelDB db;
35+
36+
private static final String TAG = "DBbenchmark";
37+
38+
private static final String[] PROJECTION = new String[] { "key", // 0
39+
"value" // 1
40+
};
41+
42+
Cursor mCursor;
43+
Uri mUri;
44+
ContentValues mContentValues;
2845

2946
/** Called when the activity is first created. */
3047
@Override
3148
public void onCreate(Bundle savedInstanceState) {
3249
super.onCreate(savedInstanceState);
3350

34-
/*
35-
* Use the files dir to store the database /data/data/package..../db
36-
*/
37-
mDBdir = this.getFilesDir().getAbsolutePath() + File.separator + "db";
38-
new File(mDBdir).mkdirs();
39-
40-
db = new LevelDB(this, "db", 1);
41-
db.onCreate(null);
51+
// Get content provider and cursor
52+
ContentResolver cr = getContentResolver();
53+
Log.d(TAG, Settings.System.CONTENT_URI.toString());
54+
Log.d(TAG, LevelDBProvider.CONTENT_URI.toString());
55+
56+
Cursor cursor = cr.query(Settings.System.CONTENT_URI, null, null, null,
57+
null);
58+
if (cursor != null) {
59+
Log.d(TAG, cursor.getColumnNames()[0]);
60+
}
61+
Cursor c = cr
62+
.query(LevelDBProvider.CONTENT_URI, null, null, null, null);
63+
if (c != null) {
64+
Log.d(TAG, c.getColumnNames()[0]);
65+
}
66+
67+
ContentValues cv = new ContentValues();
68+
cv.put("key", "thisisthekey");
69+
cv.put("value", "somestuff");
70+
Uri result = cr.insert(LevelDBProvider.CONTENT_URI, cv);
71+
Log.d(TAG, result.toString());
72+
73+
int deletedcount = cr.delete(result, null, null);
74+
Log.d(TAG, "Deleted :" + deletedcount);
75+
76+
deletedcount = cr.delete(result.withAppendedPath(result, "1"), null,
77+
null);
78+
Log.d(TAG, "Deleted :" + deletedcount);
79+
80+
Cursor cdeleted = cr.query(LevelDBProvider.CONTENT_URI, null, null,
81+
null, null);
82+
if (cdeleted != null) {
83+
cdeleted.moveToFirst();
84+
String deletedvalue = cdeleted.getString(1);
85+
if (deletedvalue != null && !"".equals(deletedvalue)) {
86+
Log.d(TAG, "Value:" + deletedvalue + ":");
87+
} else {
88+
Log.d(TAG, "No value");
89+
}
90+
Log.e(TAG,
91+
"The cursor was not null, and it was supposed to be null.");
92+
}
93+
94+
mContentValues = new ContentValues();
95+
mContentValues.put("key", "thisisanewkey");
96+
mContentValues.put("value", "somestuff");
97+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
98+
mCursor = cr.query(mUri, null, null, null, null);
99+
if (mCursor != null) {
100+
mCursor.moveToFirst();
101+
String value = mCursor.getString(1);
102+
Log.d(TAG, "Successfully inserted and queried: " + value);
103+
} else {
104+
Log.e(TAG, "The cursor was null, and its not supposed to be null.");
105+
}
106+
107+
mContentValues.put("value", "updatedvalue");
108+
int updatedcount = cr.update(LevelDBProvider.CONTENT_URI,
109+
mContentValues, null, null);
110+
111+
mCursor = cr.query(mUri, null, null, null, null);
112+
if (mCursor != null) {
113+
mCursor.moveToFirst();
114+
String value = mCursor.getString(1);
115+
Log.d(TAG, "Successfully updated and queried: " + value);
116+
} else {
117+
Log.e(TAG, "The cursor was null, and its not supposed.");
118+
}
42119
}
43120

44121
@Override
@@ -48,37 +125,59 @@ protected void onResume() {
48125
* directory Insert some keys, Delete a key, Create a TextView and show
49126
* the value of a key retrieved from the DB.
50127
*/
51-
db.dbDestroy(mDBdir);
52-
db.dbOpen(mDBdir);
53-
db.dbPut("firstkey", "this is the value of the first key");
54-
db.dbPut("secondkey", "this is the value of the first key");
55-
db.dbPut("keyToDelete",
128+
ContentResolver cr = getContentResolver();
129+
130+
mContentValues = new ContentValues();
131+
mContentValues.put("key", "firstkey");
132+
mContentValues.put("value", "this is the value of the first key");
133+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
134+
135+
mContentValues.put("key", "secondkey");
136+
mContentValues.put("value", "this is the value of the second key");
137+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
138+
139+
mContentValues.put("key", "keyToDelete");
140+
mContentValues.put("value",
56141
"this is the value of the key that i want to delete");
57-
db.dbPut("fourthkey", "this is the value of the fourth key");
58-
db.dbDelete("keyToDelete");
142+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
143+
144+
mContentValues.put("key", "fourthkey");
145+
mContentValues.put("value", "this is the value of the fourth key");
146+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
147+
148+
int deletedcount = cr.delete(mUri, null, null);
59149

60150
TextView tv = new TextView(this);
61-
tv.setText(db.dbGet("fourthkey"));
62-
setContentView(tv);
63151

64-
ArrayList<String> keystoquery = new ArrayList<String>();
65-
try {
66-
AssetManager assetManager = getAssets();
67-
InputStream in = assetManager.open("sample.json");
68-
BufferedReader sourcefile = new BufferedReader(
69-
new InputStreamReader(in, "UTF-8"));
70-
String contents = "";
71-
String line = "";
72-
while ((line = sourcefile.readLine()) != null) {
73-
contents = contents + "\n" + line;
74-
}
152+
mCursor = cr.query(Uri.withAppendedPath(LevelDBProvider.CONTENT_URI, "fourthkey"), null, null, null, null);
153+
if (mCursor != null) {
154+
mCursor.moveToFirst();
155+
String value = mCursor.getString(1);
156+
tv.setText(value);
157+
setContentView(tv);
158+
}
159+
160+
ArrayList<String> keystoquery = new ArrayList<String>();
161+
try {
162+
AssetManager assetManager = getAssets();
163+
InputStream in = assetManager.open("sample.json");
164+
BufferedReader sourcefile = new BufferedReader(
165+
new InputStreamReader(in, "UTF-8"));
166+
String contents = "";
167+
String line = "";
168+
while ((line = sourcefile.readLine()) != null) {
169+
contents = contents + "\n" + line;
170+
}
75171
sourcefile.close();
76172
JSONObject json = new JSONObject(contents);
77173
Iterator<String> keys = json.keys();
78174
while (keys.hasNext()) {
79175
String key = (String) keys.next();
80176
keystoquery.add(key);
81-
db.dbPut(key, json.get(key).toString());
177+
178+
mContentValues.put("key", key);
179+
mContentValues.put("value", json.get(key).toString());
180+
mUri = cr.insert(LevelDBProvider.CONTENT_URI, mContentValues);
82181
}
83182

84183
int j = json.length() - 1;
@@ -96,7 +195,8 @@ protected void onResume() {
96195
tv.setText("JSON problem" + e.getLocalizedMessage());
97196
setContentView(tv);
98197
}
99-
198+
199+
100200
/*
101201
* Query entries randomly
102202
*/
@@ -106,7 +206,13 @@ protected void onResume() {
106206
Random randomGenerator = new Random();
107207
for (int k = 0; k < querycount; k++) {
108208
int randomKey = randomGenerator.nextInt(maxkey);
109-
String it = db.dbGet(keystoquery.get(randomKey));
209+
mCursor = cr.query(Uri.withAppendedPath(LevelDBProvider.CONTENT_URI, keystoquery.get(randomKey)), null, null, null, null);
210+
if (mCursor != null) {
211+
mCursor.moveToFirst();
212+
String value = mCursor.getString(1);
213+
tv.setText(value);
214+
setContentView(tv);
215+
}
110216
}
111217
long endtime = System.currentTimeMillis();
112218
long querytime = (endtime - startime);
@@ -136,23 +242,23 @@ private String readInFile(String filePath) throws IOException {
136242
/*
137243
* Methods which wrap LevelDB calls, see jni/main.cc for details
138244
*/
139-
// public native String dbOpen(String dbpath);
140-
//
141-
// public native String dbClose(String dbpath);
142-
//
143-
// public native String dbPut(String key1, String value1);
144-
//
145-
// public native String dbGet(String key1);
146-
//
147-
// public native String dbDelete(String key1);
148-
//
149-
// public native String dbDestroy(String dbpath);
245+
// public native String dbOpen(String dbpath);
246+
//
247+
// public native String dbClose(String dbpath);
248+
//
249+
// public native String dbPut(String key1, String value1);
250+
//
251+
// public native String dbGet(String key1);
252+
//
253+
// public native String dbDelete(String key1);
254+
//
255+
// public native String dbDestroy(String dbpath);
150256

151257
/*
152258
* A native method that is implemented by the 'hello-jni' native library,
153259
* which is packaged with this application.
154260
*/
155-
// public native String stringFromJNI();
261+
// public native String stringFromJNI();
156262

157263
/*
158264
* This is another native method declaration that is *not* implemented by
@@ -164,24 +270,24 @@ private String readInFile(String filePath) throws IOException {
164270
* Trying to call this function will result in a
165271
* java.lang.UnsatisfiedLinkError exception !
166272
*/
167-
// public native String unimplementedStringFromJNI();
273+
// public native String unimplementedStringFromJNI();
168274

169275
/*
170276
* this is used to load the 'leveldb' library on application startup. The
171277
* library has already been unpacked into
172278
* /data/data/com.example.HelloJni/lib/libleveldb.so at installation time by
173279
* the package manager.
174280
*/
175-
// static {
176-
// System.loadLibrary("leveldb");
177-
// }
281+
// static {
282+
// System.loadLibrary("leveldb");
283+
// }
178284

179285
@Override
180286
protected void onPause() {
181287
super.onPause();
182288
/*
183289
* Close the db in the onPause to not waste memory
184290
*/
185-
db.dbClose(mDBdir);
291+
// db.dbClose(mDBdir);
186292
}
187293
}

‎jni/main.cc

+2
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ JNIEXPORT jstring JNICALL Java_com_google_code_p_leveldb_LevelDB_dbDelete(JNIEnv
214214

215215
leveldb::Status status = db->Delete(leveldb::WriteOptions(), key);
216216
if (status.ok()) {
217+
// LOGI("success");
217218
const char* re = status.ToString().c_str();
218219
return env->NewStringUTF(re);
219220
}else{
221+
// LOGI("failure");
220222
const char* re = status.ToString().c_str();
221223
return env->NewStringUTF("NotFound");
222224
}

0 commit comments

Comments
 (0)
Please sign in to comment.