john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

kafka-consumergroup

发表于 2016-10-11 | 更新于 2019-05-09

kafka 查看当前有多少group

1
kafka-run-class.bat kafka.admin.ConsumerGroupCommand --zookeeper localhost:2181 --list

kafka 查看group情况

1
kafka-run-class.bat kafka.admin.ConsumerGroupCommand --zookeeper localhost:2181 --describe --group console-consumer-8047

参考文档

database_find

发表于 2016-10-03 | 更新于 2019-05-09

hexo 的json database之find

查找当前写的源文章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	Box.prototype._getExistingFiles = function(){
var base = this.base;
var ctx = this.context;

//输出base:source\
//base:themes\landscape-f\
// console.log('base:'+base.substring(ctx.base_dir.length));

var relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));

console.log('^' + escapeRegExp(relativeBase));
//输出^source\/
//^themes\/landscape\-f\/
var regex = new RegExp('^' + escapeRegExp(relativeBase));
var baseLength = relativeBase.length;

return this.Cache.find({_id: regex}).map(function(item){
return item._id.substring(baseLength);
});
};

我们看到this.Cache.find这句代码是从缓存中查找,那么Cache是什么呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

exports.Asset = require('./asset');
exports.Cache = require('./cache');
exports.Category = require('./category');
exports.Data = require('./data');
exports.Page = require('./page');
exports.Post = require('./post');
exports.PostAsset = require('./post_asset');
exports.PostCategory = require('./post_category');
exports.PostTag = require('./post_tag');
exports.Tag = require('./tag');


var models = require('../models');

module.exports = function(ctx){
var db = ctx.database;

var keys = Object.keys(models);
var key = '';

for (var i = 0, len = keys.length; i < len; i++){
key = keys[i];
db.model(key, models[key](ctx));
}
};

是从models文件夹下注册的model,然后把model放入db中。

1
2
3
4
5
6
7
8
Database.prototype.model = function(name, schema){
if (this._models[name]){
return this._models[name];
}

var model = this._models[name] = new this.Model(name, schema);
return model;
};

我们看下Cache的Model:

1
2
3
4
5
6
7
8
9
module.exports = function(ctx){
var Cache = new Schema({
_id: {type: String, required: true},
shasum: {type: String},
modified: {type: Number, default: Date.now}
});

return Cache;
};

声明了一个Schema,Scheme构造函数中会把当前schema参数加入path中,以便于查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
if (schema){
this.add(schema);
}

Schema.prototype.add = function(schema, prefix_){
var prefix = prefix_ || '';
var keys = Object.keys(schema);
var len = keys.length;
var key, value;

if (!len) return;

for (var i = 0; i < len; i++){
key = keys[i];
value = schema[key];

this.path(prefix + key, value);
}
};

Schema.prototype.path = function(name, obj){
if (obj == null){
return this.paths[name];
}

var type;
var nested = false;

if (obj instanceof SchemaType){
type = obj;
} else {
switch (typeof obj){
case 'function':
type = getSchemaType(name, {type: obj});
break;

//比如Cache的model就是{type: String, required: true}
case 'object':
if (obj.type){
type = getSchemaType(name, obj);
} else if (isArray(obj)){
type = new Types.Array(name, {
child: obj.length ? getSchemaType(name, obj[0]) : new SchemaType(name)
});
} else {
type = new Types.Object();
nested = Object.keys(obj).length > 0;
}

break;

default:
throw new TypeError('Invalid value for schema path `' + name + '`');
}
}

this.paths[name] = type;
this._updateStack(name, type);

if (nested) this.add(obj, name + '.');
};

function getSchemaType(name, options){
var Type = options.type || options;
var typeName = Type.name;

//String就是内置的类型
if (builtinTypes[typeName]){
return new Types[typeName](name, options);
} else {
return new Type(name, options);
}
}

var builtinTypes = {
String: true,
Number: true,
Boolean: true,
Array: true,
Object: true,
Date: true
};

最重要的就是各种Types:

1
2
3
4
5
6
7
8
9
10
11
exports.Mixed = require('../schematype');
exports.String = require('./string');
exports.Number = require('./number');
exports.Boolean = require('./boolean');
exports.Array = require('./array');
exports.Object = require('./object');
exports.Date = require('./date');
exports.Virtual = require('./virtual');
exports.CUID = require('./cuid');
exports.Enum = require('./enum');
exports.Integer = require('./integer');

比如String:

1
2
3
4
5
6
7
8
9
10
11
12
function SchemaTypeString(name, options){
SchemaType.call(this, name, options);
}

//
SchemaTypeString.prototype.match = function(value, query, data){
if (typeof query.test === 'function'){
return query.test(value);
} else {
return value === query;
}
};

这边的match方法就是在schema类的_parseQuery中有可能用到的,下回再讲。

list_tag

发表于 2016-10-01 | 更新于 2019-05-07

hexo list 命令之tag

hexo list命令是由hexo实例调用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//调用hexo类的call方法
return hexo.call(cmd, args).then(function() {
return hexo.exit();
}).catch(function(err) {
return hexo.exit(err).then(function() {
handleError(err);
});
});

...省略
Hexo.prototype.call = function(name, args, callback){
if (!callback && typeof args === 'function'){
callback = args;
args = {};
}

var self = this;

return new Promise(function(resolve, reject){
//从console类获取存储的命令
var c = self.extend.console.get(name);

if (c){
c.call(self, args).then(resolve, reject);
} else {
reject(new Error('Console `' + name + '` has not been registered yet!'));
}
}).nodeify(callback);
};

先调用list命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function listConsole(args){
/* jshint validthis: true */
var type = args._.shift();
var self = this;

// Display help message if user didn't input any arguments
if (!type || !store.hasOwnProperty(type)){
return this.call('help', {_: ['list']});
}

return this.load().then(function(){
return store[type].call(self, args);
});
}

加载数据库,统计数量

1
2
3
4
5
6
7
8
9
10
11
12
13
Hexo.prototype.load = function(callback){
var self = this;

return loadDatabase(this).then(function(){
//处理 插入数据库操作
return Promise.all([
self.source.process(),
self.theme.process()
]);
}).then(function(){
return self._generate({cache: true});
}).nodeify(callback);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';

var chalk = require('chalk');
var table = require('text-table');
var common = require('./common');

function listTag(){
/* jshint validthis: true */
//获取hexo实例的model方法
var Tag = this.model('Tag');

//获取tag数据 从tag model获取
var data = Tag.sort({name: 1}).map(function(tag){
return [tag.name, String(tag.length), chalk.magenta(tag.path)];
});

// Table header
var header = ['Name', 'Posts', 'Path'].map(function(str){
return chalk.underline(str);
});

//压入表头到data数组
data.unshift(header);

var t = table(data, {
align: ['l', 'r', 'l'],
stringLength: common.stringLength
});

console.log(t);
if (data.length === 1) console.log('No tags.');
}

module.exports = listTag;

hexo scaffold的创建过程

发表于 2016-09-29 | 更新于 2019-05-07

layout从哪边来

new.js

1
2
3
4
5
6
7
8
9
10
11
var data = {
title: args._.pop(),//标题
layout: args._.length ? args._[0] : this.config.default_layout,
slug: args.s || args.slug,
path: args.p || args.path
};

...省略
return this.post.create(data, args.r || args.replace).then(function(post){
self.log.info('Created: %s', chalk.magenta(tildify(post.path)));
});

post.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//默认layout为post
data.layout = (data.layout || config.default_layout).toLowerCase();

...省略
this._getScaffold(data.layout)

...省略
Post.prototype._getScaffold = function(layout){
var ctx = this.context;

//从scaffold目录中获取post.md
return ctx.scaffold.get(layout).then(function(result){
if (result != null) return result;
return ctx.scaffold.get('normal');
});
};

hexo-front-matter/front_matter.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 var split = yfm.split(scaffold);

...省略
var rPrefixSep = /^(-{3,}|;{3,})/;
var rFrontMatter = /^(-{3,}|;{3,})\n([\s\S]+?)\n\1(?:$|\n([\s\S]*)$)/;
var rFrontMatterNew = /^([\s\S]+?)\n(-{3,}|;{3,})(?:$|\n([\s\S]*)$)/;

...省略
function split(str){
if (typeof str !== 'string') throw new TypeError('str is required!');
console.log(str);
if (rFrontMatter.test(str)) return splitOld(str);

//post.md中默认内容 匹配rFrontMatterNew
if (rFrontMatterNew.test(str)) return splitNew(str);

return {content: str};
}

function splitNew(str){
console.log('splitNew');
if (rPrefixSep.test(str)) return {content: str};

var match = str.match(rFrontMatterNew);

return {
data: match[1],
content: match[3] || '',
separator: match[2],
prefixSeparator: false
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var frontMatter = prepareFrontMatter(_.clone(data));

...省略
function prepareFrontMatter(data){
var keys = Object.keys(data);
var key = '';
var item;

for (var i = 0, len = keys.length; i < len; i++){
key = keys[i];
item = data[key];
//时间格式化
if (moment.isMoment(item)){
data[key] = item.utc().format('YYYY-MM-DD HH:mm:ss');
} else if (moment.isDate(item)){
data[key] = moment.utc(item).format('YYYY-MM-DD HH:mm:ss');
} else if (typeof item === 'string'){
data[key] = '"' + item + '"';
}
}

return data;
}

最后调用 swig模板引擎 编译splitedata对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 var renderedData = swig.compile(split.data)(frontMatter);

this.compile = function (source, options) {
var key = options ? options.filename : null,
cached = key ? cacheGet(key, options) : null,
context,
contextLength,
pre;

if (cached) {
return cached;
}

context = getLocals(options);
contextLength = utils.keys(context).length;
pre = this.precompile(source, options);

function compiled(locals) {
var lcls;
if (locals && contextLength) {
lcls = utils.extend({}, context, locals);
} else if (locals && !contextLength) {
lcls = locals;
} else if (!locals && contextLength) {
lcls = context;
} else {
lcls = {};
}
return pre.tpl(self, lcls, filters, utils, efn);
}

utils.extend(compiled, pre.tokens);

if (key) {
cacheSet(key, options, compiled);
}

return compiled;
};

hexo源码分析之加载配置文件

发表于 2016-09-25 | 更新于 2019-05-07

hexo是如何加载配置文件的

hexo/index.js

首先在构造函数里初始化配置文件路径

1
2
3
//_config.yml就是根目录下的
this.config_path = args.config ? pathFn.resolve(base, args.config)
: pathFn.join(base, '_config.yml');

hexo有个默认的配置文件

default_config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';

module.exports = {
// Site
title: 'Hexo',
subtitle: '',
description: '',
author: 'John Doe',
language: '',
timezone: '',
// URL
url: 'http://yoursite.com',
root: '/',
permalink: ':year/:month/:day/:title/',
permalink_defaults: {},
// Directory
source_dir: 'source',
public_dir: 'public',
tag_dir: 'tags',
archive_dir: 'archives',
category_dir: 'categories',
code_dir: 'downloads/code',
i18n_dir: ':lang',
skip_render: [],
// Writing
new_post_name: ':title.md',
default_layout: 'post',
titlecase: false,
external_link: true,
filename_case: 0,
render_drafts: false,
post_asset_folder: false,
relative_link: false,
future: true,
highlight: {
enable: true,
auto_detect: true, // Maintain consistent with previous version.
line_number: true,
tab_replace: '',
},
// Category & Tag
default_category: 'uncategorized',
category_map: {},
tag_map: {},
// Date / Time format
date_format: 'YYYY-MM-DD',
time_format: 'HH:mm:ss',
// Pagination
per_page: 10,
pagination_dir: 'page',
// Extensions
theme: 'landscape',
// Deployment
deploy: {}
};
1
this.config = _.clone(defaultConfig);

随后在init函数中初始化配置:

Hexo.prototype.init

1
2
3
4
5
6
7
8
9
10
11
12
13
// Load config
return Promise.each([
'update_package', // Update package.json
'load_config', // Load config
'load_plugins' // Load external plugins & scripts
], function(name){
return require('./' + name)(self);
}).then(function(){
return self.execFilter('after_init', null, {context: self});
}).then(function(){
// Ready to go!
self.emit('ready');
});

init函数就是在hexo-cli/hexo.js中加载完hexo模块后就调用的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
return findPkg(cwd, args).then(function(path) {
if (!path) return;

hexo.base_dir = path;

return loadModule(path, args).catch(function() {
log.error('Local hexo not found in %s', chalk.magenta(tildify(path)));
log.error('Try running: \'npm install hexo --save\'');
process.exit(2);
});
}).then(function(mod) {
if (mod) hexo = mod;
log = hexo.log;

require('./console')(hexo);

return hexo.init();
})

1…373839…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
|
0%