/// <summary> /// Gets an updated and ordered list of graph nodes of a specific length. /// </summary> /// <param name="maxLength">The maximum character length that will be used for the index.</param> /// <param name="acKey">The autocomplete key to potentially be included in this key index.</param> /// <param name="acBlock">The current list of graph nodes already existing and to be reordered based on the acKey property.</param> /// <returns>Returns an ordered list of key value pairs that represents what will display in the search box for the acKey property.</returns> private static GraphNode[] UpdateOrderedJsonBlock(int maxLength, GraphNode acKey, GraphNode[] acBlock) { var keyIndex = acBlock .ToList() .IndexOf(acBlock .Where(aKey => aKey.label.Equals(acKey.label, StringComparison.InvariantCultureIgnoreCase)) .First()); acBlock[keyIndex] = acKey; var acBlockList = acBlock.ToList(); acBlockList = acBlockList .OrderByDescending(acbk => acbk.size) .ToList() .Take(maxLength) .ToList(); acBlock = acBlockList .ToArray(); return acBlock; }
/// <summary> /// Update the JSON file and its stream on blob storage. /// </summary> /// <param name="queryKey">The query key to update the JSON file index.</param> /// <param name="maxLength">The maximum character length to index for each query key.</param> /// <param name="blobStream">The stream for the JSON file.</param> /// <param name="jsSerializer">For JSON serialization and deserialization.</param> /// <returns></returns> private static GraphNode[] UpdateBlobStreamForKey(IGraphNode queryKey, int maxLength, Stream blobStream, JavaScriptSerializer jsSerializer) { var acBlock = jsSerializer.Deserialize<GraphNode[]>(Regex.Replace(new StreamReader(blobStream).ReadToEnd(), @"^dataCallback\(|\)$", "", RegexOptions.IgnoreCase)); var acKey = new GraphNode() { label = queryKey.label.ToLowerInvariant(), size = queryKey.size }; // Check for key phrase var hasKey = acBlock.ToList().Any(aKey => aKey.label.Equals(acKey.label, StringComparison.InvariantCultureIgnoreCase)); // Update key weight and pushed back to storage if (hasKey) { acBlock = UpdateOrderedJsonBlock(maxLength, acKey, acBlock); } else { acBlock = GetOrderedJsonBlock(maxLength, acKey, acBlock); } return acBlock; }
/// <summary> /// Update the JSON block blob using the blob service class. Manages concurrent access conditions for parallel transactions. /// </summary> /// <param name="storage">The cloud storage account on Windows Azure platform.</param> /// <param name="containerId">The public container id on the cloud storage account.</param> /// <param name="key">The partial key query to index for.</param> /// <param name="jsSerializer">The serialization library to manage converting the JSON string to raw bytes.</param> /// <param name="acBlock">The list of graph nodes ordered by size.</param> /// <returns></returns> private static Stream UpdateJsonBlockBlob(CloudStorageAccount storage, string containerId, string key, JavaScriptSerializer jsSerializer, GraphNode[] acBlock) { Stream blobStream; var jsonString = string.Format("dataCallback({0})", jsSerializer.Serialize(acBlock)); blobStream = new MemoryStream(); var bytes = Encoding.UTF8.GetBytes(jsonString); blobStream.Write(bytes, 0, bytes.Length); blobStream.Seek(0, SeekOrigin.Begin); BlobService.PutBlob(storage, containerId, key.ToLowerInvariant(), blobStream, "cache", 0); return blobStream; }
/// <summary> /// Gets an ordered list of graph nodes of a specific length. /// </summary> /// <param name="maxLength">The maximum character length that will be used for the index.</param> /// <param name="acKey">The autocomplete key to potentially be included in this key index.</param> /// <param name="acBlock">The current list of graph nodes already existing and to be reordered based on the acKey property.</param> /// <returns>Returns an ordered list of key value pairs that represents what will display in the search box for the acKey property.</returns> private static GraphNode[] GetOrderedJsonBlock(int maxLength, GraphNode acKey, GraphNode[] acBlock) { List<GraphNode> acBlockList = acBlock.ToList(); acBlockList.Add(acKey); acBlockList = acBlockList .OrderByDescending(acbk => acbk.size) .ToList() .Take(maxLength) .ToList(); acBlock = acBlockList.ToArray(); return acBlock; }