LevelDB: a fast and lightweight key-value Repository)

Source: Internet
Author: User
Author: JeffDean, SanjayGhemawat Translator: phylips @ bmy2011-8-16 Translation: open a database a LevelDB database has a file system directory name associated with it. All contents of the database are stored in this directory. The following example shows how to open a database or create

Author: JeffDean, SanjayGhemawat Translator: phylips @ bmy2011-8-16 Translation: open a database a LevelDB database has a file system directory name associated with it. All contents of the database are stored in this directory. The following example shows how to open a database or create

Author: Jeff Dean, Sanjay Ghemawat

Original article:

Translator: phylips @ bmy 2011-8-16

Translation:

Open a database

A LevelDB database has a file system directory name associated with it. All contents of the database are stored in this directory. The following example shows how to open a database or create a database when necessary:

# Include

# Include "leveldb/db. h"

Leveldb: DB * db;

Leveldb: Options options;

Options. create_if_missing = true;

Leveldb: Status status = leveldb: DB: Open (options, "/tmp/testdb", & db );

Assert (status. OK ());

...

If you want to make the above Code generate an error when the database already exists, you need to add the following line before calling Open:

Options. error_if_exists = true;

Status)

Leveldb: Status s = ...;

If (! S. OK () cerr <s. ToString () <endl;

Close Database

After processing a database, you can directly Delete the database object.

... Open the db as described above...

... Do something with db...

Delete db;

Read and Write operations

Std: string value;

Leveldb: Status s = db-> Get (leveldb: ReadOptions (), key1, & value );

If (s. OK () s = db-> Put (leveldb: WriteOptions (), key2, value );

If (s. OK () s = db-> Delete (leveldb: WriteOptions (), key1 );

Atomic update

# Include "leveldb/write_batch.h"

...

Std: string value;

Leveldb: Status s = db-> Get (leveldb: ReadOptions (), key1, & value );

If (s. OK ()){

Leveldb: WriteBatch batch;

Batch. Delete (key1 );

Batch. Put (key2, value );

S = db-> Write (leveldb: WriteOptions (), & batch );

}

WriteBatch

Synchronous write operations

Leveldb: WriteOptions write_options;

Write_options.sync = true;

Db-> Put (write_options ,...);

Concurrency

Iteration

The following example shows how to print all key value pairs in the database.

Leveldb: Iterator * it = db-> NewIterator (leveldb: ReadOptions ());

For (it-> SeekToFirst (); it-> Valid (); it-> Next ()){

Cout <it-> key (). ToString () <":" <it-> value (). ToString () <endl;

}

Assert (it-> status (). OK ());

// Check for any errors found during the scan

Delete it;

For (it-> Seek (start );

It-> Valid () & it-> key (). ToString () <limit;

It-> Next ()){

...

}

For (it-> SeekToLast (); it-> Valid (); it-> Prev ()){

...

}

Snapshots

Leveldb: ReadOptions options;

Options. snapshot = db-> GetSnapshot ();

... Apply some updates to db...

Leveldb: Iterator * iter = db-> NewIterator (options );

... Read using iter to view the state when the snapshot was created...

Delete iter;

Db-> ReleaseSnapshot (options. snapshot );

Leveldb: Snapshot * snapshot;

Leveldb: WriteOptions write_options;

Write_options.post_write_snapshot = & snapshot;

Leveldb: Status status = db-> Write (write_options ,...);

... Perform other mutations to db...

Leveldb: ReadOptions read_options;

Read_options.snapshot = snapshot;

Leveldb: Iterator * iter = db-> NewIterator (read_options );

... Read as of the state just after the Write call returned...

Delete iter;

Db-> ReleaseSnapshot (snapshot );

Slice

Leveldb: Slice s1 = "hello ";

Std: string str ("world ");

Leveldb: Slice s2 = str;

Std: string str = s1.ToString ();

Assert (str = std: string ("hello "));

. For example, the following code is problematic:

Leveldb: Slice slice;

If (...){

Std: string str = ...;

Slice = str;

}

Use (slice );

Comparator

Class TwoPartComparator: public leveldb: Comparator {

Public:

// Three-way comparison function:

// If a <B: negative result

// If a> B: positive result

// Else: zero result

Int Compare (const leveldb: Slice & a, const leveldb: Slice & B) const {

Int a1, a2, b1, b2;

ParseKey (a, & a1, & a2 );

ParseKey (B, & b1, & b2 );

If (a1 <b1) return-1;

If (a1> b1) return + 1;

If (a2 <b2) return-1;

If (a2> b2) return + 1;

Return 0;

}

// Ignore the following methods for now:

Const char * Name () const {return "TwoPartComparator ";}

Void FindShortestSeparator (std: string *, const leveldb: Slice &) const {}

Void find1_successor (std: string *) const {}

};

Create a database using a custom comparator:

TwoPartComparator cmp;

Leveldb: DB * db;

Leveldb: Options options;

Options. create_if_missing = true;

Options. comparator = & cmp;

Leveldb: Status status = leveldb: DB: Open (options, "/tmp/testdb", & db );

Backwards compatibility)

Performance

You can adjust and optimize the performance by changing the default values in include/leveldb/options. h.

Block Size

Compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.