Advertisement
Guest User

Untitled

a guest
May 26th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import std.stdio;
  2. import std.string;
  3. import std.json;
  4. import std.regex;
  5. import std.typecons;
  6.  
  7. import pegged.grammar;
  8.  
  9.  
  10. mixin(grammar(`
  11. QueryPath:
  12. Path <- Segment (:"." Segment)*
  13. Segment <- identifier
  14. `));
  15.  
  16. class Predicate {
  17. string lhs;
  18. string op;
  19. string rhs;
  20. this(string lhs, string op, string rhs) {
  21. this.lhs = lhs;
  22. this.op = op;
  23. this.rhs = rhs;
  24. }
  25. }
  26.  
  27. class Query {
  28. string[] sourceFields;
  29. string sourceNode;
  30. Predicate[] predicates;
  31.  
  32. this(string[] sourceFields, string sourceNode, Predicate[] predicates) {
  33. this.sourceFields = sourceFields;
  34. this.sourceNode = sourceNode;
  35. this.predicates = predicates;
  36. }
  37.  
  38. Nullable!JSONValue findBaseNode(JSONValue obj, string path) {
  39. ParseTree p = QueryPath(path);
  40. string[] segments = p.matches;
  41. Nullable!JSONValue current;
  42. foreach(segment;segments) {
  43. try {
  44. current = obj[segment];
  45. } catch (Exception e) {
  46. writeln(segment ~ " does not exist");
  47. current.nullify();
  48. return current;
  49. }
  50. }
  51. return current;
  52. }
  53.  
  54.  
  55. void f(JSONValue[string] objmap) {
  56. writeln("f obj");
  57. foreach(k,v;objmap) {
  58. if(v.type() == JSON_TYPE.ARRAY ||
  59. v.type() == JSON_TYPE.OBJECT) {
  60. writeln(k);
  61. filter(v);
  62. } else {
  63. writeln(k, "->", v);
  64. }
  65. }
  66. }
  67.  
  68. void f(JSONValue[] objarray) {
  69. writeln("f array");
  70. }
  71.  
  72. void filter(JSONValue obj) {
  73. // 1) if isAnon, then iterate each key like an array
  74. // isAnon should be sensitive to the segment
  75.  
  76. switch(obj.type()) {
  77. case JSON_TYPE.ARRAY:
  78. writeln("ARRAY");
  79. auto a = obj.array();
  80. f(a);
  81. break;
  82.  
  83. case JSON_TYPE.OBJECT:
  84. writeln("OBJECT");
  85. auto o = obj.object();
  86. f(o);
  87. break;
  88.  
  89. default:
  90. writeln("WAT");
  91. }
  92. }
  93.  
  94. void filter(JSONValue[string] objs) {
  95. writeln("FILTER MULTIPLE");
  96. foreach(o;objs) {
  97. writeln(">>> ", o);
  98. writeln("");
  99. }
  100. }
  101.  
  102. bool isAnon() {
  103. bool anon = false;
  104. auto ctr = ctRegex!(`\$object`);
  105. foreach(f;sourceFields) {
  106. auto c = matchFirst(f, ctr);
  107. if(!c.empty) {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113.  
  114. void exec(string jsonText) {
  115. JSONValue root = parseJSON(jsonText);
  116. writeln(root.type() == JSON_TYPE.OBJECT);
  117. auto base = findBaseNode(root, sourceNode);
  118. if(base.isNull) {
  119. writeln("No result");
  120. return;
  121. }
  122. writeln("Prefilter = ", base.get());
  123.  
  124. bool anon = isAnon();
  125. if(anon) {
  126. filter(base.get());
  127. } else {
  128. filter(base.get());
  129. }
  130. }
  131. }
  132.  
  133. void main()
  134. {
  135. File file = File("data.json", "r");
  136. string buf;
  137.  
  138. while (!file.eof()) {
  139. string line = chomp(file.readln());
  140. buf ~= line;
  141. }
  142.  
  143. Query q = new Query(["$object.block_size", "$object.kb_size"],
  144. "filesystem",
  145. [new Predicate("kb_available",">","1000")]);
  146. q.exec(buf);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement