15
15
*/
16
16
package com .google .code .p .leveldb ;
17
17
18
- import java .io .BufferedReader ;
19
- import java .io .File ;
20
- import java .io .FileInputStream ;
21
- import java .io .IOException ;
22
- import java .io .InputStream ;
23
- import java .io .InputStreamReader ;
24
- import java .util .ArrayList ;
25
- import java .util .Iterator ;
26
- import java .util .Random ;
27
-
28
- import org .json .JSONException ;
29
- import org .json .JSONObject ;
30
-
31
- import android .app .Activity ;
32
- import android .content .res .AssetManager ;
33
- import android .widget .TextView ;
34
- import android .widget .Toast ;
35
- import android .os .Bundle ;
36
-
37
- public class LevelDB extends Activity {
38
- String mDBdir ;
39
-
40
- /** Called when the activity is first created. */
41
- @ Override
42
- public void onCreate (Bundle savedInstanceState ) {
43
- super .onCreate (savedInstanceState );
44
-
45
- /*
46
- * Use the files dir to store the database /data/data/package..../db
47
- */
48
- mDBdir = this .getFilesDir ().getAbsolutePath () + File .separator + "db" ;
49
- new File (mDBdir ).mkdirs ();
50
-
51
- }
52
-
53
- @ Override
54
- protected void onResume () {
55
- /*
56
- * Sample Database code: Open the database using the path to its
57
- * directory Insert some keys, Delete a key, Create a TextView and show
58
- * the value of a key retrieved from the DB.
59
- */
60
- dbDestroy (mDBdir );
61
- dbOpen (mDBdir );
62
- dbPut ("firstkey" , "this is the value of the first key" );
63
- dbPut ("secondkey" , "this is the value of the first key" );
64
- dbPut ("keyToDelete" ,
65
- "this is the value of the key that i want to delete" );
66
- dbPut ("fourthkey" , "this is the value of the fourth key" );
67
- dbDelete ("keyToDelete" );
68
-
69
- TextView tv = new TextView (this );
70
- tv .setText (dbGet ("fourthkey" ));
71
- setContentView (tv );
72
-
73
- ArrayList <String > keystoquery = new ArrayList <String >();
74
- try {
75
- AssetManager assetManager = getAssets ();
76
- InputStream in = assetManager .open ("sample.json" );
77
- BufferedReader sourcefile = new BufferedReader (
78
- new InputStreamReader (in , "UTF-8" ));
79
- String contents = "" ;
80
- String line = "" ;
81
- while ((line = sourcefile .readLine ()) != null ) {
82
- contents = contents + "\n " + line ;
83
- }
84
- sourcefile .close ();
85
- JSONObject json = new JSONObject (contents );
86
- Iterator <String > keys = json .keys ();
87
- while (keys .hasNext ()) {
88
- String key = (String ) keys .next ();
89
- keystoquery .add (key );
90
- dbPut (key , json .get (key ).toString ());
91
- }
92
-
93
- int j = json .length () - 1 ;
94
- tv .setText ("element" + j );
95
- setContentView (tv );
96
-
97
- } catch (IOException e ) {
98
- Toast .makeText (this , "File read problem" + e .getLocalizedMessage (),
99
- Toast .LENGTH_LONG ).show ();
100
- tv .setText ("File read problem" + e .getLocalizedMessage ());
101
- setContentView (tv );
102
- } catch (JSONException e ) {
103
- Toast .makeText (this , "Json problem" + e .getLocalizedMessage (),
104
- Toast .LENGTH_LONG ).show ();
105
- tv .setText ("JSON problem" + e .getLocalizedMessage ());
106
- setContentView (tv );
107
- }
108
-
109
- /*
110
- * Query entries randomly
111
- */
112
- long startime = System .currentTimeMillis ();
113
- int maxkey = keystoquery .size () - 1 ;
114
- int querycount = 1000 ;
115
- Random randomGenerator = new Random ();
116
- for (int k = 0 ; k < querycount ; k ++) {
117
- int randomKey = randomGenerator .nextInt (maxkey );
118
- String it = dbGet (keystoquery .get (randomKey ));
119
- }
120
- long endtime = System .currentTimeMillis ();
121
- long querytime = (endtime - startime );
122
- tv .setText ("Random quering of " + querycount
123
- + " entries took this many miliseconds: " + querytime );
124
- setContentView (tv );
125
-
126
- super .onResume ();
127
- }
128
-
129
- private String readInFile (String filePath ) throws IOException {
130
- File currentFile = new File (filePath );
131
- BufferedReader sourcefile = new BufferedReader (new InputStreamReader (
132
- new FileInputStream (currentFile ), "UTF-8" ));
133
- String contents = "" ;
134
- String line = "" ;
135
-
136
- while ((line = sourcefile .readLine ()) != null ) {
137
-
138
- contents = contents + "\n " + line ;
139
-
140
- }
141
- sourcefile .close ();
142
- return contents ;
143
- }
144
-
18
+ public class LevelDB {
145
19
/*
146
20
* Methods which wrap LevelDB calls, see jni/main.cc for details
147
21
*/
@@ -184,13 +58,4 @@ private String readInFile(String filePath) throws IOException {
184
58
static {
185
59
System .loadLibrary ("leveldb" );
186
60
}
187
-
188
- @ Override
189
- protected void onPause () {
190
- super .onPause ();
191
- /*
192
- * Close the db in the onPause to not waste memory
193
- */
194
- dbClose (mDBdir );
195
- }
196
61
}
0 commit comments