Hugh.C
5 years ago
1 changed files with 0 additions and 272 deletions
@ -1,272 +0,0 @@
|
||||
package com.fr.third.eclipse.jgit.internal.storage.dfs; |
||||
|
||||
import com.fr.third.eclipse.jgit.internal.storage.pack.PackExt; |
||||
import com.fr.third.eclipse.jgit.lib.ObjectId; |
||||
import com.fr.third.eclipse.jgit.lib.Ref; |
||||
import com.fr.third.eclipse.jgit.revwalk.RevWalk; |
||||
import com.fr.third.eclipse.jgit.util.RefList; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.nio.ByteBuffer; |
||||
import java.util.*; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import java.util.concurrent.ConcurrentMap; |
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
public class RemoteRepository extends DfsRepository { |
||||
private static final AtomicInteger packId = new AtomicInteger(); |
||||
|
||||
private final DfsObjDatabase objdb; |
||||
|
||||
private final DfsRefDatabase refdb; |
||||
|
||||
/** |
||||
* Initialize a new remote repository. |
||||
* |
||||
* @param repoDesc |
||||
* description of the repository. |
||||
* @since 2.0 |
||||
*/ |
||||
public RemoteRepository(DfsRepositoryDescription repoDesc) { |
||||
super(new DfsRepositoryBuilder<DfsRepositoryBuilder, RemoteRepository>() { |
||||
@Override |
||||
public RemoteRepository build() throws IOException { |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
}.setRepositoryDescription(repoDesc)); |
||||
|
||||
objdb = new RemoteRepository.RemoteObjDatabase(this); |
||||
refdb = new RemoteRepository.RemoteRefDatabase(); |
||||
} |
||||
|
||||
@Override |
||||
public DfsObjDatabase getObjectDatabase() { |
||||
return objdb; |
||||
} |
||||
|
||||
@Override |
||||
public DfsRefDatabase getRefDatabase() { |
||||
return refdb; |
||||
} |
||||
|
||||
private class RemoteObjDatabase extends DfsObjDatabase { |
||||
private List<DfsPackDescription> packs = new ArrayList<DfsPackDescription>(); |
||||
|
||||
RemoteObjDatabase(DfsRepository repo) { |
||||
super(repo, new DfsReaderOptions()); |
||||
} |
||||
|
||||
@Override |
||||
protected synchronized List<DfsPackDescription> listPacks() { |
||||
return packs; |
||||
} |
||||
|
||||
@Override |
||||
protected DfsPackDescription newPack(PackSource source) { |
||||
int id = packId.incrementAndGet(); |
||||
DfsPackDescription desc = new RemoteRepository.MemPack( |
||||
"pack-" + id + "-" + source.name(), //$NON-NLS-1$ //$NON-NLS-2$
|
||||
getRepository().getDescription()); |
||||
return desc.setPackSource(source); |
||||
} |
||||
|
||||
@Override |
||||
protected synchronized void commitPackImpl( |
||||
Collection<DfsPackDescription> desc, |
||||
Collection<DfsPackDescription> replace) { |
||||
List<DfsPackDescription> n; |
||||
n = new ArrayList<DfsPackDescription>(desc.size() + packs.size()); |
||||
n.addAll(desc); |
||||
n.addAll(packs); |
||||
if (replace != null) |
||||
n.removeAll(replace); |
||||
packs = n; |
||||
} |
||||
|
||||
@Override |
||||
protected void rollbackPack(Collection<DfsPackDescription> desc) { |
||||
// Do nothing. Pack is not recorded until commitPack.
|
||||
} |
||||
|
||||
@Override |
||||
protected ReadableChannel openFile(DfsPackDescription desc, PackExt ext) |
||||
throws FileNotFoundException, IOException { |
||||
RemoteRepository.MemPack memPack = (RemoteRepository.MemPack) desc; |
||||
byte[] file = memPack.fileMap.get(ext); |
||||
if (file == null) |
||||
throw new FileNotFoundException(desc.getFileName(ext)); |
||||
return new RemoteRepository.ByteArrayReadableChannel(file); |
||||
} |
||||
|
||||
@Override |
||||
protected DfsOutputStream writeFile( |
||||
DfsPackDescription desc, final PackExt ext) throws IOException { |
||||
final RemoteRepository.MemPack memPack = (RemoteRepository.MemPack) desc; |
||||
return new RemoteRepository.Out() { |
||||
@Override |
||||
public void flush() { |
||||
memPack.fileMap.put(ext, getData()); |
||||
} |
||||
}; |
||||
} |
||||
} |
||||
|
||||
private static class MemPack extends DfsPackDescription { |
||||
private final Map<PackExt, byte[]> |
||||
fileMap = new HashMap<PackExt, byte[]>(); |
||||
|
||||
MemPack(String name, DfsRepositoryDescription repoDesc) { |
||||
super(repoDesc, name); |
||||
} |
||||
} |
||||
|
||||
private abstract static class Out extends DfsOutputStream { |
||||
private final ByteArrayOutputStream dst = new ByteArrayOutputStream(); |
||||
|
||||
private byte[] data; |
||||
|
||||
@Override |
||||
public void write(byte[] buf, int off, int len) { |
||||
data = null; |
||||
dst.write(buf, off, len); |
||||
} |
||||
|
||||
@Override |
||||
public int read(long position, ByteBuffer buf) { |
||||
byte[] d = getData(); |
||||
int n = Math.min(buf.remaining(), d.length - (int) position); |
||||
if (n == 0) |
||||
return -1; |
||||
buf.put(d, (int) position, n); |
||||
return n; |
||||
} |
||||
|
||||
byte[] getData() { |
||||
if (data == null) |
||||
data = dst.toByteArray(); |
||||
return data; |
||||
} |
||||
|
||||
@Override |
||||
public abstract void flush(); |
||||
|
||||
@Override |
||||
public void close() { |
||||
flush(); |
||||
} |
||||
|
||||
} |
||||
|
||||
private static class ByteArrayReadableChannel implements ReadableChannel { |
||||
private final byte[] data; |
||||
|
||||
private int position; |
||||
|
||||
private boolean open = true; |
||||
|
||||
ByteArrayReadableChannel(byte[] buf) { |
||||
data = buf; |
||||
} |
||||
|
||||
public int read(ByteBuffer dst) { |
||||
int n = Math.min(dst.remaining(), data.length - position); |
||||
if (n == 0) |
||||
return -1; |
||||
dst.put(data, position, n); |
||||
position += n; |
||||
return n; |
||||
} |
||||
|
||||
public void close() { |
||||
open = false; |
||||
} |
||||
|
||||
public boolean isOpen() { |
||||
return open; |
||||
} |
||||
|
||||
public long position() { |
||||
return position; |
||||
} |
||||
|
||||
public void position(long newPosition) { |
||||
position = (int) newPosition; |
||||
} |
||||
|
||||
public long size() { |
||||
return data.length; |
||||
} |
||||
|
||||
public int blockSize() { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
private class RemoteRefDatabase extends DfsRefDatabase { |
||||
private final ConcurrentMap<String, Ref> refs = new ConcurrentHashMap<String, Ref>(); |
||||
|
||||
RemoteRefDatabase() { |
||||
super(RemoteRepository.this); |
||||
} |
||||
|
||||
@Override |
||||
protected RefCache scanAllRefs() throws IOException { |
||||
RefList.Builder<Ref> ids = new RefList.Builder<Ref>(); |
||||
RefList.Builder<Ref> sym = new RefList.Builder<Ref>(); |
||||
for (Ref ref : refs.values()) { |
||||
if (ref.isSymbolic()) |
||||
sym.add(ref); |
||||
ids.add(ref); |
||||
} |
||||
ids.sort(); |
||||
sym.sort(); |
||||
return new RefCache(ids.toRefList(), sym.toRefList()); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean compareAndPut(Ref oldRef, Ref newRef) |
||||
throws IOException { |
||||
ObjectId id = newRef.getObjectId(); |
||||
if (id != null) { |
||||
RevWalk rw = new RevWalk(getRepository()); |
||||
try { |
||||
// Validate that the target exists in a new RevWalk, as the RevWalk
|
||||
// from the RefUpdate might be reading back unflushed objects.
|
||||
rw.parseAny(id); |
||||
} finally { |
||||
rw.release(); |
||||
} |
||||
} |
||||
String name = newRef.getName(); |
||||
if (oldRef == null || oldRef.getStorage() == Ref.Storage.NEW) |
||||
return refs.putIfAbsent(name, newRef) == null; |
||||
Ref cur = refs.get(name); |
||||
if (cur != null && eq(cur, oldRef)) |
||||
return refs.replace(name, cur, newRef); |
||||
else |
||||
return false; |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected boolean compareAndRemove(Ref oldRef) throws IOException { |
||||
String name = oldRef.getName(); |
||||
Ref cur = refs.get(name); |
||||
if (cur != null && eq(cur, oldRef)) |
||||
return refs.remove(name, cur); |
||||
else |
||||
return false; |
||||
} |
||||
|
||||
private boolean eq(Ref a, Ref b) { |
||||
if (a.getObjectId() == null && b.getObjectId() == null) |
||||
return true; |
||||
if (a.getObjectId() != null) |
||||
return a.getObjectId().equals(b.getObjectId()); |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue