基本用法

数据结构

class DBImpl : public DB

{

private:
    struct CompactionState;
    struct Writer;// Information kept for every waiting writer
    Env* const env_; // 文件,目录,日志,Schedule线程
    const InternalKeyComparator internal_comparator_;
    const InternalFilterPolicy internal_filter_policy_; // 提高随机读的性能
    const Options options_;  // Options to control the behavior of a database (passed to DB::Open)
    bool owns_info_log_;
    bool owns_cache_;
    const std::string dbname_;
    // table_cache_ provides its own synchronization
    TableCache* table_cache_;
    // Lock over the persistent DB state.  Non-NULL iff successfully acquired.
    FileLock* db_lock_;
    // State below is protected by mutex_
    port::Mutex mutex_;
    port::AtomicPointer shutting_down_;
    port::CondVar bg_cv_;          // Signalled when background work finishes
    MemTable* mem_;
    MemTable* imm_;                // Memtable being compacted
    port::AtomicPointer has_imm_;  // So bg thread can detect non-NULL imm_
    WritableFile* logfile_;
    uint64_t logfile_number_;
    log::Writer* log_;
    uint32_t seed_;                // For sampling.
    // Queue of writers.
    std::deque<Writer*> writers_;
    WriteBatch* tmp_batch_;
    SnapshotList snapshots_;
    // Set of table files to protect from deletion because they are
    // part of ongoing compactions.
    std::set<uint64_t> pending_outputs_;
    // Has a background compaction been scheduled or is running?
    bool bg_compaction_scheduled_;
    ManualCompaction* manual_compaction_;
    VersionSet* versions_;
    // Have we encountered a background error in paranoid mode?
    Status bg_error_;

CompactionStats stats_[config::kNumLevels];

};

红色部分成员是需要重点关注的。

主要场景

public: 

Status DB::Open(const Options& options, const std::string& dbname, DB** dbptr) 

// Implementations of the DB interface

  virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
  virtual Status Delete(const WriteOptions&, const Slice& key);
  virtual Status Write(const WriteOptions& options, WriteBatch* updates);
  virtual Status Get(const ReadOptions& options,
                     const Slice& key,
                     std::string* value);
  virtual Iterator* NewIterator(const ReadOptions&);
  virtual const Snapshot* GetSnapshot();
  virtual void ReleaseSnapshot(const Snapshot* snapshot);
  virtual bool GetProperty(const Slice& property, std::string* value);
  virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
  virtual void CompactRange(const Slice* begin, const Slice* end);
  // Extra methods (for testing) that are not in the public DB interface
    // Record a sample of bytes read at the specified internal key.
  // Samples are taken approximately once every config::kReadBytesPeriod
  // bytes.
  void RecordReadSample(Slice key);

private:

Iterator* NewInternalIterator(const ReadOptions&,
                                SequenceNumber* latest_snapshot,
                                uint32_t* seed);
  Status NewDB();
  // Recover the descriptor from persistent storage.  May do a significant
  // amount of work to recover recently logged updates.  Any changes to
  // be made to the descriptor are added to *edit.
  Status Recover(VersionEdit* edit) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  void MaybeIgnoreError(Status* s) const;
  // Delete any unneeded files and stale in-memory entries.
  void DeleteObsoleteFiles();
  // Compact the in-memory write buffer to disk.  Switches to a new
  // log-file/memtable and writes a new descriptor iff successful.
  // Errors are recorded in bg_error_.
  void CompactMemTable() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  Status RecoverLogFile(uint64_t log_number,
                        VersionEdit* edit,
                        SequenceNumber* max_sequence)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  Status MakeRoomForWrite(bool force /* compact even if there is room? */)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  WriteBatch* BuildBatchGroup(Writer** last_writer);
  void RecordBackgroundError(const Status& s);
  void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  static void BGWork(void* db);
  void BackgroundCall();
  void  BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  void CleanupCompaction(CompactionState* compact)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  Status DoCompactionWork(CompactionState* compact)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  Status OpenCompactionOutputFile(CompactionState* compact);
  Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  Status InstallCompactionResults(CompactionState* compact)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);

综合介绍

LevelDB主要有以下几个部分的内存开销:memtable,immutable table,table cache,block cache

memtable和immutable table的大小由 options_.write_buffer_size决定。

table cache由max_open_files决定数量。

block cache最大可以传入 size_t的大小。

由于使用了mmap,因此需要尽量使内存大于数据规模。否则可能造成随机读爆慢。

LevelDB场景分析1--整体结构分析的更多相关文章

  1. LevelDB场景分析4--BackgroundCompaction

    1.DBImpl::Open      uint64_t new_log_number = impl->versions_->NewFileNumber();      WritableF ...

  2. LevelDB场景分析2--Open

    1.源码 1 Status DB::Open(const Options& options, const std::string& dbname,      uint64_t new_ ...

  3. EasyNVR智能云终端硬件使用场景分析:如何实现软硬一体的视频上云整体解决方案

    背景分析 在于众多的客户交流中,经常会被客户问到,"EasyNVR到底是软件还是硬件?"."EasyNVR能否出一个硬件的版本,摆脱自建服务器的压力?".&qu ...

  4. [阿里DIN] 深度兴趣网络源码分析 之 整体代码结构

    [阿里DIN] 深度兴趣网络源码分析 之 整体代码结构 目录 [阿里DIN] 深度兴趣网络源码分析 之 整体代码结构 0x00 摘要 0x01 文件简介 0x02 总体架构 0x03 总体代码 0x0 ...

  5. JVM之调优及常见场景分析

    JVM调优 GC调优是最后要做的工作,GC调优的目的可以总结为下面两点: 减少对象晋升到老年代的数量 减少FullGC的执行时间 通过监控排查问题及验证优化结果,可以分为: 命令监控:jps.jinf ...

  6. TYPESDK手游聚合SDK服务端设计思路与架构之一:应用场景分析

    TYPESDK 服务端设计思路与架构之一:应用场景分析 作为一个渠道SDK统一接入框架,TYPESDK从一开始,所面对的需求场景就是多款游戏,通过一个统一的SDK服务端,能够同时接入几十个甚至几百个各 ...

  7. Duilib源码分析(六)整体流程

    在<Duilib源码分析(一)整体框架>.<Duilib源码分析(二)控件构造器—CDialogBuilder>以及<Duilib源码分析(三)XML解析器—CMarku ...

  8. Oracle dbms_lock.sleep()存储过程使用技巧-场景-分析-实例

    <Oracle dbms_lock.sleep()存储过程使用技巧>-场景-分析-实例 摘要:今天是2014年3月10日,北京,雾霾,下午组织相关部门开会.会议的结尾一名开发工程师找到了我 ...

  9. 理解 python metaclass使用技巧与应用场景分析

    理解python metaclass使用技巧与应用场景分析       参考: decorator与metaclass:http://jfine-python-classes.readthedocs. ...

随机推荐

  1. atoi(),atof等函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

  2. 【Dagger2】简介 配置 使用 MVP案例

    简介 dagger2:https://github.com/google/dagger Maven Central  2.11版本jar包下载 dagger:https://github.com/sq ...

  3. Java 解析Excel文件为JSON

    Excel转Json的需求 反正我对SSM基本不会的情况下来到现在这家公司,都是90后,感觉很好.第二天就给我开发任务,就是把用户上传的Excel文件转成JSON返回给前台用于大屏的数据展示. 解决方 ...

  4. 13个可实现超棒数据可视化效果的Javascript框架

    随着商业及其相关需求的发展,数据成为越来越重要的元素之一,为了更加直观和明显的展示商业潜在的趋势和内在的特性,我们需要使用图表和图形的方式来直观动态的展示数据内在秘密,在今天的这篇文章中我们推荐12款 ...

  5. RS交叉表自动汇总后百分比列显示错误之解决方案

    可以说在从事Cognos开发的过程中,仅仅对数据展现而言,大多数用户使用最多的工具便是Report Studio了,此工具可以帮助我们快速的构建一些可供用户自主选择的数据报告.当然我个人认为没有什么开 ...

  6. (LeetCode 21)Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. [android错误] android-support-v*.jar包出现错误。

    可以去你安装的sdk目录中获得.\android_sdks\extras\android\support中获得各个jar包: android-support-v4.jar android-suppor ...

  8. 消息队列的使用场景(转载c)

    作者:ScienJus链接:https://www.zhihu.com/question/34243607/answer/58314162来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  9. 在线sass编译器

    工作中,我们可能遇到突发情况(无法安装考拉,gulp以及webpack以及其它的自动化工具),我们这时就要用即时编译工具了,那么它就是你的首选: http://tool.oschina.net/

  10. java线程(上)Thread和Runnable的区别

    首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈 ...