Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aw4y committed Apr 21, 2016
0 parents commit 087290f
Show file tree
Hide file tree
Showing 53 changed files with 2,435 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.idea
/.idea/*
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
![image](/images/hypercube.png =100x)
#WaspDb
WaspDB is a pure Java key/value (NoSQL) database library for Android. It supports AES256 encryption for all the disk storage. It's very small (the aar file is ~189 KB).

Keys and Values are simple Java Objects. Everything is automatically serialized using the [Kryo]() serialization library.

Data is stored by an implementation of hashmaps on disk.


###QuickStart

To use it with gradle

dependencies {
compile 'REPLACEME'
}

Let's assume a POJO (even with nested object, like Address):

class User {
private String username;
private String email;
private String telephone;
private Address address;
public User() {
}
// getters and setters...
}

No need to be Serializable or Parcelable or annotations or extending/implementing from other classes/interfaces, the only important thing is to have an **empty constructor**.

**Create a database, a WaspHash and store a POJO**

// create a database, using the default files dir as path, database name and a password
String path = getFilesDir().getPath();
String databaseName = "myDb";
String password = "passw0rd";

WaspDb db = WaspFactory.openOrCreateDatabase(path,databaseName,password);

// now create an WaspHash, it's like a sql table
WaspHash users = db.openOrCreateHash("users");

// now let's have a POJO
User p = new User();
... // do your stuff with your POJO!

// and simply store it!
users.put(p.getUsername(), p);


**To retrieve it**, it's just

User p = users.get("username1");

**Need all your objects?**

List<User> allUsers = users.getAllValues();

It returns all the users, in a standard java List.

Or you can **get all the keys** used

List<String> keys = users.getAllKeys();

or the actual **key/value map** (it's again a standard java class)

HashMap<String, User> usersMap = users.getAllData();

Please note that the process of creating an encrypted database is computationally expensive (10000 iterations to create the AES256 key), so also an async method is available:

WaspFactory.openOrCreateDatabase(path, databaseName, password, new WaspListener<WaspDb>() {
@Override
public void onDone(WaspDb waspDb) {
....
}
});


###Why WaspDb
I **hate** to store objects on sqlite! It's a lot of boiler code...for what?
Okay, let's say in the polite way :)

The object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is being used by a program written in an object-oriented programming language or style; particularly when objects or class definitions are mapped in a straightforward way to database tables or relational schema.
(from wikipedia [https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch](https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch))

###What WaspDb is NOT
This is not a SQL database. It does not have a relational data model, it does not support SQL queries, and it provides no support for indexes, nor transactions.

###Features
- it's pure java, it's standalone, no native stuff, it's not an ORM to SqlLite!

- the database addresses up to 4294967296 keys for WaspHash...enough? :)

###Limitations
- it's NOT transactional. So if you wanna store 10000, it will make 10000 actual write operations to disk in sequence. That means it will be slower (in this case) compared to transactional databases. (of course, if you store 100 items as a java List - so actually a single object - it will make a single(ish) write operation)


###Performances

I've used this project [https://github.com/Raizlabs/AndroidDatabaseLibraryComparison](https://github.com/Raizlabs/AndroidDatabaseLibraryComparison) to make some benchmarking.

This is the "simple trial" (storing 50 objects) on a Nexus 5, using WaspDb's encryption feature:

![image](/images/wasp_comparison.png =800x)

It's even faster if you don't need encryption.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "net.rehacktive.waspdbexample"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}


lintOptions {
abortOnError false
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile project(':waspdb')
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/stefano/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.rehacktive.waspdbexample;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.rehacktive.waspdbexample" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
120 changes: 120 additions & 0 deletions app/src/main/java/net/rehacktive/waspdbexample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package net.rehacktive.waspdbexample;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

import net.rehacktive.waspdb.WaspDb;
import net.rehacktive.waspdb.WaspFactory;
import net.rehacktive.waspdb.WaspHash;
import net.rehacktive.waspdb.WaspListener;
import net.rehacktive.waspdb.WaspObserver;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

ProgressBar progressBar;
// wasp objects
WaspDb db;
WaspHash hash;
WaspObserver observer;

UserAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

ListView userList = (ListView) findViewById(R.id.userlist);
adapter = new UserAdapter(this);
userList.setAdapter(adapter);

if(db==null) {
progressBar.setVisibility(View.VISIBLE);
WaspFactory.openOrCreateDatabase(getFilesDir().getPath(), "example", "Passw0rd", new WaspListener<WaspDb>() {
@Override
public void onDone(WaspDb waspDb) {
db = waspDb;
hash = db.openOrCreateHash("users");

progressBar.setVisibility(View.INVISIBLE);

getUsers();

observer = new WaspObserver() {
@Override
public void onChange() {
List<User> users = hash.getAllValues();
adapter.setUsers(users);
adapter.notifyDataSetChanged();
}
};

hash.register(observer);
}
});
}
}

private void getUsers() {
List<User> users = hash.getAllValues();
if(users==null)
users = new ArrayList<>();

adapter.setUsers(users);
adapter.notifyDataSetChanged();
}

private void addUser() {
User user = new User("user "+System.currentTimeMillis(), "");
hash.put(user.getUser_name(),user);
}

private void flushUsers() {
hash.flush();
}

@Override
protected void onDestroy() {
super.onDestroy();
hash.unregister(observer);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.add_user) {
addUser();
return true;
}

if (id == R.id.flush_user) {
flushUsers();
return true;
}

return super.onOptionsItemSelected(item);
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/net/rehacktive/waspdbexample/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.rehacktive.waspdbexample;

/**
* Created by stefano on 28/07/2015.
*/
public class User {

private String id;
private String user_name;
private String email;

public User() {
}

public User(String username, String email) {
this.user_name = username;
this.email = email;
this.id = ""+System.currentTimeMillis();
}

public String getId() {
return id;
}

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

public String getUser_name() {
return user_name;
}

public void setUser_name(String user_name) {
this.user_name = user_name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Loading

0 comments on commit 087290f

Please sign in to comment.