Permalink
Browse files

RavenDB-618 Includes for Load<T> not working in sharded environment

  • Loading branch information...
1 parent ca10f46 commit 4cc039ec8432d17a5fa3fcdf7d613424845cd93e @ayende committed Oct 15, 2012
Showing with 55 additions and 50 deletions.
  1. +55 −50 Raven.Client.Lightweight/Shard/ShardedDocumentSession.cs
@@ -32,7 +32,7 @@ namespace Raven.Client.Shard
/// Implements Unit of Work for accessing a set of sharded RavenDB servers
/// </summary>
public class ShardedDocumentSession : BaseShardedDocumentSession<IDatabaseCommands>, IDocumentQueryGenerator,
- IDocumentSessionImpl, ISyncAdvancedSessionOperation
+ IDocumentSessionImpl, ISyncAdvancedSessionOperation
{
/// <summary>
/// Initializes a new instance of the <see cref="ShardedDocumentSession"/> class.
@@ -43,7 +43,7 @@ public class ShardedDocumentSession : BaseShardedDocumentSession<IDatabaseComman
/// <param name="documentStore"></param>
/// <param name="listeners"></param>
public ShardedDocumentSession(ShardedDocumentStore documentStore, DocumentSessionListeners listeners, Guid id,
- ShardStrategy shardStrategy, IDictionary<string, IDatabaseCommands> shardDbCommands)
+ ShardStrategy shardStrategy, IDictionary<string, IDatabaseCommands> shardDbCommands)
: base(documentStore, listeners, id, shardStrategy, shardDbCommands)
{
}
@@ -52,27 +52,27 @@ protected override JsonDocument GetJsonDocument(string documentKey)
{
var shardRequestData = new ShardRequestData
{
- EntityType = typeof (object),
- Keys = {documentKey}
- };
+ EntityType = typeof(object),
+ Keys = { documentKey }
+ };
var dbCommands = GetCommandsToOperateOn(shardRequestData);
var documents = shardStrategy.ShardAccessStrategy.Apply(dbCommands,
- shardRequestData,
- (commands, i) => commands.Get(documentKey));
+ shardRequestData,
+ (commands, i) => commands.Get(documentKey));
var document = documents.FirstOrDefault(x => x != null);
if (document != null)
return document;
throw new InvalidOperationException("Document '" + documentKey + "' no longer exists and was probably deleted");
- }
+ }
protected override string GenerateKey(object entity)
- {
+ {
var shardId = shardStrategy.ShardResolutionStrategy.MetadataShardIdFor(entity);
- IDatabaseCommands value;
- if (shardDbCommands.TryGetValue(shardId, out value) == false)
+ IDatabaseCommands value;
+ if (shardDbCommands.TryGetValue(shardId, out value) == false)
throw new InvalidOperationException("Could not find shard: " + shardId);
return Conventions.GenerateDocumentKey(value, entity);
}
@@ -87,7 +87,7 @@ protected override Task<string> GenerateKeyAsync(object entity)
ISyncAdvancedSessionOperation IDocumentSession.Advanced
{
get { return this; }
- }
+ }
/// <summary>
/// Access the lazy operations
@@ -115,15 +115,15 @@ public T Load<T>(string id)
{
object existingEntity;
if (entitiesByKey.TryGetValue(id, out existingEntity))
- {
- return (T) existingEntity;
- }
+ {
+ return (T)existingEntity;
+ }
IncrementRequestCount();
var shardRequestData = new ShardRequestData
{
- EntityType = typeof (T),
- Keys = {id}
+ EntityType = typeof(T),
+ Keys = { id }
};
var dbCommands = GetCommandsToOperateOn(shardRequestData);
var results = shardStrategy.ShardAccessStrategy.Apply(dbCommands, shardRequestData, (commands, i) =>
@@ -145,7 +145,7 @@ public T Load<T>(string id)
if (shardsContainThisDocument.Count() > 1)
{
throw new InvalidOperationException("Found document with id: " + id +
- " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
+ " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
}
return shardsContainThisDocument.FirstOrDefault();
@@ -163,7 +163,7 @@ public T[] Load<T>(IEnumerable<string> ids)
public T Load<T>(ValueType id)
{
- var documentKey = Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof (T), false);
+ var documentKey = Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof(T), false);
return Load<T>(documentKey);
}
@@ -187,7 +187,7 @@ public T[] LoadInternal<T>(string[] ids, string[] includes)
var currentShardIds = shard.Select(x => x.Id).ToArray();
var multiLoadOperations = shardStrategy.ShardAccessStrategy.Apply(shard.Key, new ShardRequestData
{
- EntityType = typeof (T),
+ EntityType = typeof(T),
Keys = currentShardIds.ToList()
}, (dbCmd, i) =>
{
@@ -213,15 +213,20 @@ public T[] LoadInternal<T>(string[] ids, string[] includes)
var id = currentShardIds[i];
var itemPosition = Array.IndexOf(ids, id);
if (ReferenceEquals(results[itemPosition], default(T)) == false)
- {
+ {
throw new InvalidOperationException("Found document with id: " + id +
- " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
+ " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
}
results[itemPosition] = loadResults[i];
}
}
}
- return results;
+ return ids.Select(id => // so we get items that were skipped because they are already in the session cache
+ {
+ object val;
+ entitiesByKey.TryGetValue(id, out val);
+ return (T)val;
+ }).ToArray();
}
public ILoaderWithInclude<object> Include(string path)
@@ -251,7 +256,7 @@ public Lazy<TResult[]> Load<TResult>(IEnumerable<string> ids, Action<TResult[]>
/// </summary>
Lazy<TResult> ILazySessionOperations.Load<TResult>(string id)
{
- return Lazily.Load(id, (Action<TResult>) null);
+ return Lazily.Load(id, (Action<TResult>)null);
}
/// <summary>
@@ -261,28 +266,28 @@ public Lazy<TResult> Load<TResult>(string id, Action<TResult> onEval)
{
var cmds = GetCommandsToOperateOn(new ShardRequestData
{
- Keys = {id},
- EntityType = typeof (TResult)
+ Keys = { id },
+ EntityType = typeof(TResult)
});
var lazyLoadOperation = new LazyLoadOperation<TResult>(id, new LoadOperation(this, () =>
- {
- var list = cmds.Select(databaseCommands => databaseCommands.DisableAllCaching()).ToList();
- return new DisposableAction(() => list.ForEach(x => x.Dispose()));
- }, id));
+ {
+ var list = cmds.Select(databaseCommands => databaseCommands.DisableAllCaching()).ToList();
+ return new DisposableAction(() => list.ForEach(x => x.Dispose()));
+ }, id));
return AddLazyOperation(lazyLoadOperation, onEval, cmds);
}
internal Lazy<T> AddLazyOperation<T>(ILazyOperation operation, Action<T> onEval, IList<IDatabaseCommands> cmds)
{
pendingLazyOperations.Add(Tuple.Create(operation, cmds));
var lazyValue = new Lazy<T>(() =>
- {
- ExecuteAllPendingLazyOperations();
- return (T) operation.Result;
- });
+ {
+ ExecuteAllPendingLazyOperations();
+ return (T)operation.Result;
+ });
if (onEval != null)
- onEvaluateLazy[operation] = result => onEval((T) result);
+ onEvaluateLazy[operation] = result => onEval((T)result);
return lazyValue;
}
@@ -318,7 +323,7 @@ Lazy<TResult> ILazySessionOperations.Load<TResult>(ValueType id)
/// </remarks>
public Lazy<TResult> Load<TResult>(ValueType id, Action<TResult> onEval)
{
- var documentKey = Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof (TResult), false);
+ var documentKey = Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof(TResult), false);
return Lazily.Load<TResult>(documentKey);
}
@@ -359,8 +364,8 @@ public Lazy<T[]> LazyLoadInternal<T>(string[] ids, string[] includes, Action<T[]
id,
shards = GetCommandsToOperateOn(new ShardRequestData
{
- Keys = {id},
- EntityType = typeof (T),
+ Keys = { id },
+ EntityType = typeof(T),
})
})
.GroupBy(x => x.shards, new DbCmdsListComparer());
@@ -413,15 +418,15 @@ private bool ExecuteLazyOperationsSingleStep()
var lazyOperations = operationPerShard.Select(x => x.Item1).ToArray();
var requests = lazyOperations.Select(x => x.CraeteRequest()).ToArray();
var multiResponses = shardStrategy.ShardAccessStrategy.Apply(operationPerShard.Key, new ShardRequestData(),
- (commands, i) => commands.MultiGet(requests));
+ (commands, i) => commands.MultiGet(requests));
var sb = new StringBuilder();
foreach (var response in from shardReponses in multiResponses
from getResponse in shardReponses
where getResponse.RequestHasErrors()
select getResponse)
sb.AppendFormat("Got an error from server, status code: {0}{1}{2}", response.Status, Environment.NewLine,
- response.Result)
+ response.Result)
.AppendLine();
if (sb.Length > 0)
@@ -468,13 +473,13 @@ protected override IAsyncDocumentQuery<T> IDocumentQueryGeneratorAsyncQuery<T>(s
public IDocumentQuery<T> LuceneQuery<T>(string indexName)
{
return new ShardedDocumentQuery<T>(this, GetShardsToOperateOn, shardStrategy, indexName, null, null,
- listeners.QueryListeners);
+ listeners.QueryListeners);
}
public IDocumentQuery<T> LuceneQuery<T>()
{
return LuceneQuery<T>(GetDynamicIndexName<T>());
- }
+ }
#endregion
@@ -530,8 +535,8 @@ void ISyncAdvancedSessionOperation.Refresh<T>(T entity)
var shardRequestData = new ShardRequestData
{
- EntityType = typeof (T),
- Keys = {value.Key}
+ EntityType = typeof(T),
+ Keys = { value.Key }
};
var dbCommands = GetCommandsToOperateOn(shardRequestData);
@@ -542,7 +547,7 @@ void ISyncAdvancedSessionOperation.Refresh<T>(T entity)
return false;
value.Metadata = jsonDocument.Metadata;
- value.OriginalMetadata = (RavenJObject) jsonDocument.Metadata.CloneToken();
+ value.OriginalMetadata = (RavenJObject)jsonDocument.Metadata.CloneToken();
value.ETag = jsonDocument.Etag;
value.OriginalValue = jsonDocument.DataAsJson;
var newEntity = ConvertToEntity<T>(value.Key, jsonDocument.DataAsJson, jsonDocument.Metadata);
@@ -567,13 +572,13 @@ public IEnumerable<T> LoadStartingWith<T>(string keyPrefix, string matches = nul
IncrementRequestCount();
var shards = GetCommandsToOperateOn(new ShardRequestData
{
- EntityType = typeof (T),
- Keys = {keyPrefix}
+ EntityType = typeof(T),
+ Keys = { keyPrefix }
});
var results = shardStrategy.ShardAccessStrategy.Apply(shards, new ShardRequestData
{
- EntityType = typeof (T),
- Keys = {keyPrefix}
+ EntityType = typeof(T),
+ Keys = { keyPrefix }
}, (dbCmd, i) => dbCmd.StartsWith(keyPrefix, matches, start, pageSize));
return results.SelectMany(x => x).Select(TrackEntity<T>)
@@ -597,6 +602,6 @@ string ISyncAdvancedSessionOperation.GetDocumentUrl(object entity)
throw new InvalidOperationException("Could not find matching shard for shard id: " + shardId);
return commands.UrlFor(value.Key);
}
- }
+ }
}
#endif

0 comments on commit 4cc039e

Please sign in to comment.