leveldb阅读-Status

2024-02-24 06:58
文章标签 阅读 status leveldb

本文主要是介绍leveldb阅读-Status,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LevelDB中,使用Status用来统一处理返回状态,其设计业是遵循了之前的一贯设计风格,简单明了。为了节省空间,Status采用了const char* state_;来存储数据,利用 state_[0..3] == length of message来表示状态的长度,state_[4]    == code表示状态的类型,state_[5..]  == message表示详细信息;

Status的定义文件如下:

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// A Status encapsulates the result of an operation.  It may indicate success,
// or it may indicate an error with an associated error message.
//
// Multiple threads can invoke const methods on a Status without
// external synchronization, but if any of the threads may call a
// non-const method, all threads accessing the same Status must use
// external synchronization.#ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
#define STORAGE_LEVELDB_INCLUDE_STATUS_H_#include <string>
#include "leveldb/slice.h"namespace leveldb {class Status {public:// Create a success status.Status() : state_(NULL) { }//析构的时候,销毁消息~Status() { delete[] state_; }// Copy the specified status.Status(const Status& s);void operator=(const Status& s);// Return a success status.static Status OK() { return Status(); }// Return error status of an appropriate type.static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {return Status(kNotFound, msg, msg2);}static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {return Status(kCorruption, msg, msg2);}static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {return Status(kNotSupported, msg, msg2);}static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {return Status(kInvalidArgument, msg, msg2);}static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {return Status(kIOError, msg, msg2);}// Returns true iff the status indicates success.bool ok() const { return (state_ == NULL); }// Returns true iff the status indicates a NotFound error.bool IsNotFound() const { return code() == kNotFound; }// Returns true iff the status indicates a Corruption error.bool IsCorruption() const { return code() == kCorruption; }// Returns true iff the status indicates an IOError.bool IsIOError() const { return code() == kIOError; }// Return a string representation of this status suitable for printing.// Returns the string "OK" for success.std::string ToString() const;private://state_数组表示状态,第一部分表示消息的长度,第二部分表示消息的类型//第三部分就是消息的具体信息了;// OK status has a NULL state_.  Otherwise, state_ is a new[] array// of the following form://    state_[0..3] == length of message//    state_[4]    == code//    state_[5..]  == messageconst char* state_;enum Code {kOk = 0,kNotFound = 1,kCorruption = 2,kNotSupported = 3,kInvalidArgument = 4,kIOError = 5};Code code() const {return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);}//构造消息Status(Code code, const Slice& msg, const Slice& msg2);static const char* CopyState(const char* s);
};inline Status::Status(const Status& s) {state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
}
//进行深层次复制消息
inline void Status::operator=(const Status& s) {// The following condition catches both aliasing (when this == &s),// and the common case where both s and *this are ok.if (state_ != s.state_) {delete[] state_;state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);}
}}  // namespace leveldb#endif  // STORAGE_LEVELDB_INCLUDE_STATUS_H_


Status 具体的部分实现源码,通过提供简单的一些接口,能够快速处理消息的管理~


// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.#include <stdio.h>
#include "port/port.h"
#include "leveldb/status.h"namespace leveldb {//复制消息,消息的内容只读;
const char* Status::CopyState(const char* state) {uint32_t size;memcpy(&size, state, sizeof(size));//前面四个字节表示消息的大小char* result = new char[size + 5];//消息的大小还需要加上5个字节,才是完整的消息部分memcpy(result, state, size + 5);return result;
}//构造消息,格式为msg: msg2
Status::Status(Code code, const Slice& msg, const Slice& msg2) {assert(code != kOk);const uint32_t len1 = msg.size();const uint32_t len2 = msg2.size();const uint32_t size = len1 + (len2 ? (2 + len2) : 0);char* result = new char[size + 5];memcpy(result, &size, sizeof(size));result[4] = static_cast<char>(code);memcpy(result + 5, msg.data(), len1);if (len2) {result[5 + len1] = ':';result[6 + len1] = ' ';memcpy(result + 7 + len1, msg2.data(), len2);}state_ = result;
}//消息转换成string~
std::string Status::ToString() const {if (state_ == NULL) {return "OK";} else {char tmp[30];const char* type;switch (code()) {case kOk:type = "OK";break;case kNotFound:type = "NotFound: ";break;case kCorruption:type = "Corruption: ";break;case kNotSupported:type = "Not implemented: ";break;case kInvalidArgument:type = "Invalid argument: ";break;case kIOError:type = "IO error: ";break;default:snprintf(tmp, sizeof(tmp), "Unknown code(%d): ",static_cast<int>(code()));type = tmp;break;}std::string result(type);uint32_t length;memcpy(&length, state_, sizeof(length));result.append(state_ + 5, length);return result;}
}}  // namespace leveldb



这篇关于leveldb阅读-Status的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/741296

相关文章

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

论文阅读笔记: Segment Anything

文章目录 Segment Anything摘要引言任务模型数据引擎数据集负责任的人工智能 Segment Anything Model图像编码器提示编码器mask解码器解决歧义损失和训练 Segment Anything 论文地址: https://arxiv.org/abs/2304.02643 代码地址:https://github.com/facebookresear

软件架构模式:5 分钟阅读

原文: https://orkhanscience.medium.com/software-architecture-patterns-5-mins-read-e9e3c8eb47d2 软件架构模式:5 分钟阅读 当有人潜入软件工程世界时,有一天他需要学习软件架构模式的基础知识。当我刚接触编码时,我不知道从哪里获得简要介绍现有架构模式的资源,这样它就不会太详细和混乱,而是非常抽象和易

【阅读文献】一个使用大语言模型的端到端语音概要

摘要 ssum框架(Speech Summarization)为了 从说话人的语音提出对应的文本二题出。 ssum面临的挑战: 控制长语音的输入捕捉 the intricate cross-mdoel mapping 在长语音输入和短文本之间。 ssum端到端模型框架 使用 Q-Former 作为 语音和文本的中介连接 ,并且使用LLMs去从语音特征正确地产生文本。 采取 multi-st

你读文献的方式可能错了!掌握这些技巧,让阅读事半功倍!

我是娜姐 @迪娜学姐 ,一个SCI医学期刊编辑,探索用AI工具提效论文写作和发表。 科研新手如何精读一篇论文? 很多科研新手,一上来就疯狂下载几十上百篇文献。囫囵吞枣看完了,还是什么都不知道,大脑一片空白。究竟该如何读文献收获最大? 大佬说,要积极阅读、频繁阅读。 什么是积极阅读? 相比被动阅读,积极阅读是指在阅读之前准备好问题、设置阅读目标、保持批判性,收获更多、进步更大的一种阅读

一键部署Phi 3.5 mini+vision!多模态阅读基准数据集MRR-Benchmark上线,含550个问答对

小模型又又又卷起来了!微软开源三连发!一口气发布了 Phi 3.5 针对不同任务的 3 个模型,并在多个基准上超越了其他同类模型。 其中 Phi-3.5-mini-instruct 专为内存或算力受限的设备推出,小参数也能展现出强大的推理能力,代码生成、多语言理解等任务信手拈来。而 Phi-3.5-vision-instruct 则是多模态领域的翘楚,能同时处理文本和视觉信息,图像理解、视频摘要

深入理解计算机系统阅读笔记-第四章

第四章 处理器体系结构 一个处理器支持的指令和指令的字节级编码称为它的ISA(instruction-set architecture,指令集体系结构)。不同家族处理器有不同的ISA。ISA在编译器编写者和处理器设计人员之间提供了一个概念抽象层,编译器编写者只需要知道允许哪些指令,以及他们是如何编码的;而处理器设计者,必须建造出执行这些指令的处理器。 ISA模型看上去是顺序执行的,实际上同时处

Kafka源码阅读最最最简单的入门方法

大数据技术与架构 点击右侧关注,大数据开发领域最强公众号! 暴走大数据 点击右侧关注,暴走大数据! 1 消息处理入口 以下是Kafka消息处理的入口,即客户端发送到服务端消息处理方法。 /** * Top-level method that handles all requests and multiplexes to the right api */ def handle(r

Spark源码阅读的正确打开方式

Spark发展至今,应该说已经非常成熟了。是大数据计算领域不得不学习的框架。尤其是Spark在稳定性和社区发展的成熟度方面,吊打其他的大数据处理框架。 Spark至今只经历过1.x、2.x和3.x三个大版本的变化,在核心实现上,我们在Github能看到的最早的实现是0.5版本,这个版本只有1万多行代码,就把Spark的核心功能实现了。 当然我们不可能从这么古老的版本看,假如你接触过Spar

个性化阅读体验:Spring Boot框架的图书推荐解决方案

第5章 系统详细设计 5.1前台首页功能模块 图书个性化推荐系统,在前台首页可以查看首页、图书信息、好书推荐、留言反馈、个人中心、后台管理等内容,如图5-1所示。 图5-1首页功能界面图 学生注册、登录,在学生注册页面可以填写学号、密码、学生姓名、性别、出生日期、联系电话、班级等信息进行注册、登录,如图5-2所示。 图5-2学生注册、登录界面图 图书信息,在图书信息页面通过查看图书