14
14
import org .json .JSONObject ;
15
15
16
16
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 ;
17
19
18
20
import android .app .Activity ;
21
+ import android .content .ContentResolver ;
22
+ import android .content .ContentValues ;
19
23
import android .content .res .AssetManager ;
24
+ import android .database .Cursor ;
25
+ import android .util .Log ;
20
26
import android .widget .TextView ;
21
27
import android .widget .Toast ;
22
28
import android .net .Uri ;
23
29
import android .os .Bundle ;
30
+ import android .provider .Settings ;
24
31
25
32
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 ;
28
45
29
46
/** Called when the activity is first created. */
30
47
@ Override
31
48
public void onCreate (Bundle savedInstanceState ) {
32
49
super .onCreate (savedInstanceState );
33
50
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
+ }
42
119
}
43
120
44
121
@ Override
@@ -48,37 +125,59 @@ protected void onResume() {
48
125
* directory Insert some keys, Delete a key, Create a TextView and show
49
126
* the value of a key retrieved from the DB.
50
127
*/
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" ,
56
141
"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 );
59
149
60
150
TextView tv = new TextView (this );
61
- tv .setText (db .dbGet ("fourthkey" ));
62
- setContentView (tv );
63
151
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
+ }
75
171
sourcefile .close ();
76
172
JSONObject json = new JSONObject (contents );
77
173
Iterator <String > keys = json .keys ();
78
174
while (keys .hasNext ()) {
79
175
String key = (String ) keys .next ();
80
176
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 );
82
181
}
83
182
84
183
int j = json .length () - 1 ;
@@ -96,7 +195,8 @@ protected void onResume() {
96
195
tv .setText ("JSON problem" + e .getLocalizedMessage ());
97
196
setContentView (tv );
98
197
}
99
-
198
+
199
+
100
200
/*
101
201
* Query entries randomly
102
202
*/
@@ -106,7 +206,13 @@ protected void onResume() {
106
206
Random randomGenerator = new Random ();
107
207
for (int k = 0 ; k < querycount ; k ++) {
108
208
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
+ }
110
216
}
111
217
long endtime = System .currentTimeMillis ();
112
218
long querytime = (endtime - startime );
@@ -136,23 +242,23 @@ private String readInFile(String filePath) throws IOException {
136
242
/*
137
243
* Methods which wrap LevelDB calls, see jni/main.cc for details
138
244
*/
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);
150
256
151
257
/*
152
258
* A native method that is implemented by the 'hello-jni' native library,
153
259
* which is packaged with this application.
154
260
*/
155
- // public native String stringFromJNI();
261
+ // public native String stringFromJNI();
156
262
157
263
/*
158
264
* This is another native method declaration that is *not* implemented by
@@ -164,24 +270,24 @@ private String readInFile(String filePath) throws IOException {
164
270
* Trying to call this function will result in a
165
271
* java.lang.UnsatisfiedLinkError exception !
166
272
*/
167
- // public native String unimplementedStringFromJNI();
273
+ // public native String unimplementedStringFromJNI();
168
274
169
275
/*
170
276
* this is used to load the 'leveldb' library on application startup. The
171
277
* library has already been unpacked into
172
278
* /data/data/com.example.HelloJni/lib/libleveldb.so at installation time by
173
279
* the package manager.
174
280
*/
175
- // static {
176
- // System.loadLibrary("leveldb");
177
- // }
281
+ // static {
282
+ // System.loadLibrary("leveldb");
283
+ // }
178
284
179
285
@ Override
180
286
protected void onPause () {
181
287
super .onPause ();
182
288
/*
183
289
* Close the db in the onPause to not waste memory
184
290
*/
185
- db .dbClose (mDBdir );
291
+ // db.dbClose(mDBdir);
186
292
}
187
293
}
0 commit comments