View Javadoc
1   /*
2    * Tentackle - https://tentackle.org
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  
19  package org.tentackle.dbms.trip;
20  
21  import org.tentackle.dbms.DbModificationTracker;
22  import org.tentackle.misc.IdSerialNameTuple;
23  import org.tentackle.misc.IdSerialTuple;
24  import org.tentackle.session.MasterSerial;
25  import org.tentackle.session.MasterSerialEvent;
26  import org.tentackle.session.MasterSerialListEvent;
27  import org.tentackle.session.ModificationTracker;
28  
29  import java.util.List;
30  
31  /**
32   * Remote delegate implementation for {@link DbModificationTracker}.
33   *
34   * @param <T> the modification tracker class
35   * @author  harald
36   */
37  public class DbModificationTrackerRemoteDelegateImpl<T extends DbModificationTracker>
38               extends RemoteDbDelegateImpl<T>
39               implements DbModificationTrackerRemoteDelegate {
40  
41    public DbModificationTrackerRemoteDelegateImpl(RemoteDbSessionImpl session, Class<T> clazz) {
42      super(session, clazz);
43    }
44  
45  
46    @Override
47    public void configureRemoteSession() {
48      getServerSession().setModificationTrackerSession(true);
49    }
50  
51    @Override
52    public MasterSerial selectMasterSerial() {
53      DbModificationTracker tracker = (DbModificationTracker) ModificationTracker.getInstance();
54      return createMasterSerial(tracker.getMasterSerial(), tracker.getGeneration());
55    }
56  
57    @Override
58    public IdSerialTuple selectIdSerialForName(String tableName) {
59      return ((DbModificationTracker) ModificationTracker.getInstance()).getIdSerialForName(tableName);
60    }
61  
62    @Override
63    public List<IdSerialTuple> selectAllIdSerials() {
64      return ((DbModificationTracker) ModificationTracker.getInstance()).getAllIdSerials();
65    }
66  
67    @Override
68    public List<IdSerialNameTuple> selectAllIdSerialNames() {
69      return ((DbModificationTracker) ModificationTracker.getInstance()).getAllIdSerialNames();
70    }
71  
72  
73    /**
74     * Creates the master serial object from a long serial.
75     * <p>
76     * If {@link MasterSerialEvent}s are registered for this remote session,
77     * the next event will be polled from the queue, its serial set, and that event returned.
78     *
79     * @param serial the long master serial
80     * @param generation the generation of the modification table
81     * @return the master serial object
82     */
83    protected MasterSerial createMasterSerial(long serial, long generation) {
84      MasterSerialEvent masterSerialEvent = getServerSession().pollMasterSerialEvent();
85      if (masterSerialEvent != null) {
86        masterSerialEvent.setSerial(serial);
87        MasterSerialEvent nextEvent = getServerSession().pollMasterSerialEvent();
88        if (nextEvent != null) {
89          MasterSerialListEvent listEvent = new MasterSerialListEvent();
90          listEvent.setSerial(serial);
91          listEvent.add(masterSerialEvent);
92          while (nextEvent != null) {
93            nextEvent.setSerial(serial);
94            listEvent.add(nextEvent);
95            nextEvent = getServerSession().pollMasterSerialEvent();
96          }
97          // could be reduced to one element due to MasterSerialEvent.isOverridingOlderEvents(), see MasterSerialListEvent.add()
98          masterSerialEvent = listEvent.size() == 1 ? listEvent.get(0) : listEvent;
99        }
100       return masterSerialEvent;
101     }
102     return new DefaultMasterSerial(serial, generation);
103   }
104 
105 }