本文整理汇总了C++中ListIterator::Valid方法的典型用法代码示例。如果您正苦于以下问题:C++ ListIterator::Valid方法的具体用法?C++ ListIterator::Valid怎么用?C++ ListIterator::Valid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListIterator
的用法示例。
在下文中一共展示了ListIterator::Valid方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RenameList
int Ardb::RenameList(Context& ctx, DBID srcdb, const std::string& srckey, DBID dstdb, const std::string& dstkey)
{
Context tmpctx;
tmpctx.currentDB = srcdb;
ValueObject v;
int err = GetMetaValue(tmpctx, srckey, LIST_META, v);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
fill_error_reply(ctx.reply, "no such key or some error");
return 0;
}
if (v.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
DelKeyValue(tmpctx, v.key);
v.key.encode_buf.Clear();
v.key.db = dstdb;
v.key.key = dstkey;
v.meta.expireat = 0;
SetKeyValue(ctx, v);
}
else
{
ListIterator iter;
ListIter(ctx, v, iter, false);
tmpctx.currentDB = dstdb;
ValueObject dstmeta;
dstmeta.key.type = KEY_META;
dstmeta.key.key = dstkey;
dstmeta.type = LIST_META;
dstmeta.meta.SetFlag(COLLECTION_FLAG_SEQLIST);
dstmeta.meta.SetEncoding(COLLECTION_ENCODING_ZIPLIST);
BatchWriteGuard guard(GetKeyValueEngine());
while (iter.Valid())
{
std::string tmpstr;
ListInsert(tmpctx, dstmeta, NULL, iter.Element()->GetDecodeString(tmpstr), false, false);
iter.Next();
}
SetKeyValue(tmpctx, dstmeta);
tmpctx.currentDB = srcdb;
DeleteKey(tmpctx, srckey);
}
ctx.data_change = true;
return 0;
}
示例2: End
ListIterator<T> List<T>::Insert (ListIterator<T> i, const T& t)
// Insert t at (in front of) i; return i at new element
{
if (Empty()) // always insert
{
i = End();
}
if (!i.Valid() || i == rEnd()) // null or off-the-front
{
std::cerr << " ** cannot insert at position -1\n";
return End();
}
Link* newLink = NewLink(t);
if (newLink == nullptr) return End();
LinkIn(i.curr_,newLink);
// leave i at new entry and return
i.curr_ = newLink;
return i;
}
示例3: LClear
int Ardb::LClear(Context& ctx, ValueObject& meta)
{
BatchWriteGuard guard(GetKeyValueEngine(), meta.meta.Encoding() != COLLECTION_ENCODING_ZIPLIST);
if (meta.meta.Encoding() != COLLECTION_ENCODING_ZIPLIST)
{
ListIterator iter;
meta.meta.len = 0;
ListIter(ctx, meta, iter, false);
while (iter.Valid())
{
KeyObject fk;
fk.db = ctx.currentDB;
fk.key = meta.key.key;
fk.type = LIST_ELEMENT;
fk.score = *(iter.Score());
DelKeyValue(ctx, fk);
iter.Next();
}
}
DelKeyValue((ctx), meta.key);
return 0;
}
示例4: LTrim
int Ardb::LTrim(Context& ctx, RedisCommandFrame& cmd)
{
int64 start, end;
if (!GetInt64Value(ctx, cmd.GetArguments()[1], start) || !GetInt64Value(ctx, cmd.GetArguments()[2], end))
{
return 0;
}
ValueObject meta;
int err = GetMetaValue(ctx, cmd.GetArguments()[0], LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
fill_status_reply(ctx.reply, "OK");
return 0;
}
/* convert negative indexes */
if (start < 0)
start = meta.meta.Length() + start;
if (end < 0)
end = meta.meta.Length() + end;
if (start < 0)
start = 0;
if (end >= meta.meta.Length())
end = meta.meta.Length() - 1;
/* Invariant: start >= 0, so this test will be true when end < 0.
* The range is empty when start > end or start >= length. */
if (start > end || start >= meta.meta.Length())
{
/* Out of range start or start > end result in empty list */
DeleteKey(ctx, cmd.GetArguments()[0]);
return 0;
}
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
DataArray newzip;
for (int64 i = start; i <= end; i++)
{
newzip.push_back(meta.meta.ziplist[i]);
}
meta.meta.ziplist = newzip;
SetKeyValue(ctx, meta);
}
else
{
BatchWriteGuard guard(GetKeyValueEngine());
if (meta.meta.IsSequentialList())
{
int64 listlen = meta.meta.Length();
for (int64 s = 0; s < listlen; s++)
{
if (s == start)
{
s = end;
continue;
}
KeyObject lk;
lk.db = meta.key.db;
lk.key = meta.key.key;
lk.type = LIST_ELEMENT;
lk.score = meta.meta.min_index.IncrBy(s);
meta.meta.len--;
DelKeyValue(ctx, lk);
}
meta.meta.max_index = meta.meta.min_index;
meta.meta.min_index.IncrBy(start);
meta.meta.max_index.IncrBy(end);
}
else
{
ListIterator iter;
ListIter(ctx, meta, iter, false);
int64 cursor = 0;
while (iter.Valid())
{
if (cursor < start || cursor > end)
{
DelRaw(ctx, iter.CurrentRawKey());
meta.meta.len--;
}
if (cursor == start)
{
meta.meta.min_index = *(iter.Element());
}
else if (cursor == end)
{
meta.meta.max_index = *(iter.Element());
}
cursor++;
iter.Next();
}
}
SetKeyValue(ctx, meta);
}
return 0;
}
示例5: LSet
int Ardb::LSet(Context& ctx, RedisCommandFrame& cmd)
{
int64 index;
if (!GetInt64Value(ctx, cmd.GetArguments()[1], index))
{
fill_error_reply(ctx.reply, "value is not an integer or out of range");
return 0;
}
ValueObject meta;
int err = GetMetaValue(ctx, cmd.GetArguments()[0], LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
fill_error_reply(ctx.reply, "no such key");
return 0;
}
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
Data* entry = GetZipEntry(meta.meta.ziplist, index);
if (NULL == entry)
{
fill_error_reply(ctx.reply, "index out of range");
return 0;
}
else
{
entry->SetString(cmd.GetArguments()[2], true);
SetKeyValue(ctx, meta);
fill_status_reply(ctx.reply, "OK");
return 0;
}
}
else
{
if (index >= meta.meta.Length() || (-index) > meta.meta.Length())
{
fill_error_reply(ctx.reply, "index out of range");
return 0;
}
if (meta.meta.IsSequentialList())
{
ValueObject list_element;
list_element.key.db = meta.key.db;
list_element.key.key = meta.key.key;
list_element.key.type = LIST_ELEMENT;
list_element.key.score = meta.meta.min_index;
if (index >= 0)
{
list_element.key.score.IncrBy(index);
}
else
{
list_element.key.score.IncrBy(index + meta.meta.Length());
}
if (0 == GetKeyValue(ctx, list_element.key, &list_element))
{
list_element.element.SetString(cmd.GetArguments()[2], true);
SetKeyValue(ctx, list_element);
fill_status_reply(ctx.reply, "OK");
return 0;
}
}
else
{
ListIterator iter;
ListIter(ctx, meta, iter, index < 0);
int64 cursor = index >= 0 ? 0 : -1;
while (iter.Valid())
{
if (cursor == index)
{
ValueObject v;
v.key.db = meta.key.db;
v.key.key = meta.key.key;
v.key.type = LIST_ELEMENT;
v.key.score = *(iter.Score());
v.type = LIST_ELEMENT;
v.element.SetString(cmd.GetArguments()[2], true);
SetKeyValue(ctx, v);
fill_status_reply(ctx.reply, "OK");
return 0;
}
if (cursor >= 0)
{
cursor++;
}
else
{
cursor--;
}
if (index < 0)
{
iter.Prev();
}
else
{
iter.Next();
}
}
//.........这里部分代码省略.........
示例6: LRem
int Ardb::LRem(Context& ctx, RedisCommandFrame& cmd)
{
int64 count;
if (!GetInt64Value(ctx, cmd.GetArguments()[1], count))
{
return 0;
}
int64 toremove = std::abs(count);
ValueObject meta;
int err = GetMetaValue(ctx, cmd.GetArguments()[0], LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
fill_int_reply(ctx.reply, 0);
return 0;
}
Data element;
element.SetString(cmd.GetArguments()[2], true);
KeyLockerGuard lock(m_key_lock, ctx.currentDB, cmd.GetArguments()[0]);
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
uint32 oldlen = meta.meta.ziplist.size();
int64 removed = 0;
DataArray newzip;
if (count >= 0)
{
for (uint32 i = 0; i < oldlen; i++)
{
if (meta.meta.ziplist[i] == element)
{
if (toremove == 0 || removed < toremove)
{
removed++;
continue;
}
}
newzip.push_back(meta.meta.ziplist[i]);
}
}
else
{
for (uint32 i = 0; i < oldlen; i++)
{
if (meta.meta.ziplist[oldlen - 1 - i] == element)
{
if (toremove == 0 || removed < toremove)
{
removed++;
continue;
}
}
newzip.push_front(meta.meta.ziplist[i]);
}
}
if (removed > 0)
{
meta.meta.ziplist = newzip;
SetKeyValue(ctx, meta);
}
fill_int_reply(ctx.reply, removed);
return 0;
}
BatchWriteGuard guard(GetKeyValueEngine());
ListIterator iter;
ListIter(ctx, meta, iter, count < 0);
int64 remove = 0;
while (iter.Valid())
{
if (iter.Element()->Compare(element) == 0)
{
meta.meta.len--;
meta.meta.SetFlag(COLLECTION_FLAG_NORMAL);
DelRaw(ctx, iter.CurrentRawKey());
//DelKeyValue(ctx, k);
remove++;
if (remove == toremove)
{
break;
}
}
if (count < 0)
{
iter.Prev();
}
else
{
iter.Next();
}
}
if (remove > 0)
{
SetKeyValue(ctx, meta);
}
fill_int_reply(ctx.reply, remove);
return 0;
}
示例7: LIndex
int Ardb::LIndex(Context& ctx, RedisCommandFrame& cmd)
{
int64 index;
if (!GetInt64Value(ctx, cmd.GetArguments()[1], index))
{
return 0;
}
ValueObject meta;
int err = GetMetaValue(ctx, cmd.GetArguments()[0], LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
ctx.reply.type = REDIS_REPLY_NIL;
return 0;
}
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
Data* entry = GetZipEntry(meta.meta.ziplist, index);
if (NULL != entry)
{
fill_value_reply(ctx.reply, *entry);
}
else
{
ctx.reply.type = REDIS_REPLY_NIL;
}
return 0;
}
else
{
if ((index >= 0 && index >= meta.meta.Length()) || (index < 0 && (meta.meta.Length() + index) < 0))
{
ctx.reply.type = REDIS_REPLY_NIL;
return 0;
}
ListIterator iter;
if (meta.meta.IsSequentialList())
{
err = SequencialListIter(ctx, meta, iter, index);
if (0 == err)
{
if (iter.Valid())
{
fill_value_reply(ctx.reply, *(iter.Element()));
return 0;
}
}
}
else
{
err = ListIter(ctx, meta, iter, index < 0);
uint32 cursor = index < 0 ? 1 : 0;
while (iter.Valid())
{
if (cursor == std::abs(index))
{
fill_value_reply(ctx.reply, *(iter.Element()));
return 0;
}
cursor++;
if (index >= 0)
{
iter.Next();
}
else
{
iter.Prev();
}
}
}
ctx.reply.type = REDIS_REPLY_NIL;
}
return 0;
}
示例8: ListRange
int Ardb::ListRange(Context& ctx, const Slice& key, int64 start, int64 end)
{
ValueObject meta;
int err = GetMetaValue(ctx, key, LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
/* convert negative indexes */
if (start < 0)
start = meta.meta.Length() + start;
if (end < 0)
end = meta.meta.Length() + end;
if (start < 0)
start = 0;
/* Invariant: start >= 0, so this test will be true when end < 0.
* The range is empty when start > end or start >= length. */
ctx.reply.type = REDIS_REPLY_ARRAY;
if (start > end || start >= meta.meta.Length())
{
return 0;
}
if (end >= meta.meta.Length())
end = meta.meta.Length() - 1;
int64 rangelen = (end - start) + 1;
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
uint32 i = start;
while (rangelen-- && i < meta.meta.ziplist.size())
{
RedisReply& r = ctx.reply.AddMember();
fill_value_reply(r, meta.meta.ziplist[i++]);
}
}
else
{
ListIterator iter;
if (meta.meta.IsSequentialList())
{
SequencialListIter(ctx, meta, iter, start);
uint32 count = 0;
while (iter.Valid() && count < rangelen)
{
RedisReply& r = ctx.reply.AddMember();
fill_value_reply(r, iter.Element());
count++;
iter.Next();
}
}
else
{
if (start > meta.meta.len / 2)
{
start -= meta.meta.len;
end -= meta.meta.len;
}
ListIter(ctx, meta, iter, start < 0);
int64 cursor = start < 0 ? -1 : 0;
while (iter.Valid())
{
if (cursor >= start && cursor <= end)
{
RedisReply& r = ctx.reply.AddMember();
fill_value_reply(r, iter.Element());
}
if (start < 0)
{
if (cursor < start)
{
break;
}
cursor--;
}
else
{
if (cursor > end)
{
break;
}
cursor++;
}
iter.Next();
}
}
}
return 0;
}
示例9: ListInsert
int Ardb::ListInsert(Context& ctx, ValueObject& meta, const std::string* match, const std::string& value, bool head,
bool abort_nonexist)
{
if (WakeBlockList(ctx, meta.key.key, value))
{
fill_int_reply(ctx.reply, 1);
return 0;
}
if (NULL != match)
{
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
Data element;
element.SetString(value, true);
DataArray::iterator zit = meta.meta.ziplist.begin();
while (zit != meta.meta.ziplist.end())
{
std::string tmp;
zit->GetDecodeString(tmp);
if (tmp == *match)
{
break;
}
zit++;
}
if (zit == meta.meta.ziplist.end())
{
fill_int_reply(ctx.reply, 0);
return 0;
}
if (head)
{
meta.meta.ziplist.insert(zit, element);
}
else
{
zit++;
if (zit != meta.meta.ziplist.end())
{
meta.meta.ziplist.insert(zit, element);
}
else
{
meta.meta.ziplist.push_back(element);
}
}
if (meta.meta.Length() > 1
&& (meta.meta.Length() >= m_cfg.list_max_ziplist_entries
|| element.StringLength() >= m_cfg.list_max_ziplist_value))
{
//convert to non ziplist
ZipListConvert(ctx, meta);
}
}
else
{
ListIterator iter;
ListIter(ctx, meta, iter, false);
std::string tmp;
Data prev, next;
Data current;
bool matched = false;
while (iter.Valid())
{
if (iter.Element()->GetDecodeString(tmp) == (*match))
{
current = *(iter.Score());
matched = true;
if (head)
{
break;
}
}
if (head)
{
prev = *(iter.Score());
iter.Next();
}
else
{
if (matched)
{
next = *(iter.Score());
break;
}
}
iter.Next();
}
if (!matched)
{
fill_int_reply(ctx.reply, 0);
return 0;
}
Data score;
if (head)
{
if (prev.IsNil())
{
score = current.IncrBy(-1);
}
//.........这里部分代码省略.........
示例10: ListPop
int Ardb::ListPop(Context& ctx, const std::string& key, bool lpop)
{
ValueObject meta;
KeyLockerGuard keylock(m_key_lock, ctx.currentDB, key);
int err = GetMetaValue(ctx, key, LIST_META, meta);
CHECK_ARDB_RETURN_VALUE(ctx.reply, err);
if (0 != err)
{
ctx.reply.type = REDIS_REPLY_NIL;
return 0;
}
BatchWriteGuard guard(GetKeyValueEngine(), meta.meta.Encoding() != COLLECTION_ENCODING_ZIPLIST);
if (meta.meta.Encoding() == COLLECTION_ENCODING_ZIPLIST)
{
if (!meta.meta.ziplist.empty())
{
if (lpop)
{
Data& data = meta.meta.ziplist.front();
fill_value_reply(ctx.reply, data);
meta.meta.ziplist.pop_front();
}
else
{
Data& data = meta.meta.ziplist.back();
fill_value_reply(ctx.reply, data);
meta.meta.ziplist.pop_back();
}
if (meta.meta.ziplist.empty())
{
DelKeyValue(ctx, meta.key);
}
else
{
SetKeyValue(ctx, meta);
}
}
else
{
ctx.reply.type = REDIS_REPLY_NIL;
}
return 0;
}
else
{
bool poped = false;
if (meta.meta.IsSequentialList())
{
if (meta.meta.Length() > 0)
{
ValueObject lkv;
lkv.key.type = LIST_ELEMENT;
lkv.key.key = meta.key.key;
lkv.key.db = ctx.currentDB;
lkv.key.score = lpop ? meta.meta.min_index : meta.meta.max_index;
if (0 == GetKeyValue(ctx, lkv.key, &lkv))
{
DelKeyValue(ctx, lkv.key);
if (lpop)
{
meta.meta.min_index.IncrBy(1);
}
else
{
meta.meta.max_index.IncrBy(-1);
}
meta.meta.len--;
poped = true;
fill_value_reply(ctx.reply, lkv.element);
}
}
}
else
{
ListIterator iter;
err = ListIter(ctx, meta, iter, !lpop);
while (iter.Valid())
{
if (!poped)
{
fill_value_reply(ctx.reply, *(iter.Element()));
poped = true;
meta.meta.len--;
KeyObject k;
k.type = LIST_ELEMENT;
k.key = meta.key.key;
k.db = ctx.currentDB;
k.score = *(iter.Score());
DelKeyValue(ctx, k);
}
else
{
if (lpop)
{
meta.meta.min_index = *(iter.Score());
}
else
{
meta.meta.max_index = *(iter.Score());
}
//.........这里部分代码省略.........