Skip to content

Commit d5e51ff

Browse files
committed
Revert "Removed sample Activity stuff and made it into a bunch of static functions instead."
This reverts commit 38930bd.
1 parent 1d23077 commit d5e51ff

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

AndroidManifest.xml

+7
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@
66
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="11"/>
77
<application android:label="@string/app_name"
88
android:debuggable="true">
9+
<activity android:name="com.google.code.p.leveldb.LevelDB"
10+
android:label="@string/app_name">
11+
<intent-filter>
12+
<action android:name="android.intent.action.MAIN" />
13+
<category android:name="android.intent.category.LAUNCHER" />
14+
</intent-filter>
15+
</activity>
916
</application>
1017
</manifest>

assets/sample.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"_id": "33EE7A7F-45DF-40FD-B847-D46185A2AE0D",
3+
"_rev": "6-6993acafd1a0571e3e8c9f47471df84d",
4+
"utterance": "tusunayawan",
5+
"morphemes": "my morphemes",
6+
"gloss": "my gloss",
7+
"translation": "my translation",
8+
"grammaticalTags": "impulsative",
9+
"sessionID": 0,
10+
"status": {
11+
"statuses": [
12+
{
13+
"id": 1,
14+
"label": "Checked"
15+
},
16+
{
17+
"id": 2,
18+
"label": "To be checked",
19+
"selected": true
20+
},
21+
{
22+
"id": 3,
23+
"label": "Deleted"
24+
}
25+
],
26+
"active": 0,
27+
"defaultStatus": 0
28+
},
29+
"datumField": {
30+
"sessionID": 0,
31+
"user": "User",
32+
"informant": "",
33+
"language": "Language",
34+
"languageFamily": "Family",
35+
"dialect": "",
36+
"date": "Mon Jun 04 2012 16:07:24 GMT-0400 (EDT)",
37+
"goal": ""
38+
},
39+
"datumTag": {
40+
"script": "",
41+
"context": "",
42+
"semanticDenotation": "",
43+
"ipa": "",
44+
"segmentation": "tusu-naya-wa-n",
45+
"other": ""
46+
}
47+
}

src/com/google/code/p/leveldb/LevelDB.java

+136-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,133 @@
1515
*/
1616
package com.google.code.p.leveldb;
1717

18-
public class LevelDB {
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+
19145
/*
20146
* Methods which wrap LevelDB calls, see jni/main.cc for details
21147
*/
@@ -58,4 +184,13 @@ public class LevelDB {
58184
static {
59185
System.loadLibrary("leveldb");
60186
}
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+
}
61196
}

0 commit comments

Comments
 (0)