public void OperateMapPut() { if (!args.ValidateMap()) { return; } Key key = new Key(args.ns, args.set, "opmkey1"); client.Delete(null, key); MapPolicy putMode = MapPolicy.Default; MapPolicy addMode = new MapPolicy(MapOrder.UNORDERED, MapWriteMode.CREATE_ONLY); MapPolicy updateMode = new MapPolicy(MapOrder.UNORDERED, MapWriteMode.UPDATE_ONLY); MapPolicy orderedUpdateMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.UPDATE_ONLY); // Calling put() multiple times performs poorly because the server makes // a copy of the map for each call, but we still need to test it. // putItems() should be used instead for best performance. Record record = client.Operate(null, key, MapOperation.Put(putMode, binName, Value.Get(11), Value.Get(789)), MapOperation.Put(putMode, binName, Value.Get(10), Value.Get(999)), MapOperation.Put(addMode, binName, Value.Get(12), Value.Get(500)), MapOperation.Put(addMode, binName, Value.Get(15), Value.Get(1000)), // Ordered type should be ignored since map has already been created in first put(). MapOperation.Put(orderedUpdateMode, binName, Value.Get(10), Value.Get(1)), MapOperation.Put(updateMode, binName, Value.Get(15), Value.Get(5)) ); AssertRecordFound(key, record); IList results = record.GetList(binName); int i = 0; long size = (long)results[i++]; Assert.AreEqual(1, size); size = (long)results[i++]; Assert.AreEqual(2, size); size = (long)results[i++]; Assert.AreEqual(3, size); size = (long)results[i++]; Assert.AreEqual(4, size); size = (long)results[i++]; Assert.AreEqual(4, size); size = (long)results[i++]; Assert.AreEqual(4, size); record = client.Get(null, key, binName); IDictionary map = record.GetMap(binName); Assert.AreEqual(4, map.Count); Assert.AreEqual(1L, map[10L]); }
/// <summary> /// Create expression that writes each map item to map bin. /// </summary> public static Exp PutItems(MapPolicy policy, Exp map, Exp bin, params CTX[] ctx) { Packer packer = new Packer(); if (policy.flags != 0) { PackUtil.Init(packer, ctx); packer.PackArrayBegin(4); packer.PackNumber(MapOperation.PUT_ITEMS); map.Pack(packer); packer.PackNumber(policy.attributes); packer.PackNumber(policy.flags); } else { if (policy.itemsCommand == MapOperation.REPLACE_ITEMS) { // Replace doesn't allow map attributes because it does not create on non-existing key. PackUtil.Init(packer, ctx); packer.PackArrayBegin(2); packer.PackNumber(policy.itemsCommand); map.Pack(packer); } else { PackUtil.Init(packer, ctx); packer.PackArrayBegin(3); packer.PackNumber(policy.itemsCommand); map.Pack(packer); packer.PackNumber(policy.attributes); } } byte[] bytes = packer.ToByteArray(); return(AddWrite(bin, bytes, ctx)); }
public void OperateMapScore() { // Test score. Key key = new Key(args.ns, args.set, "opmkey10"); client.Delete(null, key); MapPolicy mapPolicy = new MapPolicy(MapOrder.KEY_VALUE_ORDERED, MapWriteMode.UPDATE); Dictionary <Value, Value> inputMap = new Dictionary <Value, Value>(); inputMap[Value.Get("weiling")] = Value.Get(0); inputMap[Value.Get("briann")] = Value.Get(0); inputMap[Value.Get("brianb")] = Value.Get(0); inputMap[Value.Get("meher")] = Value.Get(0); // Create map. Record record = client.Operate(null, key, MapOperation.PutItems(mapPolicy, binName, inputMap)); AssertRecordFound(key, record); // Change scores record = client.Operate(null, key, MapOperation.Increment(mapPolicy, binName, Value.Get("weiling"), Value.Get(10)), MapOperation.Increment(mapPolicy, binName, Value.Get("briann"), Value.Get(20)), MapOperation.Increment(mapPolicy, binName, Value.Get("brianb"), Value.Get(1)), MapOperation.Increment(mapPolicy, binName, Value.Get("meher"), Value.Get(20)) ); AssertRecordFound(key, record); // Query top 3 scores record = client.Operate(null, key, MapOperation.GetByRankRange(binName, -3, 3, MapReturnType.KEY)); AssertRecordFound(key, record); // Remove people with score 10 and display top 3 again record = client.Operate(null, key, MapOperation.RemoveByValue(binName, Value.Get(10), MapReturnType.KEY), MapOperation.GetByRankRange(binName, -3, 3, MapReturnType.KEY) ); AssertRecordFound(key, record); IList results = record.GetList(binName); int i = 0; IList list = (IList)results[i++]; string s = (string)list[0]; Assert.AreEqual("weiling", s); list = (IList)results[i++]; s = (string)list[0]; Assert.AreEqual("brianb", s); s = (string)list[1]; Assert.AreEqual("briann", s); s = (string)list[2]; Assert.AreEqual("meher", s); }
public void OperateMapPutItems() { if (!args.ValidateMap()) { return; } Key key = new Key(args.ns, args.set, "opmkey2"); client.Delete(null, key); Dictionary <Value, Value> addMap = new Dictionary <Value, Value>(); addMap[Value.Get(12)] = Value.Get("myval"); addMap[Value.Get(-8734)] = Value.Get("str2"); addMap[Value.Get(1)] = Value.Get("my default"); Dictionary <Value, Value> putMap = new Dictionary <Value, Value>(); putMap[Value.Get(12)] = Value.Get("myval12222"); putMap[Value.Get(13)] = Value.Get("str13"); Dictionary <Value, Value> updateMap = new Dictionary <Value, Value>(); updateMap[Value.Get(13)] = Value.Get("myval2"); Dictionary <Value, Value> replaceMap = new Dictionary <Value, Value>(); replaceMap[Value.Get(12)] = Value.Get(23); replaceMap[Value.Get(-8734)] = Value.Get("changed"); MapPolicy putMode = MapPolicy.Default; MapPolicy addMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.CREATE_ONLY); MapPolicy updateMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.UPDATE_ONLY); Record record = client.Operate(null, key, MapOperation.PutItems(addMode, binName, addMap), MapOperation.PutItems(putMode, binName, putMap), MapOperation.PutItems(updateMode, binName, updateMap), MapOperation.PutItems(updateMode, binName, replaceMap), MapOperation.GetByKey(binName, Value.Get(1), MapReturnType.VALUE), MapOperation.GetByKey(binName, Value.Get(-8734), MapReturnType.VALUE), MapOperation.GetByKeyRange(binName, Value.Get(12), Value.Get(15), MapReturnType.KEY_VALUE) ); AssertRecordFound(key, record); IList results = record.GetList(binName); int i = 0; long size = (long)results[i++]; Assert.AreEqual(3, size); size = (long)results[i++]; Assert.AreEqual(4, size); size = (long)results[i++]; Assert.AreEqual(4, size); size = (long)results[i++]; Assert.AreEqual(4, size); string str = (string)results[i++]; Assert.AreEqual("my default", str); str = (string)results[i++]; Assert.AreEqual("changed", str); IList list = (IList)results[i++]; Assert.AreEqual(2, list.Count); }
/// <summary> /// Create expression that increments values by incr for all items identified by key. /// Valid only for numbers. /// </summary> public static Exp Increment(MapPolicy policy, Exp key, Exp incr, Exp bin, params CTX[] ctx) { byte[] bytes = PackUtil.Pack(MapOperation.INCREMENT, key, incr, policy.attributes, ctx); return(AddWrite(bin, bytes, ctx)); }