leveldb中规定了字符串基本的比较方式,可以对字符串进行快速比较和查找。
Comparator是一个抽象类,定义了不同类型的比较接口。代码如下:
leveldb-1.9.0\include\leveldb\comparator.h
#ifndef STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
#define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
#include <string>
namespace leveldb {
class Slice;//声明Slice类
// A Comparator object provides a total order across slices that are
// used as keys in an sstable or a database. A Comparator implementation
// must be thread-safe since leveldb may invoke its methods concurrently
// from multiple threads.
//比较器对象针对那些在一个sstable或数据库中被当作键值的Slices提供了一种全序关系。
//由于leveldb可以同时从多个线程调用它的方法,因此一个比较器的实现必须是线程安全的。
class Comparator {
public:
virtual ~Comparator();//Comparator的虚析构函数
// Three-way comparison. Returns value:
// < 0 iff "a" < "b",
// == 0 iff "a" == "b",
// > 0 iff "a" > "b"
//比较两个Slice对象a和b的大小:
//返回值 <0,当且仅当"a" < "b";
//返回值==0,当且仅当"a" == "b";
//返回值 >0,当且仅当"a" > "b";
virtual int Compare(const Slice& a, const Slice& b) const = 0;
// The name of the comparator. Used to check for comparator
// mismatches (i.e., a DB created with one comparator is
// accessed using a different comparator.
//
// The client of this package should switch to a new name whenever
// the comparator implementation changes in a way that w