Skip to content

Commit

Permalink
Prefix iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
valmat committed Nov 2, 2014
1 parent 17aaae8 commit 713e778
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ operations | description
**Multi delete keys** | Delete keys from DB
**Check key exist** | Check key existing
**Imcrement** | Imcrement value by key
**Prefit** | Prefix iterator


For more details see: [protocol description](protocol.md)
Expand Down
10 changes: 10 additions & 0 deletions protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ POST /tail
Reply: same [multi get](https://github.com/valmat/RocksServer/blob/master/protocol.md#multi-get)


###Get keys and values by key-prefix

Request:
```
GET /prefit?<prefix>
```

Reply: same [multi get](https://github.com/valmat/RocksServer/blob/master/protocol.md#multi-get)


###Backup
Backup database

Expand Down
5 changes: 0 additions & 5 deletions src/Protocol/ProtocolInGetIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ namespace RocksServer {
*/
ProtocolInGetIterator operator++(int)
{
// make a copy
ProtocolInGetIterator copy(*this);

// increment current object
++(*this);

// return the original
return copy;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Protocol/ProtocolInPostKeysIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ namespace RocksServer {
*/
ProtocolInPostKeysIterator operator++(int)
{
// make a copy
ProtocolInPostKeysIterator copy(*this);

// increment current object
++(*this);

// return the original
return copy;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Protocol/ProtocolInPostPairsIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ namespace RocksServer {
*/
ProtocolInPostPairsIterator operator++(int)
{
// make a copy
ProtocolInPostPairsIterator copy(*this);

// increment current object
++(*this);

// return the original
return copy;
}

Expand Down
3 changes: 2 additions & 1 deletion src/RocksServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ int main(int argc, char **argv)
server.onRequest("/tail", new RequestTailing(rdb));
server.onRequest("/backup", new RequestBackup(rdb, cfg.get("backup_path", dfCfg.backup_path)));
server.onRequest("/stats", new RequestStats(rdb));
server.onRequest("/prefit", new RequestPrefIt(rdb));


/**
Expand All @@ -169,7 +170,7 @@ int main(int argc, char **argv)
*
*/
if (!server.dispatch()) {
std::cerr << "Failed to run messahe loop." << std::endl;
std::cerr << "Failed to run message loop." << std::endl;
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/RocksServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @github https://github.com/valmat/rocksserver
*/

#define ROCKSSERVER_VERSION "v.0.1.1"
#define ROCKSSERVER_VERSION "v.0.1.2"

// C++ headers
#include <iostream>
Expand Down Expand Up @@ -81,6 +81,7 @@
#include "listeners/RequestTailing.h"
#include "listeners/RequestBackup.h"
#include "listeners/RequestStats.h"
#include "listeners/RequestPrefIt.h"


#include "evhttp/EvServer.h"
50 changes: 50 additions & 0 deletions src/listeners/RequestPrefIt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* RequestPrefIt.h
*
* Request listener for command "prefit"
* Prefix iterator
*
* @author valmat <[email protected]>
* @github https://github.com/valmat/rocksserver
*/


namespace RocksServer {

class RequestPrefIt : public RequestBase<ProtocolInGet, ProtocolOut>
{
public:
RequestPrefIt(RocksDBWrapper &rdb) : _rdb(rdb) {}

/**
* Runs request listener
* @param protocol in object
* @param protocol out object
*/
virtual void run(const ProtocolInGet &in, const ProtocolOut &out) override
{
if(!in.check()) {
out.setFailValue();
return;
}

auto prefix = in.key();
std::unique_ptr<rocksdb::Iterator> iter(_rdb->NewIterator(rocksdb::ReadOptions()));

// filling buffer
for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
if(iter->status().ok()) {
out.setPair(iter->key(), iter->value());
} else {
out.setFailPair(iter->key());
}
}

}

virtual ~RequestPrefIt() {}
private:
RocksDBWrapper& _rdb;
};

}

0 comments on commit 713e778

Please sign in to comment.