Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fee6ed3b8dcb47b0decc2a32ab5ea30451f2cf6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ibrahim Sallam - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.objectivity.schema;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager.BranchLoader;
import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager.BranchLoader.BranchInfo;

import org.eclipse.net4j.util.collection.Pair;

import com.objy.db.app.ooId;
import com.objy.db.app.ooObj;
import com.objy.db.util.ooTreeSetX;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Ibrahim Sallam
 */
public class ObjyBranchManager extends ooObj
{
  protected int nextBranchId;

  protected int nextLocalBranchId;

  protected ooTreeSetX branchSet;

  private ObjyBranchManager()
  {
    nextBranchId = 0;
    nextLocalBranchId = 0;
  }

  protected void createTreeSet(ooObj clusterObject)
  {
    branchSet = new ooTreeSetX();
    clusterObject.cluster(branchSet);
  }

  public int getLastBranchId()
  {
    fetch();
    return nextBranchId;
  }

  public int nextBranchId()
  {
    markModified();
    return ++nextBranchId;
  }

  public int getlastLocalBranchId()
  {
    fetch();
    return nextLocalBranchId;
  }

  public int nextLocalBranchId()
  {
    markModified();
    return --nextLocalBranchId;
  }

  public Pair<Integer, Long> createBranch(int branchId, BranchInfo branchInfo)
  {
    markModified();

    if (branchId == BranchLoader.NEW_BRANCH)
    {
      branchId = nextBranchId();
    }
    else if (branchId == BranchLoader.NEW_LOCAL_BRANCH)
    {
      branchId = nextLocalBranchId();
    }

    ObjyBranch newObjyBranch = ObjyBranch.create(this, branchId, branchInfo);
    // if the baseBranchId is 0, then we just added to our branchSet, otherwise
    // we'll lookup the ObjyBranch with the id, and add the newly created
    // ObjyBranch to it's sub-branches set.
    // int baseBranchId = branchInfo.getBaseBranchID();

    // Initially I thought we could make a tree of branches, but for the
    // first implementation we can just create a TreeSet of all branches,
    // then resolve sub-branches dynamically by checking baseBranchId.
    branchSet.add(newObjyBranch); // implicit clustering.

    // if (baseBranchId == 0) // main branch.
    // {
    // branchSet.add(newObjyBranch); // implicit clustering.
    // }
    // else
    // {
    // Iterator<ObjyBranch> treeItr = branchSet.iterator();
    // ObjyBranch objyBranch = null;
    // boolean found = false;
    // while (treeItr.hasNext() && !found)
    // {
    // objyBranch = treeItr.next();
    // if (baseBranchId == objyBranch.getBaseBranchId())
    // {
    // found = true;
    // continue;
    // }
    // objyBranch = null;
    // }
    //
    // objyBranch.addSubBranch(newObjyBranch);
    // }

    return new Pair<Integer, Long>(branchId, branchInfo.getBaseTimeStamp());
  }

  public ObjyBranch getBranch(int branchId)
  {
    fetch();

    @SuppressWarnings("unchecked")
    Iterator<ObjyBranch> treeItr = branchSet.iterator();
    ObjyBranch objyBranch = null;
    while (treeItr.hasNext())
    {
      ObjyBranch tempObjyBranch = treeItr.next();
      if (branchId == tempObjyBranch.getBranchId())
      {
        objyBranch = tempObjyBranch;
        break;
      }
    }

    return objyBranch;
  }

  public static ObjyBranchManager create(ooId scopeContOid)
  {
    ObjyBranchManager branchManager = new ObjyBranchManager();
    ooObj clusterObject = ooObj.create_ooObj(scopeContOid);
    clusterObject.cluster(branchManager);

    branchManager.createTreeSet(branchManager);
    branchManager.createMainBranch();

    return branchManager;
  }

  public void createMainBranch()
  {
    ObjyBranch newObjyBranch = ObjyBranch.create(this, CDOBranch.MAIN_BRANCH_ID, CDOBranch.MAIN_BRANCH_ID,
        CDOBranch.MAIN_BRANCH_NAME, 0);
    branchSet.add(newObjyBranch); // implicit clustering.
  }

  public boolean deleteBranch(int branchId)
  {
    boolean done = false;

    markModified();

    @SuppressWarnings("unchecked")
    Iterator<ObjyBranch> treeItr = branchSet.iterator();
    ObjyBranch objyBranch = null;
    while (treeItr.hasNext())
    {
      objyBranch = treeItr.next();
      if (branchId == objyBranch.getBranchId())
      {
        done = branchSet.remove(objyBranch);
        break;
      }
    }

    return done;
  }

  public List<ObjyBranch> getSubBranches(int baseBranchId)
  {
    fetch();
    List<ObjyBranch> objyBranchList = new ArrayList<ObjyBranch>();

    Iterator<?> treeItr = branchSet.iterator();
    ObjyBranch objyBranch = null;
    while (treeItr.hasNext())
    {
      objyBranch = (ObjyBranch)treeItr.next();
      if (objyBranch.getBranchId() == objyBranch.getBaseBranchId())
      {
        continue;
      }
      if (objyBranch.getBaseBranchId() == baseBranchId)
      {
        objyBranchList.add(objyBranch);
      }
    }
    return objyBranchList;
  }

  public List<ObjyBranch> getBranches(int startId, int endId)
  {
    fetch();
    List<ObjyBranch> objyBranchList = new ArrayList<ObjyBranch>();

    int lastId = endId != 0 ? endId : Integer.MAX_VALUE;

    @SuppressWarnings("unchecked")
    Iterator<ObjyBranch> treeItr = branchSet.iterator();
    ObjyBranch objyBranch = null;
    while (treeItr.hasNext())
    {
      objyBranch = treeItr.next();
      int id = objyBranch.getBranchId();
      if (id >= startId && id <= lastId)
      {
        objyBranchList.add(objyBranch);
      }
    }
    return objyBranchList;
  }

}

Back to the top