Skip to content

Commit

Permalink
Merge pull request #1 from gitsrc/master
Browse files Browse the repository at this point in the history
thanks for pr
  • Loading branch information
wujunwei authored Jan 9, 2020
2 parents 17bd6c4 + ae696ef commit b73ae8a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 59 deletions.
104 changes: 45 additions & 59 deletions slock.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@

//
// Created by adam on 2018-11-28.
//
#include "../src/redismodule.h"
#include <ctype.h>
#include <string.h>




static RedisModuleType *SLockType;

struct SLock {
mstime_t lock_time;
unsigned long long client_id;
};
#include "slock.h"

/**
* slock.lock lock_key expire
Expand All @@ -27,30 +14,30 @@ struct SLock {
*/
int SlockLock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc < 2){
if (argc < 2) {
return RedisModule_WrongArity(ctx);
}
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ|REDISMODULE_WRITE);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
int keyType = RedisModule_KeyType(key);
if(keyType == REDISMODULE_KEYTYPE_EMPTY){
struct SLock *lock = RedisModule_Alloc(sizeof(*lock));
if (keyType == REDISMODULE_KEYTYPE_EMPTY) {
SLock *lock = RedisModule_Alloc(sizeof(*lock));
lock->client_id = RedisModule_GetClientId(ctx);
lock->lock_time = RedisModule_Milliseconds();

RedisModule_ModuleTypeSetValue(key, SLockType, lock);
if(argc == 3){
if (argc == 3) {
long long expire_time;
if (RedisModule_StringToLongLong(argv[2], &expire_time) != REDISMODULE_OK){
return RedisModule_ReplyWithError(ctx,"ERR invalid expireTime");
if (RedisModule_StringToLongLong(argv[2], &expire_time) != REDISMODULE_OK) {
return RedisModule_ReplyWithError(ctx, ERR_INVALID_EXPIRE_TIME);
}
RedisModule_SetExpire(key, expire_time);
}
RedisModule_ReplyWithLongLong(ctx,1);
} else{
if(RedisModule_ModuleTypeGetType(key) != SLockType){
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
RedisModule_ReplyWithLongLong(ctx, 1);
} else {
if (RedisModule_ModuleTypeGetType(key) != SLockType) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
}
RedisModule_ReplyWithLongLong(ctx,0);
RedisModule_ReplyWithLongLong(ctx, 0);
}
RedisModule_CloseKey(key);
return REDISMODULE_OK;
Expand All @@ -65,21 +52,20 @@ int SlockLock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
* @param agrc
* @return int
*/
int SlockInfo_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
int SlockInfo_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc != 2){
if (argc != 2) {
return RedisModule_WrongArity(ctx);
}

RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ);
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY){
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
return RedisModule_ReplyWithNull(ctx);
}
if (RedisModule_ModuleTypeGetType(key) != SLockType){ //判断键类型
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
} else{
struct SLock *lock = RedisModule_ModuleTypeGetValue(key);
if (RedisModule_ModuleTypeGetType(key) != SLockType) { //判断键类型
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
} else {
SLock *lock = RedisModule_ModuleTypeGetValue(key);
RedisModule_ReplyWithArray(ctx, 2);
RedisModule_ReplyWithLongLong(ctx, lock->client_id);
RedisModule_ReplyWithLongLong(ctx, lock->lock_time);
Expand All @@ -95,26 +81,25 @@ int SlockInfo_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
* @param argc
* @return int
*/
int SlockunLock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
int SlockunLock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc != 2){
if (argc != 2) {
return RedisModule_WrongArity(ctx);
}

RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE|REDISMODULE_READ);
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY){
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE | REDISMODULE_READ);
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
return RedisModule_ReplyWithNull(ctx);
}
if (RedisModule_ModuleTypeGetType(key) != SLockType){
return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
} else{
struct SLock *lock;
if (RedisModule_ModuleTypeGetType(key) != SLockType) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
} else {
SLock *lock;
lock = RedisModule_ModuleTypeGetValue(key);
if(lock->client_id == RedisModule_GetClientId(ctx)){
if (lock->client_id == RedisModule_GetClientId(ctx)) {
RedisModule_DeleteKey(key);
RedisModule_ReplyWithLongLong(ctx, 1);
}else{
} else {
RedisModule_ReplyWithLongLong(ctx, 0);
}
}
Expand All @@ -129,27 +114,27 @@ void *SLockRdbLoad(RedisModuleIO *rdb, int encver) {
/* RedisModule_Log("warning","Can't load data with version %d", encver);*/
return NULL;
}
struct SLock *sl = RedisModule_Alloc(sizeof(sl));
SLock *sl = RedisModule_Alloc(sizeof(sl));
sl->client_id = RedisModule_LoadUnsigned(rdb);
sl->lock_time = RedisModule_LoadSigned(rdb);
return sl;
}

void SLockRdbSave(RedisModuleIO *rdb, void *value) {
struct SLock *sl = value;
RedisModule_SaveUnsigned(rdb,sl->client_id);
SLock *sl = value;
RedisModule_SaveUnsigned(rdb, sl->client_id);
RedisModule_SaveSigned(rdb, sl->lock_time);
}

void SLockAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value) {
struct SLock *sl = value;
RedisModule_EmitAOF(aof,"SLOCK.LOCK","sll",key,sl->lock_time, sl->client_id);
SLock *sl = value;
RedisModule_EmitAOF(aof, "SLOCK.LOCK", "sll", key, sl->lock_time, sl->client_id);
}

/* The goal of this function is to return the amount of memory used by
* the HelloType value. */
size_t SLockMemUsage(const void *value) {
const struct SLock *hto = value;
const SLock *hto = value;
return sizeof(*hto);
}

Expand All @@ -158,8 +143,8 @@ void SLockFree(void *value) {
}

void SLockDigest(RedisModuleDigest *md, void *value) {
struct SLock *sl = value;
RedisModule_DigestAddLongLong(md,sl->client_id);
SLock *sl = value;
RedisModule_DigestAddLongLong(md, sl->client_id);
RedisModule_DigestAddLongLong(md, sl->lock_time);
RedisModule_DigestEndSequence(md);
}
Expand All @@ -170,8 +155,9 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
REDISMODULE_NOT_USED(argv);
REDISMODULE_NOT_USED(argc);

if (RedisModule_Init(ctx,"slock",1,REDISMODULE_APIVER_1)
== REDISMODULE_ERR) return REDISMODULE_ERR;
if (RedisModule_Init(ctx, MODULE_NAME, MODULE_VERSION, REDISMODULE_APIVER_1)
== REDISMODULE_ERR)
return REDISMODULE_ERR;
RedisModuleTypeMethods tm = {
.version = REDISMODULE_TYPE_METHOD_VERSION,
.rdb_load = SLockRdbLoad,
Expand All @@ -182,16 +168,16 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
.digest = SLockDigest
};

SLockType = RedisModule_CreateDataType(ctx,"slocktype",0,&tm);
SLockType = RedisModule_CreateDataType(ctx, "slocktype", 0, &tm);
if (SLockType == NULL) return REDISMODULE_ERR;

if (RedisModule_CreateCommand(ctx,"slock.unlock", SlockunLock_RedisCommand,"write",1,1,1) == REDISMODULE_ERR)
if (RedisModule_CreateCommand(ctx, COMMAND_SLOCK_UNLOCK, SlockunLock_RedisCommand, "write", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;

if (RedisModule_CreateCommand(ctx,"slock.lock", SlockLock_RedisCommand,"write",1,1,1) == REDISMODULE_ERR)
if (RedisModule_CreateCommand(ctx, COMMAND_SLOCK_LOCK, SlockLock_RedisCommand, "write", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;

if (RedisModule_CreateCommand(ctx,"slock.info", SlockInfo_RedisCommand,"readonly",1,1,1) == REDISMODULE_ERR)
if (RedisModule_CreateCommand(ctx, COMMAND_SLOCK_INFO, SlockInfo_RedisCommand, "readonly", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;

return REDISMODULE_OK;
Expand Down
29 changes: 29 additions & 0 deletions slock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef SLOCK_H
#define SLOCK_H

#include "../src/redismodule.h"
#include <ctype.h>
#include <string.h>

static RedisModuleType *SLockType;

typedef struct slock {
mstime_t lock_time;
unsigned long long client_id;
} SLock;


//Error message definition
#define ERR_INVALID_EXPIRE_TIME "ERR invalid expireTime"

//COMMAND TYPE
#define COMMAND_SLOCK_UNLOCK "slock.unlock"
#define COMMAND_SLOCK_LOCK "slock.lock"
#define COMMAND_SLOCK_INFO "slock.info"


//MODULE INFO
#define MODULE_NAME "slock"
#define MODULE_VERSION 1

#endif

0 comments on commit b73ae8a

Please sign in to comment.