
terom@31: CREATE TABLE inodes (
terom@31:     ino serial4 primary key, 
terom@31:     type char(3) NOT NULL, 
terom@31:     mode int2 NOT NULL, 
terom@31:     data oid
terom@31: );
terom@24: 
terom@31: CREATE TABLE file_tree (
terom@31:     "offset" serial4 primary key, 
terom@31:     name varchar(256), 
terom@31:     parent int4 references inodes(ino), 
terom@31:     inode int4 references inodes(ino) NOT NULL
terom@31: );
terom@24: 
terom@31: INSERT INTO inodes VALUES 
terom@31:     (1, 'DIR', 365, NULL),
terom@31:     (2, 'REG', 292, lo_create(0));
terom@27: 
terom@31: INSERT INTO file_tree (name, parent, inode) VALUES 
terom@31:     (NULL,  NULL,   1   ),
terom@31:     ('foo', 1,      2   );
terom@31: 
terom@31: CREATE FUNCTION lo_size (oid) RETURNS int4 LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT AS 'select lo_lseek(lo_open($1, 262144), 0, 2);';
terom@31: CREATE FUNCTION lo_pread (IN fd int4, IN len int4, IN "off" int4) RETURNS bytea LANGUAGE SQL STRICT AS 'select lo_lseek($1, $3, 0); select loread($1, $2);';
terom@31: CREATE FUNCTION lo_pwrite (IN fd int4, IN buf bytea, IN "off" int4) RETURNS int4 LANGUAGE SQL STRICT AS 'select lo_lseek($1, $3, 0); select lowrite($1, $2);';
terom@31: CREATE FUNCTION lo_otruncate (IN oid, IN len int4) RETURNS oid LANGUAGE SQL STRICT AS 'select lo_truncate(lo_open($1, 393216), $2); select $1;';
terom@31: 
terom@33: ALTER TABLE inodes ADD COLUMN link varchar(512);
terom@33: 
terom@33: CREATE SEQUENCE ino_seq START 64;
terom@33: ALTER TABLE inodes ALTER COLUMN ino SET DEFAULT nextval('ino_seq'::regclass);
terom@33: 
terom@33: ALTER TABLE file_tree ADD COLUMN ino int4;
terom@33: UPDATE file_tree SET ino = inode;
terom@33: ALTER TABLE file_tree DROP COLUMN inode;
terom@33: 
terom@33: CREATE FUNCTION dbfs_size (type char, oid, link varchar) RETURNS int4 LANGUAGE SQL STABLE AS $$
terom@33:     SELECT CASE $1 
terom@33:         WHEN 'LNK' THEN char_length($3) 
terom@33:         WHEN 'REG' THEN lo_size($2) 
terom@33:         ELSE 0 
terom@33:     END;
terom@33: $$;
terom@33: 




