# In-memory event store

> An event store for tests and experiments.

Plain Java · Modules: `occurrent-eventstore-inmemory`

## What is it?

A simple in-memory implementation of the `EventStore` interface.

## When to use?

Mainly for testing purposes or for integration tests that doesn’t require a durable event store.

## Dependencies

* Maven

  ```xml
  <dependency>
      <groupId>org.occurrent</groupId>
      <artifactId>occurrent-eventstore-inmemory</artifactId>
      <version>0.34.0</version>
  </dependency>
  ```

* Gradle

  ```groovy
  compile 'org.occurrent:occurrent-eventstore-inmemory:0.34.0'
  ```

* SBT

  ```scala
  libraryDependencies += "org.occurrent" % "occurrent-eventstore-inmemory" % "0.34.0"
  ```

* Grape

  ```groovy
  @Grab(group='org.occurrent', module='occurrent-eventstore-inmemory', version='0.34.0')
  ```

* Leiningen

  ```clojure
  [org.occurrent/occurrent-eventstore-inmemory "0.34.0"]
  ```

* Buildr

  ```ruby
  'org.occurrent:occurrent-eventstore-inmemory:jar:0.34.0'
  ```

* Ivy

  ```xml
  <dependency org="org.occurrent" name="occurrent-eventstore-inmemory" rev="0.34.0" />
  ```

## Getting Started

Once you’ve imported the dependencies you create a new instance of `org.occurrent.eventstore.inmemory.InMemoryEventStore`. For example:

* Java

  ```java
  InMemoryEventStore eventStore = new InMemoryEventStore();
  ```

* Kotlin

  ```kotlin
  val eventStore = InMemoryEventStore()
  ```

This implementation also supports filtered stream reads via `org.occurrent.eventstore.api.blocking.ReadEventStreamWithFilter`. See [Stream Filtering](/docs/layer-0/stream-filtering/) for when and how to use it.

By default this store refuses a query that filters on the event `data` payload. Call `withDataFieldReader(...)` to give it one, see [Filtering on Payload Data](/docs/layer-0/eventstore-queries/#filtering-on-payload-data).

Now you can start reading and writing events to the EventStore:

* Java

  ```java
  CloudEvent event = CloudEventBuilder.v1()
                      .withId("eventId")
                      .withSource(URI.create("urn:mydomain"))
                      .withType("HelloWorld")
                      .withTime(LocalDateTime.now().atOffset(ZoneOffset.UTC))
                      .withSubject("subject")
                      .withDataContentType("application/json")
                      .withData("{ \"message\" : \"hello\" }".getBytes(StandardCharsets.UTF_8))
                      .build();


  // Write
  eventStore.write("streamId", List.of(event));


  // Read
  EventStream<CloudEvent> eventStream = eventStore.read("streamId");


  // Read filtered events when the store supports it
  if (eventStore instanceof ReadEventStreamWithFilter filteredEventStore) {
      EventStream<CloudEvent> filteredEventStream = filteredEventStore.read(
              "streamId",
              StreamReadFilter.type("com.example.NameDefined")
      );
  }
  ```

* Kotlin

  ```kotlin
  val event = CloudEventBuilder.v1()
                      .withId("eventId")
                      .withSource(URI.create("urn:mydomain"))
                      .withType("HelloWorld")
                      .withTime(LocalDateTime.now().atOffset(ZoneOffset.UTC))
                      .withSubject("subject")
                      .withDataContentType("application/json")
                      .withData("{ \"message\" : \"hello\" }".toByteArray())
                      .build()


  // Write
  eventStore.write("streamId", listOf(event))


  // Read
  val eventStream : EventStream<CloudEvent> = eventStore.read("streamId")


  // Read filtered events when the store supports it
  val filteredEventStore = eventStore as? ReadEventStreamWithFilter
  val filteredEventStream = filteredEventStore?.read(
      "streamId",
      StreamReadFilter.type(NameDefined::class.java.name)
  )
  ```