Level DB --- Iterator

embedded/2025/2/10 8:05:36/

Iterator是Level DB中的一个基类,它定义了迭代器的基础的操作,同时对内存资源进行了维护。

虚函数

Iterator类中的虚函数操作如下:

  virtual ~Iterator();// An iterator is either positioned at a key/value pair, or// not valid.  This method returns true iff the iterator is valid.virtual bool Valid() const = 0;// Position at the first key in the source.  The iterator is Valid()// after this call iff the source is not empty.virtual void SeekToFirst() = 0;// Position at the last key in the source.  The iterator is// Valid() after this call iff the source is not empty.virtual void SeekToLast() = 0;// Position at the first key in the source that is at or past target.// The iterator is Valid() after this call iff the source contains// an entry that comes at or past target.virtual void Seek(const Slice& target) = 0;// Moves to the next entry in the source.  After this call, Valid() is// true iff the iterator was not positioned at the last entry in the source.// REQUIRES: Valid()virtual void Next() = 0;// Moves to the previous entry in the source.  After this call, Valid() is// true iff the iterator was not positioned at the first entry in source.// REQUIRES: Valid()virtual void Prev() = 0;// Return the key for the current entry.  The underlying storage for// the returned slice is valid only until the next modification of// the iterator.// REQUIRES: Valid()virtual Slice key() const = 0;// Return the value for the current entry.  The underlying storage for// the returned slice is valid only until the next modification of// the iterator.// REQUIRES: Valid()virtual Slice value() const = 0;// If an error has occurred, return it.  Else return an ok status.virtual Status status() const = 0;

注释给了详细的虚函数操作介绍,继承类需要实现这些虚函数。

RegisterCleanup

include/leveldb/iterator.h中声明了:

  // Clients are allowed to register function/arg1/arg2 triples that// will be invoked when this iterator is destroyed.//// Note that unlike all of the preceding methods, this method is// not abstract and therefore clients should not override it.using CleanupFunction = void (*)(void* arg1, void* arg2);void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);

 /table/iterator.cc中实现了RegisterCleanup

void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {assert(func != nullptr);CleanupNode* node;if (cleanup_head_.IsEmpty()) {node = &cleanup_head_;} else {node = new CleanupNode();node->next = cleanup_head_.next;cleanup_head_.next = node;}node->function = func;node->arg1 = arg1;node->arg2 = arg2;
}

Iterator中不光实现了迭代器的功能,同时它维护了一个资源的list(即cleanup_head_),在迭代器析构的时候,资源会被释放,而释放的方式是注册的clean up函数。


http://www.ppmy.cn/embedded/161032.html

相关文章

高效 MyBatis SQL 写法一

高效 MyBatis SQL 写法一 前言 MyBatis 作为一款优秀的持久层框架,极大地简化了数据库操作。 然而,在实际开发中,XML 配置的编写仍然可能显得繁琐。 本文将分享一些 MyBatis 动态 SQL 的优质写法,帮助开发者提升效率并减少错误…

WebSocket推送数据快,条数多导致前端卡顿问题解决

WebSocket推送数据快,条数多导致前端卡顿问题解决 前言方案优化消息频率使用高效的数据格式Protobuf 和 MessagePack的对比 启用压缩前端性能优化 WebSocket使用注意事项连接管理处理断开连接负载均衡监控和维护日志记录定期维护安全最佳实践限流 最后 前言 在项目…

Deepseek使用途径以及Prompt 提示词编写原理

Deepseek使用途径以及Prompt 提示词编写原理 1.Deepseek使⽤途径 1.官⽹及APP ⽹址: deepseek.com 及移动应⽤(iOS/Android) 特征:完整版R1模型,⽀持深度搜索,但⽬前因流量⼤常遇到服务器繁忙问题。 2.…

3. Strategy(策略模式)C++

3. Strategy(策略模式)C 策略模式属于“组件协作”模式里的一种。 1. 动机 举个例子,我们假设一个场景,做一个税种的计算,一个跨国的税计算,除了涉及金额等,还需要考虑不同国家的纳税比例。 /…

RK3568使用C++和FFmpeg进行视频流,并使用自带GPU加速

在RK3568平台上使用C和FFmpeg进行视频流处理时,可以利用GPU加速解码。RK3568芯片集成了Mali-G52 GPU,支持硬件加速的视频解码。以下是一个基本的示例,展示如何使用FFmpeg和RK3568的GPU加速来拉取视频流。 1. 安装FFmpeg和RKMPP 首先&#x…

mac 安装 dotnet 环境

目录 一、安装准备 二、安装方法(两种任选) 方法 1:使用官方安装包(推荐新手) 方法 2:使用 Homebrew(适合开发者) 1. 安装 Homebrew(如未安装) 2. 通过 …

鸿蒙harmony 手势密码

1.效果图 2.设置手势页面代码 /*** 手势密码设置页面*/ Entry Component struct SettingGesturePage {/*** PatternLock组件控制器*/private patternLockController: PatternLockController new PatternLockController()/*** 用来保存提示文本信息*/State message: string …

使用deepseek快速创作ppt

目录 1.在DeekSeek生成PPT脚本2.打开Kimi3.最终效果 DeepSeek作为目前最强大模型,其推理能力炸裂,但是DeepSeek官方没有提供生成PPT功能,如果让DeepSeek做PPT呢? 有个途径:在DeepSeek让其深度思考做出PPT脚本&#xf…