HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Prime Numbers Store

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbersprimestore

Problem

Let's say we need to create a store for selling prime numbers.

Users enter the store and ask to buy a number.

-
If the number asked is a prime number,

1.1. then it's either available for sale

1.2. or was purchased earlier, hence not available for sale.

-
If the number asked is not a prime number, then it doesn't exist.

Let us assume that the store contains the maximum possible number of primes that could be represented by a primitive type (in my implementation I assume the largest prime number isn't larger than Int32's MaxValue, and to actually run this in a reasonable time, I set a MAX_VALUE constant to 100000).



-
The store must handle concurrent calls.

-
Buying should be fast as possible!

-
When the store opens for business, it should already contain the numbers for sale.

-
Users cannot be blocked by shopping for numbers, nor by other buyers.

-
Example: UserA asks to purchase a number and then UserB arrives and asks to purchase a number, then UserB won't need to wait until the system's dealt with UserA's transaction. So when UserA's transaction finishes, a response will be delivered to UserA, then UserB's transaction will start and once finished a response will be delivered to UserB.


My Solution:

I define an enum called NumberType, that basically describes whats the buying state of each number.

According to the problem definition, every number could be either prime, then it may be available for sale or not available (because it was bought before), or the number isn't prime, so it doesn't exist.

NumberTypes.cs

namespace PrimeStore
{
    public enum NumberType
    {
        SoldSuccessfully,
        NotAvailable,
        NotExist
    }
}


Next, I define a Singleton class named Store, that is lazy-initiated on first-access to at most MAX_VALUE number of prime numbers in a Dictionary of {Int32, Boolean} pairs, initiating all the Boolean's to false since nothing was bought yet.

To calculate all t

Solution

foreach (var number in parallelQuery)
{
    _primes.Add(number, false);
}


You could simplify this to:

_primes = parallelQuery.ToDictionary(n => n, n => false);


public void BuyNumber(Int32 number, Action callback)


I don't understand why are you using a callback here. The whole method is completely synchronous, so I think you should simply return the result from it.

// no lock is needed here since just checking if a number is prime
// and its a readonly operation.
if (!_primes.ContainsKey(number))


Read-only operation doesn't mean that you don't need a lock. The documentation for Dictionary says:


A Dictionary can support multiple readers concurrently, as long as the collection is not modified.

The problem is, there could be a write occurring from another thread, so according to the documentation, you should use locking in this case (possibly using ReaderWriterLockSlim which allows multiple readers at the same time).

Code Snippets

foreach (var number in parallelQuery)
{
    _primes.Add(number, false);
}
_primes = parallelQuery.ToDictionary(n => n, n => false);
public void BuyNumber(Int32 number, Action<NumberType> callback)
// no lock is needed here since just checking if a number is prime
// and its a readonly operation.
if (!_primes.ContainsKey(number))

Context

StackExchange Code Review Q#26272, answer score: 5

Revisions (0)

No revisions yet.