metainfo.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/generic/yexception.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/refcount.h>
  6. #include <util/stream/input.h>
  7. #include <util/stream/str.h>
  8. #include "compressor.h"
  9. namespace NCompProto {
  10. const size_t MAX_ELEMENTS = 32;
  11. struct TScalarDefaultValue {
  12. TScalarDefaultValue()
  13. : Type(None)
  14. , Value(0)
  15. {
  16. }
  17. enum EType {
  18. None = 0,
  19. Fixed = 1,
  20. };
  21. EType Type;
  22. ui32 Value;
  23. };
  24. template <class X>
  25. struct TMetaInfo {
  26. X Index;
  27. X Count;
  28. X Mask;
  29. X Scalar[MAX_ELEMENTS];
  30. TAtomicSharedPtr<TMetaInfo> Repeated[MAX_ELEMENTS];
  31. TScalarDefaultValue Default[MAX_ELEMENTS];
  32. ui32 ScalarMask;
  33. ui32 RepeatedMask;
  34. size_t Size;
  35. TString Name;
  36. TString ChildName[MAX_ELEMENTS];
  37. size_t Id;
  38. TMetaInfo* Parent;
  39. struct TCheck {
  40. ui32 Count;
  41. ui32 Mask;
  42. ui32 LastIndex;
  43. ui32 Check(size_t id, size_t rule) {
  44. ui32 mask = 1UL << id;
  45. if (Mask & mask) {
  46. ythrow yexception() << "Mask & mask";
  47. }
  48. Mask |= mask;
  49. Y_ENSURE(rule & mask, "!(rule & mask)");
  50. return mask;
  51. }
  52. void ClearCount() {
  53. Count = 0;
  54. }
  55. void ClearMask() {
  56. Mask = 0;
  57. }
  58. void ClearIndex() {
  59. LastIndex = ui32(-1);
  60. }
  61. void ClearAll() {
  62. ClearCount();
  63. ClearIndex();
  64. ClearMask();
  65. }
  66. TCheck() {
  67. ClearAll();
  68. }
  69. };
  70. TCheck Serializer;
  71. void SetDefaults(TMetaInfo* parent) {
  72. ScalarMask = 0;
  73. RepeatedMask = 0;
  74. Size = 0;
  75. Id = 0;
  76. Parent = parent;
  77. }
  78. template <class T, class TBuilder>
  79. TMetaInfo(const TMetaInfo<T>& meta, const TBuilder& builder) {
  80. SetDefaults(nullptr);
  81. Name = meta.Name;
  82. builder.Build(Index, meta.Index);
  83. builder.Build(Count, meta.Count);
  84. builder.Build(Mask, meta.Mask);
  85. Size = meta.Size;
  86. Id = meta.Id;
  87. ScalarMask = meta.ScalarMask;
  88. RepeatedMask = meta.RepeatedMask;
  89. for (size_t i = 0; i < Size; ++i) {
  90. ui32 index = (1UL << i);
  91. if (ScalarMask & index) {
  92. builder.Build(Scalar[i], meta.Scalar[i]);
  93. ChildName[i] = meta.ChildName[i];
  94. Default[i] = meta.Default[i];
  95. }
  96. if (RepeatedMask & index) {
  97. THolder<TMetaInfo> info(new TMetaInfo(*meta.Repeated[i], builder));
  98. info->Parent = this;
  99. Repeated[i].Reset(info.Release());
  100. }
  101. }
  102. }
  103. TMetaInfo(TMetaInfo* parent) {
  104. SetDefaults(parent);
  105. }
  106. template <class T>
  107. TMetaInfo& BeginRepeated(size_t id, T& functor) {
  108. Serializer.Check(id, RepeatedMask);
  109. TMetaInfo& res = *Repeated[id].Get();
  110. res.Count.BeginElement(functor);
  111. return res;
  112. }
  113. template <class T>
  114. void BeginSelf(T& functor) {
  115. Serializer.ClearAll();
  116. Count.BeginElement(functor);
  117. }
  118. template <class T>
  119. void EndRepeated(T& functor) {
  120. Count.AddDelayed(Serializer.Count, functor);
  121. Serializer.ClearCount();
  122. Serializer.ClearIndex();
  123. Y_ENSURE(Serializer.Mask == 0, "Serializer.Mask != 0");
  124. }
  125. template <class T>
  126. void BeginElement(ui32 index, T& functor) {
  127. Y_ENSURE(!(index < Serializer.LastIndex + 1),
  128. "Name: " << Name.Quote() << " error: index < Serializer.LastIndex + 1; "
  129. << "index: " << index << " LastIndex: " << Serializer.LastIndex);
  130. Index.Add(index - Serializer.LastIndex, functor);
  131. Mask.BeginElement(functor);
  132. Serializer.LastIndex = index;
  133. ++Serializer.Count;
  134. }
  135. template <class TFunctor>
  136. void Iterate(TFunctor& functor) {
  137. Cout << Name << " "
  138. << "Index" << Endl;
  139. functor.Process(Index);
  140. Cout << "Count" << Endl;
  141. functor.Process(Count);
  142. Cout << "Mask" << Endl;
  143. functor.Process(Mask);
  144. for (size_t i = 0; i < Size; ++i) {
  145. ui32 index = (1UL << i);
  146. if (ScalarMask & index) {
  147. Cout << "scalar"
  148. << " " << i << Endl;
  149. functor.Process(Scalar[i]);
  150. }
  151. if (RepeatedMask & index) {
  152. Cout << "repeated"
  153. << " " << i << Endl;
  154. Repeated[i]->Iterate(functor);
  155. }
  156. }
  157. }
  158. template <class T>
  159. void EndElement(T& functor) {
  160. Mask.AddDelayed(Serializer.Mask, functor);
  161. Serializer.ClearMask();
  162. }
  163. template <class T>
  164. void SetScalar(size_t id, ui32 value, T& functor) {
  165. if (Default[id].Type != TScalarDefaultValue::Fixed || value != Default[id].Value) {
  166. Serializer.Check(id, ScalarMask);
  167. Scalar[id].Add(value, functor);
  168. }
  169. }
  170. ui32 Check(size_t id) {
  171. Y_ENSURE(id < MAX_ELEMENTS, "id >= MAX_ELEMENTS");
  172. ui32 mask = 1UL << id;
  173. if (ScalarMask & mask) {
  174. ythrow yexception() << "ScalarMask & mask";
  175. }
  176. if (RepeatedMask & mask) {
  177. ythrow yexception() << "RepeatedMask & mask";
  178. }
  179. Size = Max(id + 1, Size);
  180. return mask;
  181. }
  182. TMetaInfo(IInputStream& stream) {
  183. SetDefaults(nullptr);
  184. Load(stream);
  185. }
  186. TMetaInfo(const TString& str) {
  187. SetDefaults(nullptr);
  188. TStringInput stream(str);
  189. Load(stream);
  190. }
  191. void Save(IOutputStream& stream, const TString& offset = TString()) {
  192. stream << offset << "repeated " << Name << " id " << Id << Endl;
  193. TString step = " ";
  194. stream << step << offset << "index" << Endl;
  195. Index.Save(stream, step + offset);
  196. stream << step << offset << "count" << Endl;
  197. Count.Save(stream, step + offset);
  198. stream << step << offset << "mask" << Endl;
  199. Mask.Save(stream, step + offset);
  200. for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
  201. ui32 mask = 1UL << i;
  202. if (mask & RepeatedMask) {
  203. Repeated[i]->Save(stream, step + offset);
  204. } else if (mask & ScalarMask) {
  205. stream << step << offset << "scalar " << ChildName[i] << " id " << i;
  206. stream << " default " << (Default[i].Type == TScalarDefaultValue::None ? " no " : " const ");
  207. if (Default[i].Type == TScalarDefaultValue::Fixed) {
  208. stream << Default[i].Value;
  209. }
  210. stream << Endl;
  211. stream << step << offset << "table "
  212. << "id " << i << Endl;
  213. Scalar[i].Save(stream, step + offset);
  214. }
  215. }
  216. stream << offset << "end" << Endl;
  217. }
  218. void Load(IInputStream& stream) {
  219. TString name;
  220. stream >> name;
  221. if (name == "repeated") {
  222. stream >> name;
  223. }
  224. Name = name;
  225. stream >> name;
  226. Y_ENSURE(name == "id", "Name mismatch: " << name.Quote() << " != id. ");
  227. stream >> Id;
  228. while (1) {
  229. stream >> name;
  230. if (name == "index") {
  231. Index.Load(stream);
  232. } else if (name == "count") {
  233. Count.Load(stream);
  234. } else if (name == "mask") {
  235. Mask.Load(stream);
  236. } else if (name == "table") {
  237. stream >> name;
  238. Y_ENSURE(name == "id", "Name mismatch: " << name.Quote() << " != id. ");
  239. size_t id;
  240. stream >> id;
  241. Scalar[id].Load(stream);
  242. } else if (name == "repeated") {
  243. THolder<TMetaInfo> info(new TMetaInfo(this));
  244. info->Load(stream);
  245. size_t id = info->Id;
  246. RepeatedMask |= Check(id);
  247. Repeated[id].Reset(info.Release());
  248. } else if (name == "scalar") {
  249. stream >> name;
  250. TString childName = name;
  251. stream >> name;
  252. Y_ENSURE(name == "id", "Name mismatch: " << name.Quote() << " != id. ");
  253. size_t id;
  254. stream >> id;
  255. ScalarMask |= Check(id);
  256. ChildName[id] = childName;
  257. stream >> name;
  258. Y_ENSURE(name == "default", "Name mismatch: " << name.Quote() << " != default. ");
  259. stream >> name;
  260. if (name == "no") {
  261. Default[id].Type = TScalarDefaultValue::None;
  262. } else if (name == "const") {
  263. ui32 def;
  264. stream >> def;
  265. Default[id].Type = TScalarDefaultValue::Fixed;
  266. Default[id].Value = def;
  267. } else {
  268. ythrow yexception() << "Unsupported default value specification: " << name.Quote();
  269. }
  270. } else if (name == "end" /* || stream.IsEOF()*/) {
  271. return;
  272. }
  273. }
  274. }
  275. };
  276. }