Skip to content

Commit

Permalink
Merge branch 'hilo-bug' of http://github.com/hdeshev/NoRM
Browse files Browse the repository at this point in the history
  • Loading branch information
schotime committed Jul 8, 2010
2 parents 948676b + ec5625c commit 9c13a59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 3 additions & 3 deletions NoRM/Collections/CollectionHiLoIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public long GenerateId(MongoDatabase db, string collectionName)
lock (generatorLock)
{
if (keyGeneratorsByTag.TryGetValue(collectionName, out value))
return value.GenerateId(collectionName);
return value.GenerateId(collectionName, db);

value = new HiLoIdGenerator(db, _capacity);
value = new HiLoIdGenerator(_capacity);
// doing it this way for thread safety
keyGeneratorsByTag = new Dictionary<string, HiLoIdGenerator>(keyGeneratorsByTag)
{
{collectionName, value}
};
}

return value.GenerateId(collectionName);
return value.GenerateId(collectionName, db);
}
}
}
14 changes: 6 additions & 8 deletions NoRM/Collections/HiLoIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public class HiLoIdGenerator
private readonly object generatorLock = new object();
private long _currentHi;
private long _currentLo;
private MongoDatabase _db;

public HiLoIdGenerator(MongoDatabase db, long capacity)
public HiLoIdGenerator(long capacity)
{
_db = db;
_currentHi = 0;
_capacity = capacity;
_currentLo = capacity + 1;
Expand All @@ -32,7 +30,7 @@ public HiLoIdGenerator(MongoDatabase db, long capacity)
/// </summary>
/// <param name="collectionName">Collection Name</param>
/// <returns></returns>
public long GenerateId(string collectionName)
public long GenerateId(string collectionName, MongoDatabase database)
{
long incrementedCurrentLow = Interlocked.Increment(ref _currentLo);
if (incrementedCurrentLow > _capacity)
Expand All @@ -41,7 +39,7 @@ public long GenerateId(string collectionName)
{
if (Thread.VolatileRead(ref _currentLo) > _capacity)
{
_currentHi = GetNextHi(collectionName);
_currentHi = GetNextHi(collectionName, database);
_currentLo = 1;
incrementedCurrentLow = 1;
}
Expand All @@ -50,7 +48,7 @@ public long GenerateId(string collectionName)
return (_currentHi - 1) * _capacity + (incrementedCurrentLow);
}

private long GetNextHi(string collectionName)
private long GetNextHi(string collectionName, MongoDatabase database)
{
while (true)
{
Expand All @@ -59,10 +57,10 @@ private long GetNextHi(string collectionName)
var update = new Expando();
update["$inc"] = new { ServerHi = 1 };

var hiLoKey = _db.GetCollection<NormHiLoKey>().FindAndModify(new { _id = collectionName }, update);
var hiLoKey = database.GetCollection<NormHiLoKey>().FindAndModify(new { _id = collectionName }, update);
if (hiLoKey == null)
{
_db.GetCollection<NormHiLoKey>().Insert(new NormHiLoKey { CollectionName = collectionName, ServerHi = 2 });
database.GetCollection<NormHiLoKey>().Insert(new NormHiLoKey { CollectionName = collectionName, ServerHi = 2 });
return 1;
}

Expand Down

0 comments on commit 9c13a59

Please sign in to comment.