要使用的数据结构,用于来自文件的大型数据集

时间:2014-10-24 13:21:40

标签: database data-structures storage disk

美好的一天,

我正在进行涉及从给定文件(file1)处理大型数据集的研究。根据我的理解,数据存储与数据结构处于不同的层次,但重叠,例如,数据结构是内存中数据的排列,以及磁盘存储。

我需要使用另一个文件(file2,已给出),然后将每个值与第一个给定文件(file1)中的值进行比较,看看它是否存在。

在文件之间进行比较时,我会将结果转储到另一个文件(file3)中。这个文件将被构造成不同的列(因为我提到了列,也许使用数据库代替结果而不是file3?)

根据我上面所说的,看起来我只是在寻找快速搜索/查找(比较文件)。

我的问题是,您认为这更适合数据结构,还是应该按原样搜索文件?我提到我只是在寻找搜索/查找操作,我倾向于哈希表,它在搜索时有恒定的时间O(1)。另一方面,我觉得数据结构对于处理已经使用不同存储机制(文件)构建的数据来说是一种过度杀伤,并且在我们进入数据时最好留下数据结构。

希望这是有道理的。我是否处于正确的思考轨道?

1 个答案:

答案 0 :(得分:1)

对于小文件,您可以将所有内容读入哈希表,并按密钥索引。

对于大文件,您可以扫描文件,并将密钥和文件偏移量存储到哈希表中。

Pseduo-C#:

var lookup = new Dictionary<string, long>(); // Hashtable

foreach (Record record1 in EnumerateRecords(file1))
{
    lookup[record1.Key] = file1.Position - sizeof(Record);
}

foreach (Record record2 in EnumerateRecords(file2))
{
    long file1Position;
    if (lookup.TryGetValue(record2.Key, out file1Position))
    {
        // Record exists in both files
        file1.Seek(file1Position);
        Record record1 = ReadRecord(file1);
        Process(record1, record2);

        // Remove what we found, so we know what is in file1
        // but not in file2
        lookup.Remove(record2.Key);
    }
    else
    {
        // Record exists only in file2
        Process(null, record2);
    }
}

foreach (var keyValuePair in lookup)
{
    // Record exists only in file1
    var file1Position = keyValuePair.Value;
    file1.Seek(file1Position ;
    var record1 = ReadRecord(file1);
    Process(record1, null);
}

如果需要,您可以扫描两个文件,并存储偏移量。然后比较匹配的两个哈希表。

相关问题