Unseal the family atomics

A reactor runs in the write that changes its inputs, so its result is visible as soon as the write returns. This step adds two mutations that record the three threats to a house, and a reactor that reads those threats.

Add these mutations to definitions/dune/houses.dust, before the export form.

json1.7 kB
[
  {
    "#invoke": [
      "defmutation",
      {
        "#local": "betray"
      },
      [
        {
          "#local": "name"
        }
      ],
      [
        {
          "#local": "string?"
        },
        {
          "#local": "=>"
        },
        {
          "#local": "map?"
        }
      ],
      {
        "query": {
          "find": [
            {
              "#local": "?house"
            }
          ],
          "where": [
            [
              {
                "#local": "?house"
              },
              {
                "#local": "name"
              },
              {
                "#local": "name"
              }
            ]
          ]
        },
        "patch": {
          "?house": {
            "betrayed": true
          }
        }
      }
    ]
  },
  {
    "#invoke": [
      "defmutation",
      {
        "#local": "invade"
      },
      [
        {
          "#local": "name"
        }
      ],
      [
        {
          "#local": "string?"
        },
        {
          "#local": "=>"
        },
        {
          "#local": "map?"
        }
      ],
      {
        "query": {
          "find": [
            {
              "#local": "?house"
            }
          ],
          "where": [
            [
              {
                "#local": "?house"
              },
              {
                "#local": "name"
              },
              {
                "#local": "name"
              }
            ]
          ]
        },
        "patch": {
          "?house": {
            "sardaukar-landed": true,
            "heir-threatened": true
          }
        }
      }
    ]
  }
]

Each threat mutation has the parameter vector [name] and then the contract [string? => map?]. A contract gives one constraint for each parameter before =>, and one constraint for the result after it. Here, the name must be a string and the result must be a map. The SDK reads the parameter constraint, so the generated parameter is a Go string.

The body has a query member and a patch member. The clause [?house name name] reads as entity, field and value. It selects each house whose name field has the value of the name parameter. The find member [?house] makes one row for each selected house. The patch then adds its tags to the ?house of each row. betray adds <betrayed>, and invade adds <sardaukar-landed heir-threatened> in one write.

Add this reactor after the new mutations.

json763 B
{
  "$comment": "{\"\":\"; A betrayed house whose heir is threatened while Sardaukar are on its\\n; world unseals the family atomics.\"}",
  "$value": {
    "#invoke": [
      "defreactor",
      {
        "#local": "family-atomics"
      },
      {
        "query": {
          "find": [
            {
              "#local": "?house"
            }
          ],
          "where": [
            [
              {
                "#local": "?house"
              },
              {
                "betrayed": true,
                "sardaukar-landed": true,
                "heir-threatened": true
              }
            ]
          ]
        },
        "patch": {
          "?house": {
            "unsealed": true
          }
        }
      }
    ]
  }
}

defreactor has a name and a body map, and no parameters. The ;; lines above it are a comment. The clause [?house <betrayed sardaukar-landed heir-threatened>] selects each house that has all three tags. The patch adds the unsealed tag to each selected house.

The database runs the reactor in each write that changes a field that the reactor reads or writes. The changes of the reactor commit in the same transaction as that write. The export form does not name the reactor, so the SDK has no method for it. The defreactor reference gives its priority setting and the sequence of its rounds.

Replace the export form with this form, so that the SDK can call the new mutations.

json195 B
{
  "#invoke": [
    "export",
    {
      "#local": "atomics-found"
    },
    {
      "#local": "betray"
    },
    {
      "#local": "invade"
    },
    {
      "#local": "houses"
    }
  ]
}

Run go run ./seed again. It saves the changed namespace and generates the SDK again with the Betray and Invade methods. The database does not run the new reactor on the houses that are in the database at that time. The reactor runs only in subsequent writes that change a tag that it reads or writes.

Create atomics/main.go with this program. It betrays House Atreides and then invades it. After each write, it reads the house through Houses. It then prints the stats that db.Reactors() returns for each saved reactor: the rounds that ran it and the fields that it changed since the database opened.

go969 B
package main

import (
	"fmt"
	"log"

	"example.com/quickstart/sdk"
)

func main() {
	db, err := sdk.Open(sdk.Config{Plugins: &sdk.Plugins{
		Stdlib: true,
		Database: &sdk.DatabaseSettings{Path: "dune.db"},
	}})
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	steps := []struct {
		event string
		apply func(string) (sdk.MutationRowsResult, error)
	}{
		{"betrayed", db.DuneHouses.Betray},
		{"invaded", db.DuneHouses.Invade},
	}
	for _, step := range steps {
		if _, err := step.apply("Atreides"); err != nil {
			log.Fatal(err)
		}
		houses, err := db.DuneHouses.Houses()
		if err != nil {
			log.Fatal(err)
		}
		for _, house := range houses {
			if house.Name == "Atreides" {
				fmt.Printf("%s: unsealed %t\n", step.event, house.Unsealed != nil)
			}
		}
	}
	reactors, err := db.Reactors()
	if err != nil {
		log.Fatal(err)
	}
	for _, reactor := range reactors {
		fmt.Printf("%s: %d runs, %d changes\n", reactor.Name, reactor.Runs, reactor.Changes)
	}
}

Run go run ./atomics after go run ./app. It prints:

text81 B
betrayed: unsealed false
invaded: unsealed true
family-atomics: 3 runs, 1 changes

The betrayal ran the reactor once, and its query matched no house. The invasion completed the three threats and ran the reactor twice. The first round added the unsealed tag, and the second round changed nothing.

Complete definitions

After this step, definitions/dune/houses.dust contains these forms.

json4.6 kB
{
  "$comment": "{\"/4\":\"; A betrayed house whose heir is threatened while Sardaukar are on its\\n; world unseals the family atomics.\"}",
  "$value": [
    {
      "#invoke": [
        "defmutation",
        {
          "#local": "atomics-found"
        },
        [
          {
            "#local": "name"
          }
        ],
        {
          "schema": {
            "name": {
              "#local": "string?"
            }
          },
          "patch": {
            "_house": {
              "house": true,
              "name": {
                "#local": "name"
              }
            }
          }
        }
      ]
    },
    {
      "#invoke": [
        "defquery",
        {
          "#local": "houses"
        },
        [],
        {
          "where": [
            [
              {
                "#local": "?house"
              },
              {
                "house": true
              }
            ],
            [
              {
                "#local": "schema"
              },
              {
                "name": {
                  "#local": "string?"
                },
                "unsealed": [
                  {
                    "#local": "optional"
                  },
                  [
                    {
                      "#local": "maybe"
                    },
                    {
                      "#local": "boolean?"
                    }
                  ]
                ]
              },
              {
                "#local": "?house"
              }
            ]
          ],
          "project": {
            "?house": {
              "name": true,
              "unsealed": true
            }
          }
        }
      ]
    },
    {
      "#invoke": [
        "defmutation",
        {
          "#local": "betray"
        },
        [
          {
            "#local": "name"
          }
        ],
        [
          {
            "#local": "string?"
          },
          {
            "#local": "=>"
          },
          {
            "#local": "map?"
          }
        ],
        {
          "query": {
            "find": [
              {
                "#local": "?house"
              }
            ],
            "where": [
              [
                {
                  "#local": "?house"
                },
                {
                  "#local": "name"
                },
                {
                  "#local": "name"
                }
              ]
            ]
          },
          "patch": {
            "?house": {
              "betrayed": true
            }
          }
        }
      ]
    },
    {
      "#invoke": [
        "defmutation",
        {
          "#local": "invade"
        },
        [
          {
            "#local": "name"
          }
        ],
        [
          {
            "#local": "string?"
          },
          {
            "#local": "=>"
          },
          {
            "#local": "map?"
          }
        ],
        {
          "query": {
            "find": [
              {
                "#local": "?house"
              }
            ],
            "where": [
              [
                {
                  "#local": "?house"
                },
                {
                  "#local": "name"
                },
                {
                  "#local": "name"
                }
              ]
            ]
          },
          "patch": {
            "?house": {
              "sardaukar-landed": true,
              "heir-threatened": true
            }
          }
        }
      ]
    },
    {
      "#invoke": [
        "defreactor",
        {
          "#local": "family-atomics"
        },
        {
          "query": {
            "find": [
              {
                "#local": "?house"
              }
            ],
            "where": [
              [
                {
                  "#local": "?house"
                },
                {
                  "betrayed": true,
                  "sardaukar-landed": true,
                  "heir-threatened": true
                }
              ]
            ]
          },
          "patch": {
            "?house": {
              "unsealed": true
            }
          }
        }
      ]
    },
    {
      "#invoke": [
        "export",
        {
          "#local": "atomics-found"
        },
        {
          "#local": "betray"
        },
        {
          "#local": "invade"
        },
        {
          "#local": "houses"
        }
      ]
    }
  ]
}