English 中文(简体)
是否有可能写出一个真正通用的软磁盘执行?
原标题:Is it possible to write a truly generic disk-baked B+Tree implementation?

I wrote a generic in-memory B+Tree implementation in C++ few times ago, and I m thinking about making it persistent on disk (which is why B+Tree have been designed for initially). My first thought was to use mmap (I m under Linux) to be able to manipulate the file as normal memory and just rewrite the new operator of my nodes classes so that it returns pointers in the mapped portion and create a smart pointer which can convert RAM adresses to file offset to link my nodes with others. But I want my implementation to be generic, so the user can store an int, an std::string, or whatever custom class he wants in the B+tree. That s where the problem occurs: for primitive types or aggregated types that do not contain pointers that s all good, but as soon as the object contains a pointer/reference to an heap allocated object, this approach no longer works.

因此,我的问题是:是否有办法克服这一困难? 我对这个专题的亲自搜查最终没有成功,但也许我错了。

最佳回答

我知道,有三种(有些)容易的解决办法。

Approach 1: 书写std:streambuf,标明一些预留记忆。

这种做法使你能够使用<代码>operator<<,并使用任何现行法典,以明确你想要的东西。

  • Pro: re-use loads of existing code.
  • Con: no control over how operator<< spits out content.
  • Con: text-based representations only.

<>Approach 2:写上你自己的(超载)产出功能。

  • Pro: can come up with binary representation.
  • Pro: exact control over every single output format.
  • Con: re-write so many output functions... writing overloads for new types by clients is a pain because they shouldn t write functions that fall in your library s namespace... unless you resort to Koenig (argument dependant) lookup!

Approach 3: 书写<>btree_traits<>模板。

  • Pro: can come up with binary representation.
  • Pro: exact control over every single output format.
  • Pro: more control on output and format that a function, may contain meta data and all.
  • Con: still requires you / your library s users to write lots of custom overloads.
  • Pro: have the btree_traits<> detault to use operator<< unless someone overrides the traits?
问题回答

我们不能写出一个真正通用和透明的版本,因为如果非属地项目中的点子被分配到小块(或新的和新的)中,那么它就已经消失了。

非透明溶解可能会使该类分类成为一种选择,而这样做相对容易。 在你储存这几类物品之前,你必须称号序列化功能,然后把这几类机器称作“帝国”。 Boost具有良好的序列特征,可以与你的B+Tree合作。

处理点和通用参考资料意味着你需要检查你试图储存的结构类型及其田地。 C++是一种不懂的语言。

但是,即便是用一种强有力的思考语言,解决这一问题的通用解决办法也是困难的。 您可能能够从事subset等高层次语言的种类的工作,如:Adhur、Rub等。 一个相关的、更强大的范式是persistent program Language

通常通过将撰写数据组的责任下放到目标类别本身来履行你想要的职能。 它称作serialization。 这只是意味着书写一个接口,其方法是丢弃数据,以及装上数据的方法。 任何想要留在你的博特里的阶层,都只是实施这一接口。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签