Using Snappy's key-value database in Android

SnappyDB - NoSQL key-value database for Android. It is quite easy to use and is a good option if you want to use the NoSQL version of the database in your project (more details here ).
According to the developers, in write and read operations Snappy is faster than SQLite:

image

So, let's begin. First you need to add dependencies to build.gradle: Now, let's start, directly, working with the database itself. First, let's figure out how to use SnappyDB to work with primitive types and arrays, save and retrieve them. For clarity, create a small markup:

implementation 'com.snappydb:snappydb-lib:0.5.2'
implementation 'com.esotericsoftware.kryo:kryo:2.24.0'





<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/filmNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_centerInParent="true"
        android:layout_margin="8dp"
        />

    <TextView
        android:id="@+id/filmBudgetTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:layout_below="@+id/filmNameTextView"
        android:layout_margin="8dp"
        />

    <TextView
        android:id="@+id/genreTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:layout_below="@id/filmBudgetTextView"
        android:layout_margin="8dp"
        />

    <TextView
        android:id="@+id/isAdult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/genreTextView"
        android:textSize="25sp"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

Let's say that we will store movie data in our database

In MainActivity we will create the PutValues ​​method:
private void putValues(String name, int budget, boolean isAdult, String[] genres) throws SnappydbException {
        DB snappyDB = DBFactory.open(this,"Film");
        //     Film (      open)

        snappyDB.put("Name", name);
        snappyDB.putInt("budget", budget);
        snappyDB.putBoolean("isAdult", isAdult);
        snappyDB.put("genres", genres);
        //       
}

Note that the method in which we work with SnappyDB should throw a SnappydbException.

Now we write the setValues ​​method to get data from the database and output it:
private void setValues(DB snappyDB) throws SnappydbException {
        TextView filmNameTextView = findViewById(R.id.filmNameTextView),
                 filmBudgetTextView = findViewById(R.id.filmBudgetTextView),
                 genresTextView = findViewById(R.id.genreTextView),
                 isAdultTextView = findViewById(R.id.isAdult); //    

        String name = snappyDB.get("Name");
        int budget = snappyDB.getInt("budget");
        boolean isAdult = snappyDB.getBoolean("isAdult");
        String[] genres = snappyDB.getObjectArray("genres", String.class);//2  =  
        //  get...     

        filmNameTextView.setText(name);
        filmBudgetTextView.setText(String.valueOf(budget));
        isAdultTextView.setText(String.valueOf(isAdult));

        for(int i = 0;i < genres.length;i++) {
            genresTextView.setText(genresTextView.getText() + " " + genres[i]);
        }
}


Call the created methods in onCreate (be sure to surround them with a try / catch block):
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            putValues("Forrest Gump", 677000000,false, new String[] {"Drama","Melodrama"});
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        
        try {
            DB snappyDB = DBFactory.open(this, "Film");
            setValues(snappyDB);
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
}


We start and see that everything is working properly:
image

Now let's figure out how to work with our own classes and other complex types.
Create a Film class with 3 fields:
public class Film {
    private String name;
    private int budget;
    private String[] genres;

    public Film() {}

    public Film(String name, int budget, String[] genres) {
        this.name = name;
        this.budget = budget;
        this.genres = genres;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBudget() {
        return budget;
    }

    public void setBudget(int budget) {
        this.budget = budget;
    }

    public String[] getGenres() {
        return genres;
    }

    public void setGenres(String[] genres) {
        this.genres = genres;
    }
}

Remove the setValues ​​and putValues ​​method calls in onCreate. Create a new object of the Film class in it and add it to the database:
Film film = new Film("Shawshank redemption",28000000, new String[] {"Drama"});
try {
       DB snappyDB = DBFactory.open(this, "NewFilmDB");
       snappyDB.put("FilmObj",film);
} catch (SnappydbException e) {
       e.printStackTrace();
}


We rework the setValues ​​method:
private void setValues(DB snappyDB) throws SnappydbException {
        TextView filmNameTextView = findViewById(R.id.filmNameTextView),
                 filmBudgetTextView = findViewById(R.id.filmBudgetTextView),
                 genresTextView = findViewById(R.id.genreTextView);

        Film film = snappyDB.getObject("FilmObj", Film.class);
        
        String name = film.getName();
        int budget = film.getBudget();
        String[] genres = film.getGenres();

        filmNameTextView.setText(name);
        filmBudgetTextView.setText(String.valueOf(budget));

        for(int i = 0;i < genres.length;i++) {
            genresTextView.setText(genresTextView.getText() + " " + genres[i]);
        }
}

Call setValues ​​in onCreate again:
try {
        DB snappyDB = DBFactory.open(this, "NewFilmDB");
        setValues(snappyDB);
} catch (SnappydbException e) {
        e.printStackTrace();
}

We start and see that everything works:
image

Now let's look at some more interesting SnappyDB functions:
1) The ability to search for keys by prefix:
Film film1 = new Film("Green mile",60000000,new String[] {"Drama", "Fantasy"}),
  film2 = new Film("Gentlemen",22000000,new String[] {"Comedy", "Crime"}),
  film3 = new Film("In Bruges",15000000,new String[] {"Comedy", "Crime", "Thriller"});

        try {
            DB snappyDB = DBFactory.open(this, "NewFilmDB");
            snappyDB.put("FilmObj: Green Mile",film1);
            snappyDB.put("FilmObj: Gentlemen",film2);
            snappyDB.put("FilmObj: In Bruges",film3);
            String[] filmKeys = snappyDB.findKeys("FilmObj:");
            //   filmKeys    "FilmObj:"
            //      ,   
            for(int i = 0;i < filmKeys.length;i++) {
                Log.d("film_key: ",  filmKeys[i] + "\n");
            }

            Log.d("film_name: ",  snappyDB.getObject(filmKeys[2],Film.class).getName() + "\n");
            // -  ,    filmKeys

        } catch (SnappydbException e) {
            e.printStackTrace();
 }

image
Log output

2) DB iteration:
try {

            DB snappyDB = DBFactory.open(this, "NewFilmDB");

            for (String[] batch : snappyDB.allKeysIterator().byBatch(1)) {
                //    (batch),     
                for (String key : batch) {
                    Log.d("film",snappyDB.getObject(key,Film.class).getName());
                }
            }
        } catch (SnappydbException e) {
            e.printStackTrace();
}

Without using byBatch, you will only have a KeyIterator that does not implement Iterator or Iterable, so you cannot use it in a loop.
byBatch (n) creates a BatchIterable, which is Iterable and Iterator. Essentially, it just calls next (n) for KeyIterator when you call next () for it.
But byBatch should be used only with a large amount of data. Otherwise, it is worth using findKeys / findKeysBetween.

3) Removing an item by key:
snappyDB.del("FilmObj: Gentlemen");

image
Element deleted
4) Closing and deleting the database:
snappyDB.close();
snappyDB.destroy();


5) Find out if a key exists in the database:
snappyDB.put("FilmObj: Gentlemen",new Film());
boolean isExist = snappyDB.exists("FilmObj: Gentlemen");// true
snappyDB.del("FilmObj: Gentlemen");
isExist = snappyDB.exists("FilmObj: Gentlemen");//false



So that is all. You decide whether to use this database in your projects. It is fast enough and easy to use, but the choice is yours. This is not the only database for Android. Perhaps another database is better for your project (Room, SQLite, Realm, etc.). A lot of options.

PS: SnappyDB on GitHub (official documentation there).
Differences between SQL and NoSQL: 1 , 2
SQL vs Key-Value DB

All Articles