Skip to content

Commit

Permalink
Make tanglePrune work
Browse files Browse the repository at this point in the history
  • Loading branch information
Powersource committed Oct 12, 2022
1 parent b618a94 commit 8077af0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/add-group-tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const { isCloakedMsg } = require('ssb-ref')
const set = require('lodash.set')
const GetGroupTangle = require('./get-group-tangle')
const tanglePrune = require('./tangle-prune')

module.exports = function AddGroupTangle(server) {
const getGroupTangle = GetGroupTangle(server)
Expand All @@ -26,8 +27,7 @@ module.exports = function AddGroupTangle(server) {
if (err) return cb(null, content)

set(content, 'tangles.group', tangle)
//TODO: uncomment
//tanglePrune(content) // prune the group tangle down if needed
tanglePrune(content) // prune the group tangle down if needed

cb(null, content)
})
Expand Down
5 changes: 2 additions & 3 deletions lib/tangle-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const MAX_SIZE_1_recps = 6041

module.exports = function tanglePrune(content) {
const tangle = 'group'
const maxSize = content.recps > 1 ? MAX_SIZE_16_recps : MAX_SIZE_1_recps
const maxSize =
content.recps.length > 1 ? MAX_SIZE_16_recps : MAX_SIZE_1_recps
if (getLength(content) <= maxSize) return content

content.tangles[tangle].previous = content.tangles[tangle].previous.sort(() =>
Expand All @@ -36,7 +37,5 @@ module.exports = function tanglePrune(content) {
}

function getLength(obj) {
// stringify if on classic
//return JSON.stringify(obj).length
return bipf.encodingLength(obj)
}
13 changes: 6 additions & 7 deletions test/tangle-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
// SPDX-License-Identifier: LGPL-3.0-only

const test = require('tape')
const { promisify: p } = require('util')
const bipf = require('bipf')
const Testbot = require('./helpers/testbot')
const tanglePrune = require('../lib/tangle-prune')

const chars = 'abcABC123=+? '.split('')
//const encodedLength = (obj) => JSON.stringify(msgVal.content).length
const encodedLength = (obj) => bipf.encodingLength(obj)
const randomChar = () => chars.sort(() => (Math.random() < 0.5 ? -1 : +1))[0]
const randomText = (length) => {
Expand All @@ -18,7 +16,7 @@ const randomText = (length) => {
return output
}

test.only('tangle prune', async (t) => {
test('tangle prune', async (t) => {
const ssb = Testbot()
const ssbId = ssb.id

Expand Down Expand Up @@ -87,6 +85,7 @@ test.only('tangle prune', async (t) => {

const result = results.get(upper) || results.get(mid) || results.get(lower)
t.pass(`max stringied content size for ${numberRecps} recps: ${result}`)
return result
}
const max16recps = await findMaxSize(16).catch(t.error) // 5546
const max1recp = await findMaxSize(1).catch(t.error) // 6041
Expand All @@ -96,7 +95,7 @@ test.only('tangle prune', async (t) => {
const content = (prevCount, numRecps) => ({
type: 'post',
text: 'hello!',
recps: [...new Array(numRecps).fill(ssbId)],
recps: new Array(numRecps).fill(ssbId),
tangles: {
group: {
root: msgId,
Expand All @@ -105,9 +104,9 @@ test.only('tangle prune', async (t) => {
},
})

// console.time('prune')
const result16 = tanglePrune(content(4000, 16), 'group')
// console.timeEnd('prune')
//console.time('prune')
const result16 = tanglePrune(content(4000, 16))
//console.timeEnd('prune')
t.true(
encodedLength(result16) <= max16recps,
`pruned ${4000 - result16.tangles.group.previous.length}`
Expand Down

0 comments on commit 8077af0

Please sign in to comment.