LEVELDB and Design Patterns

Source: Internet
Author: User

Iterator mode:

Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. Leveldb Include/leveldb.h defines the iterator base class, which accesses a layer of SST, a SST internal kv, a memtable internal kv, and the entire DB internal KV requires an iterator. is to implement its own version of the iterator by inheriting iterator. This separates the functions that traverse data in a container from other functions, adheres to a single functional principle, and when you add a new aggregation class, you simply inherit the iterator base class to implement the new iterator version, following the open and closed principle. In addition, the Skiplist iterator uses an internal class implementation, which improves encapsulation and facilitates full access to its external classes.


Responsibility Chain Mode:

is the behavior pattern of the object. Enables multiple objects to have the opportunity to process requests, link them together, and pass the request along the chain. Compaction is the core design of LEVELDB, the more complicated part. Therefore, the better logic is to separate the complex functions into several functions in turn (the objects here are replaced with functions, involving Maybeschedulecompaction, Backgroundcall, Backgroundcompaction, Docompactionwork, Installcompactionresults, etc.), maybeschedulecompaction is responsible for determining whether the compaction, DB is being deleted, whether there is an error , whether it is necessary for compaction,backgroundcall to determine if there are no other threads executing in the background and no background errors. Backgroundcompaction is responsible for determining whether to merge manually and prepare for compaction (such as files that need to be compaction), Docompactionwork is really compaction work, Installcompactionresults is responsible for constructing a new edit and calling the Logandapply function to generate a new LEVELDB version.


Proxy mode:

A proxy object can be accessed indirectly when there is difficulty in accessing an object directly or accessing an object. The design of Lruhandle, Blockhandle and Envwrapper in Leveldb embodies the agent mode. Lruhandle can be understood as LRUCache uses the reference count of the proxy class, similar to the smart pointer, first reducing the huge cost of LRUCache replication, Lruhandle also facilitates the lookup of LRUCache, Insert, Lru_append , Lru_remove and other operations. Blockhandle is the block's proxy class, which indicates the block's offset and size to facilitate reading of the block. About Env in ENVWRAPPER,LEVELDB mainly encapsulates the operating system's file interface, background thread scheduling and lock implementation, ENV is an abstract base class, posixenv inherit the implementation of Env, and Envwrapper is the Env subclass Posixenv proxy class.


Builder mode:

is a more complex creation pattern that separates the client from the creation of complex objects that contain multiple components (or parts), and the client does not need to know the internal components and assembly methods of complex objects, only the type of builder required. The specific applications in LEVELDB are Tablebuilder, Blockbuilder, Filterblockbuilder, Versionset::builder and so on. Tablebuilder is responsible for constructing table, while Tablebuilder members have Blockbuilder, Filterblockbuilder, the former is responsible for constructing index block and data block, the latter is responsible for constructing meta Block Versionset::builder is responsible for the build and update of the version. The builder model eliminates the need for the product's users to know the details of the product's internals and does not necessarily involve the product's build process, but also breaks down the process of creating complex products in different ways, making the creation process clearer and easier to use to control the creation process.


This article is from the "Haopeng Li blog" blog, please be sure to keep this source http://lh2debug.blog.51cto.com/12490788/1890163

LEVELDB and Design Patterns

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.