Skip to content

Commit a68d250

Browse files
Merge pull request #6 from greenrobot/squidb
Add SquiDB.
2 parents 50dbc28 + cc83639 commit a68d250

7 files changed

Lines changed: 364 additions & 0 deletions

File tree

SquiDB/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
buildscript {
2+
dependencies {
3+
classpath dep.androidPlugin
4+
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
5+
}
6+
}
7+
8+
apply plugin: 'com.android.application'
9+
apply plugin: 'com.neenbedankt.android-apt'
10+
11+
android {
12+
buildToolsVersion rootProject.ext.buildToolsVersion
13+
compileSdkVersion rootProject.ext.compileSdkVersion
14+
15+
defaultConfig {
16+
applicationId 'de.greenrobot.performance.squidb'
17+
minSdkVersion 14
18+
targetSdkVersion rootProject.ext.targetSdkVersion
19+
20+
testInstrumentationRunner 'android.test.InstrumentationTestRunner'
21+
}
22+
}
23+
24+
dependencies {
25+
androidTestCompile project(':Common')
26+
compile 'com.yahoo.squidb:squidb:2.0.0'
27+
compile 'com.yahoo.squidb:squidb-annotations:2.0.0'
28+
apt 'com.yahoo.squidb:squidb-processor:2.0.0'
29+
}
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package de.greenrobot.performance.squidb;
2+
3+
import com.yahoo.squidb.data.SquidCursor;
4+
import com.yahoo.squidb.sql.Query;
5+
import de.greenrobot.performance.BasePerfTestCase;
6+
import de.greenrobot.performance.StringGenerator;
7+
import de.greenrobot.performance.Tools;
8+
import java.sql.SQLException;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
* https://github.com/yahoo/squidb/wiki
14+
*/
15+
public class PerfTestSquiDB extends BasePerfTestCase {
16+
17+
@Override
18+
protected String getLogTag() {
19+
return getClass().getSimpleName();
20+
}
21+
22+
@Override
23+
protected void tearDown() throws Exception {
24+
getApplication().deleteDatabase(MySquidDatabase.DATABASE_NAME);
25+
26+
super.tearDown();
27+
}
28+
29+
@Override
30+
protected void doIndexedStringEntityQueries() throws Exception {
31+
// set up database
32+
MySquidDatabase database = new MySquidDatabase(getApplication());
33+
log("Set up database.");
34+
35+
for (int i = 0; i < RUNS; i++) {
36+
log("----Run " + (i + 1) + " of " + RUNS);
37+
indexedStringEntityQueriesRun(database, getBatchSize());
38+
}
39+
}
40+
41+
private void indexedStringEntityQueriesRun(MySquidDatabase database, int count) {
42+
// create entities
43+
List<IndexedStringEntity> entities = new ArrayList<>(count);
44+
String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
45+
for (int i = 0; i < count; i++) {
46+
IndexedStringEntity entity = new IndexedStringEntity();
47+
// start with id 1 as 0 is treated as NO_ID
48+
entity.setId((long) i + 1);
49+
entity.setIndexedString(fixedRandomStrings[i]);
50+
entities.add(entity);
51+
}
52+
log("Built entities.");
53+
54+
// insert entities
55+
database.beginTransaction();
56+
try {
57+
for (int i = 0; i < count; i++) {
58+
database.persistWithId(entities.get(i));
59+
}
60+
database.setTransactionSuccessful();
61+
} finally {
62+
database.endTransaction();
63+
}
64+
log("Inserted entities.");
65+
66+
// query for entities by indexed string at random
67+
int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1);
68+
69+
startClock();
70+
for (int i = 0; i < getQueryCount(); i++) {
71+
int nextIndex = randomIndices[i];
72+
//noinspection unused
73+
IndexedStringEntity indexedStringEntity = database.fetchByCriterion(
74+
IndexedStringEntity.class,
75+
IndexedStringEntity.INDEXED_STRING.eq(fixedRandomStrings[nextIndex]));
76+
}
77+
stopClock(Tools.LogMessage.QUERY_INDEXED);
78+
79+
// delete all entities
80+
database.deleteAll(IndexedStringEntity.class);
81+
log("Deleted all entities.");
82+
}
83+
84+
@Override
85+
protected void doOneByOneAndBatchCrud() throws Exception {
86+
// set up database
87+
MySquidDatabase database = new MySquidDatabase(getApplication());
88+
log("Set up database.");
89+
90+
// set up database
91+
for (int i = 0; i < RUNS; i++) {
92+
log("----Run " + (i + 1) + " of " + RUNS);
93+
oneByOneCrudRun(database, getBatchSize() / 10);
94+
batchCrudRun(database, getBatchSize());
95+
}
96+
}
97+
98+
private void oneByOneCrudRun(MySquidDatabase database, int count) throws SQLException {
99+
final List<SimpleEntityNotNull> list = new ArrayList<>();
100+
for (int i = 0; i < count; i++) {
101+
list.add(createEntity((long) i));
102+
}
103+
104+
startClock();
105+
for (int i = 0; i < count; i++) {
106+
database.persistWithId(list.get(i));
107+
}
108+
stopClock(Tools.LogMessage.ONE_BY_ONE_CREATE);
109+
110+
// re-set values to set entity as modified
111+
for (int i = 0; i < list.size(); i++) {
112+
updateEntity(list.get(i));
113+
}
114+
115+
startClock();
116+
for (int i = 0; i < count; i++) {
117+
database.persist(list.get(i));
118+
}
119+
stopClock(Tools.LogMessage.ONE_BY_ONE_UPDATE);
120+
121+
deleteAll(database);
122+
}
123+
124+
@SuppressWarnings("unused")
125+
private void batchCrudRun(MySquidDatabase database, int count) throws Exception {
126+
final List<SimpleEntityNotNull> list = new ArrayList<>();
127+
for (int i = 0; i < count; i++) {
128+
list.add(createEntity((long) i));
129+
}
130+
131+
startClock();
132+
database.beginTransaction();
133+
try {
134+
for (int i = 0; i < count; i++) {
135+
database.persistWithId(list.get(i));
136+
}
137+
database.setTransactionSuccessful();
138+
} finally {
139+
database.endTransaction();
140+
}
141+
stopClock(Tools.LogMessage.BATCH_CREATE);
142+
143+
// re-set values to set entity as modified
144+
for (int i = 0; i < list.size(); i++) {
145+
updateEntity(list.get(i));
146+
}
147+
148+
startClock();
149+
database.beginTransaction();
150+
try {
151+
for (int i = 0; i < count; i++) {
152+
database.persist(list.get(i));
153+
}
154+
database.setTransactionSuccessful();
155+
} finally {
156+
database.endTransaction();
157+
}
158+
stopClock(Tools.LogMessage.BATCH_UPDATE);
159+
160+
startClock();
161+
List<SimpleEntityNotNull> reloaded = new ArrayList<>(count);
162+
163+
SquidCursor<SimpleEntityNotNull> query = database.query(SimpleEntityNotNull.class,
164+
Query.select());
165+
while (query.moveToNext()) {
166+
SimpleEntityNotNull entity = new SimpleEntityNotNull();
167+
entity.readPropertiesFromCursor(query);
168+
reloaded.add(entity);
169+
}
170+
query.close();
171+
stopClock(Tools.LogMessage.BATCH_READ);
172+
173+
startClock();
174+
for (int i = 0; i < reloaded.size(); i++) {
175+
SimpleEntityNotNull entity = reloaded.get(i);
176+
long id = entity.getId();
177+
Boolean simpleBoolean = entity.isSimpleBoolean();
178+
byte simpleByte = entity.getSimpleByte()[0];
179+
Integer simpleShort = entity.getSimpleShort();
180+
Integer simpleInt = entity.getSimpleInt();
181+
Long simpleLong = entity.getSimpleLong();
182+
Double simpleFloat = entity.getSimpleFloat();
183+
Double simpleDouble = entity.getSimpleDouble();
184+
String simpleString = entity.getSimpleString();
185+
byte[] simpleByteArray = entity.getSimpleByteArray();
186+
}
187+
stopClock(Tools.LogMessage.BATCH_ACCESS);
188+
189+
startClock();
190+
deleteAll(database);
191+
stopClock(Tools.LogMessage.BATCH_DELETE);
192+
}
193+
194+
private void deleteAll(MySquidDatabase database) {
195+
database.deleteAll(SimpleEntityNotNull.class);
196+
}
197+
198+
protected static SimpleEntityNotNull createEntity(long id) {
199+
SimpleEntityNotNull entity = new SimpleEntityNotNull();
200+
// start with id 1 as 0 is treated as NO_ID
201+
entity.setId(id + 1);
202+
entity.setIsSimpleBoolean(true);
203+
entity.setSimpleByte(new byte[] { Byte.MAX_VALUE });
204+
entity.setSimpleShort((int) Short.MAX_VALUE);
205+
entity.setSimpleInt(Integer.MAX_VALUE);
206+
entity.setSimpleLong(Long.MAX_VALUE);
207+
entity.setSimpleFloat((double) Float.MAX_VALUE);
208+
entity.setSimpleDouble(Double.MAX_VALUE);
209+
entity.setSimpleString("greenrobot greenDAO");
210+
byte[] bytes = { 42, -17, 23, 0, 127, -128 };
211+
entity.setSimpleByteArray(bytes);
212+
return entity;
213+
}
214+
215+
protected static void updateEntity(SimpleEntityNotNull entity) {
216+
entity.setIsSimpleBoolean(true);
217+
entity.setSimpleByte(new byte[] { Byte.MAX_VALUE });
218+
entity.setSimpleShort((int) Short.MAX_VALUE);
219+
entity.setSimpleInt(Integer.MAX_VALUE);
220+
entity.setSimpleLong(Long.MAX_VALUE);
221+
entity.setSimpleFloat((double) Float.MAX_VALUE);
222+
entity.setSimpleDouble(Double.MAX_VALUE);
223+
entity.setSimpleString("greenrobot greenDAO");
224+
byte[] bytes = { 42, -17, 23, 0, 127, -128 };
225+
entity.setSimpleByteArray(bytes);
226+
}
227+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="de.greenrobot.performance.squidb">
4+
5+
<application>
6+
<!-- empty application tag -->
7+
</application>
8+
</manifest>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.greenrobot.performance.squidb;
2+
3+
import com.yahoo.squidb.annotations.PrimaryKey;
4+
import com.yahoo.squidb.annotations.TableModelSpec;
5+
6+
/**
7+
* Simple entity with a string property that is indexed.
8+
*/
9+
@TableModelSpec(className = "IndexedStringEntity", tableName="indexed_string_entity")
10+
public class IndexedStringEntitySpec {
11+
12+
@PrimaryKey(autoincrement = false)
13+
public long _id;
14+
public String indexedString;
15+
16+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package de.greenrobot.performance.squidb;
2+
3+
import android.content.Context;
4+
import com.yahoo.squidb.data.SquidDatabase;
5+
import com.yahoo.squidb.data.TableModel;
6+
import com.yahoo.squidb.data.adapter.SQLiteDatabaseWrapper;
7+
import com.yahoo.squidb.sql.Index;
8+
import com.yahoo.squidb.sql.Table;
9+
10+
public class MySquidDatabase extends SquidDatabase {
11+
12+
public static final String DATABASE_NAME = "sqldelight.db";
13+
private static final int DATABASE_VERSION = 1;
14+
15+
/**
16+
* Create a new SquidDatabase
17+
*
18+
* @param context the Context, must not be null
19+
*/
20+
public MySquidDatabase(Context context) {
21+
super(context);
22+
}
23+
24+
@Override
25+
public String getName() {
26+
return DATABASE_NAME;
27+
}
28+
29+
@Override
30+
protected int getVersion() {
31+
return DATABASE_VERSION;
32+
}
33+
34+
@Override
35+
protected Table[] getTables() {
36+
return new Table[] { SimpleEntityNotNull.TABLE, IndexedStringEntity.TABLE };
37+
}
38+
39+
@Override
40+
protected Index[] getIndexes() {
41+
return new Index[] {
42+
IndexedStringEntity.TABLE.index("string_idx", IndexedStringEntity.INDEXED_STRING)
43+
};
44+
}
45+
46+
@Override
47+
protected boolean onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
48+
return false;
49+
}
50+
51+
/**
52+
* Currently inserting with existing IDs is not directly supported (it is actually bad practice
53+
* to use the row id), work around this by exposing {@link #insertRow(TableModel)}.
54+
* https://github.com/yahoo/squidb/issues/84
55+
*/
56+
public void persistWithId(TableModel item) {
57+
insertRow(item);
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.greenrobot.performance.squidb;
2+
3+
import com.yahoo.squidb.annotations.PrimaryKey;
4+
import com.yahoo.squidb.annotations.TableModelSpec;
5+
6+
/**
7+
* Simple entity for performance testing.
8+
*/
9+
@TableModelSpec(className = "SimpleEntityNotNull", tableName="simple_entity_not_null")
10+
public class SimpleEntityNotNullSpec {
11+
12+
@PrimaryKey(autoincrement = false)
13+
public long _id;
14+
public boolean simpleBoolean;
15+
public byte simpleByte;
16+
public short simpleShort;
17+
public int simpleInt;
18+
public long simpleLong;
19+
public float simpleFloat;
20+
public double simpleDouble;
21+
public String simpleString;
22+
public byte[] simpleByteArray;
23+
24+
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ include ':Parse'
1111
include ':Realm'
1212
include ':SQLDelight'
1313
include ':Sqlite'
14+
include ':SquiDB'

0 commit comments

Comments
 (0)