Exemplo n.º 1
0
static
bool KMetadataVersEqual ( const KMetadata *meta, const char *name, const char *vers )
{
    bool equal = false;
    const KMDataNode *node;
    rc_t rc = KMetadataOpenNodeRead ( meta, & node, "SOFTWARE/loader" );
    if ( rc == 0 )
    {
        size_t num_read;
        char attr [ 256 ];
        rc = KMDataNodeReadAttr ( node, "name", attr, sizeof attr, & num_read );
        if ( rc == 0 )
        {
            if ( memcmp ( attr, name, strlen ( name ) ) == 0 )
            {
                if ( vers == NULL || vers [ 0 ] == 0 )
                    equal = true;
                else
                {
                    rc = KMDataNodeReadAttr ( node, "vers", attr, sizeof attr, & num_read );
                    if ( rc == 0 )
                    {
                        if ( memcmp ( attr, vers, strlen ( vers ) ) == 0 )
                            equal = true;
                    }
                }
            }
        }

        KMDataNodeRelease ( node );
    }

    return equal;
}
Exemplo n.º 2
0
static rc_t copy_metadata_attribs ( const KMDataNode *snode, KMDataNode *dnode,
                                    const char *node_path, const bool show_meta )
{
    KNamelist *attrs;
    uint32_t i, count;
    rc_t rc = KMDataNodeListAttr ( snode, & attrs );
    DISP_RC( rc, "copy_metadata_child:KMDataNodeListAttr(src) failed" );
    if ( rc != 0 ) return rc;
    rc = KNamelistCount ( attrs, & count );
    for ( i = 0; rc == 0 && i < count; ++ i )
    {
        const char *attr;
        rc = KNamelistGet ( attrs, i, & attr );
        if ( rc == 0 )
        {
            char buffer [ 1024 ];
            size_t bytes;
            /* test for attr existence */
            rc = KMDataNodeReadAttr ( dnode, attr, buffer, sizeof buffer, & bytes );
            if ( rc != 0 )
            {
                rc = KMDataNodeReadAttr ( snode, attr, buffer, sizeof buffer, & bytes );
                if ( rc == 0 )
                {
                    if ( show_meta )
                        KOutMsg( "copy atr %s : %s\n", node_path, attr );
                    rc = KMDataNodeWriteAttr ( dnode, attr, buffer );
                }
            }
        }
        DISP_RC( rc, "copy_metadata_child:failed to copy attribute" );
    }
    KNamelistRelease ( attrs );
    return rc;
}
Exemplo n.º 3
0
static
rc_t CC VPhysicalLoadSchema ( VPhysical *self,
    VTypedecl *td, VSchema *schema, const KMDataNode *node )
{
    rc_t rc;

    KMDataNodeSchemaFillData pb;
    pb . node = node;
    pb . pos = 0;
    pb . add_v0 = false;

    /* add stored declaration to cursor schema */
    rc = VSchemaParseTextCallback ( schema,
        "VPhysicalLoadSchema", KMDataNodeFillSchema, & pb );
    if ( rc == 0 )
    {
        /* retrieve fully-resolved type attribute */
        rc = KMDataNodeReadAttr ( node, "type",
            pb . buff, sizeof pb . buff, & pb . pos );
        if ( rc == 0 )
            rc = VSchemaResolveTypedecl ( schema, td, pb . buff );
        if ( rc == 0 )
        {
            /* get encoding expression */
            rc = KMDataNodeReadAttr ( node, "expr",
                pb . buff, sizeof pb . buff, & pb . pos );
            if ( rc == 0 )
            {
                VTypedecl etd;

                /* create a new expression object */
                rc = VSchemaImplicitPhysEncExpr ( schema, & etd,
                    & self -> enc, pb . buff, "VPhysicalLoadSchema" );
                if ( rc != 0 )
                {
                    PLOGERR ( klogInt, ( klogInt, rc, "failed to establish column type from '$(expr)'",
                               "expr=%s", pb . buff ));
                }

                /* match SPhysical type against stated type */
                else if ( ! VTypedeclToTypedecl ( & etd, schema, td, NULL, NULL ) )
                {
                    rc = RC ( rcVDB, rcColumn, rcLoading, rcType, rcInconsistent );
                    PLOGERR ( klogInt, ( klogInt, rc, "inconsistent metadata for column '$(name)'"
                               , "name=%.*s"
                               , ( int ) self -> smbr -> name -> name . size
                               , self -> smbr -> name -> name . addr ));
                }
            }
            else if ( GetRCState ( rc ) == rcNotFound )
            {
                rc = 0;
            }
        }
    }

    KMDataNodeRelease ( node );
    return rc;
}
Exemplo n.º 4
0
static void get_meta_info( vdb_info_data * data, const KMetadata * meta )
{
    const KMDataNode * node;
    rc_t rc = KMetadataOpenNodeRead ( meta, &node, "schema" );
    if ( rc == 0 )
    {
        size_t size;
        rc = KMDataNodeReadAttr ( node, "name", data->schema_name, sizeof data->schema_name, &size );
        if ( rc == 0 )
            data->schema_name[ size ] = 0;
        KMDataNodeRelease ( node );
    }

    rc = KMetadataOpenNodeRead ( meta, &node, "LOAD/timestamp" );
    if ( rc == 0 )
    {
        rc = KMDataNodeReadAsU64 ( node, &data->ts.timestamp );
        if ( rc == 0 )
        {
            KTime time_rec;
            KTimeLocal ( &time_rec, data->ts.timestamp );
            data->ts.year  = time_rec.year;
            data->ts.month = time_rec.month + 1;
            data->ts.day   = time_rec.day + 1;
            data->ts.hour  = time_rec.hour;
            data->ts.minute= time_rec.minute;
        }
        KMDataNodeRelease ( node );
    }

    get_meta_event( meta, "SOFTWARE/formatter", &data->formatter );
    get_meta_event( meta, "SOFTWARE/loader", &data->loader );
    get_meta_event( meta, "SOFTWARE/update", &data->update );
}
Exemplo n.º 5
0
static void get_meta_attr( const KMDataNode * node, const char * key, char * dst, size_t dst_size )
{
    size_t size;
    rc_t rc = KMDataNodeReadAttr ( node, key, dst, dst_size, &size );
    if ( rc == 0 )
        dst[ size ] = 0;
}
Exemplo n.º 6
0
rc_t VDatabaseLoadSchema ( VDatabase *self )
{
    /* try to get schema text */
    const KMDataNode *node;
    rc_t rc = KMetadataOpenNodeRead ( self -> meta, & node, "schema" );
    if ( rc == 0 )
    {
        /* the node is probably within our 4K buffer,
           but by using the callback mechanism we don't
           have buffer or allocation issues. */
        KMDataNodeSchemaFillData pb;
        pb . node = node;
        pb . pos = 0;
        pb . add_v0 = false;

        /* add in schema text. it is not mandatory, but it is
           the design of the system to store object schema with
           the object so that it is capable of standing alone */
        rc = VSchemaParseTextCallback ( self -> schema,
            "VDatabaseLoadSchema", KMDataNodeFillSchema, & pb );
        if ( rc == 0 )
        {
            /* determine database type */
            rc = KMDataNodeReadAttr ( node, "name",
                pb . buff, sizeof pb . buff, & pb . pos );
            if ( rc == 0 )
            {
                uint32_t type;
                const SNameOverload *name;

                /* find the sdb if possible */
                self -> sdb = VSchemaFind ( self -> schema,
                    & name, & type, pb . buff, "VDatabaseLoadSchema", false );

                /* the schema must be found in this case */
                if ( self -> sdb == NULL || type != eDatabase )
                {
                    self -> sdb = NULL;
                    rc = RC ( rcVDB, rcDatabase, rcLoading, rcSchema, rcCorrupt );
                    PLOGERR ( klogInt, ( klogInt, rc, "failed to establish database type from '$(expr)'",
                                         "expr=%s", pb . buff ));
                }
            }
        }

        KMDataNodeRelease ( node );
    }
    else if ( GetRCState ( rc ) == rcNotFound )
    {
        /* the database may be under creation */
        if ( ! self -> read_only )
            rc = 0;
    }

    return rc;
}
Exemplo n.º 7
0
static
rc_t VTableLoadSchemaNode ( VTable *self, const KMDataNode *node )
{
    rc_t rc;
    
    /* the node is probably within our 4K buffer,
     but by using the callback mechanism we don't
     have buffer or allocation issues. */
    KMDataNodeSchemaFillData pb;
    pb . node = node;
    pb . pos = 0;
    pb . add_v0 = false;
    
    /* add in schema text. it is not mandatory, but it is
     the design of the system to store object schema with
     the object so that it is capable of standing alone */
    rc = VSchemaParseTextCallback ( self -> schema,
        "VTableLoadSchema", KMDataNodeFillSchema, & pb );
    if ( rc == 0 )
    {
        /* determine table type */
        rc = KMDataNodeReadAttr ( node, "name",
            pb . buff, sizeof pb . buff, & pb . pos );
        if ( rc == 0 )
        {
            uint32_t type;
            const SNameOverload *name;
            
            /* find the stbl if possible */
            self -> stbl = VSchemaFind ( self -> schema,
                & name, & type, pb . buff, "VTableLoadSchema", false );
            
            /* the schema must be found in this case */
            if ( self -> stbl == NULL || type != eTable )
            {
                self -> stbl = NULL;
                rc = RC ( rcVDB, rcTable, rcLoading, rcSchema, rcCorrupt );
                PLOGERR ( klogInt, ( klogInt, rc, "failed to establish table type from '$(expr)'",
                                     "expr=%s", pb . buff ));
            }
        }
    }
    return rc;
}
Exemplo n.º 8
0
static
bool KColumnTypeEqual ( const KTable *tbl, const char *col, const char *type )
{
    /* TBD - this operation is expensive
       should be addressed either by caching opened columns on table
       or by introducing a path to open column metadata from table */
    const KColumn *kcol;
    rc_t rc = KTableOpenColumnRead ( tbl, & kcol, "%s", col );
    if ( rc == 0 )
    {
        const KMetadata *meta;
        rc = KColumnOpenMetadataRead ( kcol, & meta );
        KColumnRelease ( kcol );
        if ( rc == 0 )
        {
            /* this is a expected to be a v1 column
               open its decoding node */
            const KMDataNode *node;
            rc = KMetadataOpenNodeRead ( meta, & node, "decoding" );
            KMetadataRelease ( meta );
            if ( rc == 0 )
            {
                /* read its type */
                size_t size;
                char type_expr [ 256 ];
                rc = KMDataNodeReadAttr ( node, "type",
                    type_expr, sizeof type_expr, & size );
                KMDataNodeRelease ( node );
                if ( rc == 0 )
                {
                    if ( memcmp ( type_expr, type, strlen ( type ) ) == 0 )
                        return true;
                }
            }
        }
    }
    return false;
}
Exemplo n.º 9
0
static rc_t get_schema_info(KMetadata const *const meta,
                            unsigned const bsz, char buffer[])
{
    KMDataNode const *node;
    rc_t rc = KMetadataOpenNodeRead(meta, &node, "schema");
    
    if (rc == 0) {
        size_t sz;
        
        rc = KMDataNodeReadAttr(node, "name", buffer, bsz - 1, &sz);
        KMDataNodeRelease(node);
        if (rc == 0) {
            buffer[sz] = '\0';
            while (sz) {
                --sz;
                if (buffer[sz] == '#') {
                    buffer[sz] = '\0';
                    break;
                }
            }
        }
    }
    return rc;
}
Exemplo n.º 10
0
static int AccessionType(VDBManager const *const mgr,
                         unsigned const N, char const accession[],
                         rc_t *const rc)
{
    char scheme[1024];
    bool isOdd = false;

    scheme[0] = '\0';
    {
        KMetadata const *meta = NULL;
        {
            VDatabase const *db = NULL;

            *rc = VDBManagerOpenDBRead(mgr, &db, NULL, "%.*s", (int)N, accession);
            if (db) {
                *rc = VDatabaseOpenMetadataRead(db, &meta);
                VDatabaseRelease(db);
            }
            else {
                VTable const *tbl = NULL;

                *rc = VDBManagerOpenTableRead(mgr, &tbl, NULL, "%.*s", (int)N, accession);
                if (tbl) {
                    *rc = VTableOpenMetadataRead(tbl, &meta);
                    VTableRelease(tbl);
                }
                else {
                    isOdd = true;
                    *rc = VDBManagerOpenTableRead(mgr, &tbl, NULL, "ncbi-acc:%.*s?vdb-ctx=refseq", (int)N, accession);
                    if (tbl) {
                        *rc = VTableOpenMetadataRead(tbl, &meta);
                        VTableRelease(tbl);
                    }
                }
            }
        }
        if (meta) {
            KMDataNode const *node = NULL;

            *rc = KMetadataOpenNodeRead(meta, &node, "schema");
            KMetadataRelease(meta);
            if (node) {
                size_t sz = 0;

                *rc = KMDataNodeReadAttr(node, "name", scheme, sizeof(scheme) - 1, &sz);
                KMDataNodeRelease(node);
                scheme[sz] = '\0';
                while (sz) {
                    --sz;
                    if (scheme[sz] == '#') {
                        scheme[sz] = '\0';
                        break;
                    }
                }
            }
        }
    }
    if (strcmp(scheme, "NCBI:WGS:db:contig") == 0)
        return refSeqType_WGS;
    if (strcmp(scheme, "NCBI:refseq:tbl:reference") == 0)
        return isOdd ? refSeqType_RefSeq_odd : refSeqType_RefSeq;
    return 0;
}
Exemplo n.º 11
0
 /* ReadAttr
  *  reads as NUL-terminated string
  *
  *  "name" [ IN ] - NUL terminated attribute name
  *
  *  "buffer" [ OUT ] and "bsize" - return parameter for attribute value
  *
  *  "size" [ OUT ] - return parameter giving size of string
  *  not including NUL byte. the size is set both upon success
  *  and insufficient buffer space error.
  */
  inline rc_t ReadAttr ( const char *name, char *buffer, 
     size_t bsize, size_t *size ) const throw()
  { return KMDataNodeReadAttr ( this, name, buffer, bsize, size ); }
Exemplo n.º 12
0
/* LoadSchema
 *  looks in metadata for stored schema
 */
static
rc_t CC VPhysicalLoadV1Schema ( VPhysical *self,
    VTypedecl *td, VSchema *schema, const KMDataNode *node )
{
    rc_t rc;

    KMDataNodeSchemaFillData pb;
    pb . node = node;
    pb . pos = 0;
    pb . add_v0 = true;

    /* add stored declaration to cursor schema */
    rc = VSchemaParseTextCallback ( schema,
        "VPhysicalLoadV1Schema", KMDataNodeFillSchema, & pb );
   if ( rc == 0 )
    {
        size_t size;
        char type_expr [ 256 ];

        /* retrieve and resolve "type" attribute */
        rc = KMDataNodeReadAttr ( node, "type",
            type_expr, sizeof type_expr, & size );
        if ( rc == 0 )
            rc = VSchemaResolveTypedecl ( schema, td, type_expr );

        /* if a decoding schema exists */
        if ( rc == 0 && pb . pos != 0 )
        {
            char sphysical_name [ 512 ];

            /* preserve schema function expression */
            size_t decoding_xsize;
            char decoding_expr [ 256 ];
            rc = KMDataNodeReadAttr ( node, "schema",
                decoding_expr, sizeof decoding_expr, & decoding_xsize );
            if ( rc == 0 )
            {
                /* look for "encoding" */
                const KMDataNode *enc;
                rc = KMetadataOpenNodeRead ( self -> meta, & enc, "encoding" );
                if ( rc == 0 )
                {
#if ALLOW_V1_UPDATE
                    if ( ! self -> read_only )
                    {
                        /* add stored declaration to cursor schema */
                        pb . node = enc;
                        pb . pos = 0;
                        pb . add_v0 = true;
                        rc = VSchemaParseTextCallback ( schema,
                            "VPhysicalLoadV1Schema", KMDataNodeFillSchema, & pb );
                    }
                    if ( rc == 0 )
#endif
                    {
                        /* preserve schema function expression */
                        size_t encoding_xsize;
                        char encoding_expr [ 256 ], enc_type [ 256 ];
                        rc = KMDataNodeReadAttr ( enc, "schema",
                            encoding_expr, sizeof encoding_expr, & encoding_xsize );
                        if ( rc == 0 )
                        {
                            rc = KMDataNodeReadAttr ( enc, "type",
                                enc_type, sizeof enc_type, & size );
                        }
                        if ( rc == 0 )
                        {
#if ALLOW_V1_UPDATE
                            if ( self -> read_only )
                            {
#endif
                                /* build sphysical name */
                                sprintf ( sphysical_name, "%s_only", decoding_expr );

                                /* build physical decl */
                                pb . pos = sprintf ( pb . buff, "version 1;"
                                                     "physical %s %s:phys#1"
                                                     "{decode{%s k=@;return %s(k);}}"
                                                     , type_expr
                                                     , sphysical_name
                                                     , enc_type
                                                     , decoding_expr
                                    );
#if ALLOW_V1_UPDATE
                            }
                            else
                            {
                                /* strip off common namespace */
                                size_t i, ns_size;
                                string_match ( decoding_expr, decoding_xsize,
                                    encoding_expr, encoding_xsize, -1, & ns_size );
                                if ( ns_size != 0 )
                                {
                                    char *p = string_rchr ( decoding_expr, ns_size, ':' );
                                    ns_size = ( p == NULL ) ? 0U : ( uint32_t ) ( p - decoding_expr ) + 1U;
                                }

                                /* build sphysical name */
                                sprintf ( sphysical_name, "%s_%s", decoding_expr, & encoding_expr [ ns_size ] );
                                for ( i = ns_size; sphysical_name [ i ] != 0; ++ i )
                                {
                                    if ( sphysical_name [ i ] == ':' )
                                        sphysical_name [ i ] = '_';
                                }

                                /* build physical decl */
                                pb . pos = sprintf ( pb . buff, "version 1;"
                                                     "physical %s %s:phys#1"
                                                     "{encode{return %s(@);}"
                                                     "decode{%s k=@;return %s(k);}}"
                                                     , type_expr
                                                     , sphysical_name
                                                     , encoding_expr
                                                     , enc_type
                                                     , decoding_expr
                                    );
                            }
#endif
                        }
                    }

                    KMDataNodeRelease ( enc );
                }
                else if ( GetRCState ( rc ) == rcNotFound )
                {
                    /* build sphysical name */
                    sprintf ( sphysical_name, "%s_only", decoding_expr );

                    /* build decode-only physical decl */
                    pb . pos = sprintf ( pb . buff, "version 1;"
                                         "physical %s %s:phys#1"
                                         "{decode{opaque k=@;return %s(k);}}"
                                         , type_expr
                                         , sphysical_name
                                         , decoding_expr
                        );
                    rc = 0;
                }
                if ( rc == 0 )
                {
                    /* parse synthesized schema into cursor VSchema */
                    rc = VSchemaParseText ( schema,
                        "VPhysicalLoadV1Schema", pb . buff, pb . pos );
                    if ( rc == 0 )
                    {
                        VTypedecl etd;

                        /* create a new expression object */
                        sprintf ( pb . buff, "%s:phys#1", sphysical_name );
                        rc = VSchemaImplicitPhysEncExpr ( schema, & etd,
                            & self -> enc, pb . buff, "VPhysicalLoadV1Schema" );
                        if ( rc != 0 )
                        {
                            PLOGERR ( klogInt, ( klogInt, rc, "failed to establish column type from '$(expr)'",
                                       "expr=%s", pb . buff ));
                        }
                        else if ( self -> smbr != NULL && self -> smbr -> type == NULL )
                        {
                            /* back-patch schema */
                            ( ( SPhysMember* ) self -> smbr ) -> type = self -> enc;
                            atomic32_inc ( & ( ( SExpression* ) self -> enc ) -> refcount );
                        }
                    }
                }
            }
        }
    }

    KMDataNodeRelease ( node );
    return rc;
}