Revert "Fix bug in mysql_next_result's return value that confused "done" with "error""
[hiphop-php.git] / hphp / doc / apc_sample_serializer.php
blobb5423afb68f396bda5dce1eb0451f9c4cbcff754
1 <?php
3 /**
4 * Write .cpp files for building libapc_prime.so that can be loaded by an
5 * HPHP-compiled server at startup time.
6 */
7 class CacheArchiveHphpSerializer extends CacheArchiveSerializer {
8 public function __construct($archive_name) {
9 $this->archive_name = $archive_name;
12 public function getName() { return 'hphp';}
13 public function unserialize($data) {}
15 public function serialize($data) {
16 $tmp = 0;
17 $out = "\n#include \"apc_prime.h\"\n\n";
18 $out .= "namespace HPHP {\n";
19 $out .= str_repeat('/', 79)."\n\n";
20 $out .= "APC_BEGIN(".$this->getArchiveId().");\n";
22 unset($data['dummy_key']);
23 $this->serializeStrings($out, $data);
24 $this->serializeInt64 ($out, $data);
25 $this->serializeObjects($out, $data);
26 $this->serializeChars ($out, $data);
27 $this->serializeThrifts($out, $data);
28 $this->serializeOthers ($out, $data);
30 $out .= "APC_END(".$this->getArchiveId().");\n\n";
31 $out .= str_repeat('/', 79)."\n";
32 $out .= "}\n";
33 return $out;
36 private function getArchiveId() {
37 return preg_replace('/-/', '_', $this->archive_name);
40 private function s($s) {
41 static $esc_chars = "\0\n\r\t\\\"?";
42 $slen = strlen($s);
43 $s = addcslashes($s, $esc_chars);
44 return "\"$s\",S($slen)";
47 private function serializeChars(&$out, &$data) {
48 $i = 0;
49 $keys = "static const char *char_keys[] = {\n";
50 $values = "static char char_values[] = {\n";
51 foreach ($data as $k => $v) {
52 if ($v === null || is_bool($v)) {
53 $keys .= $this->s($k).',';
54 if ($v === null) {
55 $values .= '2,';
56 } else if ($v) {
57 $values .= '1,';
58 } else {
59 $values .= '0,';
61 if (++$i % 10 == 0) {
62 $keys .= "\n";
63 $values .= "\n";
65 unset($data[$k]);
68 $keys .= "\nNULL};\n";
69 $values .= "\n};\n";
71 $out .= $keys;
72 $out .= $values;
75 private function serializeInt64(&$out, &$data) {
76 $i = 0;
77 $keys = "static const char *int_keys[] = {\n";
78 $values = "static int64 int_values[] = {\n";
79 foreach ($data as $k => $v) {
80 if (is_int($v)) {
81 $keys .= $this->s($k).',';
82 if ($v >= (1 << 32)) {
83 $values .= $v.'LL,';
84 } else {
85 $values .= $v.',';
87 if (++$i % 10 == 0) {
88 $keys .= "\n";
89 $values .= "\n";
91 unset($data[$k]);
94 $keys .= "\nNULL};\n";
95 $values .= "\n};\n";
97 $out .= $keys;
98 $out .= $values;
101 private function serializeStrings(&$out, &$data) {
102 $i = 0;
103 $kvs = "static const char *strings[] = {\n";
104 foreach ($data as $k => $v) {
105 if (is_string($v)) {
106 $kvs .= $this->s($k).','.$this->s($v).',';
107 if (++$i % 10 == 0) {
108 $kvs .= "\n";
110 unset($data[$k]);
113 $kvs .= "\nNULL};\n";
114 $out .= $kvs;
117 private function serializeObjects(&$out, &$data) {
118 $i = 0;
119 $kvs = "static const char *objects[] = {\n";
120 foreach ($data as $k => $v) {
121 if (is_object($v)) {
122 $kvs .= $this->s($k).','.$this->s(serialize($v)).',';
123 if (++$i % 10 == 0) {
124 $kvs .= "\n";
126 unset($data[$k]);
129 $kvs .= "\nNULL};\n";
130 $out .= $kvs;
133 private function serializeThrifts(&$out, &$data) {
134 $i = 0;
135 $kvs = "static const char *thrifts[] = {\n";
136 foreach ($data as $k => $v) {
137 $sv = fb_serialize($v);
138 if ($sv) {
139 $kvs .= $this->s($k).','.$this->s($sv).',';
140 if (++$i % 10 == 0) {
141 $kvs .= "\n";
143 unset($data[$k]);
146 $kvs .= "\nNULL};\n";
147 $out .= $kvs;
150 private function serializeOthers(&$out, &$data) {
151 $i = 0;
152 $kvs = "static const char *others[] = {\n";
153 foreach ($data as $k => $v) {
154 $kvs .= $this->s($k).','.$this->s(serialize($v)).',';
155 if (++$i % 10 == 0) {
156 $kvs .= "\n";
159 $kvs .= "\nNULL};\n";
160 $out .= $kvs;