private void cacheTypeDescriptor(SpaceTypeDescriptor typeDescriptor) {

    if (typeDescriptor == null)
      throw new IllegalArgumentException("typeDescriptor can not be null");

    if (!types.containsKey(typeDescriptor.getTypeName())) introduceType(typeDescriptor);

    types.put(typeDescriptor.getTypeName(), new SpaceTypeDescriptorContainer(typeDescriptor));
  }
  private static SpaceDocumentMapper<DBObject> getMapper(SpaceTypeDescriptor typeDescriptor) {

    SpaceDocumentMapper<DBObject> mapper = mappingCache.get(typeDescriptor.getTypeName());
    if (mapper == null) {
      mapper = new DefaultSpaceDocumentMapper(typeDescriptor);
      mappingCache.put(typeDescriptor.getTypeName(), mapper);
    }

    return mapper;
  }
  public void introduceType(SpaceTypeDescriptor typeDescriptor) {

    DBCollection m = getConnection().getCollection(METADATA_COLLECTION_NAME);

    BasicDBObjectBuilder builder =
        BasicDBObjectBuilder.start().add(Constants.ID_PROPERTY, typeDescriptor.getTypeName());
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bos);
      IOUtils.writeObject(
          out, SpaceTypeDescriptorVersionedSerializationUtils.toSerializableForm(typeDescriptor));

      builder.add(TYPE_DESCRIPTOR_FIELD_NAME, bos.toByteArray());

      WriteResult wr = m.save(builder.get());

      if (logger.isTraceEnabled()) logger.trace(wr);

      indexBuilder.ensureIndexes(typeDescriptor);

    } catch (IOException e) {
      logger.error(e);

      throw new SpaceMongoException(
          "error occurs while serialize and save type descriptor: " + typeDescriptor, e);
    }
  }