public void OperateAddFlags()
        {
            int index_bits = 4;

            // Keep record around win binName is removed.
            AssertSuccess("other bin", key, Operation.Delete(), HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits));

            // create_only
            HLLPolicy c = new HLLPolicy(HLLWriteFlags.CREATE_ONLY);

            AssertSuccess("create_only", key, HLLOperation.Add(c, binName, entries, index_bits));
            AssertThrows("create_only - error", key, typeof(AerospikeException), ResultCode.BIN_EXISTS_ERROR,
                         HLLOperation.Add(c, binName, entries, index_bits));

            // update_only
            HLLPolicy u = new HLLPolicy(HLLWriteFlags.UPDATE_ONLY);

            AssertThrows("update_only - error", key, typeof(AerospikeException), ResultCode.PARAMETER_ERROR,
                         HLLOperation.Add(u, binName, entries, index_bits));

            // create_only no_fail
            HLLPolicy cn = new HLLPolicy(HLLWriteFlags.CREATE_ONLY | HLLWriteFlags.NO_FAIL);

            AssertSuccess("create_only nofail", key, HLLOperation.Add(cn, binName, entries, index_bits));
            AssertSuccess("create_only nofail - no error", key, HLLOperation.Add(cn, binName, entries, index_bits));

            // fold
            AssertSuccess("init", key, HLLOperation.Init(HLLPolicy.Default, binName, index_bits));

            HLLPolicy f = new HLLPolicy(HLLWriteFlags.ALLOW_FOLD);

            AssertThrows("fold", key, typeof(AerospikeException), ResultCode.PARAMETER_ERROR,
                         HLLOperation.Add(f, binName, entries, index_bits));
        }
        public void GetPut()
        {
            foreach (List <int> desc in legalDescriptions)
            {
                int index_bits   = desc[0];
                int minhash_bits = desc[1];

                AssertSuccess("init record", key, Operation.Delete(), HLLOperation.Init(HLLPolicy.Default, binName, index_bits, minhash_bits));

                Record         record = client.Get(null, key);
                Value.HLLValue hll    = record.GetHLLValue(binName);

                client.Delete(null, key);
                client.Put(null, key, new Bin(binName, hll));

                record = AssertSuccess("describe", key, HLLOperation.GetCount(binName), HLLOperation.Describe(binName));

                IList result_list = record.GetList(binName);
                long  count       = (long)result_list[0];
                IList description = (IList)result_list[1];

                Assert.AreEqual(0, count);
                AssertDescription("Check description", description, index_bits, minhash_bits);
            }
        }
        public void AssertInit(int index_bits, int minhash_bits, bool should_pass)
        {
            string    msg = "Fail - index_bits " + index_bits + " minhash_bits " + minhash_bits;
            HLLPolicy p   = HLLPolicy.Default;

            Operation[] ops = new Operation[] {
                HLLOperation.Init(p, binName, index_bits, minhash_bits),
                HLLOperation.GetCount(binName),
                HLLOperation.RefreshCount(binName),
                HLLOperation.Describe(binName)
            };

            if (!should_pass)
            {
                AssertThrows(msg, key, typeof(AerospikeException), ResultCode.PARAMETER_ERROR, ops);
                return;
            }

            Record record      = AssertSuccess(msg, key, ops);
            IList  result_list = record.GetList(binName);
            long   count       = (long)result_list[1];
            long   count1      = (long)result_list[2];
            IList  description = (IList)result_list[3];

            AssertDescription(msg, description, index_bits, minhash_bits);
            Assert.AreEqual(0, count);
            Assert.AreEqual(0, count1);
        }
        public void OperateSetUnionFlags()
        {
            int    index_bits  = 6;
            int    low_n_bits  = 4;
            int    high_n_bits = 8;
            string otherName   = binName + "o";

            // Keep record around win binName is removed.
            List <Value.HLLValue> hlls = new List <Value.HLLValue>();
            Record record = AssertSuccess("other bin", key,
                                          Operation.Delete(),
                                          HLLOperation.Add(HLLPolicy.Default, otherName, entries, index_bits),
                                          Operation.Get(otherName)
                                          );
            IList result_list = record.GetList(otherName);

            Value.HLLValue hll = (Value.HLLValue)result_list[1];

            hlls.Add(hll);

            // create_only
            HLLPolicy c = new HLLPolicy(HLLWriteFlags.CREATE_ONLY);

            AssertSuccess("create_only", key, HLLOperation.SetUnion(c, binName, hlls));
            AssertThrows("create_only - error", key, typeof(AerospikeException), ResultCode.BIN_EXISTS_ERROR, HLLOperation.SetUnion(c, binName, hlls));

            // update_only
            HLLPolicy u = new HLLPolicy(HLLWriteFlags.UPDATE_ONLY);

            AssertSuccess("update_only", key, HLLOperation.SetUnion(u, binName, hlls));
            AssertSuccess("remove bin", key, Operation.Put(Bin.AsNull(binName)));
            AssertThrows("update_only - error", key, typeof(AerospikeException), ResultCode.BIN_NOT_FOUND, HLLOperation.SetUnion(u, binName, hlls));

            // create_only no_fail
            HLLPolicy cn = new HLLPolicy(HLLWriteFlags.CREATE_ONLY | HLLWriteFlags.NO_FAIL);

            AssertSuccess("create_only nofail", key, HLLOperation.SetUnion(cn, binName, hlls));
            AssertSuccess("create_only nofail - no error", key, HLLOperation.SetUnion(cn, binName, hlls));

            // update_only no_fail
            HLLPolicy un = new HLLPolicy(HLLWriteFlags.UPDATE_ONLY | HLLWriteFlags.NO_FAIL);

            AssertSuccess("update_only nofail", key, HLLOperation.SetUnion(un, binName, hlls));
            AssertSuccess("remove bin", key, Operation.Put(Bin.AsNull(binName)));
            AssertSuccess("update_only nofail - no error", key, HLLOperation.SetUnion(un, binName, hlls));

            // fold
            HLLPolicy f = new HLLPolicy(HLLWriteFlags.ALLOW_FOLD);

            // fold down
            AssertSuccess("size up", key, HLLOperation.Init(HLLPolicy.Default, binName, high_n_bits));
            AssertSuccess("fold down to index_bits", key, HLLOperation.SetUnion(f, binName, hlls));

            // fold up
            AssertSuccess("size down", key, HLLOperation.Init(HLLPolicy.Default, binName, low_n_bits));
            AssertSuccess("fold down to low_n_bits", key, HLLOperation.SetUnion(f, binName, hlls));
        }
        public void OperateRefreshCount()
        {
            int index_bits = 6;

            // Keep record around win binName is removed.
            AssertSuccess("other bin", key, Operation.Delete(), HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits), HLLOperation.Init(HLLPolicy.Default, binName, index_bits));

            // Exists.
            AssertSuccess("refresh zero count", key, HLLOperation.RefreshCount(binName), HLLOperation.RefreshCount(binName));
            AssertSuccess("add items", key, HLLOperation.Add(HLLPolicy.Default, binName, entries));
            AssertSuccess("refresh count", key, HLLOperation.RefreshCount(binName), HLLOperation.RefreshCount(binName));

            // Does not exist.
            AssertSuccess("remove bin", key, Operation.Put(Bin.AsNull(binName)));
            AssertThrows("refresh nonexistant count", key, typeof(AerospikeException), ResultCode.BIN_NOT_FOUND, HLLOperation.RefreshCount(binName));
        }
        public void OperateGetUnion()
        {
            int                    index_bits           = 14;
            long                   expected_union_count = 0;
            List <IList>           vals = new List <IList>();
            IList <Value.HLLValue> hlls = new List <Value.HLLValue>();

            for (int i = 0; i < keys.Length; i++)
            {
                List <Value> sub_vals = new List <Value>();

                for (int j = 0; j < n_entries / 3; j++)
                {
                    sub_vals.Add(new Value.StringValue("key" + i + " " + j));
                }

                Record record = AssertSuccess("init other keys", keys[i], Operation.Delete(), HLLOperation.Add(HLLPolicy.Default, binName, sub_vals, index_bits), Operation.Get(binName));

                IList result_list = record.GetList(binName);
                hlls.Add((Value.HLLValue)result_list[1]);
                expected_union_count += sub_vals.Count;
                vals.Add(sub_vals);
            }

            // Keep record around win binName is removed.
            AssertSuccess("other bin", key,
                          Operation.Delete(),
                          HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits),
                          HLLOperation.Add(HLLPolicy.Default, binName, vals[0], index_bits)
                          );

            Record r           = AssertSuccess("union and unionCount", key, HLLOperation.GetUnion(binName, hlls), HLLOperation.GetUnionCount(binName, hlls));
            IList  rlist       = r.GetList(binName);
            long   union_count = (long)rlist[1];

            AssertHLLCount("verify union count", index_bits, union_count, expected_union_count);

            Value.HLLValue union_hll = (Value.HLLValue)rlist[0];

            r     = AssertSuccess("", key, Operation.Put(new Bin(binName, union_hll)), HLLOperation.GetCount(binName));
            rlist = r.GetList(binName);
            long union_count_2 = (long)rlist[1];

            Assert.AreEqual(union_count, union_count_2, "unions equal");
        }
        public void OperateFoldExists()
        {
            int index_bits = 10;
            int fold_down  = 4;
            int fold_up    = 16;

            // Keep record around win binName is removed.
            AssertSuccess("other bin", key, Operation.Delete(), HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits), HLLOperation.Init(HLLPolicy.Default, binName, index_bits));

            // Exists.
            AssertSuccess("exists fold down", key, HLLOperation.Fold(binName, fold_down));
            AssertThrows("exists fold up", key, typeof(AerospikeException), ResultCode.OP_NOT_APPLICABLE, HLLOperation.Fold(binName, fold_up));

            // Does not exist.
            AssertSuccess("remove bin", key, Operation.Put(Bin.AsNull(binName)));

            AssertThrows("create_only - error", key, typeof(AerospikeException), ResultCode.BIN_NOT_FOUND, HLLOperation.Fold(binName, fold_down));
        }
        public void AssertSimilarityOp(double overlap, IList common, IList <IList> vals, int index_bits, int minhash_bits)
        {
            IList <Value.HLLValue> hlls = new List <Value.HLLValue>();

            for (int i = 0; i < keys.Length; i++)
            {
                Record record = AssertSuccess("init other keys", keys[i],
                                              Operation.Delete(),
                                              HLLOperation.Add(HLLPolicy.Default, binName, vals[i], index_bits, minhash_bits),
                                              HLLOperation.Add(HLLPolicy.Default, binName, common, index_bits, minhash_bits),
                                              Operation.Get(binName)
                                              );

                IList result_list = record.GetList(binName);
                hlls.Add((Value.HLLValue)result_list[2]);
            }

            // Keep record around win binName is removed.
            Record r = AssertSuccess("other bin", key,
                                     Operation.Delete(),
                                     HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits, minhash_bits),
                                     HLLOperation.SetUnion(HLLPolicy.Default, binName, hlls),
                                     HLLOperation.Describe(binName)
                                     );

            IList rlist       = r.GetList(binName);
            IList description = (IList)rlist[1];

            AssertDescription("check desc", description, index_bits, minhash_bits);

            r = AssertSuccess("similarity and intersect_count", key,
                              HLLOperation.GetSimilarity(binName, hlls),
                              HLLOperation.GetIntersectCount(binName, hlls)
                              );

            rlist = r.GetList(binName);
            double sim                      = (double)rlist[0];
            long   intersect_count          = (long)rlist[1];
            double expected_similarity      = overlap;
            long   expected_intersect_count = common.Count;

            AssertHMHSimilarity("check sim", index_bits, minhash_bits, sim, expected_similarity, intersect_count, expected_intersect_count);
        }
        public void OperateGetCount()
        {
            int index_bits = 6;

            // Keep record around win binName is removed.
            AssertSuccess("other bin", key, Operation.Delete(), HLLOperation.Init(HLLPolicy.Default, binName + "other", index_bits),
                          HLLOperation.Add(HLLPolicy.Default, binName, entries, index_bits));

            // Exists.
            Record record = AssertSuccess("exists count", key, HLLOperation.GetCount(binName));
            long   count  = record.GetLong(binName);

            AssertHLLCount("check count", index_bits, count, entries.Count);

            // Does not exist.
            AssertSuccess("remove bin", key, Operation.Put(Bin.AsNull(binName)));
            record = AssertSuccess("exists count", key, HLLOperation.GetCount(binName));
            Assert.AreEqual(null, record.GetValue(binName));
        }
        public void AssertSetUnion(IList <IList> vals, int index_bits, bool folding, bool allow_folding)
        {
            string    msg = "Fail - index_bits " + index_bits;
            HLLPolicy p   = HLLPolicy.Default;
            HLLPolicy u   = HLLPolicy.Default;

            if (allow_folding)
            {
                u = new HLLPolicy(HLLWriteFlags.ALLOW_FOLD);
            }

            long union_expected = 0;
            bool folded         = false;

            for (int i = 0; i < keys.Length; i++)
            {
                int ix = index_bits;

                if (folding)
                {
                    ix -= i;

                    if (ix < minIndexBits)
                    {
                        ix = minIndexBits;
                    }

                    if (ix < index_bits)
                    {
                        folded = true;
                    }
                }

                IList sub_vals = vals[i];

                union_expected += sub_vals.Count;

                Record record = AssertSuccess(msg, keys[i], Operation.Delete(),
                                              HLLOperation.Add(p, binName, sub_vals, ix), HLLOperation.GetCount(binName));
                IList result_list = record.GetList(binName);
                long  count       = (long)result_list[1];

                AssertHLLCount(msg, ix, count, sub_vals.Count);
            }

            List <Value.HLLValue> hlls = new List <Value.HLLValue>();

            for (int i = 0; i < keys.Length; i++)
            {
                Record         record      = AssertSuccess(msg, keys[i], Operation.Get(binName), HLLOperation.GetCount(binName));
                IList          result_list = record.GetList(binName);
                Value.HLLValue hll         = (Value.HLLValue)result_list[0];

                Assert.AreNotEqual(null, hll);
                hlls.Add(hll);
            }

            Operation[] ops = new Operation[] {
                Operation.Delete(),
                        HLLOperation.Init(p, binName, index_bits),
                HLLOperation.SetUnion(u, binName, hlls),
                HLLOperation.GetCount(binName),
                Operation.Delete(),
                HLLOperation.SetUnion(p, binName, hlls),
                HLLOperation.GetCount(binName)
            };

            if (folded && !allow_folding)
            {
                AssertThrows(msg, key, typeof(AerospikeException), ResultCode.OP_NOT_APPLICABLE, ops);
                return;
            }

            Record record_union      = AssertSuccess(msg, key, ops);
            IList  union_result_list = record_union.GetList(binName);
            long   union_count       = (long)union_result_list[2];
            long   union_count2      = (long)union_result_list[4];

            AssertHLLCount(msg, index_bits, union_count, union_expected);
            Assert.AreEqual(union_count, union_count2, msg);

            for (int i = 0; i < keys.Length; i++)
            {
                IList  sub_vals = vals[i];
                Record record   = AssertSuccess(msg, key,
                                                HLLOperation.Add(p, binName, sub_vals, index_bits),
                                                HLLOperation.GetCount(binName));

                IList result_list = record.GetList(binName);
                long  n_added     = (long)result_list[0];
                long  count       = (long)result_list[1];

                Assert.AreEqual(0, n_added, msg);
                Assert.AreEqual(union_count, count, msg);
                AssertHLLCount(msg, index_bits, count, union_expected);
            }
        }