Skip to content

Commit

Permalink
rename platform allimg to simple
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Feb 6, 2021
1 parent 81f6a1b commit 7e51706
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 31 deletions.
10 changes: 7 additions & 3 deletions md2zhihu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def weibo_specific(mdrender, n, ctx=None):
return None


def allimg_specific(mdrender, n, ctx=None):
def simple_specific(mdrender, n, ctx=None):
typ = n['type']

if typ == 'image':
Expand All @@ -264,6 +264,9 @@ def allimg_specific(mdrender, n, ctx=None):
if typ == 'table':
return table_to_jpg(mdrender, n, ctx=ctx)

if typ == 'codespan':
return [escape(n['text'])]

if typ == 'block_code':
lang = n['info'] or ''
if lang == 'mermaid':
Expand All @@ -284,7 +287,7 @@ class MDRender(object):
'zhihu': zhihu_specific,
'wechat':wechat_specific,
'weibo':weibo_specific,
'allimg': allimg_specific,
'simple': simple_specific,
}

def __init__(self, conf, platform='zhihu'):
Expand Down Expand Up @@ -869,8 +872,9 @@ def main():
parser.add_argument('-p', '--platform', action='store',
required=False,
default='zhihu',
choices=["zhihu", "wechat", "weibo", "allimg"],
choices=["zhihu", "wechat", "weibo", "simple"],
help='convert to a platform compatible format.'
'simple is a special type that it produce simplest output, only plain text and images, there wont be table, code block, math etc.'
)

parser.add_argument('--keep-meta', action='store_true',
Expand Down
File renamed without changes
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@

# 场景和问题

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/md----acbd-d573c99c6cf5bbd0.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/md----acbd-d573c99c6cf5bbd0.jpg)

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/graphLRAHardedge--LinktextBRound-38e149134ebbdae5.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/graphLRAHardedge--LinktextBRound-38e149134ebbdae5.jpg)

inline code: `foo = bar`
inline code: foo = bar

inline math ![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/Xvecbeta-Y2-36bbccd5e08c341b.jpg) foo
inline math ![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/Xvecbeta-Y2-36bbccd5e08c341b.jpg) foo

inline math in codespan `$$ ||X{\vec {\beta }}-Y||^{2} $$`
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/slim.jpg)
inline math in codespan $$ ||X{\vec {\beta }}-Y||^{2} $$
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/slim.jpg)

在时序数据库, 或列存储为基础的系统中, 很常见的形式就是存储一个整数数组,
例如 [slim](https://github.com/openacid/slim) 这个项目按天统计的 star 数:

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/slim.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/slim.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/slim.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/slim.jpg)

我们可以利用数据分布的特点, 将整体数据的大小压缩到**几分之一**.

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/DatasizeDataSetgzipsizeslimarrys-511b012906c547ff.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/DatasizeDataSetgzipsizeslimarrys-511b012906c547ff.jpg)

在达到gzip同等压缩率的前提下, 构建 slimarray 和 访问的性能也非常高:

- 构建 slimarray 时, 平均每秒可压缩 6百万 个数组元素;
- 读取一个数组元素平均花费 7 ns/op.
- 构建 slimarray 时, 平均每秒可压缩 6百万 个数组元素;
- 读取一个数组元素平均花费 `7 ns/op`.
- 读取一个数组元素平均花费 7 ns/op.

> 在达到gzip同等压缩率的前提下, 构建 slimarray 和 访问的性能也非常高:
>
> - 构建 slimarray 时, 平均每秒可压缩 6百万 个数组元素;
> - 读取一个数组元素平均花费 7 ns/op.
> - 构建 slimarray 时, 平均每秒可压缩 6百万 个数组元素;
> - 读取一个数组元素平均花费 `7 ns/op`.
> - 读取一个数组元素平均花费 7 ns/op.

按照这种思路, **在给定数组中找到一条曲线来描述点的趋势,**
Expand All @@ -43,32 +43,32 @@ inline math in codespan `$$ ||X{\vec {\beta }}-Y||^{2} $$`
# 找到趋势函数

寻找这样一条曲线就使用线性回归,
例如在 [slimarray](https://github.com/openacid/slimarray) 中使用2次曲线 `f(x) = β₁ + β₂x + β₃x²`, 所要做的就是确定每个βᵢ的值,
以使得`f(xⱼ) - yⱼ`的均方差最小. xⱼ是数组下标0, 1, 2...; yⱼ是数组中每个元素的值.
例如在 [slimarray](https://github.com/openacid/slimarray) 中使用2次曲线 f(x) = β₁ + β₂x + β₃x², 所要做的就是确定每个βᵢ的值,
以使得f(xⱼ) - yⱼ的均方差最小. xⱼ是数组下标0, 1, 2...; yⱼ是数组中每个元素的值.

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/X=beginbmatrix1x_1x_121x_2x_22vd-804a1197af934f48.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/X=beginbmatrix1x_1x_121x_2x_22vd-804a1197af934f48.jpg)

`spanIndex = OnesCount(bitmap & (1<<(i/16) - 1))`
spanIndex = OnesCount(bitmap &amp; (1&lt;&lt;(i/16) - 1))

## 读取过程

读取过程通过找span, 读取span配置,还原原始数据几个步骤完成, 假设 slimarray 的对象是`sa`:
读取过程通过找span, 读取span配置,还原原始数据几个步骤完成, 假设 slimarray 的对象是sa:

- 通过下标`i` 得到 spanIndex: `spanIndex = OnesCount(sa.bitmap & (1<<(i/16) - 1))`;
- 通过 spanIndex 得到多项式的3个系数: `[b₀, b₁, b₂] = sa.polynomials[spanIndex: spanIndex + 3]`;
- 读取 delta 数组起始位置, 和 delta 数组中每个 delta 的 bit 宽度: `config=sa.configs[spanIndex]`;
- delta 的值保存在 delta 数组的`config.offset + i*config.width`的位置, 从这个位置读取`width` bit 得到 delta 的值.
- 计算 `nums[i]` 的值: `b₀ + b₁*i + b₂*i²` 再加上 delta 的值.
- 通过下标i 得到 spanIndex: spanIndex = OnesCount(sa.bitmap &amp; (1&lt;&lt;(i/16) - 1));
- 通过 spanIndex 得到多项式的3个系数: [b₀, b₁, b₂] = sa.polynomials[spanIndex: spanIndex + 3];
- 读取 delta 数组起始位置, 和 delta 数组中每个 delta 的 bit 宽度: config=sa.configs[spanIndex];
- delta 的值保存在 delta 数组的config.offset + i*config.width的位置, 从这个位置读取width个 bit 得到 delta 的值.
- 计算 nums[i] 的值: b₀ + b₁*i + b₂*i² 再加上 delta 的值.

简化的读取逻辑如下:

<img src="https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/funcsmSlimArrayGetiint32uint32x=-8e29c97c1ebbd46d.jpg" />
<img src="https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/funcsmSlimArrayGetiint32uint32x=-8e29c97c1ebbd46d.jpg" />

formula in list:

- 对奇数节点, n = 2k+1, 还是沿用 **多数派** 节点的集合, 大部分场合都可以很好的工作:

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/Q_oddC=MC=qqsubseteqCqC2-149709b0ed354902.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/Q_oddC=MC=qqsubseteqCqC2-149709b0ed354902.jpg)

- 对偶数节点, n = 2k, **因为n/2个节点跟n/2+1个节点一定有交集**,
我们可以向 M(C) 中加入几个大小为 n/2 的节点集合,
Expand All @@ -79,11 +79,11 @@ formula in list:
- 也可以是 Q' = M(abcd) ∪ {bc, cd, bd};

要找到一个更好的偶节点的 quorum 集合, 一个方法是可以把偶数节点的集群看做是一个奇数节点集群加上一个节点x:
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/D=Ccupx-76874c18ea7bc229.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/D=Ccupx-76874c18ea7bc229.jpg)

于是偶数节点的 quorum 集合就可以是 M(D) 的一个扩张:

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/Q_evenD_x=MDcupMDsetminusx-d979aeb5e8ea9324.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/Q_evenD_x=MDcupMDsetminusx-d979aeb5e8ea9324.jpg)

当然这个x可以随意选择, 例如在abcd的例子中, 如果选x = d, 那么
Q' = M(abcd) ∪ {ab, bc, ca};
Expand All @@ -92,7 +92,7 @@ table in list:

- 链接列表:

![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/allimg/simple/---assetsslimjpgfobarabc-4eb2fd74bec19f90.jpg)
![](https://gitee.com/drdrxp/bed/raw/_md2zhihu_foo/simple/simple/---assetsslimjpgfobarabc-4eb2fd74bec19f90.jpg)



Expand Down
2 changes: 1 addition & 1 deletion test/test_md2zhihu.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_zhihu_meta(self): self._test_platform(

def test_wechat(self): self._test_platform('wechat', ['-p', 'wechat'])
def test_weibo(self): self._test_platform('weibo', ['-p', 'weibo'])
def test_allimg(self): self._test_platform('allimg', ['-p', 'allimg'])
def test_simple(self): self._test_platform('simple', ['-p', 'simple'])

def _test_platform(self, platform_type, args):

Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.1.18"
__version__ = "0.1.19"
__name__ = "md2zhihu"

0 comments on commit 7e51706

Please sign in to comment.