Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7cdd01f08c6f4c3dd6d1c38eb25b8e63ba7416f1 (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 **************************************************************************/
package org.eclipse.emf.cdo.internal.server;

import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Simon McDuff
 */
public class MEMStore extends Store
{
  public static final String TYPE = "mem";

  private Map<CDOID, List<CDORevision>> revisions = new HashMap<CDOID, List<CDORevision>>()
  {
    private static final long serialVersionUID = 1L;

    @Override
    public String toString()
    {
      List<Entry<CDOID, List<CDORevision>>> entries = new ArrayList<Entry<CDOID, List<CDORevision>>>(entrySet());
      Collections.sort(entries, new Comparator<Entry<CDOID, List<CDORevision>>>()
      {
        public int compare(Entry<CDOID, List<CDORevision>> o1, Entry<CDOID, List<CDORevision>> o2)
        {
          return o1.getKey().compareTo(o2.getKey());
        }
      });

      StringBuilder builder = new StringBuilder();
      for (Entry<CDOID, List<CDORevision>> entry : entries)
      {
        builder.append(entry.getKey());
        builder.append(" -->");
        for (CDORevision revision : entry.getValue())
        {
          builder.append(" ");
          builder.append(revision);
        }

        builder.append("\n");
      }

      return builder.toString();
    }
  };

  public MEMStore()
  {
    super(TYPE);
  }

  public synchronized CDORevision getRevision(CDOID id)
  {
    List<CDORevision> list = revisions.get(id);
    if (list != null)
    {
      return list.get(list.size() - 1);
    }

    return null;
  }

  public synchronized CDORevision getRevisionByVersion(CDOID id, int version)
  {
    List<CDORevision> list = revisions.get(id);
    if (list != null)
    {
      return getRevisionByVersion(list, version);
    }

    return null;
  }

  public synchronized void addRevision(CDORevision revision)
  {
    CDOID id = revision.getID();
    List<CDORevision> list = revisions.get(id);
    if (list == null)
    {
      list = new ArrayList<CDORevision>();
      revisions.put(id, list);
    }

    CDORevision rev = getRevisionByVersion(list, revision.getVersion());
    if (rev != null)
    {
      throw new IllegalStateException("Concurrent modification of revision " + rev);
    }

    list.add(revision);
  }

  public synchronized boolean removeRevision(CDORevision revision)
  {
    CDOID id = revision.getID();
    List<CDORevision> list = revisions.get(id);
    if (list == null)
    {
      return false;
    }

    for (Iterator<CDORevision> it = list.iterator(); it.hasNext();)
    {
      CDORevision rev = it.next();
      if (rev.getVersion() == revision.getVersion())
      {
        it.remove();
        return true;
      }
    }

    return false;
  }

  @Override
  public boolean hasWriteDeltaSupport()
  {
    return true;
  }

  @Override
  public boolean hasAuditingSupport()
  {
    return true;
  }

  public void repairAfterCrash()
  {
  }

  @Override
  public MEMStoreAccessor createReader(ISession session)
  {
    return new MEMStoreAccessor(this, session);
  }

  @Override
  public MEMStoreAccessor createWriter(IView view)
  {
    return new MEMStoreAccessor(this, view);
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    revisions.clear();
    super.doDeactivate();
  }

  private CDORevision getRevisionByVersion(List<CDORevision> list, int version)
  {
    for (CDORevision revision : list)
    {
      if (revision.getVersion() == version)
      {
        return revision;
      }
    }

    return null;
  }
}

Back to the top