4 * Write .cpp files for building libapc_prime.so that can be loaded by an
5 * HPHP-compiled server at startup time.
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) {
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";
36 private function getArchiveId() {
37 return preg_replace('/-/', '_', $this->archive_name
);
40 private function s($s) {
41 static $esc_chars = "\0\n\r\t\\\"?";
43 $s = addcslashes($s, $esc_chars);
44 return "\"$s\",S($slen)";
47 private function serializeChars(&$out, &$data) {
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).',';
68 $keys .= "\nNULL};\n";
75 private function serializeInt64(&$out, &$data) {
77 $keys = "static const char *int_keys[] = {\n";
78 $values = "static int64 int_values[] = {\n";
79 foreach ($data as $k => $v) {
81 $keys .= $this->s($k).',';
82 if ($v >= (1 << 32)) {
94 $keys .= "\nNULL};\n";
101 private function serializeStrings(&$out, &$data) {
103 $kvs = "static const char *strings[] = {\n";
104 foreach ($data as $k => $v) {
106 $kvs .= $this->s($k).','.$this->s($v).',';
107 if (++
$i %
10 == 0) {
113 $kvs .= "\nNULL};\n";
117 private function serializeObjects(&$out, &$data) {
119 $kvs = "static const char *objects[] = {\n";
120 foreach ($data as $k => $v) {
122 $kvs .= $this->s($k).','.$this->s(serialize($v)).',';
123 if (++
$i %
10 == 0) {
129 $kvs .= "\nNULL};\n";
133 private function serializeThrifts(&$out, &$data) {
135 $kvs = "static const char *thrifts[] = {\n";
136 foreach ($data as $k => $v) {
137 $sv = fb_serialize($v);
139 $kvs .= $this->s($k).','.$this->s($sv).',';
140 if (++
$i %
10 == 0) {
146 $kvs .= "\nNULL};\n";
150 private function serializeOthers(&$out, &$data) {
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) {
159 $kvs .= "\nNULL};\n";