Пример #1
0
// Returns the firstkey in this gdbm.Database.
// The traversal is ordered by gdbm‘s internal hash values, and won’t be sorted by the key values
// If there is not a key, an error will be returned in err.
func (db *Database) FirstKey() (value string, err error) {
	vdatum := C.gdbm_firstkey(db.dbf)
	if vdatum.dptr == nil {
		return "", lastError()
	}

	value = C.GoStringN(vdatum.dptr, vdatum.dsize)
	defer C.free(unsafe.Pointer(vdatum.dptr))
	return value, nil
}
Пример #2
0
func (d *gdbm) List() (keys [][]byte, err error) {
	keys = make([][]byte, 0)
	key := C.gdbm_firstkey(d.dbf)
	for key.dptr != nil {
		keys = append(keys, fromDatum(key))
		next := C.gdbm_nextkey(d.dbf, key)
		C.free(unsafe.Pointer(key.dptr))
		key = next
	}
	return
}
Пример #3
0
// Iterates through all the keys in the database. The callback will be called for
// each key. The callback should return true unless it wants to cancel the iteration.
// Keys will be traversed in an unspecified order.
func (d *Database) Iterate(callback func(key []byte) (cont bool)) {
	key := C.gdbm_firstkey(d.dbf)
	for key.dptr != nil {
		key = d.iterKey(callback, key)
	}
}