Skip to content

Commit

Permalink
update README.md with example code for reading/creating/mutating a Bag
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van Heest committed Aug 7, 2018
1 parent 0a4d336 commit aca2f24
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A deposit can be read using

```scala
import better.files.File
import nl.knaw.dans.bag.v0.Deposit
import nl.knaw.dans.bag.Deposit

val baseDir: File = ???
val deposit = Deposit.read(baseDir)
Expand All @@ -35,7 +35,7 @@ the data files that form the bag's payload.
```scala
import better.files.File
import java.util.UUID
import nl.knaw.dans.bag.v0._
import nl.knaw.dans.bag._
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat

Expand Down Expand Up @@ -70,7 +70,7 @@ In a `Deposit` object, one can access, add and delete these properties using met

```scala
import scala.util.Try
import nl.knaw.dans.bag.v0._
import nl.knaw.dans.bag._

val deposit: Deposit = ???
val doi: Option[String] = deposit.doi
Expand All @@ -85,7 +85,66 @@ After all mutations are made to the deposit properties, the user must call `.sav
`Deposit` object in order to write the changes to the deposit on file system.

### Bag
TODO
A bag can be read using

```scala
import better.files.File
import nl.knaw.dans.bag.DansBag

val baseDir: File = ???
val bag = DansBag.read(baseDir)
```

In similar fashion a `DansBag` can be created as empty or from an already existing directory containing
the data files that form the bag's payload.

```scala
import better.files.File
import nl.knaw.dans.bag.{ DansBag, ChecksumAlgorithm }
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat

val baseDir: File = ???
val emptyBag = DansBag.empty(baseDir,
Set(ChecksumAlgorithm.SHA1),
Map("Created" -> Seq(DateTime.now().toString(ISODateTimeFormat.dateTime()))))

val dataDir: File = ???
val bagFromData = DansBag.createFromData(dataDir,
Set(ChecksumAlgorithm.SHA1),
Map("Created" -> Seq(DateTime.now().toString(ISODateTimeFormat.dateTime()))))
```

After being read or created, a `Bag` object can be used to modify and access the data in the bag on
filesystem. Mutating methods always return either a `Bag` or `Try[Bag]` object, such that mutations
can be chained. Please note that mutating methods generally do not mutate the bag on filesystem, but
only the `Bag` object in memory! In order to persist the changes made to a `Bag`, please call `.save()`
after all mutations have been done.

```scala
import java.util.UUID
import better.files.File
import nl.knaw.dans.bag.{ DansBag, ChecksumAlgorithm }

val baseDir: File = ???
// using map/flatMap
DansBag.read(baseDir)
.map(_.withIsVersionOf(UUID.fromString("00000000-0000-0000-0000-000000000001")))
.map(_.withEasyUserAccount("my-user"))
.flatMap(_.addPayloadManifestAlgorithm(ChecksumAlgorithm.SHA512))
.flatMap(_.addTagManifestAlgorithm(ChecksumAlgorithm.SHA512))
.flatMap(_.save())

// using for-comprehension
for {
bag <- DansBag.read(baseDir)
versionedBag = bag.withIsVersionOf(UUID.fromString("00000000-0000-0000-0000-000000000001"))
withUser = versionedBag.withEasyUserAccount("my-user")
withExtraAlgorithm <- withUser.addPayloadManifestAlgorithm(ChecksumAlgorithm.SHA512)
withExtraTagAlgorithm <- withExtraAlgorithm.addTagManifestAlgorithm(ChecksumAlgorithm.SHA512)
_ <- withExtraTagAlgorithm.save()
} yield ()
```


INSTALLATION
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/nl/knaw/dans/bag/DansBag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ trait DansBag {
*/
def withEasyUserAccount(userId: String): DansBag


/**
* Removes the entry with key 'EASY-User-Account' from `bag-info.txt`.
*
Expand Down

0 comments on commit aca2f24

Please sign in to comment.