/// <exception cref="System.IO.IOException"/>
 private void LoadLatestSequenceNumber(TimelineStateStore.TimelineServiceState state
                                       )
 {
     byte[] data = null;
     try
     {
         data = db.Get(LatestSequenceNumberKey);
     }
     catch (DBException e)
     {
         throw new IOException(e);
     }
     if (data != null)
     {
         DataInputStream @in = new DataInputStream(new ByteArrayInputStream(data));
         try
         {
             state.latestSequenceNumber = @in.ReadInt();
         }
         finally
         {
             IOUtils.Cleanup(Log, @in);
         }
     }
 }
        /// <exception cref="System.IO.IOException"/>
        private int LoadTokens(TimelineStateStore.TimelineServiceState state)
        {
            byte[] @base = LeveldbUtils.KeyBuilder.NewInstance().Add(TokenEntryPrefix).GetBytesForLookup
                               ();
            int             numTokens = 0;
            LeveldbIterator iterator  = null;

            try
            {
                for (iterator = new LeveldbIterator(db), iterator.Seek(@base); iterator.HasNext()
                     ; iterator.Next())
                {
                    byte[] k = iterator.PeekNext().Key;
                    if (!LeveldbUtils.PrefixMatches(@base, @base.Length, k))
                    {
                        break;
                    }
                    byte[] v = iterator.PeekNext().Value;
                    LoadTokenData(state, v);
                    ++numTokens;
                }
            }
            catch (DBException e)
            {
                throw new IOException(e);
            }
            finally
            {
                IOUtils.Cleanup(Log, iterator);
            }
            return(numTokens);
        }
 /// <exception cref="System.IO.IOException"/>
 public override TimelineStateStore.TimelineServiceState LoadState()
 {
     TimelineStateStore.TimelineServiceState result = new TimelineStateStore.TimelineServiceState
                                                          ();
     result.tokenState.PutAll(state.tokenState);
     Sharpen.Collections.AddAll(result.tokenMasterKeyState, state.tokenMasterKeyState);
     result.latestSequenceNumber = state.latestSequenceNumber;
     return(result);
 }
        /// <exception cref="System.IO.IOException"/>
        public override TimelineStateStore.TimelineServiceState LoadState()
        {
            Log.Info("Loading timeline service state from leveldb");
            TimelineStateStore.TimelineServiceState state = new TimelineStateStore.TimelineServiceState
                                                                ();
            int numKeys   = LoadTokenMasterKeys(state);
            int numTokens = LoadTokens(state);

            LoadLatestSequenceNumber(state);
            Log.Info("Loaded " + numKeys + " master keys and " + numTokens + " tokens from leveldb, and latest sequence number is "
                     + state.GetLatestSequenceNumber());
            return(state);
        }
        /// <exception cref="System.IO.IOException"/>
        private static void LoadTokenMasterKeyData(TimelineStateStore.TimelineServiceState
                                                   state, byte[] keyData)
        {
            DelegationKey   key = new DelegationKey();
            DataInputStream @in = new DataInputStream(new ByteArrayInputStream(keyData));

            try
            {
                key.ReadFields(@in);
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
            }
            state.tokenMasterKeyState.AddItem(key);
        }
        /// <exception cref="System.IO.IOException"/>
        private static void LoadTokenData(TimelineStateStore.TimelineServiceState state,
                                          byte[] tokenData)
        {
            TimelineDelegationTokenIdentifierData data = new TimelineDelegationTokenIdentifierData
                                                             ();
            DataInputStream @in = new DataInputStream(new ByteArrayInputStream(tokenData));

            try
            {
                data.ReadFields(@in);
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
            }
            state.tokenState[data.GetTokenIdentifier()] = data.GetRenewDate();
        }
        public virtual void TestTokenStore()
        {
            InitAndStartTimelineServiceStateStoreService();
            TimelineStateStore.TimelineServiceState state = store.LoadState();
            NUnit.Framework.Assert.IsTrue("token state not empty", state.tokenState.IsEmpty()
                                          );
            NUnit.Framework.Assert.IsTrue("key state not empty", state.tokenMasterKeyState.IsEmpty
                                              ());
            DelegationKey key1 = new DelegationKey(1, 2, Sharpen.Runtime.GetBytesForString("keyData1"
                                                                                           ));
            TimelineDelegationTokenIdentifier token1 = new TimelineDelegationTokenIdentifier(
                new Text("tokenOwner1"), new Text("tokenRenewer1"), new Text("tokenUser1"));

            token1.SetSequenceNumber(1);
            token1.GetBytes();
            long tokenDate1 = 1L;
            TimelineDelegationTokenIdentifier token2 = new TimelineDelegationTokenIdentifier(
                new Text("tokenOwner2"), new Text("tokenRenewer2"), new Text("tokenUser2"));

            token2.SetSequenceNumber(12345678);
            token2.GetBytes();
            long tokenDate2 = 87654321L;

            store.StoreTokenMasterKey(key1);
            try
            {
                store.StoreTokenMasterKey(key1);
                NUnit.Framework.Assert.Fail("redundant store of key undetected");
            }
            catch (IOException)
            {
            }
            // expected
            store.StoreToken(token1, tokenDate1);
            store.StoreToken(token2, tokenDate2);
            try
            {
                store.StoreToken(token1, tokenDate1);
                NUnit.Framework.Assert.Fail("redundant store of token undetected");
            }
            catch (IOException)
            {
            }
            // expected
            store.Close();
            InitAndStartTimelineServiceStateStoreService();
            state = store.LoadState();
            NUnit.Framework.Assert.AreEqual("incorrect loaded token count", 2, state.tokenState
                                            .Count);
            NUnit.Framework.Assert.IsTrue("missing token 1", state.tokenState.Contains(token1
                                                                                       ));
            NUnit.Framework.Assert.AreEqual("incorrect token 1 date", tokenDate1, state.tokenState
                                            [token1]);
            NUnit.Framework.Assert.IsTrue("missing token 2", state.tokenState.Contains(token2
                                                                                       ));
            NUnit.Framework.Assert.AreEqual("incorrect token 2 date", tokenDate2, state.tokenState
                                            [token2]);
            NUnit.Framework.Assert.AreEqual("incorrect master key count", 1, state.tokenMasterKeyState
                                            .Count);
            NUnit.Framework.Assert.IsTrue("missing master key 1", state.tokenMasterKeyState.Contains
                                              (key1));
            NUnit.Framework.Assert.AreEqual("incorrect latest sequence number", 12345678, state
                                            .GetLatestSequenceNumber());
            DelegationKey key2 = new DelegationKey(3, 4, Sharpen.Runtime.GetBytesForString("keyData2"
                                                                                           ));
            DelegationKey key3 = new DelegationKey(5, 6, Sharpen.Runtime.GetBytesForString("keyData3"
                                                                                           ));
            TimelineDelegationTokenIdentifier token3 = new TimelineDelegationTokenIdentifier(
                new Text("tokenOwner3"), new Text("tokenRenewer3"), new Text("tokenUser3"));

            token3.SetSequenceNumber(12345679);
            token3.GetBytes();
            long tokenDate3 = 87654321L;

            store.RemoveToken(token1);
            store.StoreTokenMasterKey(key2);
            long newTokenDate2 = 975318642L;

            store.UpdateToken(token2, newTokenDate2);
            store.RemoveTokenMasterKey(key1);
            store.StoreTokenMasterKey(key3);
            store.StoreToken(token3, tokenDate3);
            store.Close();
            InitAndStartTimelineServiceStateStoreService();
            state = store.LoadState();
            NUnit.Framework.Assert.AreEqual("incorrect loaded token count", 2, state.tokenState
                                            .Count);
            NUnit.Framework.Assert.IsFalse("token 1 not removed", state.tokenState.Contains(token1
                                                                                            ));
            NUnit.Framework.Assert.IsTrue("missing token 2", state.tokenState.Contains(token2
                                                                                       ));
            NUnit.Framework.Assert.AreEqual("incorrect token 2 date", newTokenDate2, state.tokenState
                                            [token2]);
            NUnit.Framework.Assert.IsTrue("missing token 3", state.tokenState.Contains(token3
                                                                                       ));
            NUnit.Framework.Assert.AreEqual("incorrect token 3 date", tokenDate3, state.tokenState
                                            [token3]);
            NUnit.Framework.Assert.AreEqual("incorrect master key count", 2, state.tokenMasterKeyState
                                            .Count);
            NUnit.Framework.Assert.IsFalse("master key 1 not removed", state.tokenMasterKeyState
                                           .Contains(key1));
            NUnit.Framework.Assert.IsTrue("missing master key 2", state.tokenMasterKeyState.Contains
                                              (key2));
            NUnit.Framework.Assert.IsTrue("missing master key 3", state.tokenMasterKeyState.Contains
                                              (key3));
            NUnit.Framework.Assert.AreEqual("incorrect latest sequence number", 12345679, state
                                            .GetLatestSequenceNumber());
            store.Close();
        }
 /// <exception cref="System.IO.IOException"/>
 protected internal override void CloseStorage()
 {
     state = null;
 }
 /// <exception cref="System.IO.IOException"/>
 protected internal override void StartStorage()
 {
     state = new TimelineStateStore.TimelineServiceState();
 }