|  | 
|  | 1 | +#include "bfkey.h" | 
|  | 2 | + | 
|  | 3 | +#include <QJsonDocument> | 
|  | 4 | + | 
|  | 5 | +BloomFilterKeyModel::BloomFilterKeyModel( | 
|  | 6 | +    QSharedPointer<RedisClient::Connection> connection, QByteArray fullPath, | 
|  | 7 | +    int dbIndex, long long ttl, QString filterFamily) | 
|  | 8 | +    : KeyModel(connection, fullPath, dbIndex, ttl), m_type(filterFamily) {} | 
|  | 9 | + | 
|  | 10 | +QString BloomFilterKeyModel::type() { return m_type; } | 
|  | 11 | + | 
|  | 12 | +QStringList BloomFilterKeyModel::getColumnNames() { | 
|  | 13 | +  return QStringList() << "value"; | 
|  | 14 | +} | 
|  | 15 | + | 
|  | 16 | +QHash<int, QByteArray> BloomFilterKeyModel::getRoles() { | 
|  | 17 | +  QHash<int, QByteArray> roles; | 
|  | 18 | +  roles[Roles::Value] = "value"; | 
|  | 19 | +  return roles; | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +QVariant BloomFilterKeyModel::getData(int rowIndex, int dataRole) { | 
|  | 23 | +  if (rowIndex > 0 || !isRowLoaded(rowIndex)) return QVariant(); | 
|  | 24 | +  if (dataRole == Roles::Value) | 
|  | 25 | +    return QJsonDocument::fromVariant(m_rowsCache[rowIndex]) | 
|  | 26 | +        .toJson(QJsonDocument::Compact); | 
|  | 27 | + | 
|  | 28 | +  return QVariant(); | 
|  | 29 | +} | 
|  | 30 | + | 
|  | 31 | +void BloomFilterKeyModel::addRow(const QVariantMap& row, Callback c) { | 
|  | 32 | +  QByteArray value = row.value("value").toByteArray(); | 
|  | 33 | + | 
|  | 34 | +  executeCmd({QString("%1.ADD").arg(m_type).toLatin1(), m_keyFullPath, value}, | 
|  | 35 | +             [this, c](const QString& err) { | 
|  | 36 | +               m_rowCount++; | 
|  | 37 | +               return c(err); | 
|  | 38 | +             }); | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +void BloomFilterKeyModel::loadRows(QVariant, unsigned long, | 
|  | 42 | +                                   LoadRowsCallback callback) { | 
|  | 43 | +  auto onConnectionError = [callback](const QString& err) { | 
|  | 44 | +    return callback(err, 0); | 
|  | 45 | +  }; | 
|  | 46 | + | 
|  | 47 | +  auto responseHandler = [this, callback](RedisClient::Response r, Callback) { | 
|  | 48 | +    m_rowsCache.clear(); | 
|  | 49 | +    auto value = r.value().toList(); | 
|  | 50 | + | 
|  | 51 | +    QVariantMap row; | 
|  | 52 | + | 
|  | 53 | +    for (QVariantList::const_iterator item = value.cbegin(); item != value.cend(); | 
|  | 54 | +         ++item) { | 
|  | 55 | +      auto key = item->toByteArray(); | 
|  | 56 | +      ++item; | 
|  | 57 | + | 
|  | 58 | +      if (item == value.cend()) { | 
|  | 59 | +        emit m_notifier->error(QCoreApplication::translate( | 
|  | 60 | +            "RESP", "Data was loaded from server partially.")); | 
|  | 61 | +        break; | 
|  | 62 | +      } | 
|  | 63 | + | 
|  | 64 | +      auto keyVal = item->toByteArray(); | 
|  | 65 | +      row[key] = keyVal; | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    m_rowsCache.push_back(row); | 
|  | 69 | +    callback(QString(), 1); | 
|  | 70 | +  }; | 
|  | 71 | + | 
|  | 72 | +  executeCmd({QString("%1.INFO").arg(m_type).toLatin1(), m_keyFullPath}, | 
|  | 73 | +             onConnectionError, responseHandler, RedisClient::Response::Array); | 
|  | 74 | +} | 
0 commit comments