You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InMemoryRepository.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package org.eclipse.jgit.storage.dfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.List;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import java.util.concurrent.ConcurrentMap;
  11. import java.util.concurrent.atomic.AtomicInteger;
  12. import org.eclipse.jgit.lib.Ref;
  13. import org.eclipse.jgit.lib.Ref.Storage;
  14. import org.eclipse.jgit.util.RefList;
  15. /**
  16. * Git repository stored entirely in the local process memory.
  17. * <p>
  18. * This implementation builds on the DFS repository by storing all reference and
  19. * object data in the local process. It is not very efficient and exists only
  20. * for unit testing and small experiments.
  21. * <p>
  22. * The repository is thread-safe. Memory used is released only when this object
  23. * is garbage collected. Closing the repository has no impact on its memory.
  24. */
  25. public class InMemoryRepository extends DfsRepository {
  26. private final DfsObjDatabase objdb;
  27. private final DfsRefDatabase refdb;
  28. /**
  29. * Initialize a new in-memory repository.
  30. *
  31. * @param repoDesc
  32. * description of the repository.
  33. */
  34. public InMemoryRepository(DfsRepository repoDesc) {
  35. super(new DfsRepositoryBuilder<DfsRepositoryBuilder, InMemoryRepository>() {
  36. @Override
  37. public InMemoryRepository build() throws IOException {
  38. throw new UnsupportedOperationException();
  39. }
  40. });
  41. objdb = new MemObjDatabase(this);
  42. refdb = new MemRefDatabase();
  43. }
  44. @Override
  45. public DfsObjDatabase getObjectDatabase() {
  46. return objdb;
  47. }
  48. @Override
  49. public DfsRefDatabase getRefDatabase() {
  50. return refdb;
  51. }
  52. private class MemObjDatabase extends DfsObjDatabase {
  53. private final AtomicInteger packId = new AtomicInteger();
  54. private List<DfsPackDescription> packs = new ArrayList<DfsPackDescription>();
  55. MemObjDatabase(DfsRepository repo) {
  56. super(repo, new DfsReaderOptions());
  57. }
  58. @Override
  59. protected synchronized List<DfsPackDescription> listPacks() {
  60. return packs;
  61. }
  62. @Override
  63. protected DfsPackDescription newPack(PackSource source) {
  64. int id = packId.incrementAndGet();
  65. return new MemPack("pack-" + id + "-" + source.name(),
  66. getRepository().getDescription());
  67. }
  68. @Override
  69. protected synchronized void commitPack(
  70. Collection<DfsPackDescription> desc,
  71. Collection<DfsPackDescription> replace) {
  72. List<DfsPackDescription> n;
  73. n = new ArrayList<DfsPackDescription>(desc.size() + packs.size());
  74. n.addAll(desc);
  75. n.addAll(packs);
  76. if (replace != null)
  77. n.removeAll(replace);
  78. packs = n;
  79. }
  80. @Override
  81. protected void rollbackPack(Collection<DfsPackDescription> desc) {
  82. // Do nothing. Pack is not recorded until commitPack.
  83. }
  84. @Override
  85. protected ReadableChannel openPackFile(DfsPackDescription desc)
  86. throws FileNotFoundException {
  87. MemPack memPack = (MemPack) desc;
  88. if (memPack.packFile == null)
  89. throw new FileNotFoundException(desc.getPackName());
  90. return new ByteArrayReadableChannel(memPack.packFile);
  91. }
  92. @Override
  93. protected ReadableChannel openPackIndex(DfsPackDescription desc)
  94. throws FileNotFoundException {
  95. MemPack memPack = (MemPack) desc;
  96. if (memPack.packIndex == null)
  97. throw new FileNotFoundException(desc.getIndexName());
  98. return new ByteArrayReadableChannel(memPack.packIndex);
  99. }
  100. @Override
  101. protected DfsOutputStream writePackFile(DfsPackDescription desc) {
  102. final MemPack memPack = (MemPack) desc;
  103. return new Out() {
  104. @Override
  105. public void flush() {
  106. memPack.packFile = getData();
  107. }
  108. };
  109. }
  110. @Override
  111. protected DfsOutputStream writePackIndex(DfsPackDescription desc) {
  112. final MemPack memPack = (MemPack) desc;
  113. return new Out() {
  114. @Override
  115. public void flush() {
  116. memPack.packIndex = getData();
  117. }
  118. };
  119. }
  120. }
  121. private static class MemPack extends DfsPackDescription {
  122. private byte[] packFile;
  123. private byte[] packIndex;
  124. MemPack(String name, DfsRepositoryDescription repoDesc) {
  125. super(repoDesc, name);
  126. }
  127. }
  128. private abstract static class Out extends DfsOutputStream {
  129. private final ByteArrayOutputStream dst = new ByteArrayOutputStream();
  130. private byte[] data;
  131. @Override
  132. public void write(byte[] buf, int off, int len) {
  133. data = null;
  134. dst.write(buf, off, len);
  135. }
  136. @Override
  137. public int read(long position, ByteBuffer buf) {
  138. byte[] d = getData();
  139. int n = Math.min(buf.remaining(), d.length - (int) position);
  140. if (n == 0)
  141. return -1;
  142. buf.put(d, (int) position, n);
  143. return n;
  144. }
  145. byte[] getData() {
  146. if (data == null)
  147. data = dst.toByteArray();
  148. return data;
  149. }
  150. @Override
  151. public abstract void flush();
  152. @Override
  153. public void close() {
  154. flush();
  155. }
  156. }
  157. private static class ByteArrayReadableChannel implements ReadableChannel {
  158. private final byte[] data;
  159. private int position;
  160. private boolean open = true;
  161. ByteArrayReadableChannel(byte[] buf) {
  162. data = buf;
  163. }
  164. public int read(ByteBuffer dst) {
  165. int n = Math.min(dst.remaining(), data.length - position);
  166. if (n == 0)
  167. return -1;
  168. dst.put(data, position, n);
  169. position += n;
  170. return n;
  171. }
  172. public void close() {
  173. open = false;
  174. }
  175. public boolean isOpen() {
  176. return open;
  177. }
  178. public long position() {
  179. return position;
  180. }
  181. public void position(long newPosition) {
  182. position = (int) newPosition;
  183. }
  184. public long size() {
  185. return data.length;
  186. }
  187. public int blockSize() {
  188. return 0;
  189. }
  190. }
  191. private class MemRefDatabase extends DfsRefDatabase {
  192. private final ConcurrentMap<String, Ref> refs = new ConcurrentHashMap<String, Ref>();
  193. MemRefDatabase() {
  194. super(InMemoryRepository.this);
  195. }
  196. @Override
  197. protected RefCache scanAllRefs() throws IOException {
  198. RefList.Builder<Ref> ids = new RefList.Builder<Ref>();
  199. RefList.Builder<Ref> sym = new RefList.Builder<Ref>();
  200. for (Ref ref : refs.values()) {
  201. if (ref.isSymbolic())
  202. sym.add(ref);
  203. ids.add(ref);
  204. }
  205. ids.sort();
  206. sym.sort();
  207. return new RefCache(ids.toRefList(), sym.toRefList());
  208. }
  209. @Override
  210. protected boolean compareAndPut(Ref oldRef, Ref newRef)
  211. throws IOException {
  212. String name = newRef.getName();
  213. if (oldRef == null || oldRef.getStorage() == Storage.NEW)
  214. return refs.putIfAbsent(name, newRef) == null;
  215. Ref cur = refs.get(name);
  216. if (cur != null && eq(cur, oldRef))
  217. return refs.replace(name, cur, newRef);
  218. else
  219. return false;
  220. }
  221. @Override
  222. protected boolean compareAndRemove(Ref oldRef) throws IOException {
  223. String name = oldRef.getName();
  224. Ref cur = refs.get(name);
  225. if (cur != null && eq(cur, oldRef))
  226. return refs.remove(name, cur);
  227. else
  228. return false;
  229. }
  230. private boolean eq(Ref a, Ref b) {
  231. if (a.getObjectId() == null && b.getObjectId() == null)
  232. return true;
  233. if (a.getObjectId() != null)
  234. return a.getObjectId().equals(b.getObjectId());
  235. return false;
  236. }
  237. }
  238. }