b-tree - 模块`github.com/google/btree`中的`copyOnWriteContext`是什么意思?
问题描述
最近看了google/btree的源码。但我对 struct 感到困惑copyOnWriteContext
。它用于node
函数mutableFor
中,如下所示
func (n *node) mutableFor(cow *copyOnWriteContext) *node {
if n.cow == cow {
return n
}
fmt.Println("new node?")
out := cow.newNode()
if cap(out.items) >= len(n.items) {
out.items = out.items[:len(n.items)]
} else {
out.items = make(items, len(n.items), cap(n.items))
}
copy(out.items, n.items)
// Copy children
if cap(out.children) >= len(n.children) {
out.children = out.children[:len(n.children)]
} else {
out.children = make(children, len(n.children), cap(n.children))
}
copy(out.children, n.children)
return out
}
我查看了这个模块中的所有代码,发现只有一个地方创建了copyOnWriteContext的实例。这是创建树的时间。
func New(degree int) *BTree {
return NewWithFreeList(degree, NewFreeList(DefaultFreeListSize))
}
那么对于 是什么意思mutableFor
。copyOnWriteContext
因为整个代码中只有一个。n.cow
总是等于cow
参数。
解决方案
n.cow 可以为零。
// freeNode frees a node within a given COW context, if it's owned by that
// context. It returns what happened to the node (see freeType const
// documentation).
func (c *copyOnWriteContext) freeNode(n *node) freeType {
if n.cow == c {
// clear to allow GC
n.items.truncate(0)
n.children.truncate(0)
n.cow = nil
if c.freelist.freeNode(n) {
return ftStored
} else {
return ftFreelistFull
}
} else {
return ftNotOwned
}
}
推荐阅读
- javascript - 未捕获的 TypeError:this.props.fetchResults 不是函数
- apache - Apache SSL vhost 不会使用 DocumentRoot,而是使用服务器 webroot
- apache-flink - 我从一个 1.8GB 的保存点恢复我的工作,然后立即创建一个新的保存点,这个保存点的大小约为 50MB。我的国家去哪儿了?
- shell - 使用 awk 和 bash 用行号拆分逗号分隔列表
- amazon-web-services - 如何在 S3 中加密大量现有文件
- mysql - 通过数据库查询(EAV)获取产品报告
- algorithm - 为什么 BST 节点的后继节点定义为比删除节点大的节点?
- javascript - 文本不显示在 chrome 中,但在 IE 中工作
- python - Django ModelForm 初始或自定义字段值
- c# - 在 client.DownloadFile() 上拒绝访问