Skip to content

Commit

Permalink
Merge pull request littleinc#2 from MazyNoc/optimized-raw-sql
Browse files Browse the repository at this point in the history
New optimized raw sql executor
  • Loading branch information
daj committed May 13, 2015
2 parents 2173443 + d5475af commit c3fd32e
Show file tree
Hide file tree
Showing 6 changed files with 542 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.littleinc.orm_benchmark.greendao.GreenDaoExecutor;
import com.littleinc.orm_benchmark.ormlite.ORMLiteExecutor;
import com.littleinc.orm_benchmark.sqlite.SQLiteExecutor;
import com.littleinc.orm_benchmark.optimizedsqlite.SQLiteRawExecutor;

public class Application extends android.app.Application {

Expand All @@ -13,6 +14,7 @@ public void onCreate() {
super.onCreate();

SQLiteExecutor.INSTANCE.init(this, USE_IN_MEMORY_DB);
SQLiteRawExecutor.INSTANCE.init(this, USE_IN_MEMORY_DB);
ORMLiteExecutor.INSTANCE.init(this, USE_IN_MEMORY_DB);
GreenDaoExecutor.INSTANCE.init(this, USE_IN_MEMORY_DB);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ShareCompat.IntentBuilder;
import android.text.Html;
import android.util.Log;
import android.util.SparseArray;
Expand All @@ -31,6 +30,7 @@
import com.littleinc.orm_benchmark.greendao.GreenDaoExecutor;
import com.littleinc.orm_benchmark.ormlite.ORMLiteExecutor;
import com.littleinc.orm_benchmark.sqlite.SQLiteExecutor;
import com.littleinc.orm_benchmark.optimizedsqlite.SQLiteRawExecutor;
import com.littleinc.orm_benchmark.util.Util;

public class MainActivity extends FragmentActivity {
Expand All @@ -44,7 +44,9 @@ public class MainActivity extends FragmentActivity {
private Button mShowResultsBtn;

private BenchmarkExecutable[] mOrms = new BenchmarkExecutable[] {
SQLiteExecutor.INSTANCE, ORMLiteExecutor.INSTANCE,
SQLiteExecutor.INSTANCE,
SQLiteRawExecutor.INSTANCE,
ORMLiteExecutor.INSTANCE,
GreenDaoExecutor.INSTANCE };

private SparseArray<Map<Task, List<Long>>> mGlobalResults;
Expand All @@ -71,8 +73,13 @@ public void runBenchmark(View v) {
v.setEnabled(false);
mShowResultsBtn.setEnabled(false);

new ProfilerTask(v).execute(CREATE_DB, WRITE_DATA, READ_DATA,
READ_INDEXED, READ_SEARCH, DROP_DB);
new ProfilerTask(v).execute(
CREATE_DB,
WRITE_DATA,
READ_DATA,
READ_INDEXED,
READ_SEARCH,
DROP_DB);
} else {
mResults = buildResults();
Log.d(MainActivity.class.getSimpleName(), "Results:\n" + mResults);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.littleinc.orm_benchmark.optimizedsqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

// DB CONFIG
private static int DB_VERSION = 1;

private static String DB_NAME = "sqliteopt_db";

public DataBaseHelper(Context context, boolean isInMemory) {
super(context, (isInMemory ? null : DB_NAME), null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.littleinc.orm_benchmark.optimizedsqlite;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

import java.util.List;

public class Message {

public static final String TABLE_NAME = "message";

public static final String CONTENT = "content";

public static final String READERS = "readers";

public static final String SORTED_BY = "sorted_by";

public static final String CLIENT_ID = "client_id";

public static final String SENDER_ID = "sender_id";

public static final String CHANNEL_ID = "channel_id";

public static final String COMMAND_ID = "command_id";

public static final String CREATED_AT = "created_at";

private long mId;

private long mClientId;

private long mCommandId;

private double mSortedBy;

private int mCreatedAt;

private String mContent;

private long mSenderId;

private long mChannelId;

private List<User> mReaders;

public static final String[] PROJECTION = new String[] { CONTENT,
SORTED_BY, CLIENT_ID, SENDER_ID, CHANNEL_ID, COMMAND_ID, CREATED_AT };

public static void createTable(DataBaseHelper helper) {
SQLiteDatabase db = helper.getWritableDatabase();

db.execSQL(new StringBuilder("CREATE TABLE '").append(TABLE_NAME)
.append("' ('").append(BaseColumns._ID)
.append("' INTEGER PRIMARY KEY AUTOINCREMENT, '")
.append(CLIENT_ID).append("' INTEGER, '").append(SORTED_BY)
.append("' REAL, '").append(CREATED_AT).append("' INTEGER, '")
.append(CONTENT).append("' TEXT, '").append(SENDER_ID)
.append("' INTEGER NOT NULL, '").append(CHANNEL_ID)
.append("' INTEGER NOT NULL, '").append(COMMAND_ID)
.append("' INTEGER);").toString());

db.execSQL(new StringBuilder("CREATE INDEX IDX_MESSAGE_COMMAND_ID ON ")
.append(TABLE_NAME).append(" (").append(COMMAND_ID)
.append(");").toString());
}

public static void dropTable(DataBaseHelper helper) {
SQLiteDatabase db = helper.getWritableDatabase();

db.execSQL(new StringBuilder("DROP TABLE '").append(TABLE_NAME)
.append("';").toString());
}

public long getId() {
return mId;
}

public void setId(long id) {
this.mId = id;
}

public long getClientId() {
return mClientId;
}

public void setClientId(long clientId) {
this.mClientId = clientId;
}

public long getCommandId() {
return mCommandId;
}

public void setCommandId(long commandId) {
this.mCommandId = commandId;
}

public double getSortedBy() {
return mSortedBy;
}

public void setSortedBy(double sortedBy) {
this.mSortedBy = sortedBy;
}

public int getCreatedAt() {
return mCreatedAt;
}

public void setCreatedAt(int createdAt) {
this.mCreatedAt = createdAt;
}

public String getContent() {
return mContent;
}

public void setContent(String content) {
this.mContent = content;
}

public long getSenderId() {
return mSenderId;
}

public void setSenderId(long senderId) {
this.mSenderId = senderId;
}

public long getChannelId() {
return mChannelId;
}

public void setChannelId(long channelId) {
this.mChannelId = channelId;
}

public void setReaders(List<User> readers) {
mReaders = readers;
}

public List<User> getReaders() {
return mReaders;
}

public boolean hasReaders() {
return mReaders != null && !mReaders.isEmpty();
}

public void prepareForInsert(final SQLiteStatement insertMessage) {
insertMessage.bindString(1, mContent);
insertMessage.bindDouble(2, mSortedBy);
insertMessage.bindLong(3, mClientId);
insertMessage.bindLong(4, mSenderId);
insertMessage.bindLong(5, mChannelId);
insertMessage.bindLong(6, mCommandId);
insertMessage.bindLong(7, mCreatedAt);
}
}
Loading

0 comments on commit c3fd32e

Please sign in to comment.