Skip to content

Commit 4349365

Browse files
committed
redisdb add transaction
1 parent bb56e9d commit 4349365

12 files changed

+141
-102
lines changed

example/redisdb/db/main.cc

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ int main() {
1313
return -1;
1414
}
1515

16-
1716
int32_t ret;
1817
// Set
1918
s = db.Set("TEST_KEY", "TEST_VALUE");

example/redisdb/db/option.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct Options {
4747
// Any internal progress/error information generated by the db will
4848
// be written to info_log if it is non-null, or to a file stored
4949
// in the same directory as the DB Contents if info_log is null.
50-
std::shared_ptr <Logger> infolog;
50+
std::shared_ptr<Logger> infolog;
5151

5252
// If non-null, use the specified cache for blocks.
5353
// If null, leveldb will automatically create and use an 8MB internal cache.

example/redisdb/db/redis.h

-95
Original file line numberDiff line numberDiff line change
@@ -1104,98 +1104,3 @@ enum BeforeOrAfter {
11041104
Before,
11051105
After
11061106
};
1107-
1108-
1109-
class LockMgr {
1110-
public:
1111-
void Lock(const std::string& key) {
1112-
lockShards[std::hash<std::string>{}(key) % kShards].lock();
1113-
}
1114-
1115-
void Unlock(const std::string& key) {
1116-
lockShards[std::hash<std::string>{}(key) % kShards].unlock();
1117-
}
1118-
1119-
const static int32_t kShards = 4096;
1120-
std::array<std::mutex, kShards> lockShards;
1121-
};
1122-
1123-
class HashLock {
1124-
public:
1125-
HashLock(LockMgr* lockmgr, const std::string_view& key)
1126-
:lockmgr(lockmgr),
1127-
key(key) {
1128-
lockmgr->Lock(std::string(key.data(), key.size()));
1129-
}
1130-
1131-
~HashLock() {
1132-
lockmgr->Unlock(std::string(key.data(), key.size()));
1133-
}
1134-
private:
1135-
std::string_view key;
1136-
LockMgr* const lockmgr;
1137-
1138-
HashLock(const HashLock&);
1139-
void operator=(const HashLock&);
1140-
};
1141-
1142-
class MultiHashLock {
1143-
public:
1144-
MultiHashLock(LockMgr* lockmgr, std::vector<std::string>& keys)
1145-
:lockmgr(lockmgr),
1146-
keys(keys) {
1147-
std::string prekey;
1148-
std::sort(keys.begin(), keys.end());
1149-
if (!keys.empty() &&
1150-
keys[0].empty()) {
1151-
lockmgr->Lock(prekey);
1152-
}
1153-
1154-
for (const auto& key : keys) {
1155-
if (prekey != key) {
1156-
lockmgr->Lock(key);
1157-
prekey = key;
1158-
}
1159-
}
1160-
}
1161-
1162-
~MultiHashLock() {
1163-
std::string prekey;
1164-
if (!keys.empty() &&
1165-
keys[0].empty()) {
1166-
lockmgr->Unlock(prekey);
1167-
}
1168-
1169-
for (const auto& key : keys) {
1170-
if (prekey != key) {
1171-
lockmgr->Unlock(key);
1172-
prekey = key;
1173-
}
1174-
}
1175-
}
1176-
private:
1177-
std::vector<std::string> keys;
1178-
LockMgr* const lockmgr;
1179-
1180-
MultiHashLock(const MultiHashLock&);
1181-
void operator=(const MultiHashLock&);
1182-
};
1183-
1184-
class SnapshotLock {
1185-
public:
1186-
SnapshotLock(std::shared_ptr<DB>& db, std::shared_ptr<Snapshot>& snapshot)
1187-
:db(db),
1188-
snapshot(snapshot) {
1189-
snapshot = db->GetSnapshot();
1190-
}
1191-
1192-
~SnapshotLock() {
1193-
db->ReleaseSnapshot(snapshot);
1194-
}
1195-
private:
1196-
const std::shared_ptr<DB> db;
1197-
const std::shared_ptr<Snapshot> snapshot;
1198-
1199-
SnapshotLock(const SnapshotLock&);
1200-
void operator=(const SnapshotLock&);
1201-
};

example/redisdb/db/redisdb.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Status RedisDB::Open() {
5353
}
5454

5555
{
56-
rediszset.reset(new RedisZset(this, options, path + "/set"));
57-
Status s = rediszset->Open();
56+
redisset.reset(new RedisSet(this, options, path + "/set"));
57+
Status s = redisset->Open();
5858
assert(s.ok());
5959
}
6060

example/redisdb/db/redishash.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "coding.h"
1010
#include "db.h"
1111
#include "redis.h"
12+
#include "lockmgr.h"
1213

1314
class RedisDB;
1415

example/redisdb/db/redislist.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "coding.h"
1010
#include "db.h"
1111
#include "redis.h"
12+
#include "lockmgr.h"
1213

1314
class RedisDB;
1415

example/redisdb/db/redisset.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include "redisdb.h"
33

44
RedisSet::RedisSet(RedisDB* redis,
5-
const Options& options, const std::string& path) {
6-
5+
const Options& options, const std::string& path)
6+
:redis(redis),
7+
db(new DB(options, path)) {
78
}
89

910
RedisSet::~RedisSet() {

example/redisdb/db/redisset.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "coding.h"
1010
#include "db.h"
1111
#include "redis.h"
12+
#include "lockmgr.h"
1213

1314
class RedisDB;
1415

example/redisdb/db/redistring.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ratio>
55
#include <chrono>
66
#include <string_view>
7-
7+
#include "lockmgr.h"
88
#include "coding.h"
99
#include "db.h"
1010
#include "option.h"

example/redisdb/db/rediszset.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "coding.h"
1010
#include "db.h"
1111
#include "redis.h"
12+
#include "lockmgr.h"
1213

1314
class RedisDB;
1415
class RedisZset {

example/redisdb/db/transaction.cc

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "transaction.h"
2+
3+
Transaction::Transaction(TransactionDB *db)
4+
:db(db) {
5+
6+
}
7+
8+
Transaction::~Transaction() {
9+
10+
}
11+
12+
Status Transaction::Put(const WriteOptions& options, const std::string_view& key, const std::string_view& value) {
13+
HashLock l(db->GetLockMgr(), key);
14+
return db->GetDB()->Put(options, key, value);
15+
}
16+
17+
Status Transaction::Delete(const WriteOptions& options, const std::string_view& key) {
18+
HashLock l(db->GetLockMgr(), key);
19+
return db->GetDB()->Delete(options, key);
20+
}
21+
22+
Status Transaction::Get(const ReadOptions& options, const std::string_view& key, std::string* value) {
23+
HashLock l(db->GetLockMgr(), key);
24+
return db->GetDB()->Get(options, key, value);
25+
}
26+
27+
Status Transaction::Commit() {
28+
return db->Write(WriteOptions(), &writebatch);
29+
}
30+
31+
Status Transaction::Rollback() {
32+
writebatch.clear();
33+
}
34+
35+
TransactionDB::TransactionDB(const Options& options, const std::string& path):
36+
db(new DB(options, path)) {
37+
38+
}
39+
40+
Status TransactionDB::Open() {
41+
return db->Open();
42+
}
43+
44+
TransactionDB::~TransactionDB() {
45+
46+
}
47+
48+
Status TransactionDB::Put(const WriteOptions& options, const std::string_view& key, const std::string_view& value) {
49+
WriteSharedHashLock l(&lockmgr, key);
50+
return db->Put(options, key, value);
51+
}
52+
53+
Status TransactionDB::Delete(const WriteOptions& options, const std::string_view& key) {
54+
HashLock l(&lockmgr, key);
55+
return db->Delete(options, key);
56+
}
57+
58+
Status TransactionDB::Write(const WriteOptions& options, WriteBatch* updates) {
59+
return db->Write(options, updates);
60+
}
61+
62+
Status TransactionDB::Get(const ReadOptions& options, const std::string_view& key, std::string* value) {
63+
HashLock l(&lockmgr, key);
64+
return db->Get(options, key, value);
65+
}
66+
67+
std::shared_ptr<Transaction> TransactionDB::BeginTrasaction() {
68+
std::shared_ptr<Transaction> tran(new Transaction(this));
69+
return tran;
70+
}

example/redisdb/db/transaction.h

+60
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
11
#pragma once
2+
#include <atomic>
3+
#include <map>
4+
#include <set>
5+
#include <memory>
6+
#include <string>
7+
#include "db.h"
8+
#include "redis.h"
9+
#include "lockmgr.h"
10+
11+
class TransactionDB;
12+
13+
class Transaction {
14+
public:
15+
Transaction(TransactionDB *db);
16+
~Transaction();
17+
18+
Status Put(const WriteOptions& options, const std::string_view& key, const std::string_view& value);
19+
20+
Status Delete(const WriteOptions& options, const std::string_view& key);
21+
22+
Status Get(const ReadOptions& options, const std::string_view& key, std::string* value);
23+
24+
Status Commit();
25+
26+
Status Rollback();
27+
private:
28+
TransactionDB *db;
29+
WriteBatch writebatch;
30+
std::shared_ptr<ReadSharedHashLock> readlock;
31+
std::shared_ptr<WriteSharedHashLock> writelock;
32+
};
33+
34+
class TransactionDB {
35+
public:
36+
TransactionDB(const Options& options, const std::string& path);
37+
~TransactionDB();
38+
39+
Status Open();
40+
41+
Status Put(const WriteOptions& options, const std::string_view& key, const std::string_view& value);
42+
43+
Status Delete(const WriteOptions& options, const std::string_view& key);
44+
45+
Status Write(const WriteOptions& options, WriteBatch* updates);
46+
47+
Status Get(const ReadOptions& options, const std::string_view& key, std::string* value);
48+
49+
std::shared_ptr<Transaction> BeginTrasaction();
50+
51+
LockMgr *GetLockMgr() {
52+
return &lockmgr;
53+
}
54+
55+
std::shared_ptr<DB> GetDB() {
56+
return db;
57+
}
58+
private:
59+
std::shared_ptr<DB> db;
60+
LockMgr lockmgr;
61+
};

0 commit comments

Comments
 (0)