Пример #1
0
func (cursor Cursor) CursorGetRaw(flags DbFlag) ([]byte, []byte, error) {
	key := C.DBT{flags: C.DB_DBT_REALLOC}
	defer C.free(key.data)
	val := C.DBT{flags: C.DB_DBT_REALLOC}
	defer C.free(val.data)

	err := Err(C.db_cursor_get(cursor.ptr, &key, &val, C.u_int32_t(flags)))
	if err != nil {
		return nil, nil, err
	}
	return cloneToBytes(&key), cloneToBytes(&val), ok
}
Пример #2
0
// Retrieve the first record with matching key from the database. If
// exact is false, the first record with a key greater than or equal
// to the given one is fetched; this operation mode only makes sense
// in combination with a B-tree database.
func (cur Cursor) Set(rec proto.Message, exact bool) (err error) {
	var key, data C.DBT
	var flags C.u_int32_t = 0

	if exact {
		key.flags |= C.DB_DBT_READONLY
		flags |= C.DB_SET
	} else {
		key.flags |= C.DB_DBT_MALLOC
		flags |= C.DB_SET_RANGE
	}

	data.flags |= C.DB_DBT_REALLOC
	defer C.free(data.data)

	err = cur.db.marshalKey(&key, rec)
	if err == nil {
		odata := key.data
		defer func() {
			if key.data != odata {
				C.free(data.data)
			}
		}()
	} else {
		return
	}

	err = check(C.db_cursor_get(cur.ptr, &key, &data, flags))
	if err != nil {
		return
	}

	err = cur.db.unmarshalData(&data, rec)
	if err != nil {
		return
	}

	err = cur.db.unmarshalKey(&key, rec)
	return
}
Пример #3
0
// Retrieve the previous record from the cursor.
func (cur Cursor) Prev(rec proto.Message) (err error) {
	var key, data C.DBT

	key.flags |= C.DB_DBT_REALLOC
	defer C.free(key.data)
	data.flags |= C.DB_DBT_REALLOC
	defer C.free(data.data)

	err = check(C.db_cursor_get(cur.ptr, &key, &data, C.DB_PREV))
	if err != nil {
		return
	}

	err = cur.db.unmarshalData(&data, rec)
	if err != nil {
		return
	}

	err = cur.db.unmarshalKey(&key, rec)

	return
}