Obj* Hash_load(Hash *self, Obj *dump) { Hash *source = (Hash*)CERTIFY(dump, HASH); CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6); UNUSED_VAR(self); // Assume that the presence of the "_class" key paired with a valid class // name indicates the output of a Dump rather than an ordinary Hash. */ if (class_name && CB_Is_A(class_name, CHARBUF)) { VTable *vtable = VTable_fetch_vtable(class_name); if (!vtable) { CharBuf *parent_class = VTable_find_parent_class(class_name); if (parent_class) { VTable *parent = VTable_singleton(parent_class, NULL); vtable = VTable_singleton(class_name, parent); DECREF(parent_class); } else { // TODO: Fix Hash_Load() so that it works with ordinary hash // keys named "_class". THROW(ERR, "Can't find class '%o'", class_name); } } // Dispatch to an alternate Load() method. if (vtable) { Obj_Load_t load = METHOD_PTR(vtable, Lucy_Obj_Load); if (load == Obj_load) { THROW(ERR, "Abstract method Load() not defined for %o", VTable_Get_Name(vtable)); } else if (load != (Obj_Load_t)Hash_load) { // stop inf loop return VTable_Load_Obj(vtable, dump); } } } // It's an ordinary Hash. Hash *loaded = Hash_new(source->size); Obj *key; Obj *value; Hash_Iterate(source); while (Hash_Next(source, &key, &value)) { Hash_Store(loaded, key, Obj_Load(value, value)); } return (Obj*)loaded; }
StringType* StringType_load(StringType *self, Obj *dump) { Hash *source = (Hash*)CERTIFY(dump, HASH); CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6); VTable *vtable = (class_name != NULL && Obj_Is_A((Obj*)class_name, CHARBUF)) ? VTable_singleton(class_name, NULL) : STRINGTYPE; StringType *loaded = (StringType*)VTable_Make_Obj(vtable); Obj *boost_dump = Hash_Fetch_Str(source, "boost", 5); Obj *indexed_dump = Hash_Fetch_Str(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Str(source, "stored", 6); Obj *sortable_dump = Hash_Fetch_Str(source, "sortable", 8); UNUSED_VAR(self); StringType_init(loaded); if (boost_dump) { loaded->boost = (float)Obj_To_F64(boost_dump); } if (indexed_dump) { loaded->indexed = (bool_t)Obj_To_I64(indexed_dump); } if (stored_dump) { loaded->stored = (bool_t)Obj_To_I64(stored_dump); } if (sortable_dump) { loaded->sortable = (bool_t)Obj_To_I64(sortable_dump); } return loaded; }
BlobType* BlobType_Load_IMP(BlobType *self, Obj *dump) { Hash *source = (Hash*)CERTIFY(dump, HASH); String *class_name = (String*)Hash_Fetch_Utf8(source, "_class", 6); VTable *vtable = (class_name != NULL && Obj_Is_A((Obj*)class_name, STRING)) ? VTable_singleton(class_name, NULL) : BLOBTYPE; BlobType *loaded = (BlobType*)VTable_Make_Obj(vtable); Obj *boost_dump = Hash_Fetch_Utf8(source, "boost", 5); Obj *indexed_dump = Hash_Fetch_Utf8(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Utf8(source, "stored", 6); UNUSED_VAR(self); BlobType_init(loaded, false); BlobTypeIVARS *const loaded_ivars = BlobType_IVARS(loaded); if (boost_dump) { loaded_ivars->boost = (float)Obj_To_F64(boost_dump); } if (indexed_dump) { loaded_ivars->indexed = Obj_To_Bool(indexed_dump); } if (stored_dump){ loaded_ivars->stored = Obj_To_Bool(stored_dump); } return loaded; }
Obj* Freezer_thaw(InStream *instream) { CharBuf *class_name = CB_Deserialize((CharBuf*)VTable_Make_Obj(CHARBUF), instream); VTable *vtable = VTable_singleton(class_name, NULL); Obj *blank = VTable_Make_Obj(vtable); DECREF(class_name); return Obj_Deserialize(blank, instream); }
static Obj* S_new_testobj() { ZombieCharBuf *klass = ZCB_WRAP_STR("TestObj", 7); Obj *obj; VTable *vtable = VTable_fetch_vtable((CharBuf*)klass); if (!vtable) { vtable = VTable_singleton((CharBuf*)klass, OBJ); } obj = VTable_Make_Obj(vtable); return Obj_init(obj); }
Obj* Query_Load_IMP(Query *self, Obj *dump) { CHY_UNUSED_VAR(self); Hash *source = (Hash*)CERTIFY(dump, HASH); String *class_name = (String*)CERTIFY(Hash_Fetch_Utf8(source, "_class", 6), STRING); VTable *vtable = VTable_singleton(class_name, NULL); Query *loaded = (Query*)VTable_Make_Obj(vtable); Obj *boost = CERTIFY(Hash_Fetch_Utf8(source, "boost", 5), OBJ); Query_IVARS(loaded)->boost = (float)Obj_To_F64(boost); return (Obj*)loaded; }
FullTextType* FullTextType_load(FullTextType *self, Obj *dump) { UNUSED_VAR(self); Hash *source = (Hash*)CERTIFY(dump, HASH); CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6); VTable *vtable = (class_name != NULL && Obj_Is_A((Obj*)class_name, CHARBUF)) ? VTable_singleton(class_name, NULL) : FULLTEXTTYPE; FullTextType *loaded = (FullTextType*)VTable_Make_Obj(vtable); // Extract boost. Obj *boost_dump = Hash_Fetch_Str(source, "boost", 5); float boost = boost_dump ? (float)Obj_To_F64(boost_dump) : 1.0f; // Find boolean properties. Obj *indexed_dump = Hash_Fetch_Str(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Str(source, "stored", 6); Obj *sort_dump = Hash_Fetch_Str(source, "sortable", 8); Obj *hl_dump = Hash_Fetch_Str(source, "highlightable", 13); bool_t indexed = indexed_dump ? Obj_To_Bool(indexed_dump) : true; bool_t stored = stored_dump ? Obj_To_Bool(stored_dump) : true; bool_t sortable = sort_dump ? Obj_To_Bool(sort_dump) : false; bool_t hl = hl_dump ? Obj_To_Bool(hl_dump) : false; // Extract an Analyzer. Obj *analyzer_dump = Hash_Fetch_Str(source, "analyzer", 8); Analyzer *analyzer = NULL; if (analyzer_dump) { if (Obj_Is_A(analyzer_dump, ANALYZER)) { // Schema munged the dump and installed a shared analyzer. analyzer = (Analyzer*)INCREF(analyzer_dump); } else if (Obj_Is_A((Obj*)analyzer_dump, HASH)) { analyzer = (Analyzer*)Obj_Load(analyzer_dump, analyzer_dump); } } CERTIFY(analyzer, ANALYZER); FullTextType_init(loaded, analyzer); DECREF(analyzer); if (boost_dump) { loaded->boost = boost; } if (indexed_dump) { loaded->indexed = indexed; } if (stored_dump) { loaded->stored = stored; } if (sort_dump) { loaded->sortable = sortable; } if (hl_dump) { loaded->highlightable = hl; } return loaded; }
Obj* Obj_deserialize(Obj *self, InStream *instream) { CharBuf *class_name = CB_deserialize(NULL, instream); if (!self) { VTable *vtable = VTable_singleton(class_name, (VTable*)&OBJ); self = VTable_Make_Obj(vtable); } else { CharBuf *my_class = VTable_Get_Name(self->vtable); if (!CB_Equals(class_name, (Obj*)my_class)) THROW("Class mismatch: %o %o", class_name, my_class); } DECREF(class_name); return Obj_init(self); }
NumericType* NumType_load(NumericType *self, Obj *dump) { UNUSED_VAR(self); Hash *source = (Hash*)CERTIFY(dump, HASH); // Get a VTable CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6); CharBuf *type_spec = (CharBuf*)Hash_Fetch_Str(source, "type", 4); VTable *vtable = NULL; if (class_name != NULL && Obj_Is_A((Obj*)class_name, CHARBUF)) { vtable = VTable_singleton(class_name, NULL); } else if (type_spec != NULL && Obj_Is_A((Obj*)type_spec, CHARBUF)) { if (CB_Equals_Str(type_spec, "i32_t", 5)) { vtable = INT32TYPE; } else if (CB_Equals_Str(type_spec, "i64_t", 5)) { vtable = INT64TYPE; } else if (CB_Equals_Str(type_spec, "f32_t", 5)) { vtable = FLOAT32TYPE; } else if (CB_Equals_Str(type_spec, "f64_t", 5)) { vtable = FLOAT64TYPE; } else { THROW(ERR, "Unrecognized type string: '%o'", type_spec); } } CERTIFY(vtable, VTABLE); NumericType *loaded = (NumericType*)VTable_Make_Obj(vtable); // Extract boost. Obj *boost_dump = Hash_Fetch_Str(source, "boost", 5); float boost = boost_dump ? (float)Obj_To_F64(boost_dump) : 1.0f; // Find boolean properties. Obj *indexed_dump = Hash_Fetch_Str(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Str(source, "stored", 6); Obj *sort_dump = Hash_Fetch_Str(source, "sortable", 8); bool_t indexed = indexed_dump ? (bool_t)Obj_To_I64(indexed_dump) : true; bool_t stored = stored_dump ? (bool_t)Obj_To_I64(stored_dump) : true; bool_t sortable = sort_dump ? (bool_t)Obj_To_I64(sort_dump) : false; return NumType_init2(loaded, boost, indexed, stored, sortable); }
StringType* StringType_Load_IMP(StringType *self, Obj *dump) { Hash *source = (Hash*)CERTIFY(dump, HASH); String *class_name = (String*)Hash_Fetch_Utf8(source, "_class", 6); VTable *vtable = (class_name != NULL && Obj_Is_A((Obj*)class_name, STRING)) ? VTable_singleton(class_name, NULL) : STRINGTYPE; StringType *loaded = (StringType*)VTable_Make_Obj(vtable); Obj *boost_dump = Hash_Fetch_Utf8(source, "boost", 5); Obj *indexed_dump = Hash_Fetch_Utf8(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Utf8(source, "stored", 6); Obj *sortable_dump = Hash_Fetch_Utf8(source, "sortable", 8); UNUSED_VAR(self); float boost = boost_dump ? (float)Obj_To_F64(boost_dump) : 1.0f; bool indexed = indexed_dump ? Obj_To_Bool(indexed_dump) : true; bool stored = stored_dump ? Obj_To_Bool(stored_dump) : true; bool sortable = sortable_dump ? Obj_To_Bool(sortable_dump) : false; return StringType_init2(loaded, boost, indexed, stored, sortable); }