예제 #1
0
        private void FillKey(BytesWritable o)
        {
            int len = keyLenRNG.NextInt();

            if (len < MinKeyLen)
            {
                len = MinKeyLen;
            }
            o.SetSize(len);
            int n = MinKeyLen;

            while (n < len)
            {
                byte[] word = dict[random.Next(dict.Length)];
                int    l    = Math.Min(word.Length, len - n);
                System.Array.Copy(word, 0, o.Get(), n, l);
                n += l;
            }
            if (sorted && WritableComparator.CompareBytes(lastKey.Get(), MinKeyLen, lastKey.GetSize
                                                              () - MinKeyLen, o.Get(), MinKeyLen, o.GetSize() - MinKeyLen) > 0)
            {
                IncrementPrefix();
            }
            System.Array.Copy(prefix, 0, o.Get(), 0, MinKeyLen);
            lastKey.Set(o);
        }
            public virtual int Compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
            {
                int n1 = WritableUtils.DecodeVIntSize(b1[s1]);
                int n2 = WritableUtils.DecodeVIntSize(b2[s2]);

                return(-1 * WritableComparator.CompareBytes(b1, s1 + n1, l1 - n1, b2, s2 + n2, l2
                                                            - n2));
            }
예제 #3
0
 /// <summary>Compare bytes from {#getBytes()}.</summary>
 /// <seealso cref="WritableComparator.CompareBytes(byte[], int, int, byte[], int, int)
 ///     "/>
 public virtual int CompareTo(BinaryComparable other)
 {
     if (this == other)
     {
         return(0);
     }
     return(WritableComparator.CompareBytes(Bytes, 0, Length, other.Bytes, 0, other.Length));
 }
예제 #4
0
 /// <summary>Returns true if the byte array begins with the specified prefix.</summary>
 public static bool PrefixMatches(byte[] prefix, int prefixlen, byte[] b)
 {
     if (b.Length < prefixlen)
     {
         return(false);
     }
     return(WritableComparator.CompareBytes(prefix, 0, prefixlen, b, 0, prefixlen) ==
            0);
 }
예제 #5
0
        /// <summary>Returns true iff <code>o</code> is a UTF8 with the same contents.</summary>
        public override bool Equals(object o)
        {
            if (!(o is UTF8))
            {
                return(false);
            }
            UTF8 that = (UTF8)o;

            if (this.length != that.length)
            {
                return(false);
            }
            else
            {
                return(WritableComparator.CompareBytes(bytes, 0, length, that.bytes, 0, that.length
                                                       ) == 0);
            }
        }
 private static void TestEncoding(long l)
 {
     byte[] b = GenericObjectMapper.WriteReverseOrderedLong(l);
     NUnit.Framework.Assert.AreEqual("error decoding", l, GenericObjectMapper.ReadReverseOrderedLong
                                         (b, 0));
     byte[] buf = new byte[16];
     System.Array.Copy(b, 0, buf, 5, 8);
     NUnit.Framework.Assert.AreEqual("error decoding at offset", l, GenericObjectMapper
                                     .ReadReverseOrderedLong(buf, 5));
     if (l > long.MinValue)
     {
         byte[] a = GenericObjectMapper.WriteReverseOrderedLong(l - 1);
         NUnit.Framework.Assert.AreEqual("error preserving ordering", 1, WritableComparator
                                         .CompareBytes(a, 0, a.Length, b, 0, b.Length));
     }
     if (l < long.MaxValue)
     {
         byte[] c = GenericObjectMapper.WriteReverseOrderedLong(l + 1);
         NUnit.Framework.Assert.AreEqual("error preserving ordering", 1, WritableComparator
                                         .CompareBytes(b, 0, b.Length, c, 0, c.Length));
     }
 }
 public virtual int Compare(byte[] o1, byte[] o2)
 {
     return(WritableComparator.CompareBytes(o1, 0, o1.Length, o2, 0, o2.Length));
 }
 public virtual int Compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
 {
     return(WritableComparator.CompareBytes(b1, s1, l1, b2, s2, l2));
 }
예제 #9
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void Reduce(Org.Apache.Hadoop.IO.Text key, IEnumerator <Org.Apache.Hadoop.IO.Text
                                                                                   > values, OutputCollector <Org.Apache.Hadoop.IO.Text, Org.Apache.Hadoop.IO.Text>
                                       @out, Reporter reporter)
            {
                int vc = 0;
                int vlen;
                int preRec = nRec;
                int vcCheck;
                int recCheck;

                ((StringBuilder)fmt.Out()).Length = keylen;
                if (25 == key.GetLength())
                {
                    // tagged record
                    recCheck = 1;
                    switch ((char)key.GetBytes()[0])
                    {
                    case 'A':
                    {
                        // expect only 1 record
                        vlen    = GetValLen(++aKey, nMaps) - 128;
                        vcCheck = aKey;
                        // expect eq id
                        break;
                    }

                    case 'B':
                    {
                        vlen    = GetValLen(++bKey, nMaps);
                        vcCheck = bKey;
                        // expect eq id
                        break;
                    }

                    default:
                    {
                        vlen = vcCheck = -1;
                        Fail("Unexpected tag on record: " + ((char)key.GetBytes()[24]));
                        break;
                    }
                    }
                    kb.Set((char)key.GetBytes()[0] + fmt.Format(tagfmt, vcCheck).ToString());
                }
                else
                {
                    kb.Set(fmt.Format(tagfmt, ++nKey).ToString());
                    vlen     = 1000;
                    recCheck = nMaps;
                    // expect 1 rec per map
                    vcCheck = (int)(((uint)(nMaps * (nMaps - 1))) >> 1);
                }
                // expect eq sum(id)
                NUnit.Framework.Assert.AreEqual(kb, key);
                while (values.HasNext())
                {
                    Org.Apache.Hadoop.IO.Text val = values.Next();
                    // increment vc by map ID assoc w/ val
                    vc += val.GetBytes()[0];
                    // verify that all the fixed characters 'V' match
                    NUnit.Framework.Assert.AreEqual(0, WritableComparator.CompareBytes(vb.GetBytes(),
                                                                                       1, vlen - 1, val.GetBytes(), 1, val.GetLength() - 1));
                    @out.Collect(key, val);
                    ++nRec;
                }
                NUnit.Framework.Assert.AreEqual("Bad rec count for " + key, recCheck, nRec - preRec
                                                );
                NUnit.Framework.Assert.AreEqual("Bad rec group for " + key, vcCheck, vc);
            }
예제 #10
0
 /// <summary>Compare two UTF8s.</summary>
 public virtual int CompareTo(UTF8 o)
 {
     return(WritableComparator.CompareBytes(bytes, 0, length, o.bytes, 0, o.length));
 }
예제 #11
0
        public virtual void TestReadWriteStorage <T>()
            where T : TokenIdentifier
        {
            // create tokenStorage Object
            Credentials ts = new Credentials();

            Org.Apache.Hadoop.Security.Token.Token <T> token1 = new Org.Apache.Hadoop.Security.Token.Token
                                                                    ();
            Org.Apache.Hadoop.Security.Token.Token <T> token2 = new Org.Apache.Hadoop.Security.Token.Token
                                                                    ();
            Text service1 = new Text("service1");
            Text service2 = new Text("service2");
            ICollection <Text> services = new AList <Text>();

            services.AddItem(service1);
            services.AddItem(service2);
            token1.SetService(service1);
            token2.SetService(service2);
            ts.AddToken(new Text("sometoken1"), token1);
            ts.AddToken(new Text("sometoken2"), token2);
            // create keys and put it in
            KeyGenerator kg              = KeyGenerator.GetInstance(DefaultHmacAlgorithm);
            string       alias           = "alias";
            IDictionary <Text, byte[]> m = new Dictionary <Text, byte[]>(10);

            for (int i = 0; i < 10; i++)
            {
                Key key = kg.GenerateKey();
                m[new Text(alias + i)] = key.GetEncoded();
                ts.AddSecretKey(new Text(alias + i), key.GetEncoded());
            }
            // create file to store
            FilePath         tmpFileName = new FilePath(tmpDir, "tokenStorageTest");
            DataOutputStream dos         = new DataOutputStream(new FileOutputStream(tmpFileName));

            ts.Write(dos);
            dos.Close();
            // open and read it back
            DataInputStream dis = new DataInputStream(new FileInputStream(tmpFileName));

            ts = new Credentials();
            ts.ReadFields(dis);
            dis.Close();
            // get the tokens and compare the services
            ICollection <Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> > list = ts.GetAllTokens
                                                                                               ();

            Assert.Equal("getAllTokens should return collection of size 2"
                         , list.Count, 2);
            bool foundFirst  = false;
            bool foundSecond = false;

            foreach (Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> token in list)
            {
                if (token.GetService().Equals(service1))
                {
                    foundFirst = true;
                }
                if (token.GetService().Equals(service2))
                {
                    foundSecond = true;
                }
            }
            Assert.True("Tokens for services service1 and service2 must be present"
                        , foundFirst && foundSecond);
            // compare secret keys
            int mapLen = m.Count;

            Assert.Equal("wrong number of keys in the Storage", mapLen, ts
                         .NumberOfSecretKeys());
            foreach (Text a in m.Keys)
            {
                byte[] kTS    = ts.GetSecretKey(a);
                byte[] kLocal = m[a];
                Assert.True("keys don't match for " + a, WritableComparator.CompareBytes
                                (kTS, 0, kTS.Length, kLocal, 0, kLocal.Length) == 0);
            }
            tmpFileName.Delete();
        }
예제 #12
0
 /// <summary>Compare bytes from {#getBytes()} to those provided.</summary>
 public virtual int CompareTo(byte[] other, int off, int len)
 {
     return(WritableComparator.CompareBytes(Bytes, 0, Length, other, off, len));
 }