克隆对象应该使用单个文件句柄还是每个文件句柄都有自己的?

发布于 2024-08-16 23:37:29 字数 651 浏览 2 评论 0原文

我正在开发一个包装器,用于解析现有网络工具的数据文件。然而,我使用的是大数据文件,所以我无法将整个文件加载到内存中,所以我必须遍历它以小块加载它。现有的 Web 工具期望数据格式类似于 SimpleXML($obj->parentnode->childnode->childnode 返回字符串或某种类型的节点对象)。值得庆幸的是,其结构与 XML 类似,但语法很奇怪。由于情有可原,我无法将其转换为正常的格式。所以我必须即时模拟它。

当我浏览文件时,我不需要解析整个树,只需解析当前节点的子节点名称。每个子节点名称和关联的偏移量将存储在父节点中。如果需要访问子节点的内容,则将克隆父节点对象,更新偏移值,并且子节点对象将开始解析其内容,直到找到所请求的子节点。

我的问题是:

  • 克隆父节点对象将为子克隆提供文件句柄。所有克隆是否都应该使用相同的句柄,并在需要时使用 fseek 在文件中跳转(这是一个相当大的 if)?
  • 我需要关闭该文件吗?或者脚本执行结束时的垃圾收集会关闭它吗?如果我不这样做,我会面临什么危险?
  • 我是否需要为每个克隆创建句柄,还是应该让它们共享一个句柄?如果有的话有上限吗?
  • 有没有办法让克隆对象保存对原始对象的引用?如果我将句柄关闭在对象析构函数中,如果该对象是克隆,我可能不应该关闭它。并且能够向上追踪可能会派上用场。

I am working on a wrapper that parses a data file for an existing web tool. However, I am using large data files so I cannot load the whole thing into memory so I have to walk through it loading it in small chunks. The existing web tool expects data in a style similar to SimpleXML ($obj->parentnode->childnode->childnode returns a string or a node object of some sort). Thankfully the structure is similar to XML but the syntax is odd. And I can't just translate it to a sane format because of extenuating circumstances. So I have to emulate it on the fly.

As I walk through the file I won't be needing to parse the whole tree, just the sub-node names of the current node. Each sub-node name and associated offset will be stored in the parent node. If contents of a sub-node need to be accessed then the parent-node object will be cloned, offset values will be updated and the sub-node object will begin parsing it's content until it finds the requested subnode.

The questions I have are:

  • Cloning the parent node object will give child clones the file handle. Should all the clones use the same handle and use fseek to jump around the file if needed (and that is a pretty big if)?
  • Do I need to close the file? Or will garbage collection at the end of script execution close it? What dangers do I face if I don't?
  • Will I need to create handles for each clone, or should I stick with them sharing one? If so is there an upper limit?
  • Is there a way for a cloned object to hold a reference to the original object? If I am putting the handle close in the object destructor I probably shouldn't close it if the object is a clone. And being able to trace upwards may come in handy, possibly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-08-23 23:37:29

如果实现 __clone 方法,则在克隆对象时可以执行任何操作,例如设置属性将其标记为克隆和/或设置保存父级的属性。

编辑:

public function __clone()
{
  $clone = clone $this;
  $clone->isCloned = true;
  $clone->parent = $this;
  $clone->resource = $this->resource; // i dont think resources are copied be default
  // additional property transference
  return $clone;
}

If you implement the __clone method you can do whatever you want when cloning an object like setting a property flaggin it as a clone and or setting a property that holds the parent.

edit:

public function __clone()
{
  $clone = clone $this;
  $clone->isCloned = true;
  $clone->parent = $this;
  $clone->resource = $this->resource; // i dont think resources are copied be default
  // additional property transference
  return $clone;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文