gflow-aggregator.vala 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. namespace GFlow {
  2. public class Aggregator {
  3. private GLib.HashTable<string, AggregationPipeline?> _pipelines = new GLib.HashTable<string, AggregationPipeline?>(str_hash, str_equal);
  4. private static Aggregator _instance;
  5. private Aggregator() {}
  6. public static Aggregator get_instance() {
  7. if (_instance == null) {
  8. _instance = new Aggregator();
  9. }
  10. return _instance;
  11. }
  12. public AggregationPipeline new_aggregation_pipeline(string id) {
  13. var new_pipeline = new AggregationPipeline(id);
  14. new_pipeline.pipeline_commit.connect(commit_pipeline);
  15. _pipelines.set(id, new_pipeline);
  16. return new_pipeline;
  17. }
  18. private void commit_pipeline(string id) {
  19. _pipelines.remove(id);
  20. }
  21. public AggregationPipeline? find_aggregation_pipeline(string? id) {
  22. if (id == null) {
  23. return null;
  24. }
  25. return _pipelines.get(id);
  26. }
  27. }
  28. public interface AggregationPredicate : Object {
  29. public abstract bool should_commit(AggregationPipeline aggregation_pipeline);
  30. }
  31. public class AggregationPipeline : Object {
  32. public signal void pipeline_commit(string id);
  33. public delegate bool PipelineDelegate(AggregationPipeline pipeline);
  34. private GLib.HashTable<string, Value?> _attributes = new GLib.HashTable<string, Value?>(str_hash, str_equal);
  35. private GLib.HashTable<string, ValuesArray> _indexed_attributes = new GLib.HashTable<string, ValuesArray>(str_hash, str_equal);
  36. public string id {
  37. public get;
  38. construct set;
  39. }
  40. public AggregationPipeline(string id) {
  41. Object(id: id);
  42. }
  43. public AggregationPipeline set_attribute(string name, Value? value) {
  44. _attributes.set(name, value);
  45. return this;
  46. }
  47. public Value? get_attribute(string name) {
  48. return _attributes.get(name);
  49. }
  50. public AggregationPipeline set_array_attribute(string name, Value? value) {
  51. return set_array_attribute_at_index(name, value, 0);
  52. }
  53. public AggregationPipeline set_array_attribute_at_index(string name, Value? value, int index) {
  54. var list = _indexed_attributes.get(name);
  55. if (list == null) {
  56. list = new ValuesArray();
  57. _indexed_attributes.set(name, list);
  58. }
  59. list.insert(index, value);
  60. return this;
  61. }
  62. public Value?[]? get_array_attribute(string name) {
  63. var list = _indexed_attributes.get(name);
  64. if (list == null) {
  65. return null;
  66. }
  67. return list.to_array();
  68. }
  69. public bool has_attribute(string name) {
  70. return _attributes.contains(name) || _indexed_attributes.contains(name);
  71. }
  72. public void commit(AggregationPredicate aggregation_predicate, PipelineDelegate pipeline_delegate) {
  73. if (!aggregation_predicate.should_commit(this)) {
  74. return;
  75. }
  76. if (pipeline_delegate(this)) {
  77. pipeline_commit(id);
  78. _attributes.remove_all();
  79. _indexed_attributes.remove_all();
  80. }
  81. }
  82. }
  83. public class ValuesArray : Object {
  84. private GLib.HashTable<int, Value?> _array = new GLib.HashTable<int, Value?>(direct_hash, direct_equal);
  85. public void insert(uint index, Value? value) {
  86. _array.set((int)index, value);
  87. }
  88. public Value? get_value(uint index) {
  89. return _array.get((int)index);
  90. }
  91. public Value?[] to_array() {
  92. var ret = new Value?[_array.length];
  93. int i = 0;
  94. foreach (Value? v in _array.get_values()) {
  95. ret[i++] = v;
  96. }
  97. return ret;
  98. }
  99. }
  100. }