Skip to content

Commit

Permalink
Fix code offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Nov 28, 2023
1 parent 4470d2c commit 696f400
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 166 deletions.
3 changes: 2 additions & 1 deletion actions/describeTimeToLive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = function describeTimeToLive (store, data, cb) {

if (table.TimeToLiveDescription !== null && typeof table.TimeToLiveDescription === 'object') {
cb(null, { TimeToLiveDescription: table.TimeToLiveDescription })
} else {
}
else {
cb(null, { TimeToLiveDescription: { TimeToLiveStatus: 'DISABLED' } })
}
})
Expand Down
61 changes: 31 additions & 30 deletions actions/updateTimeToLive.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
var db = require('../db');
var db = require('../db')

module.exports = function updateTimeToLive(store, data, cb) {
var key = data.TableName,
TimeToLiveSpecification = data.TimeToLiveSpecification,
tableDb = store.tableDb,
returnValue;
module.exports = function updateTimeToLive (store, data, cb) {
var key = data.TableName,
TimeToLiveSpecification = data.TimeToLiveSpecification,
tableDb = store.tableDb,
returnValue

store.getTable(key, false, function(err, table) {
if (err) return cb(err)
store.getTable(key, false, function (err, table) {
if (err) return cb(err)

if (TimeToLiveSpecification.Enabled) {
if (table.TimeToLiveDescription && table.TimeToLiveDescription.TimeToLiveStatus === 'ENABLED') {
return cb(db.validationError('TimeToLive is already enabled'))
}
table.TimeToLiveDescription = {
AttributeName: TimeToLiveSpecification.AttributeName,
TimeToLiveStatus: 'ENABLED',
}
returnValue = TimeToLiveSpecification
} else {
if (table.TimeToLiveDescription == null || table.TimeToLiveDescription.TimeToLiveStatus === 'DISABLED') {
return cb(db.validationError('TimeToLive is already disabled'))
}
if (TimeToLiveSpecification.Enabled) {
if (table.TimeToLiveDescription && table.TimeToLiveDescription.TimeToLiveStatus === 'ENABLED') {
return cb(db.validationError('TimeToLive is already enabled'))
}
table.TimeToLiveDescription = {
AttributeName: TimeToLiveSpecification.AttributeName,
TimeToLiveStatus: 'ENABLED',
}
returnValue = TimeToLiveSpecification
}
else {
if (table.TimeToLiveDescription == null || table.TimeToLiveDescription.TimeToLiveStatus === 'DISABLED') {
return cb(db.validationError('TimeToLive is already disabled'))
}

table.TimeToLiveDescription = {
TimeToLiveStatus: 'DISABLED',
}
returnValue = {Enabled: false}
}
table.TimeToLiveDescription = {
TimeToLiveStatus: 'DISABLED',
}
returnValue = { Enabled: false }
}

tableDb.put(key, table, function(err) {
if (err) return cb(err)
tableDb.put(key, table, function (err) {
if (err) return cb(err)

cb(null, {TimeToLiveSpecification: returnValue})
})
cb(null, { TimeToLiveSpecification: returnValue })
})
})
}
80 changes: 40 additions & 40 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,50 +137,50 @@ function create (options) {
timerIdTtlScanner = setInterval(function () {
var currentUnixSeconds = Math.round(Date.now() / 1000)

function logError(err, result) {
function logError (err) {
if (err) console.error(err)
}

lazyStream(tableDb.createKeyStream({}), logError)
.join(function (tableNames) {
tableNames.forEach(function (name) {
getTable(name, false, function (err, table) {
if (err) return
if (!table.TimeToLiveDescription || table.TimeToLiveDescription.TimeToLiveStatus !== 'ENABLED') return

var keyAttrNames = table.KeySchema.map(function (i) {
return i.AttributeName
})
var source = {
getItemDb: getItemDb,
getIndexDb: getIndexDb,
}
var itemDb = getItemDb(table.TableName)
var kvStream = lazyStream(itemDb.createReadStream({}), logError)
kvStream = kvStream.filter(function (item) {
var ttl = item.value[table.TimeToLiveDescription.AttributeName]
return ttl && typeof ttl.N === 'string' && currentUnixSeconds > Number(ttl.N)
})
kvStream.join(function (kvs) {
kvs.forEach(function (kv) {
var itemKey = keyAttrNames.reduce(function (key, attrName) {
key[attrName] = kv.value[attrName]
return key
}, {})
var data = {TableName: name, Key: itemKey}
var cb = function (err) {
// Noop ?
}
deleteItem(source, data, table, itemDb, kv.key, cb)
})
.join(function (tableNames) {
tableNames.forEach(function (name) {
getTable(name, false, function (err, table) {
if (err) return
if (!table.TimeToLiveDescription || table.TimeToLiveDescription.TimeToLiveStatus !== 'ENABLED') return

var keyAttrNames = table.KeySchema.map(function (i) {
return i.AttributeName
})
var source = {
getItemDb: getItemDb,
getIndexDb: getIndexDb,
}
var itemDb = getItemDb(table.TableName)
var kvStream = lazyStream(itemDb.createReadStream({}), logError)
kvStream = kvStream.filter(function (item) {
var ttl = item.value[table.TimeToLiveDescription.AttributeName]
return ttl && typeof ttl.N === 'string' && currentUnixSeconds > Number(ttl.N)
})
kvStream.join(function (kvs) {
kvs.forEach(function (kv) {
var itemKey = keyAttrNames.reduce(function (key, attrName) {
key[attrName] = kv.value[attrName]
return key
}, {})
var data = { TableName: name, Key: itemKey }
var cb = () => {
// Noop ?
}
deleteItem(source, data, table, itemDb, kv.key, cb)
})
})
})
})
})
}, ttlScannerInterval)
}

function stopBackgroundJobs() {
function stopBackgroundJobs () {
clearInterval(timerIdTtlScanner)
}

Expand Down Expand Up @@ -244,14 +244,15 @@ function validateItem (dataItem, table) {
})
}

function deleteItem(store, data, table, itemDb, key, cb) {
itemDb.lock(key, function(release) {
function deleteItem (store, data, table, itemDb, key, cb) {
itemDb.lock(key, function (release) {
cb = release(cb)

itemDb.get(key, function(err, existingItem) {
itemDb.get(key, function (err, existingItem) {
if (err && err.name !== 'NotFoundError') return cb(err)

if ((err = checkConditional(data, existingItem)) != null) return cb(err)
let conditionalErr = checkConditional(data, existingItem)
if (conditionalErr) return cb(conditionalErr)

var returnObj = {}

Expand All @@ -260,10 +261,10 @@ function deleteItem(store, data, table, itemDb, key, cb) {

returnObj.ConsumedCapacity = addConsumedCapacity(data, false, existingItem)

updateIndexes(store, table, existingItem, null, function(err) {
updateIndexes(store, table, existingItem, null, function (err) {
if (err) return cb(err)

itemDb.del(key, function(err) {
itemDb.del(key, function (err) {
if (err) return cb(err)
cb(null, returnObj)
})
Expand All @@ -272,7 +273,6 @@ function deleteItem(store, data, table, itemDb, key, cb) {
})
}

function validateUpdates(attributeUpdates, expressionUpdates, table) {
function validateUpdates (attributeUpdates, expressionUpdates, table) {
if (attributeUpdates == null && expressionUpdates == null) return

Expand Down
Loading

0 comments on commit 696f400

Please sign in to comment.