Skip to content

Commit

Permalink
feat(bundles): add bundle/data item GQL index schema PE-3769
Browse files Browse the repository at this point in the history
Adds the DB schema required for indexing data items for GraphQL
querying. Also includes a table for tracking bundle status (processed_at
+ data_item_count). Bundles use a separate SQLite DB (similar to data)
to reduce lock contention and support greater bootstrapping flexibility.
  • Loading branch information
djwhitt committed Jul 18, 2023
1 parent 0a10fb2 commit 5f37807
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
125 changes: 125 additions & 0 deletions migrations/2023.05.15T18.06.50.bundles.init-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
CREATE TABLE IF NOT EXISTS bundle_formats (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);

INSERT INTO bundle_formats (id, name) VALUES (0, 'ans-102');
INSERT INTO bundle_formats (id, name) VALUES (1, 'ans-104');

CREATE TABLE IF NOT EXISTS bundles (
id BLOB PRIMARY KEY,
format INTEGER NOT NULL,
data_item_count INTEGER NOT NULL,
first_processed_at INTEGER NOT NULL,
last_processed_at INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS bundle_data_items (
id BLOB,
parent_id BLOB NOT NULL,
root_transaction_id BLOB NOT NULL,
indexed_at INTEGER NOT NULL,
PRIMARY KEY (id, parent_id)
);

CREATE TABLE IF NOT EXISTS wallets (
address BLOB PRIMARY KEY,
public_modulus BLOB
);

CREATE TABLE IF NOT EXISTS stable_data_items (
-- Identity
id BLOB NOT NULL,
parent_id BLOB NOT NULL,
root_transaction_id BLOB NOT NULL,
height INTEGER NOT NULL,
block_transaction_index INTEGER NOT NULL,
signature BLOB NOT NULL,
anchor BLOB NOT NULL,

-- Ownership
owner_address BLOB NOT NULL,
target BLOB,

-- Data
data_offset INTEGER NOT NULL,
data_size INTEGER NOT NULL,
content_type TEXT,

-- Metadata
tag_count INTEGER NOT NULL,
indexed_at INTEGER NOT NULL,
PRIMARY KEY (id)
);

CREATE INDEX IF NOT EXISTS stable_data_items_height_block_transaction_index_id_idx ON stable_data_items (height, block_transaction_index, id);
CREATE INDEX IF NOT EXISTS stable_data_items_target_height_block_transaction_index_id_idx ON stable_data_items (target, height, block_transaction_index, id);
CREATE INDEX IF NOT EXISTS stable_data_items_owner_address_height_block_transaction_index_id_idx ON stable_data_items (owner_address, height, block_transaction_index, id);
CREATE INDEX IF NOT EXISTS stable_data_items_parent_id_height_block_transaction_index_id_idx ON stable_data_items (parent_id, height, block_transaction_index, id);

CREATE TABLE IF NOT EXISTS tag_names (
hash BLOB PRIMARY KEY,
name BLOB NOT NULL
);

CREATE TABLE IF NOT EXISTS tag_values (
hash BLOB PRIMARY KEY,
value BLOB NOT NULL
);

CREATE TABLE IF NOT EXISTS stable_data_item_tags (
tag_name_hash BLOB NOT NULL,
tag_value_hash BLOB NOT NULL,
height INTEGER NOT NULL,
block_transaction_index INTEGER NOT NULL,
data_item_tag_index INTEGER NOT NULL,
data_item_id BLOB NOT NULL,
parent_id BLOB NOT NULL,
root_transaction_id BLOB NOT NULL,
PRIMARY KEY (tag_name_hash, tag_value_hash, height, block_transaction_index, data_item_id, data_item_tag_index)
);

CREATE INDEX IF NOT EXISTS stable_data_item_tags_transaction_id_idx ON stable_data_item_tags (data_item_id);

CREATE TABLE IF NOT EXISTS new_data_items (
-- Identity
id BLOB NOT NULL,
parent_id BLOB NOT NULL,
root_transaction_id BLOB NOT NULL,
height INTEGER,
signature BLOB NOT NULL,
anchor BLOB NOT NULL,

-- Ownership
owner_address BLOB NOT NULL,
target BLOB,

-- Data
data_offset INTEGER NOT NULL,
data_size INTEGER NOT NULL,
content_type TEXT,

-- Metadata
tag_count INTEGER NOT NULL,
indexed_at INTEGER NOT NULL,
PRIMARY KEY (id)
);

CREATE INDEX IF NOT EXISTS new_data_items_parent_id_id_idx ON new_data_items (parent_id, id);
CREATE INDEX IF NOT EXISTS new_data_items_root_transaction_id_id_idx ON new_data_items (root_transaction_id, id);
CREATE INDEX IF NOT EXISTS new_data_items_target_id_idx ON new_data_items (target, id);
CREATE INDEX IF NOT EXISTS new_data_items_owner_address_id_idx ON new_data_items (owner_address, id);
CREATE INDEX IF NOT EXISTS new_data_items_height_indexed_at_idx ON new_data_items (height, indexed_at);

CREATE TABLE IF NOT EXISTS new_data_item_tags (
tag_name_hash BLOB NOT NULL,
tag_value_hash BLOB NOT NULL,
root_transaction_id BLOB NOT NULL,
data_item_id BLOB NOT NULL,
data_item_tag_index INTEGER NOT NULL,
height INTEGER,
indexed_at INTEGER NOT NULL,
PRIMARY KEY (tag_name_hash, tag_value_hash, root_transaction_id, data_item_id, data_item_tag_index)
);

CREATE INDEX IF NOT EXISTS new_data_item_tags_height_indexed_at_idx ON new_data_item_tags (height, indexed_at);
9 changes: 9 additions & 0 deletions migrations/down/2023.05.15T18.06.50.bundles.init-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS new_data_item_tags;
DROP TABLE IF EXISTS new_data_items;
DROP TABLE IF EXISTS stable_data_item_tags;
DROP TABLE IF EXISTS tag_values;
DROP TABLE IF EXISTS tag_names;
DROP TABLE IF EXISTS stable_data_items;
DROP TABLE IF EXISTS wallets;
DROP TABLE IF EXISTS bundles;
DROP TABLE IF EXISTS bundle_formats;

0 comments on commit 5f37807

Please sign in to comment.