-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Ashuh/persistence
Implement application state persistence
- Loading branch information
Showing
51 changed files
with
2,006 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/jtorrent/data/torrent/repository/AppTorrentMetadataRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package jtorrent.data.torrent.repository; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import jtorrent.data.torrent.source.file.filemanager.BencodedTorrentFileManager; | ||
import jtorrent.data.torrent.source.file.model.BencodedTorrent; | ||
import jtorrent.data.torrent.source.file.model.BencodedTorrentFactory; | ||
import jtorrent.domain.torrent.model.TorrentMetadata; | ||
import jtorrent.domain.torrent.repository.TorrentMetadataRepository; | ||
|
||
public class AppTorrentMetadataRepository implements TorrentMetadataRepository { | ||
|
||
private final BencodedTorrentFileManager torrentFileManager = new BencodedTorrentFileManager(); | ||
|
||
@Override | ||
public TorrentMetadata getTorrentMetadata(File file) throws IOException { | ||
return torrentFileManager.read(file).toDomain(); | ||
} | ||
|
||
@Override | ||
public TorrentMetadata getTorrentMetadata(URL url) throws IOException { | ||
return torrentFileManager.read(url).toDomain(); | ||
} | ||
|
||
@Override | ||
public void saveTorrentMetadata(TorrentMetadata torrentMetadata, Path savePath) throws IOException { | ||
BencodedTorrent bencodedTorrent = BencodedTorrent.fromDomain(torrentMetadata); | ||
torrentFileManager.write(savePath, bencodedTorrent); | ||
} | ||
|
||
/** | ||
* Creates a new {@link TorrentMetadata} instance with the current time as the creation date. | ||
* | ||
* @param trackerUrls list of tiers, each containing a list of tracker URLs | ||
* @param comment comment about the torrent | ||
* @param createdBy name and version of the program used to create the .torrent | ||
* @param pieceSize size of each piece in bytes | ||
* @return a new {@link TorrentMetadata} instance | ||
*/ | ||
@Override | ||
public TorrentMetadata createTOrrentMetadata(Path source, List<List<String>> trackerUrls, String comment, | ||
String createdBy, int pieceSize) throws IOException { | ||
return BencodedTorrentFactory.create(source, trackerUrls, comment, createdBy, pieceSize).toDomain(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/jtorrent/data/torrent/repository/AppTorrentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jtorrent.data.torrent.repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import jtorrent.data.torrent.source.db.dao.TorrentDao; | ||
import jtorrent.data.torrent.source.db.model.TorrentEntity; | ||
import jtorrent.domain.common.util.Sha1Hash; | ||
import jtorrent.domain.common.util.rx.MutableRxObservableList; | ||
import jtorrent.domain.common.util.rx.RxObservableList; | ||
import jtorrent.domain.torrent.model.Torrent; | ||
import jtorrent.domain.torrent.repository.TorrentRepository; | ||
|
||
public class AppTorrentRepository implements TorrentRepository { | ||
|
||
private final MutableRxObservableList<Torrent> torrentsObservable; | ||
private final Map<Sha1Hash, Torrent> infoHashToTorrent; | ||
private final TorrentDao torrentDao = new TorrentDao(); | ||
|
||
public AppTorrentRepository() { | ||
List<Torrent> torrents = new ArrayList<>(); | ||
torrentDao.readAll().stream() | ||
.map(TorrentEntity::toDomain) | ||
.forEach(torrents::add); | ||
infoHashToTorrent = torrents.stream() | ||
.collect(HashMap::new, (map, torrent) -> map.put(torrent.getInfoHash(), torrent), Map::putAll); | ||
this.torrentsObservable = new MutableRxObservableList<>(torrents); | ||
} | ||
|
||
@Override | ||
public RxObservableList<Torrent> getTorrents() { | ||
return torrentsObservable; | ||
} | ||
|
||
@Override | ||
public Torrent getTorrent(Sha1Hash infoHash) { | ||
return infoHashToTorrent.get(infoHash); | ||
} | ||
|
||
@Override | ||
public void addTorrent(Torrent torrent) { | ||
if (isExistingTorrent(torrent)) { | ||
// TODO: maybe throw exception if torrent already exists? | ||
return; | ||
} | ||
torrentDao.create(TorrentEntity.fromDomain(torrent)); | ||
infoHashToTorrent.put(torrent.getInfoHash(), torrent); | ||
torrentsObservable.add(torrent); | ||
} | ||
|
||
@Override | ||
public void persistTorrents() { | ||
infoHashToTorrent.values().stream() | ||
.map(TorrentEntity::fromDomain) | ||
.forEach(torrentDao::update); | ||
} | ||
|
||
@Override | ||
public void removeTorrent(Torrent torrent) { | ||
torrentDao.delete(torrent.getInfoHash().getBytes()); | ||
infoHashToTorrent.remove(torrent.getInfoHash()); | ||
torrentsObservable.remove(torrent); | ||
} | ||
|
||
private boolean isExistingTorrent(Torrent torrent) { | ||
return infoHashToTorrent.containsKey(torrent.getInfoHash()); | ||
} | ||
} |
180 changes: 0 additions & 180 deletions
180
src/main/java/jtorrent/data/torrent/repository/FileTorrentRepository.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.