首页 文章 精选 留言 我的

精选列表

搜索[链源罗盘],共10000篇文章
优秀的个人博客,低调大师

区块链教程Fabric1.0源代码分析Ledger statedb(状态数据库)-兄弟连区块链

Fabric 1.0源代码笔记 之 Ledger #statedb(状态数据库) 1、statedb概述 statedb,或VersionedDB,即状态数据库,存储了交易(transaction)日志中所有键的最新值,也称世界状态(world state)。可选择基于leveldb或cauchdb实现。 statedb,代码分布在core/ledger/kvledger/txmgmt/statedb目录下,目录结构如下: statedb.go,定义了核心接口VersionedDBProvider、VersionedDB、ResultsIterator和QueryResult,以及UpdateBatch和nsIterator结构体及方法。 util.go,包括工具函数EncodeValue和DecodeValue的实现。 stateleveldb目录,VersionedDBProvider和VersionedDB接口的leveldb版本实现,即stateleveldb.VersionedDBProvider和stateleveldb.versionedDB结构体及方法。 statecouchdb目录,VersionedDBProvider和VersionedDB接口的couchdb版本实现,即statecouchdb.VersionedDBProvider和statecouchdb.VersionedDB结构体及方法。 2、核心接口定义 VersionedDBProvider接口定义: type VersionedDBProvider interface { GetDBHandle(id string) (VersionedDB, error) //获取VersionedDB句柄 Close() //关闭所有 VersionedDB 实例 } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go VersionedDB接口定义: type VersionedDB interface { //获取给定命名空间和键的值 GetState(namespace string, key string) (*VersionedValue, error) //在单个调用中获取多个键的值 GetStateMultipleKeys(namespace string, keys []string) ([]*VersionedValue, error) //返回一个迭代器, 其中包含给定键范围之间的所有键值(包括startKey,不包括endKey) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (ResultsIterator, error) //执行给定的查询并返回迭代器 ExecuteQuery(namespace, query string) (ResultsIterator, error) //批处理应用 ApplyUpdates(batch *UpdateBatch, height *version.Height) error //返回statedb一致的最高事务的高度 GetLatestSavePoint() (*version.Height, error) //测试数据库是否支持这个key(leveldb支持任何字节, 而couchdb只支持utf-8字符串) ValidateKey(key string) error //打开db Open() error //关闭db Close() } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go ResultsIterator和QueryResult接口定义: type ResultsIterator interface { Next() (QueryResult, error) Close() } type QueryResult interface{} //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go 补充CompositeKey、VersionedValue和VersionedKV结构体: type CompositeKey struct { Namespace string //命名空间 Key string //Key } type VersionedValue struct { Value []byte //Value Version *version.Height //版本 } type VersionedKV struct { CompositeKey //嵌入CompositeKey VersionedValue //嵌入VersionedValue } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go nsUpdates结构体及方法: type nsUpdates struct { m map[string]*VersionedValue //string为Key } func newNsUpdates() *nsUpdates//构造nsUpdates //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go UpdateBatch结构体及方法: type UpdateBatch struct { updates map[string]*nsUpdates //string为Namespace } //构造UpdateBatch func NewUpdateBatch() *UpdateBatch //按namespace和key获取Value func (batch *UpdateBatch) Get(ns string, key string) *VersionedValue //按namespace和key添加Value func (batch *UpdateBatch) Put(ns string, key string, value []byte, version *version.Height) //按namespace和key删除Value,即置为nil func (batch *UpdateBatch) Delete(ns string, key string, version *version.Height) //按namespace和key查找是否存在 func (batch *UpdateBatch) Exists(ns string, key string) bool //获取更新的namespace列表 func (batch *UpdateBatch) GetUpdatedNamespaces() []string //按namespace获取nsUpdates func (batch *UpdateBatch) GetUpdates(ns string) map[string]*VersionedValue //构造nsIterator func (batch *UpdateBatch) GetRangeScanIterator(ns string, startKey string, endKey string) ResultsIterator //按namespace获取或创建nsUpdates func (batch *UpdateBatch) getOrCreateNsUpdates(ns string) *nsUpdates //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go nsIterator结构体及方法: type nsIterator struct { ns string //namespace nsUpdates *nsUpdates //batch.updates[ns] sortedKeys []string //nsUpdates.m中key排序 nextIndex int //startKey lastIndex int //endKey } //构造nsIterator func newNsIterator(ns string, startKey string, endKey string, batch *UpdateBatch) *nsIterator func (itr *nsIterator) Next() (QueryResult, error) //按itr.nextIndex获取VersionedKV func (itr *nsIterator) Close() // do nothing //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go 3、statedb基于leveldb实现 3.1、VersionedDB接口实现 VersionedDB接口实现,即versionedDB结构体,定义如下: type versionedDB struct { db *leveldbhelper.DBHandle //leveldb dbName string //dbName } //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 涉及方法如下: //构造versionedDB func newVersionedDB(db *leveldbhelper.DBHandle, dbName string) *versionedDB func (vdb *versionedDB) Open() error // do nothing func (vdb *versionedDB) Close() // do nothing func (vdb *versionedDB) ValidateKey(key string) error // do nothing //按namespace和key获取Value func (vdb *versionedDB) GetState(namespace string, key string) (*statedb.VersionedValue, error) //在单个调用中获取多个键的值 func (vdb *versionedDB) GetStateMultipleKeys(namespace string, keys []string) ([]*statedb.VersionedValue, error) //返回一个迭代器, 其中包含给定键范围之间的所有键值(包括startKey,不包括endKey) func (vdb *versionedDB) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (statedb.ResultsIterator, error) //leveldb不支持ExecuteQuery方法 func (vdb *versionedDB) ExecuteQuery(namespace, query string) (statedb.ResultsIterator, error) //批处理应用 func (vdb *versionedDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version.Height) error //返回statedb一致的最高事务的高度 func (vdb *versionedDB) GetLatestSavePoint() (*version.Height, error) //拼接ns和key,ns []byte{0x00} key func constructCompositeKey(ns string, key string) []byte //分割ns和key,分割符[]byte{0x00} func splitCompositeKey(compositeKey []byte) (string, string) //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go func (vdb versionedDB) ApplyUpdates(batch statedb.UpdateBatch, height *version.Height) error代码如下: dbBatch := leveldbhelper.NewUpdateBatch() namespaces := batch.GetUpdatedNamespaces() //获取更新的namespace列表 for _, ns := range namespaces { updates := batch.GetUpdates(ns) //按namespace获取nsUpdates for k, vv := range updates { compositeKey := constructCompositeKey(ns, k) //拼接ns和key if vv.Value == nil { dbBatch.Delete(compositeKey) } else { dbBatch.Put(compositeKey, statedb.EncodeValue(vv.Value, vv.Version)) } } } //statedb一致的最高事务的高度 dbBatch.Put(savePointKey, height.ToBytes()) //var savePointKey = []byte{0x00} err := vdb.db.WriteBatch(dbBatch, true) //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 3.2、ResultsIterator接口实现 ResultsIterator接口实现,即kvScanner结构体及方法。 type kvScanner struct { namespace string dbItr iterator.Iterator } //构造kvScanner func newKVScanner(namespace string, dbItr iterator.Iterator) *kvScanner //迭代获取statedb.VersionedKV func (scanner *kvScanner) Next() (statedb.QueryResult, error) func (scanner *kvScanner) Close() //释放迭代器 //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 3.3、VersionedDBProvider接口实现 VersionedDBProvider接口实现,即VersionedDBProvider结构体及方法。 type VersionedDBProvider struct { dbProvider *leveldbhelper.Provider } func NewVersionedDBProvider() *VersionedDBProvider //构造VersionedDBProvider //获取statedb.VersionedDB func (provider *VersionedDBProvider) GetDBHandle(dbName string) (statedb.VersionedDB, error) func (provider *VersionedDBProvider) Close() //关闭statedb.VersionedDB //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 4、statedb基于cauchdb实现 暂略,待补充。

优秀的个人博客,低调大师

A股数据源和 API 怎么选?(2026 全景选型指南 + 决策树)

一句话回答: 先按需求定位,不要先问"哪个最好"。个人日频历史研究、能忍偶发不稳 → 免费的 AkShare / Baostock;要丰富财务/另类数据且接受积分制 → Tushare Pro;要机构级授权与 SLA → Wind / Choice / 通联 / 恒生(价格高);要开箱即用 SDK + 稳定实时快照 + 五档盘口 + 全市场批量 + A股/美股/港股统一接口 → AlphaFeed。AlphaFeed 专注行情类数据,不含财务报表、龙虎榜、北向、公告研报情绪、指数成分等,这类请配合上面的专门源。

优秀的个人博客,低调大师

Ozon货源补齐技术思路:通过图像检索对接1688商品数据源

做 Ozon 跨境铺货、ERP 系统开发时,经常会遇到一个业务痛点:Ozon 侧商品只有基础标题、图片,缺少供货源头的供应商信息、采购价、SKU 规格、起订量等货源维度数据。 如果人工逐个去 1688 检索匹配,效率低下,大批量处理几乎不可行。这里记录一套技术实现思路:利用图像检索接口,以商品图片作为媒介,反向匹配 1688 货源,完成货源信息补齐,分享流程、难点以及开发层面需要注意的问题。

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册