Пример #1
0
IoObject *IoTokyoCabinetPrefixCursor_key(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor key
	Returns current cursor key or nil.
	*/
	
	int size;
	char *ks;
	
	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	ks = tcbdbcurkey(TokyoCabinetPrefixCursor(self), &size);

	if (ks)
	{
		UArray *k = UArray_newWithData_type_size_copy_(ks, CTYPE_uint8_t, size, 1);
	
		if (UArray_beginsWith_(k, IoSeq_rawUArray(prefix)))
		{
			//printf("prefix '%s'\n", UArray_bytes(IoSeq_rawUArray(prefix)));
			//printf("before clip '%s'\n", UArray_bytes(k));
			UArray_clipBeforeEndOf_(k, IoSeq_rawUArray(prefix));
			UArray_removeFirst(k); // remove separator
			//printf("after clip  '%s'\n", UArray_bytes(k));
			return IoSeq_newWithUArray_copy_(IOSTATE, k, 0);
		}

		UArray_free(k);
	}

	return IONIL(self);
}
Пример #2
0
ex_t db_foreach(void *vhandle, db_foreach_t hook, void *userdata)
{
    int ret = 0;

    dbh_t *handle = vhandle;
    TCBDB *dbp = handle->dbp;
    BDBCUR *cursor;

    dbv_t dbv_key, dbv_data;
    int ksiz, dsiz;
    char *key, *data;

    cursor = tcbdbcurnew(dbp);
    ret = tcbdbcurfirst(cursor);
    if (ret) {
	while ((key = tcbdbcurkey(cursor, &ksiz))) {
	    data = tcbdbcurval(cursor, &dsiz);
	    if (data) {
		/* switch to "dbv_t *" variables */
		dbv_key.leng = ksiz;
		dbv_key.data = xmalloc(dbv_key.leng+1);
		memcpy(dbv_key.data, key, ksiz);
		((char *)dbv_key.data)[dbv_key.leng] = '\0';

		dbv_data.data = data;
		dbv_data.leng = dsiz;		/* read count */

		/* call user function */
		ret = hook(&dbv_key, &dbv_data, userdata);

		xfree(dbv_key.data);

		if (ret != 0)
		    break;
		free(data); /* not xfree() as allocated by dpget() */
	    }
	    free(key); /* not xfree() as allocated by dpiternext() */

	    tcbdbcurnext(cursor);
	}
    } else {
	print_error(__FILE__, __LINE__, "(tc) tcbdbcurfirst err: %d, %s",
		    tcbdbecode(dbp), tcbdberrmsg(tcbdbecode(dbp)));
	exit(EX_ERROR);
    }

    tcbdbcurdel(cursor);
    return EX_OK;
}
Пример #3
0
int IoTokyoCabinetPrefixCursor_keyBeginsWithPrefix_(IoObject *self, IoSeq *prefix)
{	
	int size;
	char *value = tcbdbcurkey(TokyoCabinetPrefixCursor(self), &size);
	
	if(value)
	{
		UArray o = UArray_stackAllocedWithData_type_size_(value, CTYPE_uint8_t, size);
		UArray *p = IoSeq_rawUArray(prefix);
		
		return UArray_beginsWith_(&o, p);
	}
	
	return 0;
}
Пример #4
0
/* key */
JNIEXPORT jbyteArray JNICALL Java_tokyocabinet_BDBCUR_key
(JNIEnv *env, jobject self){
  BDBCUR *cur = (BDBCUR *)(intptr_t)(*env)->GetLongField(env, self, bdbcur_fid_ptr);
  int ksiz;
  char *kbuf = tcbdbcurkey(cur, &ksiz);
  jbyteArray key;
  if(kbuf){
    key = (*env)->NewByteArray(env, ksiz);
    if(!key){
      throwoutmem(env);
      return NULL;
    }
    (*env)->SetByteArrayRegion(env, key, 0, ksiz, (jbyte *)kbuf);
    tcfree(kbuf);
  } else {
    key = NULL;
  }
  return key;
}
Пример #5
0
unsigned long long get_offset(unsigned long long size)
{
    unsigned long long mbytes;
    unsigned long long offset;
    BDBCUR *cur;
    unsigned long long *dbkey;
    unsigned long long *dboffset;
    int dbsize;
    bool found = 0;

    FUNC;
    mbytes = round_512(size);
    mbytes = mbytes / 512;
    offset = nextoffset;
    LDEBUG("get_offset : search for %llu blocks on the freelist", mbytes);
    cur = tcbdbcurnew(freelist);
    if (tcbdbcurjump(cur, (void *) &mbytes, sizeof(unsigned long long))) {
        if ((dbkey = tcbdbcurkey(cur, &dbsize)) != NULL) {
            if (0 == memcmp(dbkey, &mbytes, sizeof(unsigned long long))) {
                if ((dboffset = tcbdbcurval(cur, &dbsize)) == NULL)
                    die_dataerr("No value for key");
                memcpy(&offset, dboffset, sizeof(unsigned long long));
                found = 1;
                LDEBUG
                    ("get_offset : reclaim %llu blocks on the freelist at offset %llu",
                     mbytes, offset);
                if (!tcbdbcurout(cur)) {
                    die_dataerr
                        ("Failed to delete key, this should never happen!");
                }
                free(dboffset);
            }
            free(dbkey);
        }
    }
    if (!found)
        set_new_offset(size);
    tcbdbcurdel(cur);
    LDEBUG("get_offset returns = %llu", offset);
    EFUNC;
    return (offset);
}
Пример #6
0
IoObject *IoTokyoCabinetCursor_key(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetCursor key
	Returns current cursor key or nil.
	*/
	
	int size;
	char *value;

	IOASSERT(TokyoCabinetCursor(self), "invalid TokyoCabinetCursor");
	value = tcbdbcurkey(TokyoCabinetCursor(self), &size);

	if (value)
	{
		IoSeq *s = IoSeq_newWithData_length_(IOSTATE, (unsigned char *)value, size);
		free(value);
		return s;
	}

	return IONIL(self);
}