Browse files

redesign ttl

  • Loading branch information...
1 parent aca0535 commit 2cf6b7f3f886b05ebdae7eb81f7a7320b4de0f66 ideawu committed Feb 9, 2014
Showing with 199 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +3 −0 src/ttl.cpp
  3. +9 −1 src/util/Makefile
  4. +75 −0 src/util/sorted_set.cpp
  5. +74 −0 src/util/sorted_set.h
  6. +37 −0 src/util/test_sorted_set.cpp
View
1 .gitignore
@@ -23,4 +23,5 @@ api/cpp/html
api/php/a.php
api/php/c.php
api/java/Test.java
+src/util/test_sorted_set
View
3 src/ttl.cpp
@@ -19,6 +19,9 @@ ExpirationHandler::~ExpirationHandler(){
}
void ExpirationHandler::start(){
+ log_debug("loading expire_list");
+ // TODO:
+
thread_quit = false;
pthread_t tid;
int err = pthread_create(&tid, NULL, &ExpirationHandler::thread_func, this);
View
10 src/util/Makefile
@@ -1,6 +1,6 @@
include ../../build_config.mk
-OBJS = log.o fde.o config.o bytes.o
+OBJS = log.o fde.o config.o bytes.o sorted_set.o
EXES =
all: ${OBJS}
@@ -17,6 +17,14 @@ fde.o: fde.h fde.cpp fde_select.cpp fde_epoll.cpp
bytes.o: bytes.h bytes.cpp
g++ ${CFLAGS} -c bytes.cpp
+sorted_set.o: sorted_set.h sorted_set.cpp
+ g++ ${CFLAGS} -c sorted_set.cpp
+
+
+test: sorted_set.o log.o
+ g++ -o test_sorted_set ${CFLAGS} sorted_set.o log.o test_sorted_set.cpp
+
+
clean:
rm -f ${EXES} ${OBJS}
View
75 src/util/sorted_set.cpp
@@ -0,0 +1,75 @@
+#include "sorted_set.h"
+
+int SortedSet::size() const{
+ return (int)sorted_set.size();
+}
+
+int SortedSet::add(const std::string &key, int64_t score){
+ int ret;
+ std::map<std::string, std::set<Item>::iterator>::iterator it;
+
+ it = existed.find(key);
+ if(it == existed.end()){
+ // new item
+ ret = 1;
+ }else{
+ ret = 0;
+ std::set<Item>::iterator it2 = it->second;
+ const Item &item = *it2;
+ if(item.score == score){
+ // not updated
+ return 0;
+ }
+ // remove existing item
+ sorted_set.erase(it2);
+ }
+
+ Item item;
+ item.key = key;
+ item.score = score;
+
+ std::pair<std::set<Item>::iterator, bool> p = sorted_set.insert(item);
+ existed[key] = p.first;
+
+ return ret;
+}
+
+int SortedSet::del(const std::string &key){
+ int ret;
+ std::map<std::string, std::set<Item>::iterator>::iterator it;
+
+ it = existed.find(key);
+ if(it == existed.end()){
+ // new item
+ ret = 0;
+ }else{
+ ret = 1;
+ sorted_set.erase(it->second);
+ existed.erase(it);
+ }
+ return ret;
+}
+
+int SortedSet::front(const std::string **key, int64_t *score) const{
+ std::set<Item>::iterator it2 = sorted_set.begin();
+ if(it2 == sorted_set.end()){
+ return 0;
+ }
+ const Item &item = *it2;
+ *key = &item.key;
+ if(score){
+ *score = item.score;
+ }
+ return 1;
+}
+
+int SortedSet::pop_front(){
+ std::set<Item>::iterator it2 = sorted_set.begin();
+ if(it2 == sorted_set.end()){
+ return 0;
+ }
+ const Item &item = *it2;
+ existed.erase(item.key);
+ sorted_set.erase(it2);
+ return 1;
+}
View
74 src/util/sorted_set.h
@@ -0,0 +1,74 @@
+#ifndef UTIL_SORTED_SET_H
+#define UTIL_SORTED_SET_H
+
+#include <inttypes.h>
+#include <string>
+#include <map>
+#include <set>
+
+class SortedSet
+{
+public:
+ int size() const;
+ int add(const std::string &key, int64_t score);
+ int del(const std::string &key);
+ int front(const std::string **key, int64_t *score=NULL) const;
+ int pop_front();
+
+ /*
+ class Iterator
+ {
+ public:
+ bool next();
+ const std::string& key();
+ int64_t score();
+ };
+
+ Iterator begin();
+ */
+
+private:
+ struct Item
+ {
+ std::string key;
+ int64_t score;
+
+ bool operator<(const Item& b) const{
+ return this->score < b.score
+ || (this->score == b.score && this->key < b.key);
+ }
+ };
+
+ std::map<std::string, std::set<Item>::iterator> existed;
+ std::set<Item> sorted_set;
+};
+
+
+/*
+TODO: HashedWheel
+Each item is linked in two list, one is slot list, the other
+one is total list.
+*/
+/*
+template <class T>
+class SortedList
+{
+public:
+ void add(const T data, int64_t score);
+ T front();
+ void pop_front();
+
+ class Item
+ {
+ public:
+ int64_t score;
+ Item *prev;
+ Item *next;
+ //Item *slot_prev;
+ //Item *slot_next;
+ T data;
+ };
+};
+*/
+
+#endif
View
37 src/util/test_sorted_set.cpp
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <vector>
+#include "log.h"
+#include "sorted_set.h"
+
+int main(int argc, char **argv){
+ SortedSet zset;
+
+ std::vector<std::string> keys;
+ for(int i='a'; i<='z'; i++){
+ char buf[10];
+ snprintf(buf, sizeof(buf), "%c", i);
+ keys.push_back(buf);
+ }
+
+ log_debug("");
+ srand(time(NULL));
+ for(int i=0; i<1000 * 1000; i++){
+ std::string &key = keys[rand() % keys.size()];
+ zset.add(key, rand()%30 - 15);
+ }
+ log_debug("");
+
+ const std::string *key;
+ int64_t score;
+ int n = 0;
+ while(zset.front(&key, &score)){
+ printf("%s : %4lld\n", key->c_str(), score);
+ zset.pop_front();
+ n ++;
+ }
+ log_debug("%d", n);
+
+ return 0;
+}

0 comments on commit 2cf6b7f

Please sign in to comment.