Esempio n. 1
0
static size_t
__writefunc(void *buffer, size_t size, size_t nmemb, void *ptr)
{
    curl_buffer_t *buf=(curl_buffer_t *)ptr;
    char *cbuf=(char *)buffer;
    ham_size_t payload_size=0;

    if (buf->offset==0) {
        if (*(ham_u32_t *)&cbuf[0]!=ham_db2h32(HAM_TRANSFER_MAGIC_V1)) {
            ham_trace(("invalid protocol version"));
            return (0);
        }
        payload_size=ham_h2db32(*(ham_u32_t *)&cbuf[4]);

        /* did we receive the whole data in this packet? */
        if (payload_size+8==size*nmemb) {
            buf->wrapper=proto_unpack((ham_size_t)(size*nmemb), 
                        (ham_u8_t *)&cbuf[0]);
            if (!buf->wrapper)
                return (0);
            return (size*nmemb);
        }

        /* otherwise we have to buffer the received data */
        buf->packed_size=payload_size+8;
        buf->packed_data=allocator_alloc(buf->alloc, buf->packed_size);
        if (!buf->packed_data)
            return (0);
        memcpy(buf->packed_data, &cbuf[0], size*nmemb);
        buf->offset+=(ham_size_t)(size*nmemb);
    }
    /* append to an existing buffer? */
    else {
        memcpy(buf->packed_data+buf->offset, &cbuf[0], size*nmemb);
        buf->offset+=(ham_size_t)(size*nmemb);
    }

    /* check if we've received the whole data */
    if (buf->offset==buf->packed_size) {
        buf->wrapper=proto_unpack(buf->packed_size, buf->packed_data);
        if (!buf->wrapper)
            return (0);
        allocator_free(buf->alloc, buf->packed_data);
        if (!buf->wrapper)
            return 0;
    }

    return (size*nmemb);
}
Esempio n. 2
0
bool 
engine::find(const char *keytok)
{
    ham_u32_t numkey=0;
    ham_key_t key;
    ham_record_t rec[2];
    ham_status_t st[2];

    VERBOSE(("find: key: %s\n", keytok));

    memset(&key, 0, sizeof(key));
    memset(&rec[0], 0, sizeof(rec[0]));
    memset(&rec[1], 0, sizeof(rec[1]));

    /*
     * check flag NUMERIC_KEY
     */
    if (m_config->numeric) {
        numkey=strtoul(keytok, 0, 0);
        if (!numkey) {
            TRACE(("line %d: key is invalid\n", m_parser->get_lineno()));
            return (false);
        }
        numkey=ham_h2db32(numkey);
        key.data=(void *)&numkey;
        key.size=sizeof(numkey);
    }
    else {
        key.data=(void *)keytok;
        key.size=(ham_size_t)strlen(keytok);
    }

    for (int i=0; i<2; i++) {
        st[i]=m_db[i]->find(&key, &rec[i]);
        if (st[i])
            VERBOSE(("db[%d]: find failed w/ status %d\n", i, st[i]));
    }

    if (!compare_records(&rec[0], &rec[1])) {
        TRACE(("record mismatch\n"));
        return false;
    }

    if (!compare_status(st))
        return (false);
    if (m_config->txn_group>0)
        return (inc_opcount());
    return (true);
}
Esempio n. 3
0
bool 
engine::insert(const char *keytok, const char *data)
{
    ham_u32_t numkey=0;
    ham_size_t data_size;
    ham_key_t key;
    ham_record_t rec;
    ham_status_t st[2];

    VERBOSE(("insert: key: %s, data: %s\n", keytok, data));

    memset(&key, 0, sizeof(key));
    memset(&rec, 0, sizeof(rec));

    /*
     * check flag NUMERIC_KEY
     */
    if (m_config->numeric) {
        numkey=strtoul(keytok, 0, 0);
        if (!numkey) {
            TRACE(("line %d: key is invalid\n", m_parser->get_lineno()));
            return (false);
        }
        numkey=ham_h2db32(numkey);
        key.data=(void *)&numkey;
        key.size=sizeof(numkey);
    }
    else {
        key.data=(void *)keytok;
        key.size=(ham_size_t)strlen(keytok)+1;
    }

    /*
     * allocate and initialize data 
     */
    data_size=strtoul(data, 0, 0);
    if (data_size) {
        if (data_size>m_config->data_size) {
            m_config->data_size=data_size;
            m_config->data_ptr=realloc(m_config->data_ptr, data_size);
            if (!m_config->data_ptr) {
                TRACE(("line %d: out of memory\n", m_parser->get_lineno()));
                return 0;
            }
        }
        /* always start with a random number - otherwise berkeleydb fails
         * too often when duplicate keys are inserted with duplicate
         * records */
        for (ham_size_t i=0; i<data_size; i++)
            ((char *)m_config->data_ptr)[i]=(m_parser->get_lineno()+i)&0xff;
        if (data_size>=sizeof(unsigned))
            *(unsigned *)m_config->data_ptr=m_parser->get_lineno();

        rec.data=m_config->data_ptr;
        rec.size=data_size;
    }

    for (int i=0; i<2; i++) {
        st[i]=m_db[i]->insert(&key, &rec);
        if (st[i])
            VERBOSE(("db[%d]: insert failed w/ status %d\n", i, st[i]));
    }

    if (!compare_status(st))
        return (false);
    if (m_config->txn_group>0)
        return (inc_opcount());
    return (true);
}