Example #1
0
func (w *Worker) bdbIncrBy(req *bdbIncrByReq) {
	table, name := bdb.SplitKey(req.key)
	db, err := w.getdb(table, bdb.DBTYPE_BTREE)
	if err != nil {
		req.resp <- bdbIncrByResp{0, err}
	} else {
		txn, err := w.dbenv.Begin(bdb.DB_READ_COMMITTED)
		if err != nil {
			req.resp <- bdbIncrByResp{0, err}
			return
		}
		defer func() {
			if txn != nil {
				txn.Abort()
			}
		}()

		_value, err := db.Get(txn, name, &w.getbuff, bdb.DB_RMW)
		var value int64 = 0
		if err != nil {
			if err != bdb.ErrNotFound {
				w.checkerr(err, db)
				req.resp <- bdbIncrByResp{0, err}
				return
			}
		} else {
			value, err = readNumber(_value)
			if err != nil {
				req.resp <- bdbIncrByResp{0, err}
				return
			}
		}
		value += req.inc
		var buf [64]byte
		err = db.Set(txn, name, strconv.AppendInt(buf[:0], value, 10), 0)
		if err != nil {
			req.resp <- bdbIncrByResp{0, err}
			return
		}
		err = txn.Commit()
		if err != nil {
			req.resp <- bdbIncrByResp{0, err}
			return
		} else {
			txn = nil
		}
		req.resp <- bdbIncrByResp{value, nil}
	}
}
Example #2
0
func (w *Worker) bdbGet(req *bdbGetReq) {
	table, name := bdb.SplitKey(req.key)
	db, err := w.getdb(table, bdb.DBTYPE_BTREE)
	if err != nil {
		req.resp <- bdbGetResp{nil, err}
	} else {
		value, err := db.Get(nil, name, &w.getbuff, 0)
		if err != nil {
			if err == bdb.ErrNotFound {
				req.resp <- bdbGetResp{nil, nil}
			} else {
				w.checkerr(err, db)
				req.resp <- bdbGetResp{nil, err}
			}
		} else {
			req.resp <- bdbGetResp{value, nil}
		}
	}
}
Example #3
0
func (w *Worker) bdbSet(req *bdbSetReq) {
	table, name := bdb.SplitKey(req.key)
	db, err := w.getdb(table, bdb.DBTYPE_BTREE)
	if err != nil {
		req.resp <- bdbSetResp{err}
	} else {
		var flags uint32 = 0
		if req.nooverwrite {
			flags = flags | bdb.DB_NOOVERWRITE
		}
		err := db.Set(nil, name, req.value, flags)
		if err != nil {
			w.checkerr(err, db)
			req.resp <- bdbSetResp{err}
		} else {
			req.resp <- bdbSetResp{nil}
		}
	}
}
Example #4
0
func (w *Worker) bdbSetEx(req *bdbSetReq) {
	var err error
	if w.expiredb == nil {
		w.expiredb, err = w.dbenv.GetDb("__expire", bdb.DBTYPE_BTREE)
		if err != nil {
			log.Error("worker|GetDb|%s", err.Error())
			req.resp <- bdbSetResp{err}
			return
		}
	}
	if w.expireindex == nil {
		w.expireindex, err = w.dbenv.GetDb("__expire.index", bdb.DBTYPE_BTREE)
		if err != nil {
			log.Error("worker|GetDb|%s", err.Error())
			req.resp <- bdbSetResp{err}
			return
		}
	}

	txn, err := w.dbenv.Begin(bdb.DB_READ_UNCOMMITTED)
	if err != nil {
		req.resp <- bdbSetResp{err}
		return
	}
	defer func() {
		if txn != nil {
			txn.Abort()
		}
	}()
	w.seq++
	err = bdb.SetExpire(w.expiredb, w.expireindex, txn, req.key, req.sec, w.seq, w.id)
	if err != nil {
		if err == bdb.ErrRepDead {
			w.expiredb.Close()
			w.expiredb = nil
			w.expireindex.Close()
			w.expireindex = nil
		}
		log.Error("worker|SetExpire|%s", err.Error())
		req.resp <- bdbSetResp{err}
		return
	}

	table, name := bdb.SplitKey(req.key)
	db, err := w.getdb(table, bdb.DBTYPE_BTREE)
	if err != nil {
		w.checkerr(err, db)
		req.resp <- bdbSetResp{err}
		return
	}
	var flags uint32 = 0
	if req.nooverwrite {
		flags = flags | bdb.DB_NOOVERWRITE
	}
	err = db.Set(txn, name, req.value, flags)
	if err != nil {
		w.checkerr(err, db)
		req.resp <- bdbSetResp{err}
		return
	}

	txn.Commit()
	txn = nil
	req.resp <- bdbSetResp{nil}
}