PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/doc/apc_sample_serializer.php

https://gitlab.com/Blueprint-Marketing/hhvm
PHP | 162 lines | 143 code | 15 blank | 4 comment | 24 complexity | a555ca4e1b8f4daa526ca098c5027371 MD5 | raw file
  1. <?php
  2. /**
  3. * Write .cpp files for building libapc_prime.so that can be loaded by an
  4. * HPHP-compiled server at startup time.
  5. */
  6. class CacheArchiveHphpSerializer extends CacheArchiveSerializer {
  7. public function __construct($archive_name) {
  8. $this->archive_name = $archive_name;
  9. }
  10. public function getName() { return 'hphp';}
  11. public function unserialize($data) {}
  12. public function serialize($data) {
  13. $tmp = 0;
  14. $out = "\n#include \"apc_prime.h\"\n\n";
  15. $out .= "namespace HPHP {\n";
  16. $out .= str_repeat('/', 79)."\n\n";
  17. $out .= "APC_BEGIN(".$this->getArchiveId().");\n";
  18. unset($data['dummy_key']);
  19. $this->serializeStrings($out, $data);
  20. $this->serializeInt64 ($out, $data);
  21. $this->serializeObjects($out, $data);
  22. $this->serializeChars ($out, $data);
  23. $this->serializeThrifts($out, $data);
  24. $this->serializeOthers ($out, $data);
  25. $out .= "APC_END(".$this->getArchiveId().");\n\n";
  26. $out .= str_repeat('/', 79)."\n";
  27. $out .= "}\n";
  28. return $out;
  29. }
  30. private function getArchiveId() {
  31. return preg_replace('/-/', '_', $this->archive_name);
  32. }
  33. private function s($s) {
  34. static $esc_chars = "\0\n\r\t\\\"?";
  35. $slen = strlen($s);
  36. $s = addcslashes($s, $esc_chars);
  37. return "\"$s\",S($slen)";
  38. }
  39. private function serializeChars(&$out, &$data) {
  40. $i = 0;
  41. $keys = "static const char *char_keys[] = {\n";
  42. $values = "static char char_values[] = {\n";
  43. foreach ($data as $k => $v) {
  44. if ($v === null || is_bool($v)) {
  45. $keys .= $this->s($k).',';
  46. if ($v === null) {
  47. $values .= '2,';
  48. } else if ($v) {
  49. $values .= '1,';
  50. } else {
  51. $values .= '0,';
  52. }
  53. if (++$i % 10 == 0) {
  54. $keys .= "\n";
  55. $values .= "\n";
  56. }
  57. unset($data[$k]);
  58. }
  59. }
  60. $keys .= "\nNULL};\n";
  61. $values .= "\n};\n";
  62. $out .= $keys;
  63. $out .= $values;
  64. }
  65. private function serializeInt64(&$out, &$data) {
  66. $i = 0;
  67. $keys = "static const char *int_keys[] = {\n";
  68. $values = "static int64 int_values[] = {\n";
  69. foreach ($data as $k => $v) {
  70. if (is_int($v)) {
  71. $keys .= $this->s($k).',';
  72. if ($v >= (1 << 32)) {
  73. $values .= $v.'LL,';
  74. } else {
  75. $values .= $v.',';
  76. }
  77. if (++$i % 10 == 0) {
  78. $keys .= "\n";
  79. $values .= "\n";
  80. }
  81. unset($data[$k]);
  82. }
  83. }
  84. $keys .= "\nNULL};\n";
  85. $values .= "\n};\n";
  86. $out .= $keys;
  87. $out .= $values;
  88. }
  89. private function serializeStrings(&$out, &$data) {
  90. $i = 0;
  91. $kvs = "static const char *strings[] = {\n";
  92. foreach ($data as $k => $v) {
  93. if (is_string($v)) {
  94. $kvs .= $this->s($k).','.$this->s($v).',';
  95. if (++$i % 10 == 0) {
  96. $kvs .= "\n";
  97. }
  98. unset($data[$k]);
  99. }
  100. }
  101. $kvs .= "\nNULL};\n";
  102. $out .= $kvs;
  103. }
  104. private function serializeObjects(&$out, &$data) {
  105. $i = 0;
  106. $kvs = "static const char *objects[] = {\n";
  107. foreach ($data as $k => $v) {
  108. if (is_object($v)) {
  109. $kvs .= $this->s($k).','.$this->s(serialize($v)).',';
  110. if (++$i % 10 == 0) {
  111. $kvs .= "\n";
  112. }
  113. unset($data[$k]);
  114. }
  115. }
  116. $kvs .= "\nNULL};\n";
  117. $out .= $kvs;
  118. }
  119. private function serializeThrifts(&$out, &$data) {
  120. $i = 0;
  121. $kvs = "static const char *thrifts[] = {\n";
  122. foreach ($data as $k => $v) {
  123. $sv = fb_serialize($v);
  124. if ($sv) {
  125. $kvs .= $this->s($k).','.$this->s($sv).',';
  126. if (++$i % 10 == 0) {
  127. $kvs .= "\n";
  128. }
  129. unset($data[$k]);
  130. }
  131. }
  132. $kvs .= "\nNULL};\n";
  133. $out .= $kvs;
  134. }
  135. private function serializeOthers(&$out, &$data) {
  136. $i = 0;
  137. $kvs = "static const char *others[] = {\n";
  138. foreach ($data as $k => $v) {
  139. $kvs .= $this->s($k).','.$this->s(serialize($v)).',';
  140. if (++$i % 10 == 0) {
  141. $kvs .= "\n";
  142. }
  143. }
  144. $kvs .= "\nNULL};\n";
  145. $out .= $kvs;
  146. }
  147. }