Book Image

Getting Started with LevelDB

By : Andy Dent
Book Image

Getting Started with LevelDB

By: Andy Dent

Overview of this book

<p>Mobile and standalone desktop apps often need to store and rapidly retrieve data. LevelDB scales from a few simple values up to trillions of records and is flexible enough to store any kind of data.<br /><br />Getting Started with LevelDB progresses through the sophisticated ways to use databases. It starts by explaining how to install and build LevelDB from the basics, and moves on to explain the different Objective-C layers for LevelDB. It can be used as an introduction to database-backed iOS apps, as well as provides clarification to key-value stores and helps you understand how they relate to SQL.<br /><br />LevelDB is a flexible data solution that uses key-value stores to allow for the development of highly complex, efficient data storage systems. From the basics of data storage and retrieval to complex cases involving data storage for apps, this book covers a wide range of LevelDB topics<br /><br />Even relatively simple apps may have the need to search their data using different terms. Getting Started with LevelDB shows how to design keys to retrieve and store data efficiently. You will learn how its “levelled” nature delivers speedy writes and how to tune its settings and design for performance. You will also see a few techniques for debugging and tuning.</p> <p><br />Getting Started with LevelDB will leave you as an accomplished LevelDB programmer, confident you can build a complex database-backed app with high performance on iOS or OS/X.</p>
Table of Contents (18 chapters)
Getting Started with LevelDB
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Downloading LevelDB and Building with OS X
Index

Extending APLevelDB to expose the C++ API


If we want to add our own extensions to APLevelDB or the other wrappers, the internal leveldb::DB* has to be exposed. Just getting that pointer is enough to be able use all the C++ logic we saw in earlier chapters. This requires a minor change to APLevelDB.h.

Firstly declare a return type for getDB which can be safely included in pure Objective-C so we don't force people to move to Objective-C++ with .mm files:

#ifdef __cplusplus // forward declaration
  namespace leveldb { class DB; }
  typedef leveldb::DB* leveldbDBPtr;
#else
  typedef void* leveldbDBPtr;
#endif

Then inside @interface APLevelDB : NSObject add a public getter:

- (leveldbDBPtr) getDB;

This is simply implemented in ApLevelDB.mm:

- (leveldbDBPtr) getDB   {  return _db; }

Now we can add a class category to extend APLevelDB with other methods. Just one is shown here. We declare a method taking a prefix string and applying a block to the keys which match that prefix, passing a BOOL parameter...