Skip to content

Commit

Permalink
renamed DB indexes
Browse files Browse the repository at this point in the history
Add prefix "IPFS_" to all IPFS addresses

Starting to code the multi hash content ID algorithm

Add a property hash_algorithm("SHA256")

Rename to "level_1_content_metadata"

Added a better term for source code control in the app

Trying to get rid of direct references to IPFS
  • Loading branch information
yazz committed Jan 24, 2024
1 parent 97f9ee8 commit a86f3f8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
39 changes: 28 additions & 11 deletions public/go.html
Original file line number Diff line number Diff line change
Expand Up @@ -2078,17 +2078,34 @@
return ret
}
async function getIpfsHash ( sometext ) {
if (yz.mainVars.ipfs) {
let ret = await yz.mainVars.ipfs.add(sometext, {onlyHash: true})

return ret.path
} else {
let resulthash = await postToYazzReturnJson(
"/http_post_extract_commit_hash_id_from_code",
{text: sometext}
)

return resulthash.ipfsHash
let hashingAlgorithm = "IPFS"
let overrideHashingAlgorithm = yz.helpers.getValueOfCodeString(sometext, "hash_algorithm")
if (overrideHashingAlgorithm) {
hashingAlgorithm = overrideHashingAlgorithm
}
if (hashingAlgorithm == "IPFS") {
if (yz.mainVars.ipfs) {
let ret = await yz.mainVars.ipfs.add(sometext, {onlyHash: true})
return "IPFS_" + ret.path
} else {
let resulthash = await postToYazzReturnJson(
"/http_post_extract_commit_hash_id_from_code",
{text: sometext}
)
return resulthash.ipfsHash
}
} else if (hashingAlgorithm == "SHA256") {
debugger
if (yz.mainVars.ipfs) {
let ret = await yz.mainVars.ipfs.add(sometext, {onlyHash: true})
return "IPFS_" + ret.path
} else {
let resulthash = await postToYazzReturnJson(
"/http_post_extract_commit_hash_id_from_code",
{text: sometext}
)
return resulthash.ipfsHash
}
}
}
async function registerComponent ( vueComponentObject ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ v-if="$refs.editor_component_ref.model && $refs.editor_component_ref.model.forms
style='height:35px; margin-right: 0px;'
class='img-fluid'>
</img>
Deliver
Revisions
</a>
Expand Down
1 change: 1 addition & 0 deletions public/visifile_drivers/apps/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ async function(args) {
base_component_id("todo")
is_app(true)
display_name("Todo App")
hash_algorithm("SHA256")
visibility("PUBLIC")
component_type("APP")
description("This will create a demo todo app")
Expand Down
70 changes: 38 additions & 32 deletions src/yazz_helper_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,17 +720,17 @@ module.exports = {
// LEVEL 0
// This content may be moved into another Sqlite database eventually
//
"CREATE TABLE IF NOT EXISTS level_0_ipfs_content (ipfs_hash TEXT, ipfs_content TEXT, UNIQUE(ipfs_hash));",
"INSERT OR REPLACE INTO table_versions (table_name , version_number) VALUES ('level_0_ipfs_content',1);",
"CREATE INDEX IF NOT EXISTS ipfs_hashes_idx ON level_0_ipfs_content (ipfs_hash);",
"CREATE TABLE IF NOT EXISTS level_0_cached_content (ipfs_hash TEXT, ipfs_content TEXT, UNIQUE(ipfs_hash));",
"INSERT OR REPLACE INTO table_versions (table_name , version_number) VALUES ('level_0_cached_content',1);",
"CREATE INDEX IF NOT EXISTS level_0_cached_content_idx ON level_0_cached_content (ipfs_hash);",


// LEVEL 1
// This could be store in another Sqlite database, but it could also be derived from that data
//
"CREATE TABLE IF NOT EXISTS level_1_ipfs_hash_metadata (ipfs_hash TEXT , status TEXT , process_attempts INTEGER, content_type TEXT , scope TEXT , stored_in_ipfs INTEGER , sent_to_master TEXT , read_from_local_ipfs INTEGER, error TEXT , last_ipfs_ping_millis INTEGER, temp_debug_content TEXT, UNIQUE(ipfs_hash));",
"INSERT OR REPLACE INTO table_versions (table_name , version_number) VALUES ('level_1_ipfs_hash_metadata',1);",
"CREATE INDEX IF NOT EXISTS ipfs_hashes_idx ON level_1_ipfs_hash_metadata (ipfs_hash);",
"CREATE TABLE IF NOT EXISTS level_1_content_metadata (ipfs_hash TEXT , status TEXT , process_attempts INTEGER, content_type TEXT , scope TEXT , stored_in_ipfs INTEGER , sent_to_master TEXT , read_from_local_ipfs INTEGER, error TEXT , last_ipfs_ping_millis INTEGER, temp_debug_content TEXT, UNIQUE(ipfs_hash));",
"INSERT OR REPLACE INTO table_versions (table_name , version_number) VALUES ('level_1_content_metadata',1);",
"CREATE INDEX IF NOT EXISTS level_1_content_metadata_idx ON level_1_content_metadata (ipfs_hash);",


// LEVEL 2
Expand Down Expand Up @@ -864,7 +864,7 @@ module.exports = {
let baseComponentIdOfItem = yz.helpers.getValueOfCodeString(code,"base_component_id")
let itemName = yz.helpers.getValueOfCodeString(code,"display_name")
let iconUrl = yz.helpers.getValueOfCodeString(code,"logo_url")
let ipfsHashId = await OnlyIpfsHash.of(code)
let ipfsHashId = await yz.getDistributedKey(code)
let readWriteStatus = ""
let readOnly = yz.helpers.getValueOfCodeString(code,"read_only")

Expand Down Expand Up @@ -924,7 +924,7 @@ module.exports = {
}
},
getIpfsHash: async function ( sometext ) {
let ipfsHash = await OnlyIpfsHash.of(sometext)
let ipfsHash = await yz.getDistributedKey(sometext)
return ipfsHash
},
saveCodeV3: async function ( thisDb , code , options ) {
Expand Down Expand Up @@ -982,7 +982,7 @@ module.exports = {
let readWriteStatus = null
let codeChangesStr = null
let numCodeChanges = null
let sha1sum = await OnlyIpfsHash.of(code)
let sha1sum = await yz.getDistributedKey(code)
let userId = null
let propertiesAsJsonString = null
let existingCodeAlreadyInSystemCodeTable
Expand Down Expand Up @@ -1059,7 +1059,7 @@ module.exports = {


// ********** if the code has been changed then DO NOT SAVE IT! This is a basic tamper proof mechanism **********
let sha1sum2 = await OnlyIpfsHash.of(code)
let sha1sum2 = await yz.getDistributedKey(code)
if (sha1sum2 != sha1sum) {
console.log("Code SHA do not match - code has been changed while saving")
throw "Code SHA do not match - code has been changed while saving"
Expand Down Expand Up @@ -1807,7 +1807,7 @@ module.exports = {
//
//
try {
let contentRecord = await mm.getQuickSqlOneRow(thisDb,"select ipfs_content from level_0_ipfs_content where ipfs_hash = ?",[ipfsHash])
let contentRecord = await mm.getQuickSqlOneRow(thisDb,"select ipfs_content from level_0_cached_content where ipfs_hash = ?",[ipfsHash])
if (contentRecord) {
let returnValue = contentRecord.ipfs_content
if (returnValue) {
Expand Down Expand Up @@ -1844,7 +1844,7 @@ module.exports = {

await mm.executeQuickSql(thisDb,
`update
level_1_ipfs_hash_metadata
level_1_content_metadata
set
status = ?,
process_attempts = process_attempts + 1
Expand Down Expand Up @@ -1895,7 +1895,7 @@ module.exports = {

await mm.executeQuickSql(thisDb,
`update
level_1_ipfs_hash_metadata
level_1_content_metadata
set
status = ?,
process_attempts = process_attempts + 1
Expand All @@ -1909,7 +1909,7 @@ module.exports = {
console.log("No corresponding code record available yet")
await mm.executeQuickSql(thisDb,
`update
level_1_ipfs_hash_metadata
level_1_content_metadata
set
process_attempts = process_attempts + 1
where
Expand Down Expand Up @@ -1948,7 +1948,7 @@ module.exports = {

await mm.executeQuickSql(thisDb,
`update
level_1_ipfs_hash_metadata
level_1_content_metadata
set
status = ?,
process_attempts = process_attempts + 1
Expand Down Expand Up @@ -2090,8 +2090,12 @@ module.exports = {
// Yazz we often need the IPFS hash of some content (via the getIpfsHash( ) fn)
// just in case the front end IPFS server is not available
//---------------------------------------------------------------------------
let ipfsHash = await OnlyIpfsHash.of( content )
return ipfsHash
let yz = this
return "IPFS_" + ipfsHash
} else if (hashingAlgorithm == "SHA256") {
let ipfsHash = await OnlyIpfsHash.of(content)
return "IPFS_" + ipfsHash
}
},
getDistributedContent: async function ( { thisDb , ipfsHash } ) {
//---------------------------------------------------------------------------
Expand Down Expand Up @@ -2122,8 +2126,8 @@ module.exports = {


try {
contentStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_0_ipfs_content where ipfs_hash = ?", [ ipfsHash ])
metadataStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_1_ipfs_hash_metadata where ipfs_hash = ?", [ ipfsHash ])
contentStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_0_cached_content where ipfs_hash = ?", [ ipfsHash ])
metadataStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_1_content_metadata where ipfs_hash = ?", [ ipfsHash ])

// if the content is stored in Sqlite then get the content from sqlite
if (metadataStoredInSqlite && contentStoredInSqlite) {
Expand Down Expand Up @@ -2166,6 +2170,7 @@ module.exports = {

// figure out the content options and scope
let mm = this
let yz = this
let contentValueToStore = content
let contentType = "STRING"
let scope = "GLOBAL";
Expand Down Expand Up @@ -2193,12 +2198,12 @@ module.exports = {
}
}

justHash = await OnlyIpfsHash.of(contentValueToStore)
justHash = await yz.getDistributedKey(contentValueToStore)

//
try {
contentStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_0_ipfs_content where ipfs_hash = ?", [ justHash ])
metadataStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_1_ipfs_hash_metadata where ipfs_hash = ?", [ justHash ])
contentStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_0_cached_content where ipfs_hash = ?", [ justHash ])
metadataStoredInSqlite = await mm.getQuickSqlOneRow(thisDb, "select * from level_1_content_metadata where ipfs_hash = ?", [ justHash ])

// if the content is stored in Sqlite then do nothing
if (metadataStoredInSqlite && contentStoredInSqlite) {
Expand All @@ -2209,13 +2214,13 @@ module.exports = {
} else {
await mm.executeQuickSql(
thisDb,
"insert into level_0_ipfs_content (ipfs_hash,ipfs_content) values (?,?)",
"insert into level_0_cached_content (ipfs_hash,ipfs_content) values (?,?)",
[justHash,contentValueToStore])

await mm.executeQuickSql(
thisDb,
`insert or replace into
level_1_ipfs_hash_metadata
level_1_content_metadata
(
ipfs_hash,
content_type,
Expand Down Expand Up @@ -2321,7 +2326,7 @@ module.exports = {
res.on('end', async function () {
//console.log('end: ' );
await mm.executeQuickSql(thisDb,
"update level_1_ipfs_hash_metadata set sent_to_master = 'TRUE' where ipfs_hash = ?",
"update level_1_content_metadata set sent_to_master = 'TRUE' where ipfs_hash = ?",
[ipfs_hash] )
await mm.executeQuickSql(thisDb,
`update
Expand All @@ -2347,6 +2352,7 @@ module.exports = {
mm.inDistributeContentToPeer = false
},
saveItemToIpfs: async function ( srcCode ) {
let yz = this
//---------------------------------------------------------------------------
//
// saveItemToIpfs( .. )
Expand All @@ -2359,7 +2365,7 @@ module.exports = {
let promise = new Promise(async function(returnfn) {
let justHash = null
try {
justHash = await OnlyIpfsHash.of( srcCode )
justHash = await yz.getDistributedKey( srcCode )
let fullIpfsFilePath = path.join( fullIpfsFolderPath , justHash )

fs.writeFileSync( fullIpfsFilePath , srcCode );
Expand Down Expand Up @@ -2464,7 +2470,7 @@ module.exports = {
`select
ipfs_hash
from
level_1_ipfs_hash_metadata
level_1_content_metadata
where
scope='GLOBAL'
and
Expand Down Expand Up @@ -2516,7 +2522,7 @@ module.exports = {
`select
ipfs_hash
from
level_1_ipfs_hash_metadata
level_1_content_metadata
where
scope='GLOBAL' and sent_to_master is null
LIMIT 1`)
Expand All @@ -2525,7 +2531,7 @@ module.exports = {

await mm.executeQuickSql(thisDb,
`update
level_1_ipfs_hash_metadata
level_1_content_metadata
set
sent_to_master = ?
where
Expand Down Expand Up @@ -2717,7 +2723,7 @@ module.exports = {
`select
ipfs_content
from
level_0_ipfs_content
level_0_cached_content
where
ipfs_hash = ?`,
[nextIpfsQueueRecord.ipfs_hash])
Expand Down Expand Up @@ -2779,7 +2785,7 @@ module.exports = {
`select
ipfs_hash
from
level_1_ipfs_hash_metadata
level_1_content_metadata
where
status is null
order by
Expand Down Expand Up @@ -2860,7 +2866,7 @@ module.exports = {
},
oldsynchonizeContentAmongPeers: async function ( thisDb ) {
//console.log("Sync")
let contentNotSentToPeer = await this.getQuickSql(thisDb, "select ipfs_hash from level_1_ipfs_hash_metadata where sent_to_master != 'TRUE' limit 1", params)
let contentNotSentToPeer = await this.getQuickSql(thisDb, "select ipfs_hash from level_1_content_metadata where sent_to_master != 'TRUE' limit 1", params)
if (rows.length == 0) {
return null
}
Expand Down

0 comments on commit a86f3f8

Please sign in to comment.