2

I have a trie-based word detection algorithm for a custom dictionary. Note that regular expressions are too brittle with this dictionary as entries may contain spaces, periods, etc.

I've implemented the algorithm in a local C# app that reads in the dictionary from file and stores the trie in memory (it's compact, so no RAM size issues at all). Now I would like to use this algorithm in an MVC 3 app on a cloud host like AppHarbor, with the added twist that I want a web interface to enable adding/editing words.

It's fast enough that loading the dictionary from file and building the trie every time a user uploads their text would not be an issue (< 1s on my laptop). However, if I want to enable admins to edit the dictionary via the web interface, that would seem tricky since the dictionary would potentially be getting updated while a user is trying to upload text for analysis.

What is the best strategy for storing, loading, and updating the trie in an MVC 3 app?

4

4 回答 4

1

1 将 trie 存储在缓存中:它不是动态数据,缓存可以帮助我们完成其他任务(例如 admin 和 user 对 trie 的并发访问)

2 清除对缓存的访问:

public class TrieHelper
{
public Trie MyTrie
{
    get
    {
        if (HttpContext.Current.Cache["myTrieKey"] == null)
            HttpContext.Current.Cache["myTrieKey"] = LoadTrieFromFile(); //Returns Trie object
        return (Trie)HttpContext.Current.Cache["myTrieKey"];
    }
}

3 在添加操作时锁定 trie 对象

public void AddWordToTrie(string word)
{
    var trie = MyTrie;
    lock (HttpContext.Current.Cache["myTrieKey"])
    {
    trie.AddWord(word);
    } // notify that trie object locking when write data to file is not reuired
    WriteNewWordToTrieFile(word); // should lock FileWriter object
    }
}

4 如果编辑一次由 1 个管理员执行 - 将 trie 存储在 xml 文件中 - 很容易实现搜索元素的逻辑,在你的词应该添加什么词之后(你可以创建函数,它将使用内存中的 MyTrie 对象),然后使用 linq to xml 添加它。

于 2011-06-06T14:24:16.153 回答
1

我不确定您是否正在寻找具体的实现细节,或者更多关于如何处理的概念性想法,但我现在会提出一些想法。

实际 Trie 类- 这是用于设置 Trie 的类的一个很好的C# 示例。听起来你已经弄清楚了这部分。

存储:除非您已经在使用数据库并且需要将其保存在 dbms 中,否则我会将 trie 数据保存到 XML。XML 将很容易在 MVC 应用程序中使用,您无需担心数据库连接问题或数据库的额外成本。我还会在服务器上有两个版本的 trie 数据,一个生产副本和一个生产支持副本,您的管理员可以针对第二个版本执行交易。

加载在应用程序的管理模块中,您可以实现将 trie 数据加载到内存中的功能,数据加载的频率取决于您的应用程序需要。它可以被安排或作为手动功能使用。就像在 wordpress 网站中一样,如果用户在更新时应该访问它,他们会收到一条消息,指出该网站正在进行维护。您可以选择仅按需加载到内存中,并始终保持 trie 加载,除非出现问题。

更新——我有第二个数据库(或 XML 文件)用于应用更新。将更新应用于生产的方法将部分取决于更新的频率、数量和时间。一种安全的方法可能是存储管理员输入的交易。例如:

  • trie.put("约翰", 112);
  • trie.put("Doe", 222);
  • trie.Remove("约翰");

然后根据需要通过管理功能将这些事务应用于您的生产数据。如果需要,将您的网站置于“维护”模式。如果更新很少且速度很快,您可以对网站进行编码,以便在处理交易之前保持所有工作,用户可能需要等待几毫秒才能获得结果,但您不必担心变异数据问题。

这很模糊,但只是提出一些想法......如果您提供评论,我会尝试提供更多。

于 2011-06-04T18:49:00.490 回答
0

当您要在云环境中执行应用程序时,我建议您看一下 CQRS 和持久消息传递并提供一些并发模型(可能是乐观并发和智能冲突检测http://skillsmatter.com/podcast /design-architecture/cqrs-not-just-for-server-systems 5:00)

此外,显然,您需要更准确地分析您的业务需求,因为正如Udi Dahan 所提到的,竞争条件是缺乏业务分析的结果。

于 2011-06-09T21:01:53.483 回答
0

我有一个相同的,但大 10 倍 :)

客户端设计了自己的日历,其中包含问题和可能的答案,同时一些在线并被普通用户使用。

我想出的是testdeploy。管理员输入日历值并正确设置,在他可以使用Preview按钮查看它是否像他需要/想要的那样之后,为了使更改对所有最终用户都有效,他需要按下Deploy

作为管理员,他知道,在他按下 DEPLOY 按钮之前,所有访问日历的用户都将拥有旧值。很快,他点击 deploy all is set in the Database,并将他上传的文件推送到 Amazon S3(以便更快地访问)。

我用新日历更新缓存,新日历对象被缓存,直到应用程序池另有说明或他再次点击部署按钮。

你可以做这样的事情。

于 2011-06-04T17:42:32.953 回答