/// <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);
        }
        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();
        }