View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.store.derby;
21  
22  
23  import java.sql.Connection;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.sql.Statement;
27  
28  import org.apache.directory.mitosis.common.CSN;
29  import org.apache.directory.mitosis.common.DefaultCSN;
30  import org.apache.directory.mitosis.operation.Operation;
31  import org.apache.directory.mitosis.operation.OperationCodec;
32  import org.apache.directory.mitosis.store.ReplicationLogIterator;
33  import org.apache.directory.mitosis.store.ReplicationStoreException;
34  import org.apache.directory.server.schema.registries.Registries;
35  
36  
37  class DerbyReplicationLogIterator implements ReplicationLogIterator
38  {
39      private final OperationCodec codec;
40      private final Connection con;
41      private final Statement stmt;
42      private final ResultSet rs;
43  
44  
45      DerbyReplicationLogIterator( OperationCodec codec, Connection con, Statement stmt, ResultSet rs )
46      {
47          this.codec = codec;
48          this.con = con;
49          this.stmt = stmt;
50          this.rs = rs;
51      }
52  
53  
54      public boolean next()
55      {
56          try
57          {
58              return rs.next();
59          }
60          catch ( SQLException e )
61          {
62              throw new ReplicationStoreException( e );
63          }
64      }
65  
66  
67      public void close()
68      {
69          SQLUtil.cleanup( con, stmt, rs );
70      }
71  
72  
73      public CSN getCSN()
74      {
75          try
76          {
77              String replicaId = rs.getString( 1 );
78              long timestamp = rs.getLong( 2 );
79              int operationSequence = rs.getInt( 3 );
80              return new DefaultCSN( timestamp, replicaId, operationSequence );
81          }
82          catch ( Exception e )
83          {
84              throw new ReplicationStoreException( e );
85          }
86      }
87  
88  
89      public Operation getOperation( Registries registries )
90      {
91          try
92          {
93              return codec.decode( registries, rs.getBytes( 4 ) );
94          }
95          catch ( Exception e )
96          {
97              throw new ReplicationStoreException( e );
98          }
99      }
100 }