//****************************************************************************************************** // BrokerRouterExtensions.cs - Gbtc // // Copyright © 2015, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 10/27/2015 - Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Linq; using Misakai.Kafka; namespace KafkaAdapters { /// /// Defines extensions methods for instances. /// public static class BrokerRouterExtensions { /// /// Fetches a single result for the specified and . /// /// Router used to fetch result. /// Topic to fetch result from. /// Partition ID to fetch result from. /// Offset of to begin fetching result. /// Defines maximum size of request that can be adjusted for large serializations. /// A instance, if available; otherwise, null. public static FetchResponse Fetch(this BrokerRouter router, string topic, int partitionID = 0, long offset = 0, int maxBytes = 32768) { if ((object)router == null) throw new ArgumentNullException(nameof(router)); if (string.IsNullOrWhiteSpace(topic)) throw new ArgumentNullException(nameof(topic)); // Create a fetch request with a single item to request FetchRequest request = new FetchRequest() { Fetches = new List(new[] { new Fetch { Topic = topic, PartitionId = partitionID, Offset = offset, MaxBytes = maxBytes } }) }; return router.SelectBrokerRoute(topic, partitionID).Connection.SendAsync(request).Result.FirstOrDefault(); } /// /// Gets the latest offset for a Topic partition. /// /// Router used to derive latest offset. /// Topic to determine latest offset for. /// Topic partition to determine latest offset for. /// The latest offset for a partition in a topic. public static long LatestOffset(this BrokerRouter router, string topic, int partitionID = 0) { long offset = 0; FetchResponse response = router.Fetch(topic, partitionID); if ((object)response != null) offset = response.HighWaterMark; return offset; } } }