Example #1
0
func (d *Database) iterKey(callback func([]byte) bool, key C.datum) (next C.datum) {
	defer C.free(unsafe.Pointer(key.dptr))
	bytes := C.GoBytes(unsafe.Pointer(key.dptr), key.dsize)
	if callback(bytes) {
		next = C.gdbm_nextkey(d.dbf, key)
	}
	return
}
Example #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
}
Example #3
0
/*
Returns the nextkey after `key`. If there is not a next key, an
NoError error will be returned.

An Iteration might look like:

  k, err := db.FirstKey()
  if err != nil {
    return err
  }
  for {
    v, err := db.Fetch(k)
    if err != nil {
      return err
    }
    fmt.Println(v)

    k, err = db.NextKey(k)
    if err == gdbm.NoError {
      break
    } else if err != nil {
      return err
    }
  }

*/
func (db *Database) NextKey(key string) (value string, err error) {
	kcs := C.CString(key)
	k := C.mk_datum(kcs)
	defer C.free(unsafe.Pointer(kcs))

	vdatum := C.gdbm_nextkey(db.dbf, k)
	if vdatum.dptr == nil {
		return "", lastError()
	}

	value = C.GoStringN(vdatum.dptr, vdatum.dsize)
	defer C.free(unsafe.Pointer(vdatum.dptr))
	return value, nil
}