You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

  1. /*
  2. * node-rdkafka - Node.js wrapper for RdKafka C/C++ library
  3. *
  4. * Copyright (c) 2016 Blizzard Entertainment
  5. *
  6. * This software may be modified and distributed under the terms
  7. * of the MIT license. See the LICENSE.txt file for details.
  8. */
  9. var Topic = require('./topic');
  10. module.exports = TopicPartition;
  11. /**
  12. * Map an array of topic partition js objects to real topic partition objects.
  13. *
  14. * @param array The array of topic partition raw objects to map to topic
  15. * partition objects
  16. */
  17. TopicPartition.map = function(array) {
  18. return array.map(function(element) {
  19. return TopicPartition.create(element);
  20. });
  21. };
  22. /**
  23. * Take a topic partition javascript object and convert it to the class.
  24. * The class will automatically convert offset identifiers to special constants
  25. *
  26. * @param element The topic partition raw javascript object
  27. */
  28. TopicPartition.create = function(element) {
  29. // Just ensure we take something that can have properties. The topic partition
  30. // class will
  31. element = element || {};
  32. return new TopicPartition(element.topic, element.partition, element.offset);
  33. };
  34. /**
  35. * Create a topic partition. Just does some validation and decoration
  36. * on topic partitions provided.
  37. *
  38. * Goal is still to behave like a plain javascript object but with validation
  39. * and potentially some extra methods
  40. */
  41. function TopicPartition(topic, partition, offset) {
  42. if (!(this instanceof TopicPartition)) {
  43. return new TopicPartition(topic, partition, offset);
  44. }
  45. // Validate that the elements we are iterating over are actual topic partition
  46. // js objects. They do not need an offset, but they do need partition
  47. if (!topic) {
  48. throw new TypeError('"topic" must be a string and must be set');
  49. }
  50. if (partition === null || partition === undefined) {
  51. throw new TypeError('"partition" must be a number and must set');
  52. }
  53. // We can just set topic and partition as they stand.
  54. this.topic = topic;
  55. this.partition = partition;
  56. if (offset === undefined || offset === null) {
  57. this.offset = Topic.OFFSET_STORED;
  58. } else if (typeof offset === 'string') {
  59. switch (offset.toLowerCase()) {
  60. case 'earliest':
  61. case 'beginning':
  62. this.offset = Topic.OFFSET_BEGINNING;
  63. break;
  64. case 'latest':
  65. case 'end':
  66. this.offset = Topic.OFFSET_END;
  67. break;
  68. case 'stored':
  69. this.offset = Topic.OFFSET_STORED;
  70. break;
  71. default:
  72. throw new TypeError('"offset", if provided as a string, must be beginning, end, or stored.');
  73. }
  74. } else if (typeof offset === 'number') {
  75. this.offset = offset;
  76. } else {
  77. throw new TypeError('"offset" must be a special string or number if it is set');
  78. }
  79. }