Coder Social home page Coder Social logo

snappydb's Introduction

SnappyDB

SnappyDB is a key-value database for Android it's an alternative for SQLite if you want to use a NoSQL approach.

It allows you to store and get primitive types, but also a Serializable object or array in a type-safe way.

SnappyDB can outperform SQLite in read/write operations. benchmark

SnappyDB is based on leveldb and use snappy compression algorithm, on redundant content you could achieve a good compression ratio

Usage

try {
   DB snappydb = DBFactory.open(context); //create or open an existing databse using the default name
   
   snappydb.put("name", "Jack Reacher"); 
   snappydb.putInt("age", 42);  
   snappydb.putBoolean("single", true);
   snappydb.put("books", new String[]{"One Shot", "Tripwire", "61 Hours"}); 
   
   String 	 name   =  snappydb.get("name");
   int 	 age    =  snappydb.getInt("age");
   boolean  single =  snappydb.getBoolean("single");
   String[] books  =  snappydb.getArray("books", String.class);// get array of string
   	
   snappydb.close();
   
   } catch (SnappydbException e) {
   }

For more recipes please take a look at the Cookbook.

With SnappyDB you could seamlessly store and retrieve your object/array, it use Kryo serialization which it faster than regular Java serialization.

Installation

SnappyDB use native code for performance, it's available for the three main architecture of Android: ARM, x86 and mips.

<dependency>
  <groupId>com.snappydb</groupId>
  <artifactId>snappydb-api</artifactId>
  <version>0.2.0</version>
</dependency>

<!-- ARM libraries  -->
<dependency>
  <groupId>com.snappydb</groupId>
  <artifactId>snappydb-native</artifactId>
  <version>0.2.0</version>
  <classifier>armeabi</classifier>
  <type>so</type>
</dependency>
<dependency>
  <groupId>com.snappydb</groupId>
  <artifactId>snappydb-native</artifactId>
  <version>0.2.0</version>
  <classifier>armeabi-v7a</classifier>
  <type>so</type>
</dependency>

<!-- x86 library  -->            
<dependency>
  <groupId>com.snappydb</groupId>
  <artifactId>snappydb-native</artifactId>
  <version>0.2.0</version>
  <classifier>x86</classifier>
  <type>so</type>
</dependency>

<!-- MIPS library  -->            
<dependency>
  <groupId>com.snappydb</groupId>
  <artifactId>snappydb-native</artifactId>
  <version>0.2.0</version>
  <classifier>mips</classifier>
  <type>so</type>
</dependency>

For non maven users, You need to download a zip containing the native libraries (.so) and the api.copy them under your libs directory

nomaven

Cookbook

Common tasks snippets

Create database

Create using the default name
     DB snappydb = DBFactory.open(context);
Create with a given name
     DB snappydb = DBFactory.open(context, "books");

SnappyDB use the internal storage to create your database. It creates a directory containing all the necessary files Ex: /data/data/com.snappydb/files/mydatabse

Open database

Open using the default name
     DB snappydb = DBFactory.open(context);
Open using a given name
     DB snappydb = DBFactory.open(context, "books");

Close database

     snappydb.close();

Destroy database

     snappydb.destroy();

Insert primitive types

     snappyDB.put("quote", "bazinga!");
     
     snappyDB.putShort("myshort", (short)32768);
     
     snappyDB.putInt("max_int", Integer.MAX_VALUE);
     
     snappyDB.putLong("max_long", Long.MAX_VALUE);
     
     snappyDB.putDouble("max_double", Double.MAX_VALUE);
     
     snappyDB.putFloat("myfloat", 10.30f);
     
     snappyDB.putBoolean("myboolean", true);

Read primitive types

     String quote      = snappyDB.get("quote");
     
     short myshort     = snappyDB.getShort("myshort");
     
     int maxInt        = snappyDB.getInt("max_int");
     
     long maxLong      = snappyDB.getLong("max_long");
     
     double maxDouble  = snappyDB.getDouble("max_double");
     
     float myFloat     = snappyDB.getFloat("myfloat");
     
     boolean myBoolean = snappyDB.getBoolean("myboolean");

Insert Serializable

     AtomicInteger objAtomicInt = new AtomicInteger (42);
     snappyDB.put("atomic integer", objAtomicInt);

Read Serializable

     AtomicInteger myObject = snappyDB.get("atomic integer", AtomicInteger.class);

Insert Array

     Number[] array = {new AtomicInteger (42), new BigDecimal("10E8"), Double.valueOf(Math.PI)};
     
     snappyDB.put("array", array);

Read Array

     Number [] numbers = snappyDB.getArray("array", Number.class);

Check Key

     boolean isKeyExists = snappyDB.exists("key");

Delete Key

     snappyDB.del("key");

License

SnappyDB is opensource, contribution and feedback are welcomed

Copyright 2013 Nabil HACHICHA.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Follow @nabil_hachicha

<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-46288191-1', 'github.com'); ga('send', 'pageview'); </script>

snappydb's People

Contributors

nhachicha avatar simpleton avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.