-
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.
- Loading branch information
1 parent
fc799f9
commit 5634108
Showing
8 changed files
with
251 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package lee.code.pets.database; | ||
|
||
import lee.code.pets.Pets; | ||
import lee.code.pets.database.cache.CachePets; | ||
import lombok.Getter; | ||
|
||
public class CacheManager { | ||
private final Pets pets; | ||
@Getter private final CachePets cachePets; | ||
|
||
public CacheManager(Pets pets, DatabaseManager databaseManager) { | ||
this.pets = pets; | ||
this.cachePets = new CachePets(databaseManager); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/lee/code/pets/database/DatabaseManager.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,100 @@ | ||
package lee.code.pets.database; | ||
|
||
import com.j256.ormlite.dao.Dao; | ||
import com.j256.ormlite.dao.DaoManager; | ||
import com.j256.ormlite.jdbc.JdbcConnectionSource; | ||
import com.j256.ormlite.jdbc.db.DatabaseTypeUtils; | ||
import com.j256.ormlite.logger.LogBackendType; | ||
import com.j256.ormlite.logger.LoggerFactory; | ||
import com.j256.ormlite.support.ConnectionSource; | ||
import com.j256.ormlite.table.TableUtils; | ||
import lee.code.pets.Pets; | ||
import lee.code.pets.database.tables.PetTable; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.io.File; | ||
import java.sql.SQLException; | ||
|
||
public class DatabaseManager { | ||
private final Pets pets; | ||
private Dao<PetTable, Integer> petsDao; | ||
private ConnectionSource connectionSource; | ||
|
||
public DatabaseManager(Pets pets) { | ||
this.pets = pets; | ||
} | ||
|
||
private String getDatabaseURL() { | ||
//Setup MongoDB | ||
if (!pets.getDataFolder().exists()) pets.getDataFolder().mkdir(); | ||
return "jdbc:sqlite:" + new File(pets.getDataFolder(), "database.db"); | ||
} | ||
|
||
public void initialize(boolean debug) { | ||
if (!debug) LoggerFactory.setLogBackendFactory(LogBackendType.NULL); | ||
try { | ||
final String databaseURL = getDatabaseURL(); | ||
connectionSource = new JdbcConnectionSource( | ||
databaseURL, | ||
"test", | ||
"test", | ||
DatabaseTypeUtils.createDatabaseType(databaseURL)); | ||
createOrCacheTables(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void closeConnection() { | ||
try { | ||
connectionSource.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void createOrCacheTables() throws SQLException { | ||
final CacheManager cacheManager = pets.getCacheManager(); | ||
|
||
//Pet data | ||
TableUtils.createTableIfNotExists(connectionSource, PetTable.class); | ||
petsDao = DaoManager.createDao(connectionSource, PetTable.class); | ||
|
||
int highestID = 0; | ||
for (PetTable petTable : petsDao.queryForAll()) { | ||
cacheManager.getCachePets().setPetTable(petTable); | ||
if (petTable.getId() > highestID) highestID = petTable.getId(); | ||
} | ||
cacheManager.getCachePets().getNextID().set(highestID); | ||
} | ||
|
||
public synchronized void createPetTable(PetTable petTable) { | ||
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> { | ||
try { | ||
petsDao.createIfNotExists(petTable); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
|
||
public synchronized void updatePetTable(PetTable petTable) { | ||
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> { | ||
try { | ||
petsDao.update(petTable); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
|
||
public synchronized void deletePetTable(PetTable petTable) { | ||
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> { | ||
try { | ||
petsDao.delete(petTable); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
} |
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,39 @@ | ||
package lee.code.pets.database.cache; | ||
|
||
import lee.code.pets.database.DatabaseManager; | ||
import lee.code.pets.database.cache.data.PlayerPetData; | ||
import lee.code.pets.database.handlers.DatabaseHandler; | ||
import lee.code.pets.database.tables.PetTable; | ||
import lombok.Getter; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class CachePets extends DatabaseHandler { | ||
@Getter private final AtomicInteger nextID = new AtomicInteger(0); | ||
@Getter private final PlayerPetData playerPetData; | ||
private final HashMap<Integer, PetTable> petsCache = new HashMap<>(); | ||
|
||
public CachePets(DatabaseManager databaseManager) { | ||
super(databaseManager); | ||
this.playerPetData = new PlayerPetData(this); | ||
} | ||
|
||
public PetTable getPetTable(int id) { | ||
return petsCache.get(id); | ||
} | ||
|
||
public void setPetTable(PetTable petTable) { | ||
petsCache.put(petTable.getId(), petTable); | ||
playerPetData.cachePlayerPetTable(petTable); | ||
} | ||
|
||
public void createNewPet(UUID uuid, String data) { | ||
nextID.addAndGet(1); | ||
final PetTable petTable = new PetTable(nextID.get(), uuid, data); | ||
setPetTable(petTable); | ||
createPetDatabase(petTable); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/lee/code/pets/database/cache/data/PlayerPetData.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,33 @@ | ||
package lee.code.pets.database.cache.data; | ||
|
||
import lee.code.pets.database.cache.CachePets; | ||
import lee.code.pets.database.tables.PetTable; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
public class PlayerPetData { | ||
private final CachePets cachePets; | ||
private final HashMap<UUID, Set<Integer>> playerPetCache = new HashMap<>(); | ||
|
||
public PlayerPetData(CachePets cachePets) { | ||
this.cachePets = cachePets; | ||
} | ||
|
||
public void cachePlayerPetTable(PetTable petTable) { | ||
if (playerPetCache.containsKey(petTable.getOwner())) { | ||
playerPetCache.get(petTable.getOwner()).add(petTable.getId()); | ||
} else { | ||
final Set<Integer> pets = new HashSet<>(); | ||
pets.add(petTable.getId()); | ||
playerPetCache.put(petTable.getOwner(), pets); | ||
} | ||
} | ||
|
||
public Set<Integer> getAllPets(UUID uuid) { | ||
if (!playerPetCache.containsKey(uuid)) return new HashSet<>(); | ||
else return playerPetCache.get(uuid); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/lee/code/pets/database/handlers/DatabaseHandler.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,24 @@ | ||
package lee.code.pets.database.handlers; | ||
|
||
import lee.code.pets.database.DatabaseManager; | ||
import lee.code.pets.database.tables.PetTable; | ||
|
||
public class DatabaseHandler { | ||
private final DatabaseManager databaseManager; | ||
|
||
public DatabaseHandler(DatabaseManager databaseManager) { | ||
this.databaseManager = databaseManager; | ||
} | ||
|
||
public void createPetDatabase(PetTable petTable) { | ||
databaseManager.createPetTable(petTable); | ||
} | ||
|
||
public void updatePetDatabase(PetTable petTable) { | ||
databaseManager.updatePetTable(petTable); | ||
} | ||
|
||
public void deletePetDatabase(PetTable petTable) { | ||
databaseManager.deletePetTable(petTable); | ||
} | ||
} |
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,30 @@ | ||
package lee.code.pets.database.tables; | ||
|
||
import com.j256.ormlite.field.DatabaseField; | ||
import com.j256.ormlite.table.DatabaseTable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.UUID; | ||
|
||
@Setter | ||
@Getter | ||
@NoArgsConstructor | ||
@DatabaseTable(tableName = "pets") | ||
public class PetTable { | ||
@DatabaseField(id = true, canBeNull = false) | ||
private int id; | ||
|
||
@DatabaseField(columnName = "owner", canBeNull = false) | ||
private UUID owner; | ||
|
||
@DatabaseField(columnName = "data", canBeNull = false) | ||
private String data; | ||
|
||
public PetTable(int id, UUID owner, String data) { | ||
this.id = id; | ||
this.owner = owner; | ||
this.data = data; | ||
} | ||
} |
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