Example #1
0
File: tdb.go Project: ypb/gotdb
func (file DB) cFetch(key interface{}) (tdb_DTA, Error) {
	zero := tdb_DTA{nil, 0}
	var err Error
	var Key DATA
	if Key, err = NewData(key); err != nil {
		return zero, mkError(err.Errno(), "tdb.Fetch() key"+err.String())
	}
	// duplicatti code ;/
	dbg := file.db.dbg
	if dbg {
		println("tdb.Fetch()", file.String())
		println("  tdb.Fetch() key: \"" + Key.String() + "\"")
	}
	if file.db.cld {
		if dbg {
			println("  tdb.Fetch()", "db.ctx =", file.db.ctx)
		}
		return zero, mkError(ERR_IO, "tdb.Fetch() already closed")
	}
	var cK, cV tdb_DTA
	cK.dptr = (*C.uchar)(Key.Dptr)
	cK.dsize = C.size_t(Key.Dsize)
	cV = tdb_DTA(C.tdb_fetch(file.db.ctx, C.TDB_DATA(cK)))
	if cV.dptr == nil {
		// TODO: here we really need to get at the REAL errno...
		return zero, mkError(ERR_EINVAL, "tdb.Fetch() nil TDB_DATA.dptr")
	}
	// rety := cV.toString()
	// println("tdb.Fetch() rety: \"" + rety + "\"")
	// finalizer HERE? or you'll eventually get lost where what how why but or may be who knows
	return cV, nil
}
Example #2
0
File: tdb.go Project: ypb/gotdb
// Store, well, it stores. For now []byte and string, but in the future we
// should consider exploiting native Go serialization protocol like pkg/gob.
// Possible flag values are:
//
//  /* tdb_store() flags */
//  REPLACE    /* Unused */
//  INSERT     /* Don't overwrite an existing entry */
//  MODIFY     /* Don't create an existing entry    */
//
// It's important to remember that libtdb permits zero length keys and values,
// therefore one has to check the Error status and not rely on the boolean of
// DATA.String() == "".
//
func (file DB) Store(key, value interface{}, flag int) Error {
	// probably should fail out right on wrong flag type...
	var err Error
	var Key, Value DATA
	if Key, err = NewData(key); err != nil {
		return mkError(err.Errno(), "tdb.Store() key "+err.String())
	}
	if Value, err = NewData(value); err != nil {
		return mkError(err.Errno(), "tdb.Store() value "+err.String())
	}
	dbg := file.db.dbg
	if dbg {
		println("tdb.Store()", file.String())
		print("  tdb.Store() key: \"", Key.String(), "\" value: \"", Value.String(), "\" flag: ")
		switch flag {
		case INSERT:
			println("INSERT")
		case MODIFY:
			println("MODIFY")
		default:
			println(flag)
		}
	}
	// duplicatti code ;/
	if file.db.cld {
		if dbg {
			println("  tdb.Store()", "db.ctx =", file.db.ctx)
		}
		return mkError(ERR_IO, "tdb.Store() already closed")
	}
	var cK, cV tdb_DTA
	// hrmmm... so what did we need this DATA type for?!?
	// to factor type switch code into one place, i guess...
	cK.dptr = (*C.uchar)(Key.Dptr)
	cK.dsize = C.size_t(Key.Dsize)
	cV.dptr = (*C.uchar)(Value.Dptr)
	cV.dsize = C.size_t(Value.Dsize)
	var status = int(C.tdb_store(file.db.ctx, C.TDB_DATA(cK), C.TDB_DATA(cV), C.int(flag)))
	if status == SUCCESS {
		return nil
	}
	return mkError(status, "tdb.Store() SUCCESS not")
}