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

Using Get and Put with binary values


Now that you understand how Slice objects are used, let's go back to the simple Put and Get operations. Binary values can be stored just the same as string values, remembering a std::string value can be regarded, such as Slice as containing just data bytes and a length. The difference is that a std::string retains ownership of its bytes and so is a safe container for binary data. In the following testBin() sample we create a Slice object with a pointer and length of an arbitrary binary struct, and do the opposite to get the binary struct back out of the std::string value returned from Get:

  struct binValues {
    int intVal;
    double realVal;
  };    
  binValues b = {-99, 3.14};
  Slice binSlice((const char*)&b, sizeof(binValues) );
  assert( db->Put(WriteOptions(), "BinSample", binSlice).ok() );
  std::string binRead;
  assert( db->Get(ReadOptions(), "BinSample", &binRead).ok() );
// treat the std::string as a container for arbitary...