store.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v2v3
  15. import (
  16. "context"
  17. "fmt"
  18. "path"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/clientv3/concurrency"
  23. "github.com/coreos/etcd/etcdserver/v2error"
  24. "github.com/coreos/etcd/etcdserver/v2store"
  25. "github.com/coreos/etcd/mvcc/mvccpb"
  26. )
  27. // store implements the Store interface for V2 using
  28. // a v3 client.
  29. type v2v3Store struct {
  30. c *clientv3.Client
  31. // pfx is the v3 prefix where keys should be stored.
  32. pfx string
  33. ctx context.Context
  34. }
  35. const maxPathDepth = 63
  36. var errUnsupported = fmt.Errorf("TTLs are unsupported")
  37. func NewStore(c *clientv3.Client, pfx string) v2store.Store { return newStore(c, pfx) }
  38. func newStore(c *clientv3.Client, pfx string) *v2v3Store { return &v2v3Store{c, pfx, c.Ctx()} }
  39. func (s *v2v3Store) Index() uint64 { panic("STUB") }
  40. func (s *v2v3Store) Get(nodePath string, recursive, sorted bool) (*v2store.Event, error) {
  41. key := s.mkPath(nodePath)
  42. resp, err := s.c.Txn(s.ctx).Then(
  43. clientv3.OpGet(key+"/"),
  44. clientv3.OpGet(key),
  45. ).Commit()
  46. if err != nil {
  47. return nil, err
  48. }
  49. if kvs := resp.Responses[0].GetResponseRange().Kvs; len(kvs) != 0 || isRoot(nodePath) {
  50. nodes, err := s.getDir(nodePath, recursive, sorted, resp.Header.Revision)
  51. if err != nil {
  52. return nil, err
  53. }
  54. cidx, midx := uint64(0), uint64(0)
  55. if len(kvs) > 0 {
  56. cidx, midx = mkV2Rev(kvs[0].CreateRevision), mkV2Rev(kvs[0].ModRevision)
  57. }
  58. return &v2store.Event{
  59. Action: v2store.Get,
  60. Node: &v2store.NodeExtern{
  61. Key: nodePath,
  62. Dir: true,
  63. Nodes: nodes,
  64. CreatedIndex: cidx,
  65. ModifiedIndex: midx,
  66. },
  67. EtcdIndex: mkV2Rev(resp.Header.Revision),
  68. }, nil
  69. }
  70. kvs := resp.Responses[1].GetResponseRange().Kvs
  71. if len(kvs) == 0 {
  72. return nil, v2error.NewError(v2error.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  73. }
  74. return &v2store.Event{
  75. Action: v2store.Get,
  76. Node: s.mkV2Node(kvs[0]),
  77. EtcdIndex: mkV2Rev(resp.Header.Revision),
  78. }, nil
  79. }
  80. func (s *v2v3Store) getDir(nodePath string, recursive, sorted bool, rev int64) ([]*v2store.NodeExtern, error) {
  81. rootNodes, err := s.getDirDepth(nodePath, 1, rev)
  82. if err != nil || !recursive {
  83. return rootNodes, err
  84. }
  85. nextNodes := rootNodes
  86. nodes := make(map[string]*v2store.NodeExtern)
  87. // Breadth walk the subdirectories
  88. for i := 2; len(nextNodes) > 0; i++ {
  89. for _, n := range nextNodes {
  90. nodes[n.Key] = n
  91. if parent := nodes[path.Dir(n.Key)]; parent != nil {
  92. parent.Nodes = append(parent.Nodes, n)
  93. }
  94. }
  95. if nextNodes, err = s.getDirDepth(nodePath, i, rev); err != nil {
  96. return nil, err
  97. }
  98. }
  99. return rootNodes, nil
  100. }
  101. func (s *v2v3Store) getDirDepth(nodePath string, depth int, rev int64) ([]*v2store.NodeExtern, error) {
  102. pd := s.mkPathDepth(nodePath, depth)
  103. resp, err := s.c.Get(s.ctx, pd, clientv3.WithPrefix(), clientv3.WithRev(rev))
  104. if err != nil {
  105. return nil, err
  106. }
  107. nodes := make([]*v2store.NodeExtern, len(resp.Kvs))
  108. for i, kv := range resp.Kvs {
  109. nodes[i] = s.mkV2Node(kv)
  110. }
  111. return nodes, nil
  112. }
  113. func (s *v2v3Store) Set(
  114. nodePath string,
  115. dir bool,
  116. value string,
  117. expireOpts v2store.TTLOptionSet,
  118. ) (*v2store.Event, error) {
  119. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  120. return nil, errUnsupported
  121. }
  122. if isRoot(nodePath) {
  123. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  124. }
  125. ecode := 0
  126. applyf := func(stm concurrency.STM) error {
  127. // build path if any directories in path do not exist
  128. dirs := []string{}
  129. for p := path.Dir(nodePath); !isRoot(p); p = path.Dir(p) {
  130. pp := s.mkPath(p)
  131. if stm.Rev(pp) > 0 {
  132. ecode = v2error.EcodeNotDir
  133. return nil
  134. }
  135. if stm.Rev(pp+"/") == 0 {
  136. dirs = append(dirs, pp+"/")
  137. }
  138. }
  139. for _, d := range dirs {
  140. stm.Put(d, "")
  141. }
  142. key := s.mkPath(nodePath)
  143. if dir {
  144. if stm.Rev(key) != 0 {
  145. // exists as non-dir
  146. ecode = v2error.EcodeNotDir
  147. return nil
  148. }
  149. key = key + "/"
  150. } else if stm.Rev(key+"/") != 0 {
  151. ecode = v2error.EcodeNotFile
  152. return nil
  153. }
  154. stm.Put(key, value, clientv3.WithPrevKV())
  155. stm.Put(s.mkActionKey(), v2store.Set)
  156. return nil
  157. }
  158. resp, err := s.newSTM(applyf)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if ecode != 0 {
  163. return nil, v2error.NewError(ecode, nodePath, mkV2Rev(resp.Header.Revision))
  164. }
  165. createRev := resp.Header.Revision
  166. var pn *v2store.NodeExtern
  167. if pkv := prevKeyFromPuts(resp); pkv != nil {
  168. pn = s.mkV2Node(pkv)
  169. createRev = pkv.CreateRevision
  170. }
  171. vp := &value
  172. if dir {
  173. vp = nil
  174. }
  175. return &v2store.Event{
  176. Action: v2store.Set,
  177. Node: &v2store.NodeExtern{
  178. Key: nodePath,
  179. Value: vp,
  180. Dir: dir,
  181. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  182. CreatedIndex: mkV2Rev(createRev),
  183. },
  184. PrevNode: pn,
  185. EtcdIndex: mkV2Rev(resp.Header.Revision),
  186. }, nil
  187. }
  188. func (s *v2v3Store) Update(nodePath, newValue string, expireOpts v2store.TTLOptionSet) (*v2store.Event, error) {
  189. if isRoot(nodePath) {
  190. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  191. }
  192. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  193. return nil, errUnsupported
  194. }
  195. key := s.mkPath(nodePath)
  196. ecode := 0
  197. applyf := func(stm concurrency.STM) error {
  198. if rev := stm.Rev(key + "/"); rev != 0 {
  199. ecode = v2error.EcodeNotFile
  200. return nil
  201. }
  202. if rev := stm.Rev(key); rev == 0 {
  203. ecode = v2error.EcodeKeyNotFound
  204. return nil
  205. }
  206. stm.Put(key, newValue, clientv3.WithPrevKV())
  207. stm.Put(s.mkActionKey(), v2store.Update)
  208. return nil
  209. }
  210. resp, err := s.newSTM(applyf)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if ecode != 0 {
  215. return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  216. }
  217. pkv := prevKeyFromPuts(resp)
  218. return &v2store.Event{
  219. Action: v2store.Update,
  220. Node: &v2store.NodeExtern{
  221. Key: nodePath,
  222. Value: &newValue,
  223. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  224. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  225. },
  226. PrevNode: s.mkV2Node(pkv),
  227. EtcdIndex: mkV2Rev(resp.Header.Revision),
  228. }, nil
  229. }
  230. func (s *v2v3Store) Create(
  231. nodePath string,
  232. dir bool,
  233. value string,
  234. unique bool,
  235. expireOpts v2store.TTLOptionSet,
  236. ) (*v2store.Event, error) {
  237. if isRoot(nodePath) {
  238. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  239. }
  240. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  241. return nil, errUnsupported
  242. }
  243. ecode := 0
  244. applyf := func(stm concurrency.STM) error {
  245. ecode = 0
  246. key := s.mkPath(nodePath)
  247. if unique {
  248. // append unique item under the node path
  249. for {
  250. key = nodePath + "/" + fmt.Sprintf("%020s", time.Now())
  251. key = path.Clean(path.Join("/", key))
  252. key = s.mkPath(key)
  253. if stm.Rev(key) == 0 {
  254. break
  255. }
  256. }
  257. }
  258. if stm.Rev(key) > 0 || stm.Rev(key+"/") > 0 {
  259. ecode = v2error.EcodeNodeExist
  260. return nil
  261. }
  262. // build path if any directories in path do not exist
  263. dirs := []string{}
  264. for p := path.Dir(nodePath); !isRoot(p); p = path.Dir(p) {
  265. pp := s.mkPath(p)
  266. if stm.Rev(pp) > 0 {
  267. ecode = v2error.EcodeNotDir
  268. return nil
  269. }
  270. if stm.Rev(pp+"/") == 0 {
  271. dirs = append(dirs, pp+"/")
  272. }
  273. }
  274. for _, d := range dirs {
  275. stm.Put(d, "")
  276. }
  277. if dir {
  278. // directories marked with extra slash in key name
  279. key += "/"
  280. }
  281. stm.Put(key, value)
  282. stm.Put(s.mkActionKey(), v2store.Create)
  283. return nil
  284. }
  285. resp, err := s.newSTM(applyf)
  286. if err != nil {
  287. return nil, err
  288. }
  289. if ecode != 0 {
  290. return nil, v2error.NewError(ecode, nodePath, mkV2Rev(resp.Header.Revision))
  291. }
  292. var v *string
  293. if !dir {
  294. v = &value
  295. }
  296. return &v2store.Event{
  297. Action: v2store.Create,
  298. Node: &v2store.NodeExtern{
  299. Key: nodePath,
  300. Value: v,
  301. Dir: dir,
  302. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  303. CreatedIndex: mkV2Rev(resp.Header.Revision),
  304. },
  305. EtcdIndex: mkV2Rev(resp.Header.Revision),
  306. }, nil
  307. }
  308. func (s *v2v3Store) CompareAndSwap(
  309. nodePath string,
  310. prevValue string,
  311. prevIndex uint64,
  312. value string,
  313. expireOpts v2store.TTLOptionSet,
  314. ) (*v2store.Event, error) {
  315. if isRoot(nodePath) {
  316. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  317. }
  318. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  319. return nil, errUnsupported
  320. }
  321. key := s.mkPath(nodePath)
  322. resp, err := s.c.Txn(s.ctx).If(
  323. s.mkCompare(nodePath, prevValue, prevIndex)...,
  324. ).Then(
  325. clientv3.OpPut(key, value, clientv3.WithPrevKV()),
  326. clientv3.OpPut(s.mkActionKey(), v2store.CompareAndSwap),
  327. ).Else(
  328. clientv3.OpGet(key),
  329. clientv3.OpGet(key+"/"),
  330. ).Commit()
  331. if err != nil {
  332. return nil, err
  333. }
  334. if !resp.Succeeded {
  335. return nil, compareFail(nodePath, prevValue, prevIndex, resp)
  336. }
  337. pkv := resp.Responses[0].GetResponsePut().PrevKv
  338. return &v2store.Event{
  339. Action: v2store.CompareAndSwap,
  340. Node: &v2store.NodeExtern{
  341. Key: nodePath,
  342. Value: &value,
  343. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  344. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  345. },
  346. PrevNode: s.mkV2Node(pkv),
  347. EtcdIndex: mkV2Rev(resp.Header.Revision),
  348. }, nil
  349. }
  350. func (s *v2v3Store) Delete(nodePath string, dir, recursive bool) (*v2store.Event, error) {
  351. if isRoot(nodePath) {
  352. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  353. }
  354. if !dir && !recursive {
  355. return s.deleteNode(nodePath)
  356. }
  357. if !recursive {
  358. return s.deleteEmptyDir(nodePath)
  359. }
  360. dels := make([]clientv3.Op, maxPathDepth+1)
  361. dels[0] = clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV())
  362. for i := 1; i < maxPathDepth; i++ {
  363. dels[i] = clientv3.OpDelete(s.mkPathDepth(nodePath, i), clientv3.WithPrefix())
  364. }
  365. dels[maxPathDepth] = clientv3.OpPut(s.mkActionKey(), v2store.Delete)
  366. resp, err := s.c.Txn(s.ctx).If(
  367. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), ">", 0),
  368. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, maxPathDepth)+"/"), "=", 0),
  369. ).Then(
  370. dels...,
  371. ).Commit()
  372. if err != nil {
  373. return nil, err
  374. }
  375. if !resp.Succeeded {
  376. return nil, v2error.NewError(v2error.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  377. }
  378. dresp := resp.Responses[0].GetResponseDeleteRange()
  379. return &v2store.Event{
  380. Action: v2store.Delete,
  381. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  382. EtcdIndex: mkV2Rev(resp.Header.Revision),
  383. }, nil
  384. }
  385. func (s *v2v3Store) deleteEmptyDir(nodePath string) (*v2store.Event, error) {
  386. resp, err := s.c.Txn(s.ctx).If(
  387. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, 1)), "=", 0).WithPrefix(),
  388. ).Then(
  389. clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV()),
  390. clientv3.OpPut(s.mkActionKey(), v2store.Delete),
  391. ).Commit()
  392. if err != nil {
  393. return nil, err
  394. }
  395. if !resp.Succeeded {
  396. return nil, v2error.NewError(v2error.EcodeDirNotEmpty, nodePath, mkV2Rev(resp.Header.Revision))
  397. }
  398. dresp := resp.Responses[0].GetResponseDeleteRange()
  399. if len(dresp.PrevKvs) == 0 {
  400. return nil, v2error.NewError(v2error.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  401. }
  402. return &v2store.Event{
  403. Action: v2store.Delete,
  404. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  405. EtcdIndex: mkV2Rev(resp.Header.Revision),
  406. }, nil
  407. }
  408. func (s *v2v3Store) deleteNode(nodePath string) (*v2store.Event, error) {
  409. resp, err := s.c.Txn(s.ctx).If(
  410. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), "=", 0),
  411. ).Then(
  412. clientv3.OpDelete(s.mkPath(nodePath), clientv3.WithPrevKV()),
  413. clientv3.OpPut(s.mkActionKey(), v2store.Delete),
  414. ).Commit()
  415. if err != nil {
  416. return nil, err
  417. }
  418. if !resp.Succeeded {
  419. return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  420. }
  421. pkvs := resp.Responses[0].GetResponseDeleteRange().PrevKvs
  422. if len(pkvs) == 0 {
  423. return nil, v2error.NewError(v2error.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  424. }
  425. pkv := pkvs[0]
  426. return &v2store.Event{
  427. Action: v2store.Delete,
  428. Node: &v2store.NodeExtern{
  429. Key: nodePath,
  430. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  431. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  432. },
  433. PrevNode: s.mkV2Node(pkv),
  434. EtcdIndex: mkV2Rev(resp.Header.Revision),
  435. }, nil
  436. }
  437. func (s *v2v3Store) CompareAndDelete(nodePath, prevValue string, prevIndex uint64) (*v2store.Event, error) {
  438. if isRoot(nodePath) {
  439. return nil, v2error.NewError(v2error.EcodeRootROnly, nodePath, 0)
  440. }
  441. key := s.mkPath(nodePath)
  442. resp, err := s.c.Txn(s.ctx).If(
  443. s.mkCompare(nodePath, prevValue, prevIndex)...,
  444. ).Then(
  445. clientv3.OpDelete(key, clientv3.WithPrevKV()),
  446. clientv3.OpPut(s.mkActionKey(), v2store.CompareAndDelete),
  447. ).Else(
  448. clientv3.OpGet(key),
  449. clientv3.OpGet(key+"/"),
  450. ).Commit()
  451. if err != nil {
  452. return nil, err
  453. }
  454. if !resp.Succeeded {
  455. return nil, compareFail(nodePath, prevValue, prevIndex, resp)
  456. }
  457. // len(pkvs) > 1 since txn only succeeds when key exists
  458. pkv := resp.Responses[0].GetResponseDeleteRange().PrevKvs[0]
  459. return &v2store.Event{
  460. Action: v2store.CompareAndDelete,
  461. Node: &v2store.NodeExtern{
  462. Key: nodePath,
  463. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  464. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  465. },
  466. PrevNode: s.mkV2Node(pkv),
  467. EtcdIndex: mkV2Rev(resp.Header.Revision),
  468. }, nil
  469. }
  470. func compareFail(nodePath, prevValue string, prevIndex uint64, resp *clientv3.TxnResponse) error {
  471. if dkvs := resp.Responses[1].GetResponseRange().Kvs; len(dkvs) > 0 {
  472. return v2error.NewError(v2error.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  473. }
  474. kvs := resp.Responses[0].GetResponseRange().Kvs
  475. if len(kvs) == 0 {
  476. return v2error.NewError(v2error.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  477. }
  478. kv := kvs[0]
  479. indexMatch := (prevIndex == 0 || kv.ModRevision == int64(prevIndex))
  480. valueMatch := (prevValue == "" || string(kv.Value) == prevValue)
  481. var cause string
  482. switch {
  483. case indexMatch && !valueMatch:
  484. cause = fmt.Sprintf("[%v != %v]", prevValue, string(kv.Value))
  485. case valueMatch && !indexMatch:
  486. cause = fmt.Sprintf("[%v != %v]", prevIndex, kv.ModRevision)
  487. default:
  488. cause = fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, string(kv.Value), prevIndex, kv.ModRevision)
  489. }
  490. return v2error.NewError(v2error.EcodeTestFailed, cause, mkV2Rev(resp.Header.Revision))
  491. }
  492. func (s *v2v3Store) mkCompare(nodePath, prevValue string, prevIndex uint64) []clientv3.Cmp {
  493. key := s.mkPath(nodePath)
  494. cmps := []clientv3.Cmp{clientv3.Compare(clientv3.Version(key), ">", 0)}
  495. if prevIndex != 0 {
  496. cmps = append(cmps, clientv3.Compare(clientv3.ModRevision(key), "=", mkV3Rev(prevIndex)))
  497. }
  498. if prevValue != "" {
  499. cmps = append(cmps, clientv3.Compare(clientv3.Value(key), "=", prevValue))
  500. }
  501. return cmps
  502. }
  503. func (s *v2v3Store) JsonStats() []byte { panic("STUB") }
  504. func (s *v2v3Store) DeleteExpiredKeys(cutoff time.Time) { panic("STUB") }
  505. func (s *v2v3Store) Version() int { return 2 }
  506. // TODO: move this out of the Store interface?
  507. func (s *v2v3Store) Save() ([]byte, error) { panic("STUB") }
  508. func (s *v2v3Store) Recovery(state []byte) error { panic("STUB") }
  509. func (s *v2v3Store) Clone() v2store.Store { panic("STUB") }
  510. func (s *v2v3Store) SaveNoCopy() ([]byte, error) { panic("STUB") }
  511. func (s *v2v3Store) HasTTLKeys() bool { panic("STUB") }
  512. func (s *v2v3Store) mkPath(nodePath string) string { return s.mkPathDepth(nodePath, 0) }
  513. func (s *v2v3Store) mkNodePath(p string) string {
  514. return path.Clean(p[len(s.pfx)+len("/k/000/"):])
  515. }
  516. // mkPathDepth makes a path to a key that encodes its directory depth
  517. // for fast directory listing. If a depth is provided, it is added
  518. // to the computed depth.
  519. func (s *v2v3Store) mkPathDepth(nodePath string, depth int) string {
  520. normalForm := path.Clean(path.Join("/", nodePath))
  521. n := strings.Count(normalForm, "/") + depth
  522. return fmt.Sprintf("%s/%03d/k/%s", s.pfx, n, normalForm)
  523. }
  524. func (s *v2v3Store) mkActionKey() string { return s.pfx + "/act" }
  525. func isRoot(s string) bool { return len(s) == 0 || s == "/" || s == "/0" || s == "/1" }
  526. func mkV2Rev(v3Rev int64) uint64 {
  527. if v3Rev == 0 {
  528. return 0
  529. }
  530. return uint64(v3Rev - 1)
  531. }
  532. func mkV3Rev(v2Rev uint64) int64 {
  533. if v2Rev == 0 {
  534. return 0
  535. }
  536. return int64(v2Rev + 1)
  537. }
  538. // mkV2Node creates a V2 NodeExtern from a V3 KeyValue
  539. func (s *v2v3Store) mkV2Node(kv *mvccpb.KeyValue) *v2store.NodeExtern {
  540. if kv == nil {
  541. return nil
  542. }
  543. n := &v2store.NodeExtern{
  544. Key: s.mkNodePath(string(kv.Key)),
  545. Dir: kv.Key[len(kv.Key)-1] == '/',
  546. CreatedIndex: mkV2Rev(kv.CreateRevision),
  547. ModifiedIndex: mkV2Rev(kv.ModRevision),
  548. }
  549. if !n.Dir {
  550. v := string(kv.Value)
  551. n.Value = &v
  552. }
  553. return n
  554. }
  555. // prevKeyFromPuts gets the prev key that is being put; ignores
  556. // the put action response.
  557. func prevKeyFromPuts(resp *clientv3.TxnResponse) *mvccpb.KeyValue {
  558. for _, r := range resp.Responses {
  559. pkv := r.GetResponsePut().PrevKv
  560. if pkv != nil && pkv.CreateRevision > 0 {
  561. return pkv
  562. }
  563. }
  564. return nil
  565. }
  566. func (s *v2v3Store) newSTM(applyf func(concurrency.STM) error) (*clientv3.TxnResponse, error) {
  567. return concurrency.NewSTM(s.c, applyf, concurrency.WithIsolation(concurrency.Serializable))
  568. }