๐ŸŒŠ Core Concepts

Pika Streams

Pika Streams provide a powerful abstraction layer that combines the familiar Java Stream API with database query execution. Unlike standard Java Streams that operate on in-memory collections, Pika Streams execute queries lazily against your database โ€” allowing you to process large datasets efficiently without loading entire result sets into memory.

๐Ÿ’พLazy evaluation
๐Ÿ“‰Low memory usage
๐Ÿ”—Java Stream compatible
โšกDB-level optimization

Standard Streams vs Pika Streams

Pika Streams

  • Execute database queries on-demand
  • Stream results directly from the database
  • Memory-efficient for large result sets
  • Lazy evaluation with DB-level optimization

Standard Java Streams

  • Operate on in-memory collections only
  • Process data already loaded into JVM heap
  • Limited by available memory for large datasets
  • No DB awareness or optimization

Getting Started with Pika Streams

Pika Streams are accessed through the main ORM instance using the stream() method:

// Get a stream finder for your entity class
PikaStreamFinder<SampleModel> streamFinder = orm.stream(SampleModel.class);

// Use within a connection context for proper resource management
try (var conn = orm.establishConnection()) {
    Stream<SampleModel> results = streamFinder.all();
    // Process your stream...
}

Query Methods

Single Record Queries

byId(Object id) โ€” retrieves a single entity by its primary key.

try (var conn = orm.establishConnection()) {
    Stream<SampleModel> modelStream = orm.stream(SampleModel.class).byId(123);
    SampleModel model = modelStream.findFirst().orElse(null);
}

byKey(String column, Object value) โ€” finds a single entity by any column value.

try (var conn = orm.establishConnection()) {
    Stream<SampleModel> modelStream = orm.stream(SampleModel.class)
        .byKey("str_val", "specificValue");
    SampleModel model = modelStream.findFirst().get();
}

Collection Queries

all() โ€” retrieves all records from the entity's table.

try (var conn = orm.establishConnection()) {
    List<SampleModel> allModels = orm.stream(SampleModel.class).all().toList();
}

Dynamic Queries

where(String whereClause, Map<String, Object> args) โ€” custom WHERE clauses with named parameters.

try (var conn = orm.establishConnection()) {
    List<SampleModel> results = orm.stream(SampleModel.class)
        .where("int_val = :val", Map.of("val", 10))
        .toList();
}

Performance Note: Streams are not executed until a terminal operation (like toList(), findFirst(), or forEach()) is called. Always use streams within a connection context to ensure automatic cleanup.