示例#1
0
文件: oleg.c 项目: carriercomm/OlegDB
struct tm *ol_sniff(ol_database *db, const char *key, size_t klen) {
    char _key[KEY_SIZE] = {'\0'};
    size_t _klen = 0;
    ol_bucket *bucket = ol_get_bucket(db, key, klen, &_key, &_klen);
    check_warn(_klen > 0, "Key length of zero not allowed.");

    if (bucket != NULL && bucket->expiration != NULL) {
        if (!_has_bucket_expired(bucket)) {
            return bucket->expiration;
        } else {
            /* It's dead, get rid of it. */
            check(ol_scoop(db, key, klen) == 0, "Could not delete a bucket!")
        }
    }

error:
    return NULL;
}
示例#2
0
int olt_unjar(ol_transaction *tx, const char *key, size_t klen, unsigned char **data, size_t *dsize) {
    char _key[KEY_SIZE] = {'\0'};
    size_t _klen = 0;
    ol_database *operating_db = NULL;
    ol_bucket *bucket = ol_get_bucket(tx->transaction_db, key, klen, &_key, &_klen);
    check(_klen > 0, "Key length of zero not allowed.");

    /* Fall through to the parent db: */
    if (bucket == NULL) {
        bucket = ol_get_bucket(tx->parent_db, key, klen, &_key, &_klen);
        /* This is getting messy... */
        if (bucket != NULL)
            operating_db = tx->parent_db;
    } else {
        operating_db = tx->transaction_db;
    }

    if (bucket != NULL) {
        if (!_has_bucket_expired(bucket)) {
            /* We don't need to fill out the data so just return 'we found the key'. */
            if (data == NULL)
                return OL_SUCCESS;

            const int ret = _ol_get_value_from_bucket(operating_db, bucket, data, dsize);
            check(ret == 0, "Could not retrieve value from bucket.");

            /* Key found, tell somebody. */
            return OL_SUCCESS;
        } else {
            /* It's dead, get rid of it. */
            /* NOTE: We explicitly say the transaction_db here because ITS A
             * F*****G TRANSACTION. ACID, bro. */
            check(olt_scoop(tx, key, klen) == 0, "Scoop failed");
        }
    }

    /* TODO: Set error code here (could not find key) */
    return OL_FAILURE;

error:
    /* TODO: Set error code here (generic error? Theres a couple failure modes here.)*/
    return OL_FAILURE;
}
示例#3
0
文件: oleg.c 项目: carriercomm/OlegDB
int ol_squish(ol_database *db) {
    check(db != NULL, "Cannot squish null database.");
    int fflush_turned_off = 0;
    const int flags = db->feature_set;
    if (db->is_enabled(OL_F_APPENDONLY, &flags)) {
        /* Turn off fflush for the time being. We'll do it once at the end. */
        if (db->is_enabled(OL_F_AOL_FFLUSH, &db->feature_set)) {
            db->disable(OL_F_AOL_FFLUSH, &db->feature_set);
            fflush_turned_off = 1;
        }

        /* AOL is enabled. Create a new aol file that we'll be using. */
        fflush(db->aolfd);
        fclose(db->aolfd);

        /* Create a new file which we'll move into the old ones place later */
        db->get_db_file_name(db, "aol.new", db->aol_file);

        /* Get a new file descriptor */
        db->aolfd = fopen(db->aol_file, AOL_FILEMODE);
    }

    /* Iterate through the hash table instead of using the tree just
     * so you can use this in case the tree isn't enabled. */
    const unsigned int iterations = ol_ht_bucket_max(db->cur_ht_size);

    int i = 0;
    for (; i < iterations; i++) {
        if (db->hashes[i] != NULL) {
            /* Found a bucket. */
            ol_bucket *ptr, *next;
            /* Start traversing the linked list of collisions, starting with
             * the bucket we found. */
            for (ptr = db->hashes[i]; NULL != ptr; ptr = next) {
                if (!_has_bucket_expired(ptr)) {
                    /* Bucket hasn't been deleted or expired. */
                    if (db->is_enabled(OL_F_APPENDONLY, &db->feature_set)) {
                        /* AOL is enabled. Write it to the new AOL file. */
                        ol_aol_write_cmd(db, "JAR", ptr);

                        /* See if theres an expiration date we care about: */
                        if (ptr->expiration != NULL) {
                            ol_aol_write_cmd(db, "SPOIL", ptr);
                        }
                    }
                }
                /* Get the next bucket in the collision chain. */
                next = ptr->next;
            }
        }
    }

    if (db->is_enabled(OL_F_APPENDONLY, &db->feature_set)) {
        /* Turn off fflush for the time being. We'll do it once at the end. */
        if (fflush_turned_off) {
            db->enable(OL_F_AOL_FFLUSH, &db->feature_set);
        }
        /* Make sure all of the new stuff is written */
        fflush(db->aolfd);
        fclose(db->aolfd);

        char new_filename[AOL_FILENAME_ALLOC] = {0};
        /* Set the old filename. */
        db->get_db_file_name(db, "aol.new", new_filename);
        db->get_db_file_name(db, AOL_FILENAME, db->aol_file);
        /* Rename the .aol.new file to just be .aol */
        check(rename(new_filename, db->aol_file) == 0, "Could not rename new AOL to old AOL.");

        /* Get a new file descriptor */
        db->aolfd = fopen(db->aol_file, AOL_FILEMODE);
    }

    return OL_SUCCESS;

error:
    return OL_FAILURE;
}