Example #1
0
func (tbl *Table) Sync(req *PkgArgs) ([]byte, bool) {
	var in proto.PkgMultiOp
	_, err := in.Decode(req.Pkg)
	if err == nil {
		// Sync full DB, including DB 0
		var wb = tbl.db.NewWriteBatch()
		for i := 0; i < len(in.Kvs); i++ {
			tbl.setSyncKV(wb, in.DbId, &in.Kvs[i])
		}
		tbl.rwMtx.RLock()
		err = tbl.db.Commit(wb)
		tbl.rwMtx.RUnlock()
		wb.Destroy()

		if err == nil {
			return nil, true
		} else {
			in.SetErrCode(table.EcWriteFail)
			log.Printf("setRawKV failed: %s\n", err)
		}
	} else {
		in.SetErrCode(table.EcDecodeFail)
	}

	return replyHandle(&in), table.EcOk == in.ErrCode
}
Example #2
0
func replyMulti(out *proto.PkgMultiOp) []byte {
	var pkgLen = out.Length()
	if pkgLen > proto.MaxPkgLen {
		out.Kvs = nil
		out.SetErrCode(table.EcInvPkgLen)
		pkgLen = out.Length()
	}

	var pkg = make([]byte, pkgLen)
	_, err := out.Encode(pkg)
	if err != nil {
		log.Fatalf("Encode failed: %s\n", err)
	}
	return pkg
}