Giter Club home page Giter Club logo

simpledbw's Introduction

SimpleDBW

A simple to use persistente storage wrapper around Badger for Android.

usage preview

Installation

Download (or clone) the repository then use Android Studio to import the library to your project file > new > import module

In your app's module build.gradle file include

repositories {
    flatDir {
        dirs './simpledbw/libs'
    }
}

The library uses lifecycle-extensions. So you have to also include this dependacy.

dependencies {
   implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
   implementation (name:'simpledbw')
}

initialisation

In your module's main activity onCreate function add this line

ProcessLifecycleOwner.get().getLifecycle().addObserver(new DBWhelper());

ie.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // DB init
        // Add Lifecycle Observer
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new DBWhelper());

Usage

Set value

Database db = new Database();

The database uses byte[] for both keys and values.

db.Update("key".getBytes(), "value".getBytes());

Get value

A db.View query returns a byte[] with the requested value. If key does not exist it returns "key does not exist".

byte[] returnedvalue = db.View("key".getBytes());

This can be for example converted to string and used with TextView like so

String text = new String(returnedvalue);
exampleTextView.setText(text);

Delete key

db.Delete("key".getBytes());

Iterate

Prefix

Iterated over the database quering a specific prefix

ArrayMap<byte[], byte[]> entries =  db.ViewPrefix("prefix".getBytes());

and you get an ArrayMap with all the associated keys and values. Please check Android's documentation for more information and usage.

Delete all keys assosiated with spedific prefix.

db.PrefixDrop("prefix".getBytes());
Dump returns an ArrayMap with every entry in the database.
ArrayMap<byte[], byte[]> entries =  db.DumpAll();

it can be used for example to inflate a TableLayout

ie.

ArrayMap<byte[], byte[]> tables =  db.DumpAll();
TableLayout dump = findViewById(R.id.ret_vals);
dump.removeAllViews();
Iterator it = tables.entrySet().iterator();

while (it.hasNext()) {
  ArrayMap.Entry pair = (ArrayMap.Entry)it.next();
  LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService
                            (Context.LAYOUT_INFLATER_SERVICE);
  View row = inflater.inflate(R.layout.row, null);
                    dump.addView(row);
   //View rowID = row.findViewById(R.id.tableraw);
   TextView keyfield = row.findViewById(R.id.printkey);
   TextView valuefield = row.findViewById(R.id.printvalue);
   String key = new String((byte[]) pair.getKey());
   String value = new String((byte[]) pair.getValue());
   keyfield.setText(key);
   valuefield.setText(value);
    it.remove(); // avoids a ConcurrentModificationException
}

Monotonically increasing integers

You can get monotonically increasing integers with strong durability

long nextInt = db.Mii("monotonic_key".getBytes());

Reset the count by deleting the key

db.Delete("monotonic_key".getBytes());

Drop all

db.DropDB();

Pre-populated entries

Place this on your module's main activity onCreate function. Please note if you drop the DB or delete the key the next time the app starts and opens the database these keys will regenerate.

db.PrePopulate("PREFIX_sample".getBytes(), "sample_value".getBytes());

ie.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // DB init
        // Add Lifecycle Observer
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new DBWhelper());
        // call the class
        Database db = new Database();
        // add some dummy pre-populated values
        db.PrePopulate("PREFIX_sample".getBytes(), "sample_value".getBytes());
        db.PrePopulate("PREFIX_dummy".getBytes(), "dummy_value".getBytes());

Build Go source

Unfortunatly gomobile does not support gomodules at the moment so you have to work within $GOPATH.

First ensure you have gomobile and badger installed. For more information click here and here respectively.

  • go get github.com/dgraph-io/badger/...

and

  • go get golang.org/x/mobile/cmd/gomobile
  • gomobile init

then

  • run go get https://github.com/bh90210/SimpleDBW
  • and gomobile bind -o simpledbw/dbwrapper.aar -target=android $GOPATH/src/github.com/bh90210/SimpleDBW

simpledbw's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.