Consider this (partial) model:

[Index("Text", Unique = true)]
public class UniqueText: Entity
{
...
        [Field]
        public string Text { get; set; }
}

I have multiple threads and lists of strings that should be stored, if the particular string don't exist. Currently i have a ugly bad implementation for this that just checks for every string if it exists and add it if it don't.

The string sets -to check and to insert - are quite large (e.g. 8000 strings at once).

I would like to do something like this for performance reasons: Get all strings that don't exist in one batch - insert all entities with new strings in one batch.

I have multiple worker threads and sometimes they have the same string which should be inserted. To two inserts with the same string are executed. One of them fail with a unique constraint violation. How can this be avoided / handled best?

asked Jul 08 '11 at 05:36

Thomas%20Maierhofer's gravatar image

Thomas Maierhofer
738812


One Answer:

Hello Thomas,

I'd start with the following pattern:

// a set of strings to store
var strings;

using (var s = Domain.OpenSession()) {
  using (var t = s.OpenTransaction()) {

    // This query creates a temporary table
    // and fill it with strings to store
    // As a result, it returns strings that already exist
    var existing = s.Query.All<TextEntity>()
      .Where(i => i.Text.In(strings))
      .Select(i => i.Text);

    // Creating entities for strings that don't exist
    foreach (var text in strings.Except(existing))
      new TextEntity { Text = text };

    t.Complete();
  }
}

Hope that helps.

answered Jul 10 '11 at 05:08

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

edited Jul 10 '11 at 05:09

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×11
×2
×1

Asked: Jul 08 '11 at 05:36

Seen: 3,283 times

Last updated: Jul 10 '11 at 05:09

powered by OSQA