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
6439bf1
commit b671122
Showing
26 changed files
with
580 additions
and
250 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
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
32 changes: 32 additions & 0 deletions
32
node/src/main/scala/org/bitcoins/node/models/CRUDAutoInc.scala
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,32 @@ | ||
package org.bitcoins.node.models | ||
import slick.dbio.Effect.Write | ||
import slick.jdbc.PostgresProfile.api._ | ||
import slick.lifted.TableQuery | ||
|
||
import scala.concurrent.Future | ||
|
||
abstract class CRUDAutoInc[T <: DbRowAutoInc[T]] extends CRUD[T,Long] { | ||
|
||
/** The table inside our database we are inserting into */ | ||
val table: TableQuery[_ <: TableAutoInc[T]] | ||
|
||
|
||
override def createAll(ts: Vector[T]): Future[Vector[T]] = { | ||
val query = table | ||
.returning(table.map(_.id)) | ||
.into((t, id) => t.copyWithId(id = id)) | ||
val actions: Vector[DBIOAction[query.SingleInsertResult, NoStream, Write]] = | ||
ts.map(r => query.+=(r)) | ||
database.runVec(DBIO.sequence(actions)) | ||
} | ||
|
||
override def findByPrimaryKeys(ids: Vector[Long]): Query[Table[_], T, Seq] = { | ||
table.filter(_.id.inSet(ids)) | ||
} | ||
|
||
|
||
override def findAll(ts: Vector[T]): Query[Table[_], T, Seq] = { | ||
val ids = ts.filter(_.id.isDefined).map(_.id.get) | ||
findByPrimaryKeys(ids) | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
node/src/main/scala/org/bitcoins/node/models/DbRowAutoInc.scala
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,17 @@ | ||
package org.bitcoins.node.models | ||
|
||
|
||
/** This is meant to be coupled with [[org.bitcoins.node.models.CRUDAutoInc]] | ||
* and [[TableAutoInc]] to allow for automatically incrementing an id | ||
* when inserting something into a database. This removes the boiler | ||
* boiler plate from this having to happen every where a [[CRUD]] | ||
* is created | ||
*/ | ||
abstract class DbRowAutoInc[T] { | ||
|
||
def id: Option[Long] | ||
|
||
|
||
def copyWithId(id: Long): T | ||
|
||
} |
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 org.bitcoins.node.models | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import org.bitcoins.node.util.NetworkIpAddress | ||
|
||
case class Peer(id: Option[Long], networkIpAddress: NetworkIpAddress) extends DbRowAutoInc[Peer] { | ||
|
||
def socket: InetSocketAddress = new InetSocketAddress(networkIpAddress.address, networkIpAddress.port) | ||
|
||
|
||
override def copyWithId(id: Long): Peer = { | ||
this.copy(id = Some(id)) | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
object Peer { | ||
def fromNetworkIpAddress(networkIpAddress: NetworkIpAddress): Peer = { | ||
Peer(None, networkIpAddress) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
node/src/main/scala/org/bitcoins/node/models/PeerDAO.scala
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,21 @@ | ||
package org.bitcoins.node.models | ||
|
||
import org.bitcoins.node.db.DbConfig | ||
import slick.jdbc.PostgresProfile.api._ | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
abstract class PeerDAO extends CRUDAutoInc[Peer] { | ||
override val table = TableQuery[PeerTable] | ||
} | ||
|
||
|
||
object PeerDAO { | ||
private case class PeerDAOImpl(dbConfig: DbConfig) (override implicit val ec: ExecutionContext) extends PeerDAO | ||
|
||
|
||
|
||
def apply(dbConfig: DbConfig)(implicit ec: ExecutionContext): PeerDAO = { | ||
PeerDAOImpl(dbConfig) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
node/src/main/scala/org/bitcoins/node/models/PeerTable.scala
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,10 @@ | ||
package org.bitcoins.node.models | ||
|
||
import slick.jdbc.PostgresProfile.api._ | ||
import slick.lifted.Tag | ||
|
||
class PeerTable(tag: Tag) extends TableAutoInc[Peer](tag, "peer_table") { | ||
|
||
|
||
def * = ??? | ||
} |
18 changes: 18 additions & 0 deletions
18
node/src/main/scala/org/bitcoins/node/models/TableAutoInc.scala
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,18 @@ | ||
package org.bitcoins.node.models | ||
|
||
|
||
import slick.jdbc.PostgresProfile.api._ | ||
|
||
|
||
/** Defines a table that has an auto incremented fields that is named id. | ||
* This is useful for things we want to store that don't have an | ||
* inherent id such as a hash. | ||
* @param tag | ||
* @param tableName | ||
* @tparam T | ||
*/ | ||
abstract class TableAutoInc[T](tag: Tag, tableName: String) extends Table[T](tag,tableName) { | ||
|
||
def id: Rep[Long] = column[Long]("id",O.PrimaryKey,O.AutoInc) | ||
|
||
} |
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
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
Oops, something went wrong.